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

Hosts a server application, to be subclassed from TCPServerProcess (which is a sSimpleModule). More...

#include <TCPSrvHostApp.h>

Inheritance diagram for inet::TCPSrvHostApp:
inet::ILifecycle

Public Member Functions

virtual void removeThread (TCPServerThreadBase *thread)
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 

Protected Member Functions

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
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 

Protected Attributes

TCPSocket serverSocket
 
TCPSocketMap socketMap
 

Detailed Description

Hosts a server application, to be subclassed from TCPServerProcess (which is a sSimpleModule).

Creates one instance (using dynamic module creation) for each incoming connection. More info in the corresponding NED file.

Member Function Documentation

void inet::TCPSrvHostApp::finish ( )
overrideprotectedvirtual
86 {
87 }
void inet::TCPSrvHostApp::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
58 {
59  if (msg->isSelfMessage()) {
60  TCPServerThreadBase *thread = (TCPServerThreadBase *)msg->getContextPointer();
61  thread->timerExpired(msg);
62  }
63  else {
64  TCPSocket *socket = socketMap.findSocketFor(msg);
65 
66  if (!socket) {
67  // new connection -- create new socket object and server process
68  socket = new TCPSocket(msg);
69  socket->setOutputGate(gate("tcpOut"));
70 
71  const char *serverThreadClass = par("serverThreadClass");
72  TCPServerThreadBase *proc =
73  check_and_cast<TCPServerThreadBase *>(inet::utils::createOne(serverThreadClass));
74 
75  socket->setCallbackObject(proc);
76  proc->init(this, socket);
77 
78  socketMap.addSocket(socket);
79  }
80 
81  socket->processMessage(msg);
82  }
83 }
TCPSocketMap socketMap
Definition: TCPSrvHostApp.h:41
cObject * createOne(const char *className, const char *defaultNamespace)
Like cObjectFactory::createOne(), except it starts searching for the class in the given namespace...
Definition: INETUtils.cc:105
void addSocket(TCPSocket *socket)
Registers the given socket.
Definition: TCPSocketMap.cc:36
TCPSocket * findSocketFor(cMessage *msg)
Finds the socket (by connId) for the given message.
Definition: TCPSocketMap.cc:24
virtual bool inet::TCPSrvHostApp::handleOperationStage ( LifecycleOperation operation,
int  stage,
IDoneCallback doneCallback 
)
inlineoverrideprotectedvirtual

Perform one stage of a lifecycle operation.

Processing may be done entirely within this method, or may be a longer process that involves nonzero simulation time or several events, and is triggered by this method call.

Return value: true = "done"; false = "not yet done, will invoke doneCallback when done"

Implements inet::ILifecycle.

50  { Enter_Method_Silent(); throw cRuntimeError("Unsupported lifecycle operation '%s'", operation->getClassName()); return true; }
void inet::TCPSrvHostApp::initialize ( int  stage)
overrideprotectedvirtual
30 {
31  cSimpleModule::initialize(stage);
32 
33  if (stage == INITSTAGE_APPLICATION_LAYER) {
34  const char *localAddress = par("localAddress");
35  int localPort = par("localPort");
36 
37  serverSocket.setOutputGate(gate("tcpOut"));
39  serverSocket.bind(localAddress[0] ? L3Address(localAddress) : L3Address(), localPort);
41 
42  bool isOperational;
43  NodeStatus *nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
44  isOperational = (!nodeStatus) || nodeStatus->getState() == NodeStatus::UP;
45  if (!isOperational)
46  throw cRuntimeError("This module doesn't support starting in node DOWN state");
47  }
48 }
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
void bind(int localPort)
Bind the socket to a local port number.
Definition: TCPSocket.cc:101
TCPSocket serverSocket
Definition: TCPSrvHostApp.h:40
void listen(bool fork)
Definition: TCPSocket.cc:127
void setOutputGate(cGate *toTcp)
Sets the gate on which to send to TCP.
Definition: TCPSocket.h:228
void readDataTransferModePar(cComponent &component)
Read "dataTransferMode" parameter from ini/ned, and set dataTransferMode member value.
Definition: TCPSocket.cc:355
Initialization of applications.
Definition: InitStages.h:106
Definition: NodeStatus.h:40
virtual int inet::TCPSrvHostApp::numInitStages ( ) const
inlineoverrideprotectedvirtual
44 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::TCPSrvHostApp::refreshDisplay ( ) const
overrideprotectedvirtual
51 {
52  char buf[32];
53  sprintf(buf, "%d threads", socketMap.size());
54  getDisplayString().setTagArg("t", 0, buf);
55 }
TCPSocketMap socketMap
Definition: TCPSrvHostApp.h:41
unsigned int size() const
Returns the number of sockets stored.
Definition: TCPSocketMap.h:73
void inet::TCPSrvHostApp::removeThread ( TCPServerThreadBase thread)
virtual

Referenced by inet::TCPServerThreadBase::closed(), and inet::TCPServerThreadBase::failure().

90 {
91  // remove socket
92  socketMap.removeSocket(thread->getSocket());
93 
94  // remove thread object
95  delete thread;
96 }
TCPSocketMap socketMap
Definition: TCPSrvHostApp.h:41
TCPSocket * removeSocket(TCPSocket *socket)
Removes the given socket from the data structure.
Definition: TCPSocketMap.cc:42

Member Data Documentation

TCPSocket inet::TCPSrvHostApp::serverSocket
protected

Referenced by initialize().

TCPSocketMap inet::TCPSrvHostApp::socketMap
protected

The documentation for this class was generated from the following files: