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

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