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