#include <BaseTcpSupport.h>
Public Types | |
enum | EvCode { NO_EST_CONNECTION, PEER_CLOSED, PEER_TIMEDOUT, PEER_REFUSED, CONNECTION_RESET, CONNECTION_SUCC_ClOSED } |
Public Member Functions | |
virtual void | socketDataArrived (int connId, void *yourPtr, cPacket *msg, bool urgent) |
virtual void | socketEstablished (int connId, void *yourPtr) |
virtual void | socketPeerClosed (int connId, void *yourPtr) |
virtual void | socketFailure (int connId, void *yourPtr, int code) |
virtual void | socketStatusArrived (int connId, void *yourPtr, TCPStatusInfo *status) |
Protected Member Functions | |
void | handleTCPMessage (cMessage *msg) |
Member function to handle incoming TCP messages. | |
void | bindAndListenTcp (int port) |
Member function to bind service to the specified port and listen afterwards. | |
bool | isAlreadyConnected (TransportAddress address) |
Member function to check if the service is already connected. | |
void | establishTcpConnection (TransportAddress address) |
Member function to establish a connection to the specified node. | |
void | sendTcpData (cPacket *msg, TransportAddress address) |
Member function to send TCP data to the specified node. | |
virtual void | handleConnectionEvent (EvCode code, TransportAddress address) |
Member function to handle passive connection events. | |
virtual void | handleDataReceived (TransportAddress address, cPacket *msg, bool urgent) |
Member function to handle incoming data. | |
virtual void | handleIncomingConnection (TransportAddress address) |
Member function to handle newly opened connections. | |
void | closeTcpConnection (TransportAddress address) |
Member function to close an established connection. | |
void | setTcpOut (cGate *gate) |
Member function to set local gate towards the TCP module during init phase. | |
cGate * | getTcpOut () |
Member function to get local gate towards the TCP module. | |
Private Types | |
typedef std::vector< cMessage * > | msgQueue |
typedef std::map < TransportAddress, msgQueue * > | transQueue |
Private Attributes | |
ExtTCPSocketMap | sockets |
Socket map with extended functionality to find sockets. | |
transQueue | queuedTx |
msg queue partitioned by destination | |
cGate * | tcpOut |
local gate towards the TCP module |
Definition at line 33 of file BaseTcpSupport.h.
typedef std::vector<cMessage*> BaseTcpSupport::msgQueue [private] |
Definition at line 132 of file BaseTcpSupport.h.
typedef std::map<TransportAddress, msgQueue*> BaseTcpSupport::transQueue [private] |
Definition at line 133 of file BaseTcpSupport.h.
NO_EST_CONNECTION | |
PEER_CLOSED | |
PEER_TIMEDOUT | |
PEER_REFUSED | |
CONNECTION_RESET | |
CONNECTION_SUCC_ClOSED |
Definition at line 38 of file BaseTcpSupport.h.
{NO_EST_CONNECTION, PEER_CLOSED, PEER_TIMEDOUT, PEER_REFUSED, CONNECTION_RESET, CONNECTION_SUCC_ClOSED};
void BaseTcpSupport::bindAndListenTcp | ( | int | port | ) | [protected] |
Member function to bind service to the specified port and listen afterwards.
port | local portnumber to bind on |
Definition at line 50 of file BaseTcpSupport.cc.
Referenced by TCPExampleApp::initializeApp().
void BaseTcpSupport::closeTcpConnection | ( | TransportAddress | address | ) | [protected] |
Member function to close an established connection.
address | transport address of the remote host |
Definition at line 196 of file BaseTcpSupport.cc.
Referenced by TCPExampleApp::handleDataReceived().
{ if (!isAlreadyConnected(address)) { return; } TCPSocket* oldSocket = sockets.findSocketFor(address.getIp(), address.getPort()); oldSocket->close(); }
void BaseTcpSupport::establishTcpConnection | ( | TransportAddress | address | ) | [protected] |
Member function to establish a connection to the specified node.
address | transport address of the remote host |
Definition at line 75 of file BaseTcpSupport.cc.
Referenced by handleConnectionEvent(), and TCPExampleApp::handleTimerEvent().
{ if (isAlreadyConnected(address)) { return; } TCPSocket* newSocket = new TCPSocket(); newSocket->setOutputGate(getTcpOut()); newSocket->setCallbackObject(this); newSocket->connect(address.getIp(), address.getPort()); sockets.addSocket(newSocket); }
cGate* BaseTcpSupport::getTcpOut | ( | ) | [inline, protected] |
Member function to get local gate towards the TCP module.
Definition at line 127 of file BaseTcpSupport.h.
Referenced by bindAndListenTcp(), establishTcpConnection(), and handleTCPMessage().
{return tcpOut;}
void BaseTcpSupport::handleConnectionEvent | ( | EvCode | code, | |
TransportAddress | address | |||
) | [protected, virtual] |
Member function to handle passive connection events.
code | event code of the current event | |
address | transport address of the remote host |
Reimplemented in TCPExampleApp.
Definition at line 114 of file BaseTcpSupport.cc.
Referenced by sendTcpData(), socketFailure(), and socketPeerClosed().
{ if (code == NO_EST_CONNECTION) { establishTcpConnection(address); } }
void BaseTcpSupport::handleDataReceived | ( | TransportAddress | address, | |
cPacket * | msg, | |||
bool | urgent | |||
) | [protected, virtual] |
Member function to handle incoming data.
address | transport address of the remote host | |
msg | incoming data message | |
urgent | message urgency |
Reimplemented in TCPExampleApp.
Definition at line 121 of file BaseTcpSupport.cc.
Referenced by socketDataArrived().
{ }
void BaseTcpSupport::handleIncomingConnection | ( | TransportAddress | address | ) | [protected, virtual] |
Member function to handle newly opened connections.
address | transport address of the remote host |
Definition at line 126 of file BaseTcpSupport.cc.
Referenced by handleTCPMessage().
{ }
void BaseTcpSupport::handleTCPMessage | ( | cMessage * | msg | ) | [protected] |
Member function to handle incoming TCP messages.
Definition at line 30 of file BaseTcpSupport.cc.
Referenced by BaseOverlay::handleMessage(), and BaseApp::handleMessage().
{ TCPSocket* socket = sockets.findSocketFor(msg); if (socket == NULL) { socket = new TCPSocket(msg); socket->setCallbackObject(this); socket->setOutputGate(getTcpOut()); sockets.addSocket(socket); TransportAddress newAddress = TransportAddress(socket->getRemoteAddress(), socket->getRemotePort()); socket->processMessage(msg); handleIncomingConnection(newAddress); } else { socket->processMessage(msg); } }
bool BaseTcpSupport::isAlreadyConnected | ( | TransportAddress | address | ) | [protected] |
Member function to check if the service is already connected.
address | transport address of the remote host |
Definition at line 64 of file BaseTcpSupport.cc.
Referenced by closeTcpConnection(), establishTcpConnection(), and sendTcpData().
{ TCPSocket* newSocket = sockets.findSocketFor(address.getIp(), address.getPort()); if (newSocket == NULL) { return false; } else if (newSocket->getState() >= TCPSocket::PEER_CLOSED) { return false; } else return true; }
void BaseTcpSupport::sendTcpData | ( | cPacket * | msg, | |
TransportAddress | address | |||
) | [protected] |
Member function to send TCP data to the specified node.
msg | data message | |
address | transport address of the remote host |
Definition at line 89 of file BaseTcpSupport.cc.
Referenced by TCPExampleApp::handleDataReceived(), and TCPExampleApp::handleTimerEvent().
{ if (!isAlreadyConnected(address)) { handleConnectionEvent(NO_EST_CONNECTION, address); return; } TCPSocket* socket = sockets.findSocketFor(address.getIp(), address.getPort()); if (socket->getState() == TCPSocket::CONNECTED) { socket->send(msg); } transQueue::iterator tx = queuedTx.find(address); if (tx != queuedTx.end()) { tx->second->push_back(msg); } else { msgQueue* newQueue = new msgQueue(); newQueue->push_back(msg); queuedTx[address] = newQueue; } }
void BaseTcpSupport::setTcpOut | ( | cGate * | gate | ) | [inline, protected] |
Member function to set local gate towards the TCP module during init phase.
gate | local gate towards the TCP module |
Definition at line 120 of file BaseTcpSupport.h.
Referenced by BaseOverlay::initialize(), and BaseApp::initialize().
{tcpOut = gate;}
void BaseTcpSupport::socketDataArrived | ( | int | connId, | |
void * | yourPtr, | |||
cPacket * | msg, | |||
bool | urgent | |||
) | [virtual] |
Definition at line 130 of file BaseTcpSupport.cc.
{ TCPSocket* socket = sockets.findSocketFor(connId); TransportAddress remoteAddress(socket->getRemoteAddress(), socket->getRemotePort()); handleDataReceived(remoteAddress, msg, urgent); }
void BaseTcpSupport::socketEstablished | ( | int | connId, | |
void * | yourPtr | |||
) | [virtual] |
Definition at line 140 of file BaseTcpSupport.cc.
{ TCPSocket* socket = sockets.findSocketFor(connId); if (socket == NULL) { return; } TransportAddress remoteAddress(socket->getRemoteAddress(), socket->getRemotePort()); transQueue::iterator tx = queuedTx.find(remoteAddress); if (tx != queuedTx.end()) { for (uint32 i = 0 ; i < tx->second->size(); i++) { socket->send(tx->second->at(i)); } tx->second->clear(); delete tx->second; queuedTx.erase(remoteAddress); } }
void BaseTcpSupport::socketFailure | ( | int | connId, | |
void * | yourPtr, | |||
int | code | |||
) | [virtual] |
Definition at line 179 of file BaseTcpSupport.cc.
{ TCPSocket* socket = sockets.findSocketFor(connId); TransportAddress remoteAddress(socket->getRemoteAddress(), socket->getRemotePort()); if (code == TCP_I_CONNECTION_REFUSED) { handleConnectionEvent(PEER_REFUSED, remoteAddress); } else if (code == TCP_I_TIMED_OUT) { handleConnectionEvent(PEER_TIMEDOUT, remoteAddress); } else if (code == TCP_I_CONNECTION_RESET) { handleConnectionEvent(CONNECTION_RESET, remoteAddress); } else { throw new cRuntimeError("Invalid error code on socketFailure."); } }
void BaseTcpSupport::socketPeerClosed | ( | int | connId, | |
void * | yourPtr | |||
) | [virtual] |
Definition at line 164 of file BaseTcpSupport.cc.
{ TCPSocket* socket = sockets.findSocketFor(connId); TransportAddress remoteAddress(socket->getRemoteAddress(), socket->getRemotePort()); if (socket->getState() == TCPSocket::PEER_CLOSED) { socket->close(); handleConnectionEvent(PEER_CLOSED, remoteAddress); } else if (socket->getState() == TCPSocket::CLOSED) { sockets.removeSocket(socket); handleConnectionEvent(CONNECTION_SUCC_ClOSED, remoteAddress); } }
virtual void BaseTcpSupport::socketStatusArrived | ( | int | connId, | |
void * | yourPtr, | |||
TCPStatusInfo * | status | |||
) | [inline, virtual] |
Definition at line 46 of file BaseTcpSupport.h.
{delete status;}
transQueue BaseTcpSupport::queuedTx [private] |
msg queue partitioned by destination
Definition at line 134 of file BaseTcpSupport.h.
Referenced by sendTcpData(), and socketEstablished().
ExtTCPSocketMap BaseTcpSupport::sockets [private] |
Socket map with extended functionality to find sockets.
Definition at line 131 of file BaseTcpSupport.h.
Referenced by bindAndListenTcp(), closeTcpConnection(), establishTcpConnection(), handleTCPMessage(), isAlreadyConnected(), sendTcpData(), socketDataArrived(), socketEstablished(), socketFailure(), and socketPeerClosed().
cGate* BaseTcpSupport::tcpOut [private] |
local gate towards the TCP module
Definition at line 136 of file BaseTcpSupport.h.
Referenced by getTcpOut(), and setTcpOut().