00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4CommandRunnable.h"
00017
00018 #include <iostream.h>
00019
00020 #include "Go4Log/TGo4Log.h"
00021 #include "Go4ThreadManager/TGo4ThreadManager.h"
00022 #include "Go4CommandsTaskHandler/Go4CommandsTaskHandler.h"
00023 #include "Go4Queue/TGo4BufferQueue.h"
00024 #include "Go4Socket/TGo4Socket.h"
00025 #include "Go4Socket/TGo4SocketSignalHandler.h"
00026 #include "Go4CommandsBase/TGo4CommandInvoker.h"
00027 #include "Go4CommandsBase/TGo4Command.h"
00028 #include "TGo4TaskHandler.h"
00029 #include "TGo4TaskHandlerAbortException.h"
00030 #include "TGo4ClientTask.h"
00031 #include <iostream>
00032 #include <sstream>
00033
00034 using namespace std;
00035
00036 TGo4CommandRunnable::TGo4CommandRunnable(const char* name,
00037 TGo4ThreadManager* man,
00038 TGo4TaskHandler* hand,
00039 Bool_t receivermode)
00040 :TGo4TaskHandlerRunnable(name,man,hand,receivermode)
00041 {
00042 fxBufferQueue=dynamic_cast<TGo4BufferQueue*> (fxTaskHandler->GetCommandQueue() );
00043 fxTransport=fxTaskHandler->GetCommandTransport();
00044 fxInvoker=TGo4CommandInvoker::Instance();
00045 }
00046
00047
00048 TGo4CommandRunnable::~TGo4CommandRunnable()
00049 {
00050 }
00051
00052 Int_t TGo4CommandRunnable::Run(void* ptr)
00053 {
00054 if(!CheckTransportOpen()) return 0;
00055 if(fbReceiverMode)
00056 {
00057
00058 Int_t rev=fxTransport->ReceiveBuffer();
00059 if(rev>=0)
00060 {
00061 TBuffer* buffer=const_cast<TBuffer*> (fxTransport->GetBuffer());
00062 Int_t val=0;
00063 if(CheckStopBuffer(buffer,&val)) return 0;
00064 if(val>=0)
00065 {
00066 fxTransport->Send(TGo4TaskHandler::fgcOK);
00067 TGo4Task* cli = dynamic_cast<TGo4Task*>(fxManager);
00068
00069 TGo4ComQuit* qcommand=0;
00070 Go4EmergencyCommand_t comvalue= (Go4EmergencyCommand_t) (val);
00071
00072 switch(comvalue)
00073 {
00074 case kComQuit:
00075 if(fxTaskHandler->GetRole()==kGo4ComModeObserver) break;
00076 TGo4Log::Debug(" Command runnable executing direct command QUIT... ");
00077
00078
00079
00080 qcommand = new TGo4ComQuit;
00081 cli->SubmitLocalCommand(qcommand);
00082 break;
00083 case kComKillMain:
00084 if(fxTaskHandler->GetRole()==kGo4ComModeObserver) break;
00085 TGo4Log::Debug(" Command runnable executing direct command KILL MAIN... ");
00086 cli->KillMain();
00087 break;
00088 case kComRestartMain:
00089 if(fxTaskHandler->GetRole()==kGo4ComModeObserver) break;
00090 TGo4Log::Debug(" Command runnable executing direct command RESTART MAIN... ");
00091 cli->RestartMain();
00092 break;
00093 case kComCloseInput:
00094
00095 TGo4Log::Debug("NEVER COME HERE: Command runnable has direct command CLOSE INPUT");
00096
00097
00098 break;
00099 default:
00100 TGo4Log::Debug(" Command runnable direct command value %d UNKNOWN! ",
00101 comvalue);
00102 break;
00103
00104
00105 }
00106
00107 }
00108 else
00109 {
00110 fxBufferQueue->AddBuffer(buffer, kTRUE);
00111 fxTransport->Send(TGo4TaskHandler::fgcOK);
00112 }
00113 }
00114 else
00115 {
00116 if (TGo4SocketSignalHandler::fgiLastSignal == SIGWINCH)
00117 {
00118
00119 TGo4Log::Debug(" %s: caught SIGWINCH ", GetName());
00120 TGo4SocketSignalHandler::fgiLastSignal = 0;
00121 }
00122 else if(fxManager->IsTerminating())
00123 {
00124
00125 TGo4Log::Debug("Receive Error in %s during threadmanager termination. Ignored.",GetName());
00126 GetThread()->Stop();
00127
00128 }
00129 else
00130 {
00131 if(fxTaskHandler->IsClientMode()) RedirectIO();
00132 TGo4Log::Debug("Receive Error in CommandRunnable, terminating...");
00133 throw TGo4TaskHandlerAbortException(this);
00134 }
00135 }
00136 }
00137 else
00138 {
00139
00140 TBuffer* buf= fxBufferQueue->WaitBuffer();
00141 if (buf)
00142
00143 {
00144 Bool_t stopmode=CheckStopBuffer(buf);
00145
00146 fxTransport->SendBuffer(buf);
00147
00148 fxBufferQueue->FreeBuffer(buf);
00149 if(stopmode) return 0;
00150 Text_t* revchar=fxTransport->RecvRaw("dummy");
00151 if(revchar==0)
00152 {
00153
00154 if (TGo4SocketSignalHandler::fgiLastSignal == SIGWINCH)
00155
00156 {
00157
00158 TGo4Log::Debug(" %s: caught SIGWINCH ",GetName());
00159 TGo4SocketSignalHandler::fgiLastSignal = 0;
00160
00161 }
00162 else
00163 {
00164
00165 TGo4Log::Debug(" !!!Connection Error -1 in CommandRunnable ''%s''!!!",GetName());
00166 throw TGo4TaskHandlerAbortException(this);
00167 }
00168 }
00169 else
00170 {
00171
00172 }
00173
00174 if(!strcmp(revchar,TGo4TaskHandler::fgcOK) )
00175 {
00176
00177 }
00178 else if(!strcmp(revchar,TGo4TaskHandler::fgcERROR))
00179 {
00180
00181 TGo4Log::Debug(" CommandRunnable ''%s'' received command error string!!!",GetName());
00182 }
00183 else
00184 {
00185
00186 TGo4Log::Debug(" !!!Connection Error -3 in CommandRunnable ''%s''!!!",GetName());
00187
00188 }
00189 }
00190 else
00191 {
00192 if(!GetThread()->IsRunning())
00193 {
00194
00195
00196
00197
00198 return 0;
00199 }
00200
00201 }
00202 }
00203 return 0;
00204 }
00205
00206
00207 void TGo4CommandRunnable::RedirectIO()
00208 {
00209
00210
00211 ostringstream* strout = new ostringstream;
00212
00213 cout.rdbuf(strout->rdbuf());
00214 ostringstream* strerr = new ostringstream;
00215
00216 cerr.rdbuf(strerr->rdbuf());
00217 }
00218
00219