00001
00002
00003
00004
00005
00006
00007 #include <arpa/inet.h>
00008 #include "ComPlusPlus/ASrv.h"
00009 #include "SysPlusPlus/Tools.h"
00010 #include "SysPlusPlus/Logger.h"
00011
00012 #include "Sig.h"
00013
00014 compp::ASrv::~ASrv () {
00015
00016 }
00017 compp::ASrv::ASrv () {
00018
00019 }
00020
00021 compp::ASrv::ASrv (const int p) {
00022
00023 setPort ( p );
00024 setIPAddr ( "0.0.0.0" );
00025
00026 }
00027
00028 compp::ASrv::ASrv (const std::string & IPAddr, const int p) {
00029
00030 setPort ( p );
00031 setIPAddr ( IPAddr );
00032
00033 }
00034
00035 void compp::ASrv::Broadcast (const bool BC ) {
00036 Server.SetSockOptBroadcast( BC ) ;
00037 }
00038
00039 void compp::ASrv::AddMultiCastGroup (const std::string & IPAddr ) {
00040 Server.SetSockOptJoinMulticastGroup ( IPAddr ) ;
00041 }
00042
00043 void compp::ASrv::commenceWork () {
00044 syspp::Logger *Log = syspp::Logger::Instance();
00045
00046 Log->Info ("ASR Instance commencing main loop.");
00047
00048 Server.Bind (IPAddr, port) ;
00049
00050 Server.AttachFD();
00051 Server.SetSockOptReuseAddress ( true);
00052 Server.SetLingerTime(0);
00053
00054 Log->Info ("ASR Instance starting hread.");
00055 this->Start ( (void *) &Server );
00056 this->Join();
00057
00058 }
00059
00060
00061
00062
00063
00064 void *compp::ASrv::Run (void*v) {
00065
00066 syspp::Logger *Log = syspp::Logger::Instance();
00067
00068 compp::SocketUdp * worker = (compp::SocketUdp *) v;
00069 if ( worker == NULL ){
00070 Log->Notice ("ASR Instance: Instance == NULL in Run()" );
00071 return NULL;
00072 }
00073 int LRequest, LSEC, SIGNATURE, lenraw, port, SECRAW;
00074 std::string retval, request, raw, host;
00075
00076 ASrvContext Context;
00077
00078 #define MAXUDPPacket 65500
00079
00080 try {
00081 do {
00082
00083
00084 lenraw = worker->Read ( raw, host, port ) ;
00085 Log->Debug ("ASR Instance: lenraw: %d", lenraw );
00086
00087 if ( lenraw < 12 ) {
00088 Log->Notice ("ASR Instance: aborting processing because data is unsuitable(1)" );
00089 continue;
00090 }
00091
00092 if ( (unsigned int ) lenraw != raw.size() ) {
00093 Log->Notice ("ASR Instance: aborting processing because data is unsuitable(2)" );
00094 continue;
00095 }
00096
00097
00098 raw.copy ( (char *) &LSEC, 4, 0 );
00099 SECRAW = LSEC;
00100 raw.copy ( (char *) &LRequest, 4, 4 );
00101 raw.copy ( (char *) &SIGNATURE, 4, 8 );
00102
00103 LSEC = ntohl ( LSEC );
00104 if (LSEC != _COMM_SEC && LSEC != _COMM_CYPH) {
00105 Log->Notice ("ASR Instance: aborting processing because data is unsuitable(3)" );
00106 return NULL;
00107 }
00108
00109 LRequest = ntohl ( LRequest );
00110
00111 Log->Debug ("ASR Instance: rawsize: %d LRequest: %d", raw.size(), LRequest );
00112 if ( LRequest < 0 ) {
00113 Log->Notice ("ASR Instance: aborting processing because data is unsuitable(4)" );
00114 return NULL;
00115 }
00116
00117
00118 if ( lenraw != LRequest ) {
00119 Log->Notice ("ASR Instance: aborting processing because data is unsuitable(5)" );
00120 return NULL;
00121 }
00122
00123 request = raw.substr ( 12, lenraw -12 );
00124 Context.SetContextString ( worker->GetContextString());
00125
00126 if (LSEC != _COMM_CYPH) {
00127 Log->Debug ("ASR Instance: Data is of type 1" );
00128 syspp::Tools::Dec (request);
00129 } else {
00130 Log->Debug ("ASR Instance: Data is of type 2" );
00131 }
00132
00133 retval = executeTransaction ( request, Context ) ;
00134
00135
00136 if (LSEC == _COMM_CYPH) {
00137 syspp::Tools::Enc ( retval );
00138 }
00139
00140 std::string R = "";
00141
00142 char c[sizeof (int)];
00143 memcpy ( (void*) c, (void*) &SECRAW, sizeof (SECRAW));
00144 R.append ( c, sizeof (SECRAW));
00145
00146
00147 int Len = htonl ( retval.size() + 12 );
00148 memcpy ( (void*) c, (void*) &Len, sizeof (Len));
00149 R.append ( c, sizeof ( Len ) );
00150
00151
00152 memcpy ( (void*) c, (void*) &SIGNATURE, sizeof (SIGNATURE));
00153 R.append ( c, sizeof ( SIGNATURE ) );
00154
00155 R.append ( retval );
00156
00157 if ( worker->Sendto ( R.data(), R.size(), host, port ) < 0 ) {
00158 Log->Notice ("ASR Instance: Cannot send data errno = %d", errno );
00159 return NULL;
00160 }
00161
00162 } while ( true ) ;
00163 } catch ( syspp::ComException & e ) {
00164 Log->Notice ("ASR Instance Exception: %s", e.what () );
00165 } catch ( ... ) {
00166 Log->Notice ("ASR Instance Exception: other" );
00167
00168 }
00169 return NULL;
00170 }
00171
00172 std::string compp::ASrv::executeTransaction ( const std::string & request, ASrvContext & Context ) {
00173 std::string ret = "ASRvTest ";
00174 ret += Context.GetContextString();
00175 return ret ;
00176 }