00001
00002
00003
00004
00005
00006
00007 #include <arpa/inet.h>
00008 #include "ComPlusPlus/Comm.h"
00009 #include "ComPlusPlus/AClnt.h"
00010
00011 #include "SysPlusPlus/Tools.h"
00012 #include "SysPlusPlus/Logger.h"
00013
00014 #include "Sig.h"
00015
00016
00017 compp::AClnt::AClnt( const std::string & IPAddr, const int port ) {
00018
00019 S.Connect ( IPAddr, port);
00020 S.AttachFD();
00021
00022 }
00023
00024 bool compp::AClnt::disConnect( ) {
00025
00026 S.Close();
00027 return true;
00028 }
00029
00030 bool compp::AClnt::Connect( const std::string & IPAddr, const int port ) {
00031
00032 S.Connect ( IPAddr, port);
00033 S.AttachFD();
00034
00035 return true;
00036 }
00037
00038 compp::AClnt::AClnt( ) {
00039
00040
00041 }
00042
00043 compp::AClnt::~AClnt( ) {
00044
00045
00046
00047 }
00048
00049
00050 std::string compp::AClnt:: requestTransaction ( const std::string & request, const bool cyphered ) {
00051 syspp::Logger *Log = syspp::Logger::Instance();
00052
00053 long Sec = htonl ( _COMM_SEC );
00054 long Cyph = htonl ( _COMM_CYPH );
00055 int Len = htonl ( (int)request.size() + 12 );
00056 int SIGNATURE= 123, i;
00057 char c[sizeof (int) ];
00058 std::string R = "";
00059 int senderport;
00060 std::string retval, senderhost;
00061 bool responsecorrect;
00062 std::string response;
00063
00064
00065 try {
00066 if ( cyphered ) {
00067 memcpy ( (void*) c, (void*) &Cyph, sizeof (Cyph));
00068 Log->Debug ("AClnt cyphered request." );
00069
00070 } else {
00071 memcpy ( (void*) c, (void*) &Sec, sizeof (Sec));
00072 Log->Debug ("AClnt uncyphered request." );
00073 }
00074
00075 R.append ( c, sizeof ( Len ) );
00076
00077
00078 memcpy ( (void*) c, (void*) &Len, sizeof (Len));
00079 R.append ( c, sizeof ( Len ) );
00080
00081
00082 memcpy ( (void*) c, (void*) &SIGNATURE, sizeof (SIGNATURE));
00083 R.append ( c, sizeof ( SIGNATURE ) );
00084
00085 if ( cyphered ) {
00086 std::string cs = request;
00087 syspp::Tools::Enc (cs);
00088 #ifdef SPECIAL_DEBUG
00089 std::cout << request << cs << "\n" ;
00090 #endif
00091 R.append ( cs );
00092 } else {
00093 R.append ( request );
00094 #ifdef SPECIAL_DEBUG
00095 std::cout << request << "\n" ;
00096 #endif
00097 }
00098
00099 #ifdef SPECIAL_DEBUG
00100 std::cout << ":" << R << "\n" ;
00101 for ( int u = 0; u < R.size(); ++ u )
00102 printf ("%d ", R[u] );
00103 std::cout << "\n";
00104 #endif
00105
00106 if ( S.Write ( R) <= 0 ) {
00107 Log->Error ("Server does not accept command." );
00108 throw syspp::ComException ( "Server does not accept comands." ) ;
00109 }
00110
00111 do {
00112
00113 if ( false == S.PollRcv ( 1000000 ) ) {
00114 Log->Error ("Server does not respond." );
00115 throw syspp::ComException ( "Server does not respond (7)." ) ;
00116 }
00117
00118 if ( S.Read ( retval, senderhost, senderport ) <= 0 ) {
00119 Log->Error ("Server does not respond (2)." );
00120 throw syspp::ComException ( "Server does not respond (2)." ) ;
00121 }
00122
00123 if ( retval.size() < 12 ) {
00124 Log->Error ("Server does not respond (3)." );
00125 throw syspp::ComException ( "Server does not respond (3)." ) ;
00126 }
00127
00128 retval.copy ( (char *) &i, 4, 0 );
00129
00130 if ( cyphered ) {
00131 if ( i != Cyph ) {
00132 Log->Error ("Server does not respond (4)." );
00133 throw syspp::ComException ( "Server does not respond (4)." ) ;
00134 }
00135 } else {
00136 if ( i != Sec ) {
00137 Log->Error ("Server does not respond (5)." );
00138 throw syspp::ComException ( "Server does not respond (5)." ) ;
00139 }
00140 }
00141
00142 retval.copy ( (char *) &i, 4, 4 );
00143 i = ntohl ( i );
00144
00145 if ( i != (int) ( retval.size() ) ) {
00146 Log->Error ("Server does not respond (6)." );
00147 throw syspp::ComException ( "Server does not respond (6)." ) ;
00148 }
00149
00150
00151 retval.copy ( (char *) &i, 4, 8 );
00152 if ( i == SIGNATURE ) {
00153 responsecorrect = true;
00154 } else {
00155 responsecorrect = false;
00156 }
00157
00158 } while ( responsecorrect == false );
00159
00160 response = retval.substr ( 12, retval.size() -12 );
00161
00162 if ( cyphered ) {
00163 #ifdef SPECIAL_DEBUG
00164 std::cout << "CYPH res " << response << "\n" ;
00165 #endif
00166 syspp::Tools::Dec (response);
00167 #ifdef SPECIAL_DEBUG
00168 std::cout << "CYPH res(2) " << response << "\n" ;
00169 #endif
00170 } else {
00171 #ifdef SPECIAL_DEBUG
00172 std::cout << "NORM res " << response << "\n" ;
00173 #endif
00174 }
00175
00176 } catch ( syspp::ComException &e ) {
00177 std::string s = "Server Communication Exception : (" ;
00178 s += e.what () ;
00179 s += ")";
00180 Log->Error ( s.c_str() );
00181 throw syspp::ComException ( s.c_str() ) ;
00182 } catch ( ... ) {
00183 std::string s = "Server Communication Exception : (General)" ;
00184 Log->Error ( s.c_str() );
00185 throw syspp::ComException ( s.c_str() ) ;
00186 }
00187
00188 Log->Debug ("AClnt response OK." );
00189 return response;
00190 }