Debian Codegraph @

protolib/src/sim/opnet/12.0/opnetProtoSimProcess.h

raw
1 #ifndef _OPNET_PROTO_SIM_PROCESS
2 #define _OPNET_PROTO_SIM_PROCESS
3
4 // The "OpnetProtoSimProcess" plays a role similar to that of the
5 // "ProtoDispatcher" class which provides liason between Protolib
6 // classes and the computer operating system.
7
8 // Opnet agents based on Protolib code can use this as a base
9 // class. ProtoSockets used by the Protolib code _must_ have
10 // their "notifier" set as a pointer to their corresponding
11 // OpnetProtoSimProcess instance. Similarly, ProtoTimerMgrs used by the
12 // Protolib code _must_ have their "installer" set to their
13 // corresponding "OpnetProtoSimProcess" instance.
14
15 #include "protoSimAgent.h"
16 #include "protoSocket.h"
17 #include "protoTimer.h"
18 #include "opnet.h"
19 #include <tcp_api_v3.h>
20 #include "mgen.h"
21
22 #define SELF_INTRT_CODE_TIMEOUT_EVENT 0
23 #define MAX_NUM_TCP_CONNECTIONS 32
24
25 class OpnetProtoMessageSink : public ProtoMessageSink
26 {
27 public:
28 OpnetProtoMessageSink();
29 OpnetProtoMessageSink(Ici* iciptr, int ostrm, int port);
30 ~OpnetProtoMessageSink();
31 bool HandleMessage(const char* txBuffer, unsigned int len,const ProtoAddress& srcAddr);
32
33 private:
34 Ici* app_ici_ptr;
35 int outstrm_to_mgen;
36 int loc_port;
37 };
38
39 class OpnetProtoSimProcess : public ProtoSimAgent
40 {
41 public:
42 // LP 6-13-04 - replaced
43 // virtual ~OpnetProtoSimProcess();
44 ~OpnetProtoSimProcess();
45
46 // end LP
47
48 // These must be overridden by derived OPNET process class
49 virtual bool OnStartup(int argc, const char*const* argv) = 0;
50 virtual bool ProcessCommands(int argc, const char*const* argv) = 0;
51 virtual void OnShutdown() = 0;
52
53 // Some helper methods
54 void ActivateTimer(ProtoTimer& theTimer)
55 {ProtoSimAgent::ActivateTimer(theTimer);}
56
57 void DeactivateTimer(ProtoTimer& theTimer)
58 {ProtoSimAgent::DeactivateTimer(theTimer);}
59
60 ProtoSocket::Notifier& GetSocketNotifier()
61 {return ProtoSimAgent::GetSocketNotifier();}
62
63 ProtoTimerMgr& GetTimerMgr()
64 {return ProtoSimAgent::GetTimerMgr();}
65
66 virtual bool GetLocalAddress(ProtoAddress& localAddr)
67 {
68 localAddr.SimSetAddress((unsigned int)op_id_self());
69 return true;
70 }
71
72 void OnSystemTimeout() {ProtoSimAgent::OnSystemTimeout();}
73
74 // Some OPNET specific utility methods
75 void SetUdpProcessId(Objid udpProcessId)
76 {udp_process_id = udpProcessId;}
77 void SetTcpHostAddress(ProtoAddress hostAddr)
78 {tcp_host_addr = hostAddr;}
79 void SetTcpRemAddress(ProtoAddress remAddr) // JPH 7/27/2006 - TCP support
80 {tcp_ind_rem_addr = remAddr;}
81 void SetTcpAppHandle(ApiT_Tcp_App_Handle tcpHandle)
82 {tcp_handle[0] = tcpHandle;}
83 void SetTcpAppHandle(ApiT_Tcp_App_Handle tcpHandle, int cid)
84 {tcp_handle[cid] = tcpHandle;}
85 void OnReceive(int strm_indx,char* node_name); // JPH 4/11/06 added strm_indx, 5/24/07 added node_name
86 void OnAccept(char* node_name); // JPH 5/25/07
87 SocketProxy* FindProxyByConn(int connId)
88 {return socket_proxy_list.FindProxyByConn(connId);} // JPH 8/13/2006 - TCP support
89
90 // These can be overridden for packet stats, etc ...
91 virtual void ReceivePacketMonitor(Ici* ici, Packet* pkt) {}
92 virtual void TransmitPacketMonitor(Ici* ici, Packet* pkt) {}
93 virtual void HandleMgenMessage(char* buffer,
94 unsigned int len,
95 const ProtoAddress& srcAddr) {};
96
97 protected:
98 OpnetProtoSimProcess();
99
100 // override of ProtoSimAgent::UpdateSystemTimer() method
101 bool UpdateSystemTimer(ProtoTimer::Command command,
102 double delay)
103 {
104 // printf("\t\tOpnetProtoSimProcess.h - Class OpnetProtoSimProces::UpdateSystemTimer(command,delay)\n"); // LP
105 switch (command)
106 {
107 case ProtoTimer::MODIFY:
108 op_ev_cancel(timer_handle);
109 case ProtoTimer::INSTALL:
110 timer_handle =
111 op_intrpt_schedule_self(op_sim_time() + delay, SELF_INTRT_CODE_TIMEOUT_EVENT);
112 break;
113 case ProtoTimer::REMOVE:
114 op_ev_cancel(timer_handle);
115 //timer_handle = (Evhandle)NULL; // ???
116 break;
117 } // end switch
118 return true;
119 } // end bool UpdatesSystemTime()
120
121
122 // The "UdpSocketProxy" provides liason between a UDP "ProtoSocket"
123 // instance and the neighboring OPNET UDP process
124 class UdpSocketProxy;
125 friend UdpSocketProxy;
126 class UdpSocketProxy : public ProtoSimAgent::SocketProxy
127 {
128 friend class OpnetProtoSimProcess;
129
130 public:
131 UdpSocketProxy(OpnetProtoSimProcess* sim_process); // JPH sim_process param
132 virtual ~UdpSocketProxy();
133
134 bool Bind(UINT16& thePort);
135 bool Connect(const ProtoAddress& theAddress){return false;}
136 bool Listen(UINT16 thePort){return false;}
137 bool Accept(ProtoSocket* theSocket){return false;}
138 bool RecvFrom(char* buffer, unsigned int& numBytes, ProtoAddress& srcAddr);
139
140 bool JoinGroup(const ProtoAddress& groupAddr);
141 bool LeaveGroup(const ProtoAddress& groupAddr);
142 void SetTTL(unsigned char ttl) {mcast_ttl = ttl;}
143 void SetLoopback(bool loopback) {mcast_loopback = loopback;}
144 void SetStream(int theStream) {strm_index = theStream;} // JPH 3/30/06
145
146 virtual bool SendTo(const char* buffer,
147 unsigned int& numBytes,
148 const ProtoAddress& dstAddr);
149
150 void OnReceive(char* recvData,
151 unsigned int recvLen,
152 const ProtoAddress& srcAddr,
153 const ProtoAddress& dstAddr);
154 int GetConn(){return 0;}
155
156 protected:
157 //Objid udp_process_id;
158 OpnetProtoSimProcess* sim_process; // JPH need sim_process rather than udp_process_id
159 unsigned char mcast_ttl;
160 bool mcast_loopback;
161 char* recv_data;
162 UINT16 recv_data_len;
163 ProtoAddress src_addr;
164 Ici* udp_command_ici; // LP 6-21-04 - added
165 int strm_index; // JPH 3/30/06 - added
166 }; // end class OpnetProtoSimProcess::UdpSocketProxy
167
168 // The "TcpSocketProxy" provides liason between a TCP "ProtoSocket" - JPH 7/3/06
169 // instance and the neighboring OPNET TCP process
170 class TcpSocketProxy;
171 class TcpSocketProxy : public ProtoSimAgent::SocketProxy
172 {
173 friend class OpnetProtoSimProcess;
174
175 public:
176 TcpSocketProxy(OpnetProtoSimProcess* sim_process); // JPH sim_process param
177 virtual ~TcpSocketProxy();
178
179 bool Bind(UINT16& thePort);
180 bool Connect(const ProtoAddress& theAddress);
181 bool Listen(UINT16 thePort);
182 bool Accept(ProtoSocket* theSocket);
183 bool RecvFrom(char* buffer, unsigned int& numBytes, ProtoAddress& srcAddr);
184 bool JoinGroup(const ProtoAddress& groupAddr);
185 bool LeaveGroup(const ProtoAddress& groupAddr);
186 void SetTTL(unsigned char ttl) {}
187 void SetLoopback(bool loopback) {}
188 void SetStream(int theStream) {strm_index = theStream;} // JPH 3/30/06
189
190 virtual bool SendTo(const char* buffer,
191 unsigned int& numBytes,
192 const ProtoAddress& dstAddr);
193
194
195 void OnReceive(char* recvData,
196 unsigned int recvLen,
197 const ProtoAddress& srcAddr,
198 const ProtoAddress& dstAddr);
199
200 UINT16 GetPort(){return local_port;}
201 ProtoAddress GetTcpHostAddress(){return sim_process->tcp_host_addr;}
202 ProtoAddress GetTcpRemAddress() {return sim_process->tcp_ind_rem_addr;}
203 bool Send(const char* buffer,
204 unsigned int& numBytes);
205 bool Recv(char* buffer,
206 unsigned int& numBytes);
207 int GetConn(){return conn_id;}
208 void SetSrcAddr(ProtoAddress saddr){src_addr = saddr;}
209 ProtoAddress GetSrcAddr(){return src_addr;}
210
211 protected:
212 class TcpSockList : public ProtoSimAgent::SocketProxy::List
213 {
214 public:
215 TcpSockList(){}
216 ~TcpSockList(){}
217 SocketProxy* FindProxyByConn(int connId);
218 }; // end class TcpSockList
219
220 friend class TcpSockList;
221
222 //ProtoSocket* proto_socket;
223
224 //SocketProxy* prev;
225 //SocketProxy* next;
226 OpnetProtoSimProcess* sim_process;
227 char* recv_data;
228 UINT16 recv_data_len;
229 ProtoAddress src_addr;
230 Ici* tcp_command_ici;
231 int strm_index;
232 int conn_id;
233 UINT16 local_port;
234 int recv_data_read_index;
235 }; // end class OpnetProtoSimProcess::TcpSocketProxy
236
237
238 ProtoSimAgent::SocketProxy* OpenSocket(ProtoSocket& theSocket);
239 void CloseSocket(ProtoSocket& theSocket);
240 Objid GetUdpProcId() {return udp_process_id;}
241 ApiT_Tcp_App_Handle* GetTcpAppHandle(){return &tcp_handle[0];}
242 ApiT_Tcp_App_Handle* GetTcpAppHandle(int cid){return &tcp_handle[cid];}
243
244 Objid udp_process_id;
245 ProtoAddress tcp_host_addr;
246 ProtoAddress tcp_ind_rem_addr;
247 //ProtoSimAgent::SocketProxy::List socket_proxy_list;
248 TcpSocketProxy::TcpSockList socket_proxy_list;
249 Evhandle timer_handle;
250 ApiT_Tcp_App_Handle tcp_handle[MAX_NUM_TCP_CONNECTIONS]; // JPH - needed to declare this last??
251
252
253 }; // end class OpnetProtoSimProcess
254
255 #endif //_OPNET_PROTO_SIM_PROCESS