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
00036
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
00050
00051 } else {
00052 _state = disconnected;
00053 _socket.Close();
00054 }
00055 }
00056
00057 void ippSpyClient::ProcessLine(const char* inputline)
00058 {
00059 _parent->SendResponseLine(inputline,true);
00060 }
00061 void ippSpyClient::ProcessMisformedLine(const char* inputline)
00062 {
00063 _parent->SendResponseLine(inputline,false);
00064 }
00065 void ippSpyClient::HeartBeat()
00066 {
00067 ippSleep(1);
00068 switch(_state){
00069 case trying_to_connect:
00070 {
00071 switch(_socket.CanSendData()){
00072 case ippSocket::SOCKET_OK:
00073
00074
00075 _state = connected;
00076 break;
00077 case ippSocket::SOCKET_WOULD_BLOCK:
00078
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
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
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
00114
00115 }
00116
00117 void ippSpyClient::Disconnect()
00118 {
00119 ProbeConnection();
00120 _state = disconnected;
00121 _socket.Close();
00122
00123
00124 LogMessage(" disconnected","");
00125
00126
00127 }
00128
00129
00130