00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <string.h>
00013 #include <OPENR/OSyslog.h>
00014 #include <OPENR/OPENRAPI.h>
00015 #include <ant.h>
00016 #include <EndpointTypes.h>
00017 #include <UDPEndpointMsg.h>
00018 #include "UDPEchoServer.h"
00019 #include "entry.h"
00020
00021 UDPEchoServer::UDPEchoServer ()
00022 {
00023 }
00024
00025 OStatus
00026 UDPEchoServer::DoInit(const OSystemEvent& event)
00027 {
00028 OSYSDEBUG(("UDPEchoServer::DoInit()\n"));
00029
00030 ipstackRef = antStackRef("IPStack");
00031
00032 for (int index = 0; index < UDPECHOSERVER_CONNECTION_MAX; index++) {
00033 OStatus result = InitUDPBuffer(index);
00034 if (result != oSUCCESS) return oFAIL;
00035 }
00036
00037 return oSUCCESS;
00038 }
00039
00040 OStatus
00041 UDPEchoServer::DoStart(const OSystemEvent& event)
00042 {
00043 OStatus result;
00044 OSYSDEBUG(("UDPEchoServer::DoStart()\n"));
00045
00046 for (int index = 0; index < UDPECHOSERVER_CONNECTION_MAX; index++) {
00047
00048
00049
00050 result = CreateUDPEndpoint(index);
00051 if (result != oSUCCESS) {
00052 OSYSLOG1((osyslogERROR, "%s : %s[%d]",
00053 "UDPEchoServer::DoStart()",
00054 "CreateUDPEndpoint() fail", index));
00055 continue;
00056 }
00057 result = Bind(index);
00058 if (result != oSUCCESS) {
00059 OSYSLOG1((osyslogERROR, "%s : %s[%d]",
00060 "UDPEchoServer::DoStart()",
00061 "Bind() fail", index));
00062 continue;
00063 }
00064 }
00065
00066 return oSUCCESS;
00067 }
00068
00069 OStatus
00070 UDPEchoServer::DoStop(const OSystemEvent& event)
00071 {
00072 OSYSDEBUG(("UDPEchoServer::DoStop()\n"));
00073
00074 for (int index = 0; index < UDPECHOSERVER_CONNECTION_MAX; index++) {
00075 if (connection[index].state != CONNECTION_CLOSED &&
00076 connection[index].state != CONNECTION_CLOSING) {
00077
00078
00079 UDPEndpointCloseMsg closeMsg(connection[index].endpoint);
00080 closeMsg.Call(ipstackRef, sizeof(closeMsg));
00081 connection[index].state = CONNECTION_CLOSED;
00082 }
00083 }
00084
00085 return oSUCCESS;
00086 }
00087
00088 OStatus
00089 UDPEchoServer::DoDestroy(const OSystemEvent& event)
00090 {
00091 for (int index = 0; index < UDPECHOSERVER_CONNECTION_MAX; index++) {
00092
00093 connection[index].sendBuffer.UnMap();
00094 antEnvDestroySharedBufferMsg destroySendBufferMsg(connection[index].sendBuffer);
00095 destroySendBufferMsg.Call(ipstackRef, sizeof(destroySendBufferMsg));
00096
00097
00098 connection[index].recvBuffer.UnMap();
00099 antEnvDestroySharedBufferMsg destroyRecvBufferMsg(connection[index].recvBuffer);
00100 destroyRecvBufferMsg.Call(ipstackRef, sizeof(destroyRecvBufferMsg));
00101 }
00102
00103 return oSUCCESS;
00104 }
00105
00106 OStatus
00107 UDPEchoServer::Send(int index)
00108 {
00109 OSYSDEBUG(("UDPEchoServer::Send()\n"));
00110
00111 if (connection[index].sendSize == 0 ||
00112 connection[index].state != CONNECTION_CONNECTED) return oFAIL;
00113
00114 UDPEndpointSendMsg sendMsg(connection[index].endpoint,
00115 connection[index].sendAddress,
00116 connection[index].sendPort,
00117 connection[index].sendData,
00118 connection[index].sendSize);
00119 sendMsg.continuation = (void*)index;
00120 sendMsg.Send(ipstackRef, myOID_,
00121 Extra_Entry[entrySendCont], sizeof(UDPEndpointSendMsg));
00122
00123
00124 connection[index].state = CONNECTION_SENDING;
00125 return oSUCCESS;
00126 }
00127
00128 void
00129 UDPEchoServer::SendCont(ANTENVMSG msg)
00130 {
00131 OSYSDEBUG(("UDPEchoServer::SendCont()\n"));
00132
00133 UDPEndpointSendMsg* sendMsg = (UDPEndpointSendMsg*)antEnvMsg::Receive(msg);
00134 int index = (int)(sendMsg->continuation);
00135 if (connection[index].state == CONNECTION_CLOSED)
00136 return;
00137
00138 if (sendMsg->error != UDP_SUCCESS) {
00139 OSYSLOG1((osyslogERROR, "%s : %s %d",
00140 "UDPEchoServer::SendCont()",
00141 "FAILED. sendMsg->error", sendMsg->error));
00142 Close(index);
00143 return;
00144 }
00145 char in_buff[64];
00146 OSYSPRINT(("sendData : %s", connection[index].sendData));
00147 OSYSPRINT(("sendAddress : %s\n", connection[index].sendAddress.GetAsString(in_buff)));
00148 OSYSPRINT(("sendPort : %d\n\n", connection[index].sendPort));
00149
00150 connection[index].state = CONNECTION_CONNECTED;
00151 connection[index].recvSize = UDPECHOSERVER_BUFFER_SIZE;
00152 Receive(index);
00153 }
00154
00155 OStatus
00156 UDPEchoServer::Receive(int index)
00157 {
00158 OSYSDEBUG(("UDPEchoServer::Receive()\n"));
00159
00160 if (connection[index].state != CONNECTION_CONNECTED &&
00161 connection[index].state != CONNECTION_SENDING) return oFAIL;
00162
00163 UDPEndpointReceiveMsg receiveMsg(connection[index].endpoint,
00164 connection[index].recvData,
00165 connection[index].recvSize);
00166 receiveMsg.continuation = (void*)index;
00167
00168 receiveMsg.Send(ipstackRef, myOID_,
00169 Extra_Entry[entryReceiveCont], sizeof(receiveMsg));
00170
00171 return oSUCCESS;
00172 }
00173
00174 void
00175 UDPEchoServer::ReceiveCont(ANTENVMSG msg)
00176 {
00177 OSYSDEBUG(("UDPEchoServer::ReceiveCont()\n"));
00178
00179 UDPEndpointReceiveMsg* receiveMsg
00180 = (UDPEndpointReceiveMsg*)antEnvMsg::Receive(msg);
00181
00182 int index = (int)(receiveMsg->continuation);
00183 if (connection[index].state == CONNECTION_CLOSED) return;
00184
00185 if (receiveMsg->error != UDP_SUCCESS) {
00186 OSYSLOG1((osyslogERROR, "%s : %s %d",
00187 "UDPEchoServer::ReceiveCont()",
00188 "FAILED. receiveMsg->error", receiveMsg->error));
00189 Close(index);
00190 return;
00191 }
00192
00193 OSYSPRINT(("recvData : %s", receiveMsg->buffer));
00194
00195
00196
00197
00198 connection[index].sendAddress = receiveMsg->address;
00199 connection[index].sendPort = receiveMsg->port;
00200 connection[index].sendSize = receiveMsg->size;
00201 memcpy(connection[index].sendData, receiveMsg->buffer, receiveMsg->size);
00202
00203 Send(index);
00204 }
00205
00206 OStatus
00207 UDPEchoServer::Close(int index)
00208 {
00209 OSYSDEBUG(("UDPEchoServer::Close()\n"));
00210
00211 if (connection[index].state == CONNECTION_CLOSED ||
00212 connection[index].state == CONNECTION_CLOSING) return oFAIL;
00213
00214 UDPEndpointCloseMsg closeMsg(connection[index].endpoint);
00215 closeMsg.continuation = (void*)index;
00216
00217 closeMsg.Send(ipstackRef, myOID_,
00218 Extra_Entry[entryCloseCont], sizeof(closeMsg));
00219
00220 connection[index].state = CONNECTION_CLOSING;
00221
00222 return oSUCCESS;
00223 }
00224
00225 void
00226 UDPEchoServer::CloseCont(ANTENVMSG msg)
00227 {
00228 OStatus result;
00229 OSYSDEBUG(("UDPEchoServer::CloseCont()\n"));
00230
00231 UDPEndpointCloseMsg* closeMsg = (UDPEndpointCloseMsg*)antEnvMsg::Receive(msg);
00232 int index = (int)(closeMsg->continuation);
00233 if (connection[index].state == CONNECTION_CLOSED)
00234 return;
00235
00236 connection[index].state = CONNECTION_CLOSED;
00237
00238 result = CreateUDPEndpoint(index);
00239 if (result != oSUCCESS) {
00240 OSYSLOG1((osyslogERROR, "%s : %s[%d]",
00241 "UDPEchoServer::CloseCont()",
00242 "CreateUDPEndpoint() fail", index));
00243 return;
00244 }
00245 result = Bind(index);
00246 if (result != oSUCCESS) {
00247 OSYSLOG1((osyslogERROR, "%s : %s[%d]",
00248 "UDPEchoServer::CloseCont()",
00249 "Bind() fail", index));
00250 }
00251 }
00252
00253 OStatus
00254 UDPEchoServer::InitUDPBuffer(int index)
00255 {
00256 OSYSDEBUG(("UDPEchoServer::InitUDPBuffer()\n"));
00257
00258 connection[index].state = CONNECTION_CLOSED;
00259
00260
00261
00262
00263 antEnvCreateSharedBufferMsg sendBufferMsg(UDPECHOSERVER_BUFFER_SIZE);
00264
00265 sendBufferMsg.Call(ipstackRef, sizeof(sendBufferMsg));
00266 if (sendBufferMsg.error != ANT_SUCCESS) {
00267 OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00268 "UDPEchoServer::InitUDPBuffer()",
00269 "Can't allocate send buffer",
00270 index, sendBufferMsg.error));
00271 return oFAIL;
00272 }
00273
00274 connection[index].sendBuffer = sendBufferMsg.buffer;
00275 connection[index].sendBuffer.Map();
00276 connection[index].sendData
00277 = (byte*)(connection[index].sendBuffer.GetAddress());
00278
00279
00280
00281
00282 antEnvCreateSharedBufferMsg recvBufferMsg(UDPECHOSERVER_BUFFER_SIZE);
00283
00284 recvBufferMsg.Call(ipstackRef, sizeof(recvBufferMsg));
00285 if (recvBufferMsg.error != ANT_SUCCESS) {
00286 OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00287 "UDPEchoServer::InitUDPBuffer()",
00288 "Can't allocate receive buffer",
00289 index, recvBufferMsg.error));
00290 return oFAIL;
00291 }
00292
00293 connection[index].recvBuffer = recvBufferMsg.buffer;
00294 connection[index].recvBuffer.Map();
00295 connection[index].recvData
00296 = (byte*)(connection[index].recvBuffer.GetAddress());
00297 connection[index].recvSize = UDPECHOSERVER_BUFFER_SIZE;
00298
00299 return oSUCCESS;
00300 }
00301
00302 OStatus
00303 UDPEchoServer::CreateUDPEndpoint(int index)
00304 {
00305 OSYSDEBUG(("UDPEchoServer::CreateUDPEndpoint()\n"));
00306
00307 if (connection[index].state != CONNECTION_CLOSED) return oFAIL;
00308
00309
00310
00311
00312 antEnvCreateEndpointMsg udpCreateMsg(EndpointType_UDP,
00313 UDPECHOSERVER_BUFFER_SIZE * 2);
00314 udpCreateMsg.Call(ipstackRef, sizeof(udpCreateMsg));
00315 if (udpCreateMsg.error != ANT_SUCCESS) {
00316 OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00317 "UDPEchoServer::CreateUDPEndpoint()",
00318 "Can't create endpoint",
00319 index, udpCreateMsg.error));
00320 return oFAIL;
00321 }
00322 connection[index].endpoint = udpCreateMsg.moduleRef;
00323
00324 return oSUCCESS;
00325 }
00326
00327
00328 OStatus
00329 UDPEchoServer::Bind(int index)
00330 {
00331 OSYSDEBUG(("UDPEchoServer::Bind()\n"));
00332
00333 if (connection[index].state != CONNECTION_CLOSED) return oFAIL;
00334
00335
00336
00337
00338 UDPEndpointBindMsg bindMsg(connection[index].endpoint,
00339 IP_ADDR_ANY, UDPECHOSERVER_PORT);
00340 bindMsg.Call(ipstackRef,sizeof(antEnvCreateEndpointMsg));
00341 if (bindMsg.error != UDP_SUCCESS) {
00342 return oFAIL;
00343 }
00344
00345 connection[index].state = CONNECTION_CONNECTED;
00346 connection[index].recvSize = UDPECHOSERVER_BUFFER_SIZE;
00347
00348 Receive(index);
00349
00350 return oSUCCESS;
00351 }