| 1 |
#ifndef _PROTO_SIM_AGENT |
| 2 |
#define _PROTO_SIM_AGENT |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
|
| 6 |
#include "protoTimer.h" |
| 7 |
#include "protoSocket.h" |
| 8 |
|
| 9 |
/** |
| 10 |
* @class ProtoSimAgent |
| 11 |
* |
| 12 |
* @brief Provides a base class for developing |
| 13 |
* support for Protolib code in various network simulation environments |
| 14 |
*(e.g. ns-2, OPNET, etc). |
| 15 |
* |
| 16 |
* Protolib-based code must init its |
| 17 |
* ProtoTimerMgr instances such that |
| 18 |
*/ |
| 19 |
class ProtoSimAgent : public ProtoSocket::Notifier, public ProtoTimerMgr |
| 20 |
{ |
| 21 |
public: |
| 22 |
virtual ~ProtoSimAgent(); |
| 23 |
|
| 24 |
/*void ActivateTimer(ProtoTimer& theTimer) |
| 25 |
{timer_mgr.ActivateTimer(theTimer);} |
| 26 |
|
| 27 |
void DeactivateTimer(ProtoTimer& theTimer) |
| 28 |
{timer_mgr.DeactivateTimer(theTimer);}*/ |
| 29 |
|
| 30 |
ProtoSocket::Notifier& GetSocketNotifier() |
| 31 |
{ |
| 32 |
return static_cast<ProtoSocket::Notifier&>(*this); |
| 33 |
} |
| 34 |
|
| 35 |
ProtoTimerMgr& GetTimerMgr() |
| 36 |
{ |
| 37 |
return static_cast<ProtoTimerMgr&>(*this); |
| 38 |
//return timer_mgr; |
| 39 |
} |
| 40 |
|
| 41 |
virtual bool GetLocalAddress(ProtoAddress& localAddr) = 0; |
| 42 |
virtual bool InvokeSystemCommand(const char* cmd) { |
| 43 |
fprintf(stderr,"ProtoSimAgent: invoking system command \"%s\"\n", cmd); |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
friend class ProtoSocket; |
| 48 |
|
| 49 |
/** |
| 50 |
* @class SocketProxy |
| 51 |
* |
| 52 |
* @brief ProtoSocket Proxy class. Provides a liason between a ProtoSocket |
| 53 |
* instance and a corresponding simulation transport "Agent" instance. |
| 54 |
*/ |
| 55 |
class SocketProxy : public ProtoSocket::Proxy |
| 56 |
{ |
| 57 |
// I.T. Added 27/2/07 |
| 58 |
int socketProxyID_; |
| 59 |
|
| 60 |
friend class ProtoSimAgent; |
| 61 |
public: |
| 62 |
virtual ~SocketProxy(); |
| 63 |
|
| 64 |
virtual bool Bind(UINT16& thePort) = 0; |
| 65 |
|
| 66 |
virtual bool Connect(const ProtoAddress& theAddress) = 0; // JPH 7/11/2006 - TCP support |
| 67 |
virtual bool Accept(ProtoSocket* theSocket) = 0; // JPH 7/11/2006 - TCP support |
| 68 |
virtual bool Listen(UINT16 thePort) = 0; // JPH 7/11/2006 - TCP support |
| 69 |
virtual bool Shutdown()=0; // a TCP socket needs to be able to shutdown |
| 70 |
|
| 71 |
virtual bool SendTo(const char* buffer, |
| 72 |
unsigned int& numBytes, |
| 73 |
const ProtoAddress& dstAddr) = 0; |
| 74 |
virtual bool RecvFrom(char* buffer, |
| 75 |
unsigned int& numBytes, |
| 76 |
ProtoAddress& srcAddr) = 0; |
| 77 |
|
| 78 |
virtual bool JoinGroup(const ProtoAddress& groupAddr) = 0; |
| 79 |
virtual bool LeaveGroup(const ProtoAddress& groupAddr) = 0; |
| 80 |
virtual void SetTTL(unsigned char ttl) = 0; |
| 81 |
virtual void SetLoopback(bool loopback) = 0; |
| 82 |
// Override this in your socket proxy if ecn-capable transport |
| 83 |
virtual bool SetEcnCapable(bool state) {return false;} |
| 84 |
virtual bool GetEcnStatus() const {return false;} |
| 85 |
|
| 86 |
// I.T Modified these - they must be involved in the node that want these values |
| 87 |
// better to implement at sub-classes |
| 88 |
virtual bool SetTxBufferSize(unsigned int bufferSize) { return false; } // I.T. modified 7/17/08 |
| 89 |
virtual unsigned int GetTxBufferSize() { return 0; } // I.T. modified 7/17/08 |
| 90 |
virtual bool SetRxBufferSize(unsigned int bufferSize) { return false; }; // I.T. modified 7/17/08 |
| 91 |
virtual unsigned int GetRxBufferSize() { return 0; } // I.T. I.T. modified 7/17/08 |
| 92 |
|
| 93 |
// This method should return whether output notification is enabled. |
| 94 |
// Default is false since but this should be over-riden by the underlying SocketProxy implementations |
| 95 |
virtual bool SetOutputNotification(bool outputnotify) { return false; } // I.T. modified 7/30/08 |
| 96 |
|
| 97 |
ProtoSocket* GetSocket() {return proto_socket;} |
| 98 |
UINT16 GetPort() |
| 99 |
{return (proto_socket ? proto_socket->GetPort() : 0);} |
| 100 |
|
| 101 |
// I.T. Added generic labelling and access mechanism - 27/2/07 |
| 102 |
/** |
| 103 |
* Sets the internal identifier for this socket. You can use the findBySocketProxyID |
| 104 |
* method to find the socket you name with this id |
| 105 |
*/ |
| 106 |
void SetSocketProxyID(int ID) { socketProxyID_=ID; } |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets the internal identifier for this socket. |
| 110 |
*/ |
| 111 |
int GetSocketProxyID() { return socketProxyID_; } |
| 112 |
|
| 113 |
protected: |
| 114 |
SocketProxy(); |
| 115 |
void AttachSocket(ProtoSocket& protoSocket) |
| 116 |
{proto_socket = &protoSocket;} |
| 117 |
|
| 118 |
SocketProxy* GetPrev() {return prev;} |
| 119 |
void SetPrev(SocketProxy* broker) {prev = broker;} |
| 120 |
SocketProxy* GetNext() {return next;} |
| 121 |
void SetNext(SocketProxy* broker) {next = broker;} |
| 122 |
/** |
| 123 |
* @class SocketProxy::List |
| 124 |
* |
| 125 |
* @brief Maintains a list of socket proxies. |
| 126 |
*/ |
| 127 |
|
| 128 |
class List |
| 129 |
{ |
| 130 |
public: |
| 131 |
List(); |
| 132 |
void Prepend(SocketProxy& broker); |
| 133 |
void Remove(SocketProxy& broker); |
| 134 |
SocketProxy* FindProxyByPort(UINT16 thePort); |
| 135 |
|
| 136 |
// Added by I.T. 27/feb 2007 |
| 137 |
/** |
| 138 |
* Finds the Socket proxy by searching the socket proxies IDs which |
| 139 |
* can be set and retrieved using the GetSocketPrioxyID and |
| 140 |
* GetSocketPrioxyID respectively. |
| 141 |
*/ |
| 142 |
SocketProxy* FindProxyByIdentifier(int socketProxyID); |
| 143 |
|
| 144 |
protected: // JPH 8/14/2006 - allow derived class TcpSockList to inspect head |
| 145 |
// I.T. 27/2/07 - I think a generic search facility should be incuded here ... |
| 146 |
// and keep this private ... I added one above. This could be used by |
| 147 |
// Opnet too rather than having subclasses have to write their own |
| 148 |
SocketProxy* head; |
| 149 |
}; // end class ProtoSimAgent::SocketProxy::List |
| 150 |
friend class List; |
| 151 |
|
| 152 |
ProtoSocket* proto_socket; |
| 153 |
|
| 154 |
SocketProxy* prev; |
| 155 |
SocketProxy* next; |
| 156 |
}; // end class ProtoSimAgent::SocketProxy |
| 157 |
|
| 158 |
protected: |
| 159 |
ProtoSimAgent(); |
| 160 |
|
| 161 |
virtual bool UpdateSystemTimer(ProtoTimer::Command command, |
| 162 |
double delay) = 0; |
| 163 |
|
| 164 |
//void OnSystemTimeout() {timer_mgr.OnSystemTimeout();} |
| 165 |
|
| 166 |
virtual SocketProxy* OpenSocket(ProtoSocket& theSocket) = 0; |
| 167 |
virtual void CloseSocket(ProtoSocket& theSocket) = 0; |
| 168 |
|
| 169 |
private: |
| 170 |
/*class TimerMgr; |
| 171 |
friend class TimerMgr; |
| 172 |
class TimerMgr : public ProtoTimerMgr |
| 173 |
{ |
| 174 |
public: |
| 175 |
TimerMgr(ProtoSimAgent& theAgent); |
| 176 |
bool UpdateSystemTimer(ProtoTimer::Command command, |
| 177 |
double delay) |
| 178 |
{ |
| 179 |
return agent.UpdateSystemTimer(command, delay); |
| 180 |
} |
| 181 |
|
| 182 |
private: |
| 183 |
ProtoSimAgent& agent; |
| 184 |
}; // end class TimerMgr |
| 185 |
|
| 186 |
|
| 187 |
TimerMgr timer_mgr;*/ |
| 188 |
}; // end class ProtoSimAgent |
| 189 |
|
| 190 |
/** |
| 191 |
* @class ProtoMessageSink |
| 192 |
* |
| 193 |
* @brief The ProtoMessageSink is a base class defining a |
| 194 |
* simple interface for generic passing of a buffer |
| 195 |
* (e.g. message). Classes that derive from ProtoMessageSink |
| 196 |
* may be passed messages via the "HandleMessage()" method. |
| 197 |
*/ |
| 198 |
class ProtoMessageSink |
| 199 |
{ |
| 200 |
public: |
| 201 |
virtual ~ProtoMessageSink(); |
| 202 |
virtual bool HandleMessage(const char* buffer, |
| 203 |
unsigned int len, |
| 204 |
const ProtoAddress& srcAddr); |
| 205 |
|
| 206 |
}; // end class ProtoMessageSink |
| 207 |
|
| 208 |
|
| 209 |
#endif // _PROTO_SIM_AGENT |