A Discrete-Event Network Simulator
API
ns3tcp-socket-writer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "ns3tcp-socket-writer.h"
19 
20 #include "ns3/packet.h"
21 #include "ns3/tcp-socket-factory.h"
22 
23 namespace ns3
24 {
25 
27  : m_node(nullptr),
28  m_socket(nullptr),
29  m_isSetup(false),
30  m_isConnected(false)
31 {
32 }
33 
35 {
36  m_socket = nullptr;
37  m_node = nullptr;
38 }
39 
40 /* static */
41 TypeId
43 {
44  static TypeId tid = TypeId("ns3::SocketWriter")
46  .SetGroupName("Stats")
47  .AddConstructor<SocketWriter>();
48  return tid;
49 }
50 
51 void
53 {
55  m_socket->Bind();
56 }
57 
58 void
60 {
61 }
62 
63 void
65 {
66  m_peer = peer;
67  m_node = node;
68  m_isSetup = true;
69 }
70 
71 void
73 {
74  if (!m_isSetup)
75  {
76  NS_FATAL_ERROR("Forgot to call Setup() first");
77  }
79  m_isConnected = true;
80 }
81 
82 void
83 SocketWriter::Write(uint32_t numBytes)
84 {
85  if (!m_isConnected)
86  {
87  Connect();
88  }
89  Ptr<Packet> packet = Create<Packet>(numBytes);
90  m_socket->Send(packet);
91 }
92 
93 void
95 {
96  m_socket->Close();
97 }
98 } // namespace ns3
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:72
virtual int Close()=0
Close a socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Simple class to write data to sockets.
Address m_peer
Peer's address.
static TypeId GetTypeId()
Register this type.
void Connect()
Connect the socket.
void Setup(Ptr< Node > node, Address peer)
Setup the socket.
void Write(uint32_t numBytes)
Write to the socket.
Ptr< Node > m_node
Node pointer.
void Close()
Close the socket.
bool m_isSetup
True if the socket is connected.
void StopApplication() override
Application specific shutdown code.
Ptr< Socket > m_socket
Socket.
bool m_isConnected
True if the socket setup has been done.
void StartApplication() override
Application specific startup code.
static TypeId GetTypeId()
Get the type ID.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
Every class exported by the ns3 library is enclosed in the ns3 namespace.