Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ippSpyClient.cpp

Go to the documentation of this file.
00001 #include "stdafx.h"
00002 
00003 #include "ippdme/Spy/ippClientServerSpy.h"
00004 #include "ippdme/assert.h"
00005 
00006 
00007 ippSpyClient::ippSpyClient()
00008 : _state(disconnected) 
00009 , _parent(0)
00010 { 
00011 
00012 }
00013 ippSpyClient::~ippSpyClient()
00014 {
00015 
00016 }
00017 
00018 void ippSpyClient::setParent(ippClientServerSpy* parent)
00019 {
00020   _parent=parent;
00021 }
00022 
00023 void ippSpyClient::Connect(
00024   const char* hostname,
00025   int port
00026 )
00027 {
00028   IPP_ASSERT(_state== disconnected);
00029   
00030   if(_state != disconnected) {
00031     return;
00032   }
00033   
00034   
00035 //  while(!_pending_commands.empty()) {
00036 //    _pending_commands.pop();
00037 //  }
00038 
00039   bool bSuccess = _socket.Create();
00040   IPP_ASSERT(bSuccess);
00041  
00042 
00043   ippSocket::SERROR nRet =  _socket.Connect(hostname,port);
00044   if (nRet == ippSocket::SOCKET_WOULD_BLOCK) {
00045     //
00046     _state = trying_to_connect;
00047   } else  if (nRet == ippSocket::SOCKET_IS_CONNECTED) { 
00048     _state = connected;
00049 //    _in_error_state = false;
00050 //    _ready_to_send = true;
00051   } else {
00052     _state = disconnected;
00053     _socket.Close();
00054   }
00055 }
00056 
00057 void ippSpyClient::ProcessLine(const char* inputline)
00058 {
00059   _parent->SendResponseLine(inputline,/* wellformed = */true);
00060 }
00061 void ippSpyClient::ProcessMisformedLine(const char* inputline)
00062 {
00063   _parent->SendResponseLine(inputline,/* wellformed = */false);
00064 }
00065 void ippSpyClient::HeartBeat()
00066 {
00067   ippSleep(1); // allow context switch
00068   switch(_state){
00069   case trying_to_connect:
00070     {
00071       switch(_socket.CanSendData()){ 
00072         case ippSocket::SOCKET_OK: 
00073           //xx _ready_to_send = true;
00074           //xx _in_error_state = false;
00075           _state = connected; 
00076         break;
00077         case ippSocket::SOCKET_WOULD_BLOCK:
00078           // this mean that the server is probably busy
00079           Disconnect();
00080           break;
00081         default:
00082           _state = disconnected;
00083           _socket.Close();
00084         }
00085       } break;
00086   case connected:
00087     {
00088       PerformRead();
00089     }
00090   }
00091 }
00092 
00093 void ippSpyClient::SendCommand(const char* command)
00094 {
00095   //xx IPP_ASSERT(_ready_to_send);
00096   LogMessage("     send :",command);
00097   
00098   int _pending_tag=0;
00099   if(command[0]=='E') {
00100     sscanf(command+1,"%4d",&_pending_tag);
00101   } else {
00102     sscanf(command,"%5d",&_pending_tag);
00103   }
00104  
00105   _socket.Write(command);
00106   
00107   // see if we need to send the line termination
00108   int l = strlen(command);
00109   if (l > 2 && command[l-1] != '\n' && command[l-2]!='\r') { 
00110      _socket.Write("\r\n");
00111   }
00112 
00113  //xx _ready_to_send = false;
00114   // read the acknowledge tag
00115 }
00116 
00117 void ippSpyClient::Disconnect()
00118 {
00119    ProbeConnection();
00120   _state = disconnected;
00121   _socket.Close();
00122 //xx  _ready_to_send = false;
00123 //xx  _in_error_state = false;
00124   LogMessage(" disconnected","");
00125 
00126  //xx _parent->Stop();
00127 }
00128 
00129 
00130 

Generated on Wed Nov 8 00:20:07 2006 for IPPDME by  doxygen 1.4.1