00001 00002 #include "stdafx.h" 00003 00004 #include "ippdme/Spy/ippClientServerSpy.h" 00005 #include "ippdme/assert.h" 00006 00007 00008 00009 ippSpyServer::ippSpyServer() 00010 : _state(uninitialized) 00011 , _parent(0) 00012 { 00013 00014 } 00015 00016 ippSpyServer::~ippSpyServer() 00017 { 00018 00019 } 00020 00021 void ippSpyServer::setParent(ippClientServerSpy* parent) 00022 { 00023 _parent=parent; 00024 } 00025 00026 00027 void ippSpyServer::HeartBeat() 00028 { 00029 ippSleep(1); // allow context switch 00030 switch(_state) 00031 { 00032 case uninitialized: 00033 return; 00034 case listening: 00035 PerformListening(); 00036 return; 00037 case connected: 00038 PerformRead(); 00039 ProbeConnection(); 00040 return; 00041 } 00042 } 00043 00044 void ippSpyServer::PerformListening() 00045 { 00046 IPP_ASSERT(_state == listening); 00047 IPP_ASSERT(!_socket.IsValid()); 00048 IPP_ASSERT(_listenSocket.IsValid()); 00049 00050 ippSocket::SERROR nRet = _listenSocket.Accept(_socket); 00051 00052 switch(nRet) { 00053 case ippSocket::SOCKET_WOULD_BLOCK: 00054 // xx PRINTSTATUS("Waiting for connection ..."); 00055 break; 00056 case ippSocket::SOCKET_OK: 00057 // we don't want to accept new connection 00058 _listenSocket.Close(); 00059 _state = connected; 00060 OnClientConnected(); 00061 break; 00062 default: 00063 // a major problem has occur ... 00064 _listenSocket.Close(); 00065 _state = uninitialized; 00066 break; 00067 } 00068 } 00069 00070 void ippSpyServer::OnClientConnected() 00071 { 00072 LogMessage("ippSpyServer::OnClientConnected"); 00073 00074 } 00075 void ippSpyServer::OnClientDisconnected() 00076 { 00077 LogMessage("ippSpyServer::OnClientDisconnected"); 00078 } 00079 00080 bool ippSpyServer::PowerOff() 00081 { 00082 _listenSocket.Close(); 00083 _socket.Close(); 00084 _state = uninitialized; 00085 return true; 00086 } 00087 00088 bool ippSpyServer::PowerOn(int portNumber) 00089 { 00090 00091 _portNumber = portNumber; 00092 if (_state!=uninitialized ) { 00093 return false; 00094 } 00095 // Create a TCP/IP stream socket to "listen" 00096 // incoming client connection 00097 if (!_listenSocket.Listen(_portNumber)) { 00098 return false; 00099 } 00100 _state = listening; 00101 return true; 00102 } 00103 #include "ippdme/Parser/ippCommandParser.h" 00104 void ippSpyServer::ProcessLine(const char* inputline) 00105 { 00106 // verify that line received by the server from the external client 00107 // is syntacticly correct 00108 ippCommandParser parser; 00109 parser.setInput(inputline); 00110 ippCommandPtr ptr = parser.parseCommand(); 00111 00112 _parent->SendCommandLine(inputline,true); 00113 00114 } 00115 void ippSpyServer::ProcessMisformedLine(const char* inputline) 00116 { 00117 _parent->SendCommandLine(inputline,true); 00118 } 00119 00120 void ippSpyServer::WriteToSocket(const char* line) 00121 { 00122 int nRet = _socket.Write(line); 00123 if (nRet) { 00124 OnConnectionLost(); 00125 } 00126 } 00127 00128 void ippSpyServer::OnConnectionLost() 00129 { 00130 ippEngine::OnConnectionLost(); 00131 OnClientDisconnected(); 00132 00133 // when a connection is lost the server goes 00134 // back to listen mode. 00135 // Create a TCP/IP stream socket to "listen" with 00136 if (!_listenSocket.Listen(_portNumber)) { 00137 _state=uninitialized; 00138 } else { 00139 _state=listening; 00140 } 00141 }