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

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

#include <TcpServerHostApp.h>

Inheritance diagram for inet::TcpServerHostApp:
inet::ApplicationBase inet::TcpSocket::ICallback inet::OperationalBase inet::OperationalMixin< cSimpleModule > inet::ILifecycle inet::TcpEchoApp inet::TcpSinkApp

Public Member Functions

virtual ~TcpServerHostApp ()
 
virtual void removeThread (TcpServerThreadBase *thread)
 
virtual void threadClosed (TcpServerThreadBase *thread)
 
- Public Member Functions inherited from inet::ApplicationBase
 ApplicationBase ()
 
- Public Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual ~OperationalMixin ()
 }@ More...
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 
- Public Member Functions inherited from inet::TcpSocket::ICallback
virtual ~ICallback ()
 

Protected Types

typedef std::set< TcpServerThreadBase * > ThreadSet
 
- Protected Types inherited from inet::OperationalMixin< cSimpleModule >
enum  State
 

Protected Member Functions

virtual void initialize (int stage) override
 
virtual int numInitStages () const override
 
virtual void handleMessageWhenUp (cMessage *msg) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
virtual void socketDataArrived (TcpSocket *socket, Packet *packet, bool urgent) override
 Notifies about data arrival, packet ownership is transferred to the callee. More...
 
virtual void socketAvailable (TcpSocket *socket, TcpAvailableInfo *availableInfo) override
 
virtual void socketEstablished (TcpSocket *socket) override
 
virtual void socketPeerClosed (TcpSocket *socket) override
 
virtual void socketClosed (TcpSocket *socket) override
 
virtual void socketFailure (TcpSocket *socket, int code) override
 
virtual void socketStatusArrived (TcpSocket *socket, TcpStatusInfo *status) override
 
virtual void socketDeleted (TcpSocket *socket) override
 
virtual void handleStartOperation (LifecycleOperation *operation) override
 
virtual void handleStopOperation (LifecycleOperation *operation) override
 
virtual void handleCrashOperation (LifecycleOperation *operation) override
 
- Protected Member Functions inherited from inet::ApplicationBase
virtual bool isInitializeStage (int stage) const override
 
virtual bool isModuleStartStage (int stage) const override
 
virtual bool isModuleStopStage (int stage) const override
 
- Protected Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual int numInitStages () const override
 
virtual void refreshDisplay () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void handleMessageWhenDown (cMessage *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
virtual State getInitialOperationalState () const
 Returns initial operational state: OPERATING or NOT_OPERATING. More...
 
virtual void handleActiveOperationTimeout (cMessage *message)
 
virtual bool isUp () const
 utility functions More...
 
virtual bool isDown () const
 
virtual void setOperationalState (State newState)
 
virtual void scheduleOperationTimeout (simtime_t timeout)
 
virtual void setupActiveOperation (LifecycleOperation *operation, IDoneCallback *doneCallback, State)
 
virtual void delayActiveOperationFinish (simtime_t timeout)
 
virtual void startActiveOperationExtraTime (simtime_t delay=SIMTIME_ZERO)
 
virtual void startActiveOperationExtraTimeOrFinish (simtime_t extraTime)
 
virtual void finishActiveOperation ()
 

Protected Attributes

TcpSocket serverSocket
 
SocketMap socketMap
 
ThreadSet threadSet
 
- Protected Attributes inherited from inet::OperationalMixin< cSimpleModule >
State operationalState
 
simtime_t lastChange
 
Operation activeOperation
 
cMessage * activeOperationTimeout
 
cMessage * activeOperationExtraTimer
 

Friends

class TcpServerThreadBase
 

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 Typedef Documentation

◆ ThreadSet

Constructor & Destructor Documentation

◆ ~TcpServerHostApp()

virtual inet::TcpServerHostApp::~TcpServerHostApp ( )
inlinevirtual

Member Function Documentation

◆ finish()

void inet::TcpServerHostApp::finish ( )
overrideprotectedvirtual

Reimplemented in inet::TcpEchoApp, and inet::TcpSinkApp.

90 {
91  // remove and delete threads
92  while (!threadSet.empty())
93  removeThread(*threadSet.begin());
94 }

Referenced by inet::TcpSinkApp::finish(), and inet::TcpEchoApp::finish().

◆ handleCrashOperation()

void inet::TcpServerHostApp::handleCrashOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

45 {
46  // remove and delete threads
47  while (!threadSet.empty()) {
48  auto thread = *threadSet.begin();
49  // TODO destroy!!!
50  thread->getSocket()->close();
51  removeThread(thread);
52  }
53  // TODO always?
54  if (operation->getRootModule() != getContainingNode(this))
56 }

◆ handleMessageWhenUp()

void inet::TcpServerHostApp::handleMessageWhenUp ( cMessage *  msg)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

68 {
69  if (msg->isSelfMessage()) {
70  TcpServerThreadBase *thread = (TcpServerThreadBase *)msg->getContextPointer();
71  if (!contains(threadSet, thread))
72  throw cRuntimeError("Invalid thread pointer in the timer (msg->contextPointer is invalid)");
73  thread->timerExpired(msg);
74  }
75  else {
76  TcpSocket *socket = check_and_cast_nullable<TcpSocket *>(socketMap.findSocketFor(msg));
77  if (socket)
78  socket->processMessage(msg);
79  else if (serverSocket.belongsToSocket(msg))
81  else {
82 // throw cRuntimeError("Unknown incoming message: '%s'", msg->getName());
83  EV_ERROR << "message " << msg->getFullName() << "(" << msg->getClassName() << ") arrived for unknown socket \n";
84  delete msg;
85  }
86  }
87 }

◆ handleStartOperation()

void inet::TcpServerHostApp::handleStartOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

26 {
27  const char *localAddress = par("localAddress");
28  int localPort = par("localPort");
29 
30  serverSocket.setOutputGate(gate("socketOut"));
32  serverSocket.bind(localAddress[0] ? L3Address(localAddress) : L3Address(), localPort);
34 }

◆ handleStopOperation()

void inet::TcpServerHostApp::handleStopOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

37 {
38  for (auto thread : threadSet)
39  thread->getSocket()->close();
41  delayActiveOperationFinish(par("stopOperationTimeout"));
42 }

◆ initialize()

void inet::TcpServerHostApp::initialize ( int  stage)
overrideprotectedvirtual

◆ numInitStages()

virtual int inet::TcpServerHostApp::numInitStages ( ) const
inlineoverrideprotectedvirtual

Reimplemented in inet::TcpEchoApp, and inet::TcpSinkApp.

34 { return NUM_INIT_STAGES; }

◆ refreshDisplay()

void inet::TcpServerHostApp::refreshDisplay ( ) const
overrideprotectedvirtual

Reimplemented in inet::TcpEchoApp, and inet::TcpSinkApp.

59 {
61 
62  char buf[32];
63  sprintf(buf, "%d threads", socketMap.size());
64  getDisplayString().setTagArg("t", 0, buf);
65 }

◆ removeThread()

void inet::TcpServerHostApp::removeThread ( TcpServerThreadBase thread)
virtual
126 {
127  // remove socket
128  socketMap.removeSocket(thread->getSocket());
129  threadSet.erase(thread);
130 
131  // remove thread object
132  thread->deleteModule();
133 }

Referenced by inet::TcpServerThreadBase::failure(), finish(), and handleCrashOperation().

◆ socketAvailable()

void inet::TcpServerHostApp::socketAvailable ( TcpSocket socket,
TcpAvailableInfo availableInfo 
)
overrideprotectedvirtual

Implements inet::TcpSocket::ICallback.

97 {
98  // new TCP connection -- create new socket object and server process
99  TcpSocket *newSocket = new TcpSocket(availableInfo);
100  newSocket->setOutputGate(gate("socketOut"));
101 
102  const char *serverThreadModuleType = par("serverThreadModuleType");
103  cModuleType *moduleType = cModuleType::get(serverThreadModuleType);
104  char name[80];
105  sprintf(name, "thread_%i", newSocket->getSocketId());
106  TcpServerThreadBase *proc = check_and_cast<TcpServerThreadBase *>(moduleType->create(name, this));
107  proc->finalizeParameters();
108  proc->callInitialize();
109 
110  newSocket->setCallback(proc);
111  proc->init(this, newSocket);
112 
113  socketMap.addSocket(newSocket);
114  threadSet.insert(proc);
115 
116  socket->accept(availableInfo->getNewSocketId());
117 }

◆ socketClosed()

void inet::TcpServerHostApp::socketClosed ( TcpSocket socket)
overrideprotectedvirtual

Implements inet::TcpSocket::ICallback.

120 {
121  if (operationalState == State::STOPPING_OPERATION && threadSet.empty() && !serverSocket.isOpen())
122  startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
123 }

Referenced by threadClosed().

◆ socketDataArrived()

virtual void inet::TcpServerHostApp::socketDataArrived ( TcpSocket socket,
Packet packet,
bool  urgent 
)
inlineoverrideprotectedvirtual

Notifies about data arrival, packet ownership is transferred to the callee.

Implements inet::TcpSocket::ICallback.

39 { throw cRuntimeError("Unexpected data"); }

◆ socketDeleted()

virtual void inet::TcpServerHostApp::socketDeleted ( TcpSocket socket)
inlineoverrideprotectedvirtual

◆ socketEstablished()

virtual void inet::TcpServerHostApp::socketEstablished ( TcpSocket socket)
inlineoverrideprotectedvirtual

Implements inet::TcpSocket::ICallback.

41 {}

◆ socketFailure()

virtual void inet::TcpServerHostApp::socketFailure ( TcpSocket socket,
int  code 
)
inlineoverrideprotectedvirtual

Implements inet::TcpSocket::ICallback.

44 {}

◆ socketPeerClosed()

virtual void inet::TcpServerHostApp::socketPeerClosed ( TcpSocket socket)
inlineoverrideprotectedvirtual

Implements inet::TcpSocket::ICallback.

42 {}

◆ socketStatusArrived()

virtual void inet::TcpServerHostApp::socketStatusArrived ( TcpSocket socket,
TcpStatusInfo status 
)
inlineoverrideprotectedvirtual

Implements inet::TcpSocket::ICallback.

45 {}

◆ threadClosed()

void inet::TcpServerHostApp::threadClosed ( TcpServerThreadBase thread)
virtual
136 {
137  // remove socket
138  socketMap.removeSocket(thread->getSocket());
139  threadSet.erase(thread);
140 
141  socketClosed(thread->getSocket());
142 
143  // remove thread object
144  thread->deleteModule();
145 }

Referenced by inet::TcpServerThreadBase::socketClosed().

Friends And Related Function Documentation

◆ TcpServerThreadBase

friend class TcpServerThreadBase
friend

Member Data Documentation

◆ serverSocket

TcpSocket inet::TcpServerHostApp::serverSocket
protected

◆ socketMap

◆ threadSet


The documentation for this class was generated from the following files:
inet::OperationalMixin< cSimpleModule >::operationalState
State operationalState
Definition: OperationalMixin.h:23
inet::TcpServerHostApp::serverSocket
TcpSocket serverSocket
Definition: TcpServerHostApp.h:28
inet::TcpServerHostApp::threadSet
ThreadSet threadSet
Definition: TcpServerHostApp.h:31
inet::TcpSocket::setCallback
void setCallback(ICallback *cb)
Sets a callback object, to be used with processMessage().
Definition: TcpSocket.cc:276
inet::TcpServerHostApp::TcpServerThreadBase
friend class TcpServerThreadBase
Definition: TcpServerHostApp.h:57
inet::getContainingNode
cModule * getContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:40
inet::OperationalMixin< cSimpleModule >::initialize
virtual void initialize(int stage) override
Definition: OperationalMixinImpl.h:26
inet::TcpSocket::isOpen
virtual bool isOpen() const override
Definition: TcpSocket.cc:257
inet::TcpServerHostApp::socketMap
SocketMap socketMap
Definition: TcpServerHostApp.h:29
inet::SocketMap::deleteSockets
void deleteSockets()
Deletes the socket objects.
Definition: SocketMap.cc:44
inet::TcpSocket::belongsToSocket
virtual bool belongsToSocket(cMessage *msg) const override
Returns true if the message belongs to this socket instance (message has a TcpCommand as getControlIn...
Definition: TcpSocket.cc:365
inet::SocketMap::findSocketFor
ISocket * findSocketFor(cMessage *msg)
Finds the socket for the given message.
Definition: SocketMap.cc:19
inet::SocketMap::addSocket
void addSocket(ISocket *socket)
Adds the given socket.
Definition: SocketMap.cc:28
inet::SocketMap::size
unsigned int size() const
Returns the number of sockets stored.
Definition: SocketMap.h:52
inet::contains
bool contains(const std::vector< T > &v, const Tk &a)
Definition: stlutils.h:65
inet::TcpServerHostApp::removeThread
virtual void removeThread(TcpServerThreadBase *thread)
Definition: TcpServerHostApp.cc:125
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
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::TcpSocket::destroy
virtual void destroy() override
Destroy the connection.
Definition: TcpSocket.cc:186
inet::OperationalMixin< cSimpleModule >::delayActiveOperationFinish
virtual void delayActiveOperationFinish(simtime_t timeout)
Definition: OperationalMixinImpl.h:161
inet::OperationalMixin< cSimpleModule >::startActiveOperationExtraTimeOrFinish
virtual void startActiveOperationExtraTimeOrFinish(simtime_t extraTime)
Definition: OperationalMixinImpl.h:179
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::TcpSocket::close
void close() override
Closes the local end of the connection.
Definition: TcpSocket.cc:161
inet::OperationalMixin< cSimpleModule >::refreshDisplay
virtual void refreshDisplay() const override
Definition: OperationalMixinImpl.h:200
inet::TcpServerHostApp::socketClosed
virtual void socketClosed(TcpSocket *socket) override
Definition: TcpServerHostApp.cc:119
inet::SocketMap::removeSocket
ISocket * removeSocket(ISocket *socket)
Removes the given socket.
Definition: SocketMap.cc:34