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