EchoServer.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2002,2003 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
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 <TCPEndpointMsg.h>
00018 #include "EchoServer.h"
00019 #include "entry.h"
00020 
00021 EchoServer::EchoServer ()
00022 {
00023 }
00024 
00025 OStatus
00026 EchoServer::DoInit(const OSystemEvent& event)
00027 {
00028     OSYSDEBUG(("EchoServer::DoInit()\n"));
00029     return oSUCCESS;
00030 }
00031 
00032 OStatus
00033 EchoServer::DoStart(const OSystemEvent& event)
00034 {
00035     OSYSDEBUG(("EchoServer::DoStart()\n"));
00036 
00037     ipstackRef = antStackRef("IPStack");
00038 
00039     for (int index = 0; index < ECHOSERVER_CONNECTION_MAX; index++) {
00040         OStatus result = InitTCPConnection(index);
00041         if (result != oSUCCESS) return oFAIL;
00042     }
00043 
00044     Listen(0);
00045 
00046     return oSUCCESS;
00047 }    
00048 
00049 OStatus
00050 EchoServer::DoStop(const OSystemEvent& event)
00051 {
00052     OSYSDEBUG(("EchoServer::DoStop()\n"));
00053     return oSUCCESS;
00054 }
00055 
00056 OStatus
00057 EchoServer::DoDestroy(const OSystemEvent& event)
00058 {
00059     return oSUCCESS;
00060 }
00061 
00062 OStatus
00063 EchoServer::Listen(int index)
00064 {
00065     OSYSDEBUG(("EchoServer::Listen()\n"));
00066 
00067     if (connection[index].state != CONNECTION_CLOSED) return oFAIL;
00068 
00069     //
00070     // Create endpoint
00071     //
00072     antEnvCreateEndpointMsg tcpCreateMsg(EndpointType_TCP,
00073                                          ECHOSERVER_BUFFER_SIZE * 2);
00074     tcpCreateMsg.Call(ipstackRef, sizeof(tcpCreateMsg));
00075     if (tcpCreateMsg.error != ANT_SUCCESS) {
00076         OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00077                   "EchoServer::Listen()",
00078                   "Can't create endpoint",
00079                   index, tcpCreateMsg.error));
00080         return oFAIL;
00081     }
00082     connection[index].endpoint = tcpCreateMsg.moduleRef;
00083 
00084     //
00085     // Listen
00086     //
00087     TCPEndpointListenMsg listenMsg(connection[index].endpoint,
00088                                    IP_ADDR_ANY, ECHOSERVER_PORT);
00089     listenMsg.continuation = (void*)index;
00090 
00091     listenMsg.Send(ipstackRef, myOID_,
00092                    Extra_Entry[entryListenCont], sizeof(listenMsg));
00093     
00094     connection[index].state = CONNECTION_LISTENING;
00095 
00096     return oSUCCESS;
00097 }
00098 
00099 void
00100 EchoServer::ListenCont(ANTENVMSG msg)
00101 {
00102     OSYSDEBUG(("EchoServer::ListenCont()\n"));
00103 
00104     TCPEndpointListenMsg* listenMsg
00105         = (TCPEndpointListenMsg*)antEnvMsg::Receive(msg);
00106     int index = (int)listenMsg->continuation;
00107 
00108     if (listenMsg->error != TCP_SUCCESS) {
00109         OSYSLOG1((osyslogERROR, "%s : %s %d",
00110                   "EchoServer::ListenCont()",
00111                   "FAILED. listenMsg->error", listenMsg->error));
00112         Close(index);
00113         return;
00114     }
00115 
00116     connection[index].state = CONNECTION_CONNECTED;
00117 
00118     SetReceiveData(index);
00119     Receive(index);
00120 }
00121 
00122 OStatus
00123 EchoServer::Send(int index)
00124 {
00125     OSYSDEBUG(("EchoServer::Send()\n"));
00126 
00127     if (connection[index].sendSize == 0 ||
00128         connection[index].state != CONNECTION_CONNECTED) return oFAIL;
00129 
00130     TCPEndpointSendMsg sendMsg(connection[index].endpoint,
00131                                connection[index].sendData,
00132                                connection[index].sendSize);
00133     sendMsg.continuation = (void*)index;
00134 
00135     sendMsg.Send(ipstackRef, myOID_,
00136                  Extra_Entry[entrySendCont],
00137                  sizeof(TCPEndpointSendMsg));
00138 
00139     connection[index].state = CONNECTION_SENDING;
00140     connection[index].sendSize = 0;
00141     return oSUCCESS;
00142 }
00143 
00144 void
00145 EchoServer::SendCont(ANTENVMSG msg)
00146 {
00147     OSYSDEBUG(("EchoServer::SendCont()\n"));
00148 
00149     TCPEndpointSendMsg* sendMsg = (TCPEndpointSendMsg*)antEnvMsg::Receive(msg);
00150     int index = (int)(sendMsg->continuation);
00151 
00152     if (sendMsg->error != TCP_SUCCESS) {
00153         OSYSLOG1((osyslogERROR, "%s : %s %d",
00154                   "EchoServer::SendCont()",
00155                   "FAILED. sendMsg->error", sendMsg->error));
00156         Close(index);
00157         return;
00158     }
00159 
00160     OSYSPRINT(("sendData : %s", connection[index].sendData));
00161     
00162     connection[index].state = CONNECTION_CONNECTED;
00163 
00164     SetReceiveData(index);
00165     Receive(index);
00166 }
00167 
00168 OStatus
00169 EchoServer::Receive(int index)
00170 {
00171     OSYSDEBUG(("EchoServer::Receive()\n"));
00172 
00173     if (connection[index].state != CONNECTION_CONNECTED &&
00174         connection[index].state != CONNECTION_SENDING) return oFAIL;
00175 
00176     TCPEndpointReceiveMsg receiveMsg(connection[index].endpoint,
00177                                      connection[index].recvData,
00178                                      1, connection[index].recvSize);
00179     receiveMsg.continuation = (void*)index;
00180 
00181     receiveMsg.Send(ipstackRef, myOID_,
00182                     Extra_Entry[entryReceiveCont], sizeof(receiveMsg));
00183 
00184     return oSUCCESS;
00185 }
00186 
00187 void
00188 EchoServer::ReceiveCont(ANTENVMSG msg)
00189 {
00190     OSYSDEBUG(("EchoServer::ReceiveCont()\n"));
00191 
00192     TCPEndpointReceiveMsg* receiveMsg
00193         = (TCPEndpointReceiveMsg*)antEnvMsg::Receive(msg);
00194     int index = (int)(receiveMsg->continuation);
00195 
00196     if (receiveMsg->error != TCP_SUCCESS) {
00197         OSYSLOG1((osyslogERROR, "%s : %s %d",
00198                   "EchoServer::ReceiveCont()",
00199                   "FAILED. receiveMsg->error", receiveMsg->error));
00200         Close(index);
00201         return;
00202     }
00203 
00204     OSYSPRINT(("recvData : %s", connection[index].recvData));
00205                
00206     connection[index].sendSize = receiveMsg->sizeMin;
00207     SetSendData(index);
00208     Send(index);
00209 }
00210 
00211 OStatus
00212 EchoServer::Close(int index)
00213 {
00214     OSYSDEBUG(("EchoServer::Close()\n"));
00215 
00216     if (connection[index].state == CONNECTION_CLOSED ||
00217         connection[index].state == CONNECTION_CLOSING) return oFAIL;
00218 
00219     TCPEndpointCloseMsg closeMsg(connection[index].endpoint);
00220     closeMsg.continuation = (void*)index;
00221 
00222     closeMsg.Send(ipstackRef, myOID_,
00223                   Extra_Entry[entryCloseCont], sizeof(closeMsg));
00224 
00225     connection[index].state = CONNECTION_CLOSING;
00226 
00227     return oSUCCESS;
00228 }
00229 
00230 void
00231 EchoServer::CloseCont(ANTENVMSG msg)
00232 {
00233     OSYSDEBUG(("EchoServer::CloseCont()\n"));
00234     
00235     TCPEndpointCloseMsg* closeMsg
00236         = (TCPEndpointCloseMsg*)antEnvMsg::Receive(msg);
00237     int index = (int)(closeMsg->continuation);
00238 
00239     connection[index].state = CONNECTION_CLOSED;
00240     Listen(index);
00241 }
00242 
00243 OStatus
00244 EchoServer::InitTCPConnection(int index)
00245 {
00246     OSYSDEBUG(("EchoServer::InitTCPConnection()\n"));
00247 
00248     connection[index].state = CONNECTION_CLOSED;
00249 
00250     // 
00251     // Allocate send buffer
00252     //
00253     antEnvCreateSharedBufferMsg sendBufferMsg(ECHOSERVER_BUFFER_SIZE);
00254 
00255     sendBufferMsg.Call(ipstackRef, sizeof(sendBufferMsg));
00256     if (sendBufferMsg.error != ANT_SUCCESS) {
00257         OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00258                   "EchoServer::InitTCPConnection()",
00259                   "Can't allocate send buffer",
00260                   index, sendBufferMsg.error));
00261         return oFAIL;
00262     }
00263 
00264     connection[index].sendBuffer = sendBufferMsg.buffer;
00265     connection[index].sendBuffer.Map();
00266     connection[index].sendData
00267         = (byte*)(connection[index].sendBuffer.GetAddress());
00268 
00269     //
00270     // Allocate receive buffer
00271     //
00272     antEnvCreateSharedBufferMsg recvBufferMsg(ECHOSERVER_BUFFER_SIZE);
00273 
00274     recvBufferMsg.Call(ipstackRef, sizeof(recvBufferMsg));
00275     if (recvBufferMsg.error != ANT_SUCCESS) {
00276         OSYSLOG1((osyslogERROR, "%s : %s[%d] antError %d",
00277                   "EchoServer::InitTCPConnection()",
00278                   "Can't allocate receive buffer",
00279                   index, recvBufferMsg.error));
00280         return oFAIL;
00281     }
00282 
00283     connection[index].recvBuffer = recvBufferMsg.buffer;
00284     connection[index].recvBuffer.Map();
00285     connection[index].recvData
00286         = (byte*)(connection[index].recvBuffer.GetAddress());
00287 
00288     return oSUCCESS;
00289 }
00290 
00291 void
00292 EchoServer::SetSendData(int index)
00293 {
00294     memcpy(connection[index].sendData,
00295            connection[index].recvData, connection[index].sendSize);
00296 }
00297 
00298 void
00299 EchoServer::SetReceiveData(int index)
00300 {
00301     connection[index].recvSize = ECHOSERVER_BUFFER_SIZE;
00302 }

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1