INET Framework for OMNeT++/OMNEST
inet::TcpGenericServerApp Class Reference

Generic server application. More...

#include <TcpGenericServerApp.h>

Inheritance diagram for inet::TcpGenericServerApp:
inet::LifecycleUnsupported inet::ILifecycle

Protected Member Functions

virtual void sendBack (cMessage *msg)
 
virtual void sendOrSchedule (cMessage *msg, simtime_t delay)
 
virtual void initialize (int stage) override
 
virtual int numInitStages () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 

Protected Attributes

TcpSocket socket
 
simtime_t delay
 
simtime_t maxMsgDelay
 
long msgsRcvd
 
long msgsSent
 
long bytesRcvd
 
long bytesSent
 
std::map< int, ChunkQueuesocketQueue
 

Additional Inherited Members

- Public Member Functions inherited from inet::LifecycleUnsupported
virtual bool handleOperationStage (LifecycleOperation *operation, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 

Detailed Description

Generic server application.

It serves requests coming in GenericAppMsg request messages. Clients are usually subclassed from TcpAppBase.

See also
GenericAppMsg, TcpAppBase

Member Function Documentation

◆ finish()

void inet::TcpGenericServerApp::finish ( )
overrideprotectedvirtual
158 {
159  EV_INFO << getFullPath() << ": sent " << bytesSent << " bytes in " << msgsSent << " packets\n";
160  EV_INFO << getFullPath() << ": received " << bytesRcvd << " bytes in " << msgsRcvd << " packets\n";
161 }

◆ handleMessage()

void inet::TcpGenericServerApp::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
85 {
86  if (msg->isSelfMessage()) {
87  sendBack(msg);
88  }
89  else if (msg->getKind() == TCP_I_PEER_CLOSED) {
90  // we'll close too, but only after there's surely no message
91  // pending to be sent back in this connection
92  int connId = check_and_cast<Indication *>(msg)->getTag<SocketInd>()->getSocketId();
93  delete msg;
94  auto request = new Request("close", TCP_C_CLOSE);
95  request->addTag<SocketReq>()->setSocketId(connId);
96  sendOrSchedule(request, delay + maxMsgDelay);
97  }
98  else if (msg->getKind() == TCP_I_DATA || msg->getKind() == TCP_I_URGENT_DATA) {
99  Packet *packet = check_and_cast<Packet *>(msg);
100  int connId = packet->getTag<SocketInd>()->getSocketId();
101  ChunkQueue& queue = socketQueue[connId];
102  auto chunk = packet->peekDataAt(B(0), packet->getTotalLength());
103  queue.push(chunk);
104  emit(packetReceivedSignal, packet);
105 
106  bool doClose = false;
107  while (const auto& appmsg = queue.pop<GenericAppMsg>(b(-1), Chunk::PF_ALLOW_NULLPTR)) {
108  msgsRcvd++;
109  bytesRcvd += B(appmsg->getChunkLength()).get();
110  B requestedBytes = appmsg->getExpectedReplyLength();
111  simtime_t msgDelay = appmsg->getReplyDelay();
112  if (msgDelay > maxMsgDelay)
113  maxMsgDelay = msgDelay;
114 
115  if (requestedBytes > B(0)) {
116  Packet *outPacket = new Packet(msg->getName(), TCP_C_SEND);
117  outPacket->addTag<SocketReq>()->setSocketId(connId);
118  const auto& payload = makeShared<GenericAppMsg>();
119  payload->setChunkLength(requestedBytes);
120  payload->setExpectedReplyLength(B(0));
121  payload->setReplyDelay(0);
122  payload->addTag<CreationTimeTag>()->setCreationTime(simTime());
123  outPacket->insertAtBack(payload);
124  sendOrSchedule(outPacket, delay + msgDelay);
125  }
126  if (appmsg->getServerClose()) {
127  doClose = true;
128  break;
129  }
130  }
131  delete msg;
132 
133  if (doClose) {
134  auto request = new Request("close", TCP_C_CLOSE);
135  TcpCommand *cmd = new TcpCommand();
136  request->addTag<SocketReq>()->setSocketId(connId);
137  request->setControlInfo(cmd);
138  sendOrSchedule(request, delay + maxMsgDelay);
139  }
140  }
141  else if (msg->getKind() == TCP_I_AVAILABLE)
142  socket.processMessage(msg);
143  else {
144  // some indication -- ignore
145  EV_WARN << "drop msg: " << msg->getName() << ", kind:" << msg->getKind() << "(" << cEnum::get("inet::TcpStatusInd")->getStringFor(msg->getKind()) << ")\n";
146  delete msg;
147  }
148 }

◆ initialize()

void inet::TcpGenericServerApp::initialize ( int  stage)
overrideprotectedvirtual
26 {
27  cSimpleModule::initialize(stage);
28 
29  if (stage == INITSTAGE_LOCAL) {
30  delay = par("replyDelay");
31  maxMsgDelay = 0;
32 
33  // statistics
35 
36  WATCH(msgsRcvd);
37  WATCH(msgsSent);
38  WATCH(bytesRcvd);
39  WATCH(bytesSent);
40  }
41  else if (stage == INITSTAGE_APPLICATION_LAYER) {
42  const char *localAddress = par("localAddress");
43  int localPort = par("localPort");
44  socket.setOutputGate(gate("socketOut"));
45  socket.bind(localAddress[0] ? L3AddressResolver().resolve(localAddress) : L3Address(), localPort);
46  socket.listen();
47 
48  cModule *node = findContainingNode(this);
49  NodeStatus *nodeStatus = node ? check_and_cast_nullable<NodeStatus *>(node->getSubmodule("status")) : nullptr;
50  bool isOperational = (!nodeStatus) || nodeStatus->getState() == NodeStatus::UP;
51  if (!isOperational)
52  throw cRuntimeError("This module doesn't support starting in node DOWN state");
53  }
54 }

◆ numInitStages()

virtual int inet::TcpGenericServerApp::numInitStages ( ) const
inlineoverrideprotectedvirtual
42 { return NUM_INIT_STAGES; }

◆ refreshDisplay()

void inet::TcpGenericServerApp::refreshDisplay ( ) const
overrideprotectedvirtual
151 {
152  char buf[64];
153  sprintf(buf, "rcvd: %ld pks %ld bytes\nsent: %ld pks %ld bytes", msgsRcvd, bytesRcvd, msgsSent, bytesSent);
154  getDisplayString().setTagArg("t", 0, buf);
155 }

◆ sendBack()

void inet::TcpGenericServerApp::sendBack ( cMessage *  msg)
protectedvirtual
65 {
66  Packet *packet = dynamic_cast<Packet *>(msg);
67 
68  if (packet) {
69  msgsSent++;
70  bytesSent += packet->getByteLength();
71  emit(packetSentSignal, packet);
72 
73  EV_INFO << "sending \"" << packet->getName() << "\" to TCP, " << packet->getByteLength() << " bytes\n";
74  }
75  else {
76  EV_INFO << "sending \"" << msg->getName() << "\" to TCP\n";
77  }
78 
79  auto& tags = check_and_cast<ITaggedObject *>(msg)->getTags();
80  tags.addTagIfAbsent<DispatchProtocolReq>()->setProtocol(&Protocol::tcp);
81  send(msg, "socketOut");
82 }

Referenced by handleMessage(), and sendOrSchedule().

◆ sendOrSchedule()

void inet::TcpGenericServerApp::sendOrSchedule ( cMessage *  msg,
simtime_t  delay 
)
protectedvirtual
57 {
58  if (delay == 0)
59  sendBack(msg);
60  else
61  scheduleAfter(delay, msg);
62 }

Referenced by handleMessage().

Member Data Documentation

◆ bytesRcvd

long inet::TcpGenericServerApp::bytesRcvd
protected

◆ bytesSent

long inet::TcpGenericServerApp::bytesSent
protected

◆ delay

simtime_t inet::TcpGenericServerApp::delay
protected

◆ maxMsgDelay

simtime_t inet::TcpGenericServerApp::maxMsgDelay
protected

Referenced by handleMessage(), and initialize().

◆ msgsRcvd

long inet::TcpGenericServerApp::msgsRcvd
protected

◆ msgsSent

long inet::TcpGenericServerApp::msgsSent
protected

◆ socket

TcpSocket inet::TcpGenericServerApp::socket
protected

Referenced by handleMessage(), and initialize().

◆ socketQueue

std::map<int, ChunkQueue> inet::TcpGenericServerApp::socketQueue
protected

Referenced by handleMessage().


The documentation for this class was generated from the following files:
inet::Chunk::PF_ALLOW_NULLPTR
@ PF_ALLOW_NULLPTR
Definition: Chunk.h:278
inet::findContainingNode
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:31
inet::Protocol::tcp
static const Protocol tcp
Definition: Protocol.h:112
inet::TcpGenericServerApp::maxMsgDelay
simtime_t maxMsgDelay
Definition: TcpGenericServerApp.h:28
inet::TCP_I_PEER_CLOSED
@ TCP_I_PEER_CLOSED
Definition: TcpCommand_m.h:129
inet::TCP_I_AVAILABLE
@ TCP_I_AVAILABLE
Definition: TcpCommand_m.h:127
DispatchProtocolReq
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down DispatchProtocolReq
Definition: IUdp-gates.txt:25
inet::TcpGenericServerApp::delay
simtime_t delay
Definition: TcpGenericServerApp.h:27
inet::TcpGenericServerApp::sendOrSchedule
virtual void sendOrSchedule(cMessage *msg, simtime_t delay)
Definition: TcpGenericServerApp.cc:56
inet::packetSentSignal
simsignal_t packetSentSignal
Definition: Simsignals.cc:96
connId
az accept haszálja pcb új connId
Definition: lwip_tcp.txt:42
inet::TcpGenericServerApp::msgsSent
long msgsSent
Definition: TcpGenericServerApp.h:31
inet::TCP_I_URGENT_DATA
@ TCP_I_URGENT_DATA
Definition: TcpCommand_m.h:126
inet::TcpGenericServerApp::socketQueue
std::map< int, ChunkQueue > socketQueue
Definition: TcpGenericServerApp.h:35
inet::TCP_C_SEND
@ TCP_C_SEND
Definition: TcpCommand_m.h:85
inet::TcpGenericServerApp::socket
TcpSocket socket
Definition: TcpGenericServerApp.h:26
inet::TcpGenericServerApp::msgsRcvd
long msgsRcvd
Definition: TcpGenericServerApp.h:30
inet::units::units::B
intscale< b, 1, 8 > B
Definition: Units.h:1168
inet::packetReceivedSignal
simsignal_t packetReceivedSignal
Definition: Simsignals.cc:97
inet::TcpSocket::listen
void listen(bool fork)
Definition: TcpSocket.cc:95
inet::TcpSocket::processMessage
void processMessage(cMessage *msg) override
Examines the message (which should have arrived from TCP), updates socket state, and if there is a ca...
Definition: TcpSocket.cc:281
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::units::values::b
value< int64_t, units::b > b
Definition: Units.h:1241
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::TCP_C_CLOSE
@ TCP_C_CLOSE
Definition: TcpCommand_m.h:86
inet::INITSTAGE_APPLICATION_LAYER
INET_API InitStage INITSTAGE_APPLICATION_LAYER
Initialization of applications.
inet::TcpGenericServerApp::sendBack
virtual void sendBack(cMessage *msg)
Definition: TcpGenericServerApp.cc:64
inet::NodeStatus::UP
@ UP
Definition: NodeStatus.h:28
tags
* tags
Definition: IUdp-gates.txt:3
inet::TcpGenericServerApp::bytesSent
long bytesSent
Definition: TcpGenericServerApp.h:33
inet::TcpGenericServerApp::bytesRcvd
long bytesRcvd
Definition: TcpGenericServerApp.h:32
inet::TcpSocket::bind
void bind(int localPort)
Bind the socket to a local port number.
Definition: TcpSocket.cc:69
inet::TcpSocket::setOutputGate
void setOutputGate(cGate *toTcp)
Sets the gate on which to send to TCP.
Definition: TcpSocket.h:242
inet::TCP_I_DATA
@ TCP_I_DATA
Definition: TcpCommand_m.h:125