00001
00002
00003
00004
00005
00006
00007 #include <arpa/inet.h>
00008 #include "ComPlusPlus/Comm.h"
00009 #include "ComPlusPlus/SClnt.h"
00010
00011 #include "SysPlusPlus/Logger.h"
00012 #include "SysPlusPlus/Tools.h"
00013
00014 #include "Sig.h"
00015
00016 compp::SClnt::SClnt( const std::string & IPAddr, const int port ) {
00017
00018 S.SetOptNonBlocking();
00019 S.Connect ( IPAddr, port);
00020 S.AttachFD();
00021 S.SetReadAhead ( true );
00022
00023 }
00024
00025 bool compp::SClnt::disConnect( ) {
00026 S.Shutdown();
00027 S.Close();
00028 return true;
00029 }
00030
00031 bool compp::SClnt::Connect( const std::string & IPAddr, const int port ) {
00032
00033 syspp::Logger *Log = syspp::Logger::Instance();
00034 Log->Info ( "SClnt : Connection attempt to %s:%d", IPAddr.c_str(), port );
00035 S.SetOptNonBlocking();
00036 S.Connect ( IPAddr, port);
00037 S.AttachFD();
00038 S.SetReadAhead ( true );
00039 Log->Info ( "SClnt : Connection to %s:%d succeeded", IPAddr.c_str(), port );
00040 return true;
00041 }
00042
00043 compp::SClnt::SClnt( ) {
00044
00045
00046 }
00047
00048 compp::SClnt::~SClnt( ) {
00049
00050 S.Shutdown (0);
00051
00052 }
00053
00054 std::string compp::SClnt:: requestTransaction ( const std::string & request, const bool cyphered ) {
00055
00056 syspp::Logger *Log = syspp::Logger::Instance();
00057
00058 long Sec = htonl ( _COMM_SEC );
00059 long Cyph = htonl ( _COMM_CYPH );
00060
00061 int Len = htonl ( request.size() );
00062 char c[sizeof (int) ];
00063 std::string R = "";
00064 int LResponse;
00065 std::string retval;
00066
00067
00068 try {
00069 if ( cyphered ) {
00070 Log->Debug ( "SClnt cyphering." );
00071 memcpy ( (void*) c, (void*) &Cyph, sizeof (Cyph));
00072 } else {
00073 Log->Debug ( "SClnt pain." );
00074 memcpy ( (void*) c, (void*) &Sec, sizeof (Sec));
00075 }
00076
00077 R.append ( c, sizeof ( Len ) );
00078
00079 memcpy ( (void*) c, (void*) &Len, sizeof (Len));
00080 R.append ( c, sizeof ( Len ) );
00081
00082 if ( cyphered ) {
00083 std::string cs = request;
00084 syspp::Tools::Enc (cs);
00085
00086 R.append ( cs );
00087 } else {
00088 R.append ( request );
00089
00090 }
00091
00092 if ( S.Writen ( R) <= 0 ) {
00093 Log->Info ( "SClnt. Sending of request failed(%s).", strerror (errno) );
00094 throw syspp::ComException ( "Server does not accept comands." ) ;
00095 }
00096
00097 if ( S.Readn ( (char *) & LResponse, sizeof ( LResponse )) <= 0 ) {
00098 Log->Info ( "SClnt. Retrieving of response failed(%s).", strerror (errno) );
00099 throw syspp::ComException ( "Server does not respond.(1)" ) ;
00100 }
00101
00102
00103 LResponse = ntohl ( LResponse );
00104 if ( LResponse < 0 ) {
00105 Log->Info ( "SClnt. Response not valid." );
00106 throw syspp::ComException ( "Server response does not make any sense." ) ;
00107 }
00108
00109 if ( S.Readn ( retval, LResponse ) <= 0 ) {
00110 Log->Info ( "SClnt. Response not valid." );
00111 throw syspp::ComException ( "Server does not respond (2)." ) ;
00112 }
00113
00114 } catch ( syspp::ComException &e ) {
00115 std::string s = "Server Communication Exception : (" ;
00116 s += e.what () ;
00117 s += ")";
00118 Log->Info ( s.c_str() );
00119 throw syspp::ComException ( s.c_str() ) ;
00120 } catch ( ... ) {
00121 std::string s = "Server Communication Exception : (General)" ;
00122 Log->Info ( s.c_str() );
00123 throw syspp::ComException ( s.c_str() ) ;
00124 }
00125
00126 if ( cyphered ) {
00127 syspp::Tools::Dec (retval);
00128 }
00129 Log->Debug ( "SClnt: Response OK." );
00130
00131 return retval;
00132
00133 }