| 1 |
#ifndef _NS_PROTO_SIM_AGENT |
| 2 |
#define _NS_PROTO_SIM_AGENT |
| 3 |
|
| 4 |
#include "protoSimAgent.h" |
| 5 |
#include "protoSocket.h" |
| 6 |
#include "protoTimer.h" |
| 7 |
#include "agent.h" // for class Agent |
| 8 |
#include "timer-handler.h" // for class TimerHandler |
| 9 |
#include "udp.h" // for class UdpAgent |
| 10 |
/** |
| 11 |
* @class NsProtoSimAgent |
| 12 |
* |
| 13 |
* @brief The "NsProtoSimAgent" plays a role similar to that of the |
| 14 |
* "ProtoDispatcher" class which provides liason between Protolib |
| 15 |
* classes and the computer operating system. |
| 16 |
* |
| 17 |
* ns-2 agents based on Protolib code can use this as a base |
| 18 |
* class. ProtoSockets used by the Protolib code _must_ have |
| 19 |
* their "notifier" set as a pointer to their corresponding |
| 20 |
* NsProtoSimAgent instance. Similarly, ProtoTimerMgrs used by the |
| 21 |
* Protolib code _must_ have their "installer" set to their |
| 22 |
* corresponding "NsProtoSimAgent" instance. |
| 23 |
*/ |
| 24 |
class NsProtoSimAgent : public Agent, public TimerHandler, public ProtoSimAgent |
| 25 |
{ |
| 26 |
public: |
| 27 |
virtual ~NsProtoSimAgent(); |
| 28 |
|
| 29 |
virtual bool OnStartup(int argc, const char*const* argv) = 0; |
| 30 |
virtual bool ProcessCommands(int argc, const char*const* argv) = 0; |
| 31 |
virtual void OnShutdown() = 0; |
| 32 |
|
| 33 |
// Some helper methods |
| 34 |
void ActivateTimer(ProtoTimer& theTimer) |
| 35 |
{ProtoSimAgent::ActivateTimer(theTimer);} |
| 36 |
|
| 37 |
void DeactivateTimer(ProtoTimer& theTimer) |
| 38 |
{ProtoSimAgent::DeactivateTimer(theTimer);} |
| 39 |
|
| 40 |
ProtoSocket::Notifier& GetSocketNotifier() |
| 41 |
{return ProtoSimAgent::GetSocketNotifier();} |
| 42 |
|
| 43 |
ProtoTimerMgr& GetTimerMgr() |
| 44 |
{return ProtoSimAgent::GetTimerMgr();} |
| 45 |
|
| 46 |
// Override ProtoTimerMgr::GetSystemTime() |
| 47 |
virtual void GetSystemTime(struct timeval& currentTime) |
| 48 |
{ |
| 49 |
ASSERT(scheduler); |
| 50 |
double now = scheduler->clock(); |
| 51 |
currentTime.tv_sec = (unsigned long)(now); |
| 52 |
currentTime.tv_usec = (unsigned long)((now - currentTime.tv_sec) * 1.0e06); |
| 53 |
} |
| 54 |
|
| 55 |
bool GetLocalAddress(ProtoAddress& localAddr) |
| 56 |
{ |
| 57 |
// if attached to node |
| 58 |
localAddr.SimSetAddress(Agent::addr()); |
| 59 |
localAddr.SetPort(Agent::port()); |
| 60 |
return true; |
| 61 |
// (TB) if not attached to node, return false; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
bool InvokeSystemCommand(const char* cmd); |
| 67 |
|
| 68 |
// I.T. - I made this public so I could subclass it from outside of an NSProtoSimAgent's scope |
| 69 |
// |
| 70 |
// The "NSSocketProxy" provides liason between a "ProtoSocket" |
| 71 |
// instance and a corresponding ns-2 transport "ProtoSocketAgent" |
| 72 |
// instance. |
| 73 |
class NSSocketProxy : public ProtoSimAgent::SocketProxy |
| 74 |
{ |
| 75 |
friend class NsProtoSimAgent; |
| 76 |
|
| 77 |
public: |
| 78 |
virtual ~NSSocketProxy(); |
| 79 |
|
| 80 |
bool Bind(UINT16& thePort); |
| 81 |
bool RecvFrom(char* buffer, unsigned int& numBytes, ProtoAddress& srcAddr); |
| 82 |
|
| 83 |
bool JoinGroup(const ProtoAddress& groupAddr); |
| 84 |
bool LeaveGroup(const ProtoAddress& groupAddr); |
| 85 |
void SetTTL(unsigned char ttl) {mcast_ttl = ttl;} |
| 86 |
void SetLoopback(bool loopback) {mcast_loopback = loopback;} |
| 87 |
bool SetEcnCapable(bool state) |
| 88 |
{ |
| 89 |
ecn_capable = state; |
| 90 |
return true; |
| 91 |
} |
| 92 |
// This returns the ecn status of the last packet read |
| 93 |
bool GetEcnStatus() const |
| 94 |
{return ecn_status;} |
| 95 |
|
| 96 |
virtual bool SendTo(const char* buffer, |
| 97 |
unsigned int& numBytes, |
| 98 |
const ProtoAddress& dstAddr) = 0; |
| 99 |
|
| 100 |
// Ian T. Added this function so sockets decide how they should be gracefully closed |
| 101 |
// and detached from Ns-2 |
| 102 |
|
| 103 |
virtual bool Close(); |
| 104 |
virtual bool Shutdown() { return false; } // override this |
| 105 |
|
| 106 |
// virtual const char *DetachSocket(const char *forNodeName)=0; |
| 107 |
|
| 108 |
// (TBD) These need to be implemented properly for TCP support |
| 109 |
virtual bool Connect(const ProtoAddress& theAddress) {return false;} |
| 110 |
virtual bool Accept(ProtoSocket* theSocket) {return false;} |
| 111 |
virtual bool Listen(UINT16 thePort) {return false;} |
| 112 |
|
| 113 |
bool SetNodeColor(const char* color); |
| 114 |
|
| 115 |
// I.T. Added access method for recv_data_offset - used to detect whether |
| 116 |
// there is more data to read from buffer. |
| 117 |
|
| 118 |
UINT16 GetRecvDataOffset() { return recv_data_offset; } // I.T. Added to implement read buffer |
| 119 |
|
| 120 |
// Returns the number of bytes available to read from the socket buffer |
| 121 |
|
| 122 |
UINT16 AvailableToRead() { return recv_data_len-recv_data_offset; } |
| 123 |
|
| 124 |
protected: |
| 125 |
NSSocketProxy(); |
| 126 |
bool ecn_capable; |
| 127 |
bool ecn_status; |
| 128 |
unsigned char mcast_ttl; |
| 129 |
bool mcast_loopback; |
| 130 |
char* recv_data; |
| 131 |
UINT16 recv_data_len; |
| 132 |
UINT16 recv_data_offset; // I.T. Added to implement read buffer |
| 133 |
ProtoAddress src_addr; |
| 134 |
}; // end class NsProtoSimAgent::NSSocketProxy |
| 135 |
|
| 136 |
protected: |
| 137 |
NsProtoSimAgent(); |
| 138 |
|
| 139 |
// override of ns-2 Agent::command() method |
| 140 |
virtual int command(int argc, const char*const* argv); |
| 141 |
|
| 142 |
// override of ns-2 TimerHandler::expire() method |
| 143 |
void expire(Event*) |
| 144 |
{ |
| 145 |
force_cancel(); |
| 146 |
ProtoSimAgent::OnSystemTimeout(); |
| 147 |
} |
| 148 |
|
| 149 |
// override of ProtoSimAgent::UpdateSystemTimer() method |
| 150 |
bool UpdateSystemTimer(ProtoTimer::Command command, |
| 151 |
double delay) |
| 152 |
{ |
| 153 |
switch (command) |
| 154 |
{ |
| 155 |
case ProtoTimer::INSTALL: |
| 156 |
case ProtoTimer::MODIFY: |
| 157 |
//TRACE("rescheduling system timer delay:%lf\n", delay); |
| 158 |
resched(delay); |
| 159 |
break; |
| 160 |
case ProtoTimer::REMOVE: |
| 161 |
//TRACE("canceling system timer\n"); |
| 162 |
force_cancel(); |
| 163 |
break; |
| 164 |
} |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
// here was NSSocketProxy |
| 169 |
|
| 170 |
ProtoSimAgent::SocketProxy* OpenSocket(ProtoSocket& theSocket); |
| 171 |
void CloseSocket(ProtoSocket& theSocket); |
| 172 |
|
| 173 |
friend class ProtoSocketUdpAgentClass; |
| 174 |
/** |
| 175 |
* @class UdpSocketAgent |
| 176 |
* |
| 177 |
* @brief Extends ns-2 UdpAgent to provide interface to NSSocketProxy |
| 178 |
* |
| 179 |
* @note For TCP implementation, see nsTCPProtoSocketAgent |
| 180 |
*/ |
| 181 |
class UdpSocketAgent : public Agent, public NSSocketProxy |
| 182 |
{ |
| 183 |
friend class ProtoSocketUdpAgentClass; |
| 184 |
public: |
| 185 |
bool SendTo(const char* buffer, |
| 186 |
unsigned int& numBytes, |
| 187 |
const ProtoAddress& dstAddr); |
| 188 |
void recv(Packet* pkt, Handler* h); |
| 189 |
|
| 190 |
private: |
| 191 |
UdpSocketAgent(); |
| 192 |
|
| 193 |
}; // end class NsProtoSimAgent::UdpSocketAgent |
| 194 |
|
| 195 |
|
| 196 |
//ProtoSimAgent::SocketProxy::List socket_proxy_list; |
| 197 |
NSSocketProxy::List socket_proxy_list; |
| 198 |
Scheduler* scheduler; |
| 199 |
|
| 200 |
}; // end class NsProtoSimAgent |
| 201 |
|
| 202 |
// See common/protoExample.cpp for an example application/agent |
| 203 |
// using Protolib code. |
| 204 |
|
| 205 |
#endif //_NS_PROTO_SIM_AGENT |