BaseTcpSupport.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2010 Institut fuer Telematik, Karlsruher Institut fuer Technologie (KIT)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #ifndef BASETCPSUPPORT_H_
00025 #define BASETCPSUPPORT_H_
00026 
00027 #include <omnetpp.h>
00028 #include <map>
00029 #include <TransportAddress.h>
00030 #include <TCPSocket.h>
00031 #include <ExtTCPSocketMap.h>
00032 
00033 class BaseTcpSupport : public TCPSocket::CallbackInterface
00034 {
00035 public:
00036 
00037     // Event codes
00038     enum EvCode {NO_EST_CONNECTION, PEER_CLOSED, PEER_TIMEDOUT, PEER_REFUSED, CONNECTION_RESET, CONNECTION_SUCC_ClOSED};
00039 
00040     // Utility methods of the CallbackInterface
00041     virtual void socketDataArrived(int connId, void *yourPtr, cPacket *msg, bool urgent);
00042     virtual void socketEstablished(int connId, void *yourPtr);
00043     virtual void socketPeerClosed(int connId, void *yourPtr);
00044 //    virtual void socketClosed(int connId, void *yourPtr);
00045     virtual void socketFailure(int connId, void *yourPtr, int code);
00046     virtual void socketStatusArrived(int connId, void *yourPtr, TCPStatusInfo *status) {delete status;}
00047 
00048 protected:
00049 
00053     void handleTCPMessage(cMessage* msg);
00054 
00060     void bindAndListenTcp(int port);
00061 
00067     bool isAlreadyConnected(TransportAddress address);
00068 
00074     void establishTcpConnection(TransportAddress address);
00075 
00082     void sendTcpData(cPacket* msg, TransportAddress address);
00083 
00090     virtual void handleConnectionEvent(EvCode code, TransportAddress address);
00091 
00099     virtual void handleDataReceived(TransportAddress address, cPacket* msg, bool urgent);
00100 
00106     virtual void handleIncomingConnection(TransportAddress address);
00107 
00113     void closeTcpConnection(TransportAddress address);
00114 
00120     void setTcpOut(cGate* gate) {tcpOut = gate;}
00121 
00127     cGate* getTcpOut() {return tcpOut;}
00128 
00129 private:
00130 
00131     ExtTCPSocketMap sockets;  
00132     typedef std::vector<cMessage*> msgQueue;
00133     typedef std::map<TransportAddress, msgQueue*> transQueue;
00134     transQueue queuedTx; 
00135 
00136     cGate* tcpOut; 
00137 };
00138 
00139 #endif /* BASETCPSUPPORT_H_ */