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