00001 // $Id: TGo4TaskHandlerAbortException.cxx 934 2013-01-29 15:59:24Z linev $ 00002 //----------------------------------------------------------------------- 00003 // The GSI Online Offline Object Oriented (Go4) Project 00004 // Experiment Data Processing at EE department, GSI 00005 //----------------------------------------------------------------------- 00006 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH 00007 // Planckstr. 1, 64291 Darmstadt, Germany 00008 // Contact: http://go4.gsi.de 00009 //----------------------------------------------------------------------- 00010 // This software can be used under the license agreements as stated 00011 // in Go4License.txt file which is part of the distribution. 00012 //----------------------------------------------------------------------- 00013 00014 #include "TGo4TaskHandlerAbortException.h" 00015 00016 #include "TApplication.h" 00017 00018 #include "TGo4Log.h" 00019 #include "TGo4Thread.h" 00020 #include "TGo4Task.h" 00021 #include "TGo4TaskHandler.h" 00022 #include "TGo4TaskHandlerRunnable.h" 00023 #include "TGo4Slave.h" 00024 #include "TGo4Master.h" 00025 #include "TGo4ComRemoveClient.h" 00026 #include "TGo4ComDisconnectSlave.h" 00027 00028 TGo4TaskHandlerAbortException::TGo4TaskHandlerAbortException (TGo4TaskHandler* taskhandler) 00029 : TGo4TaskHandlerException(taskhandler) 00030 { 00031 fxDescription= "!!!-- Go4 TaskHandler Abort Exception --!!!"; 00032 } 00033 00034 TGo4TaskHandlerAbortException::TGo4TaskHandlerAbortException(TGo4TaskHandlerRunnable * run) 00035 : TGo4TaskHandlerException(run) 00036 { 00037 fxDescription= "!!!-- Go4 TaskHandler Abort Exception --!!!"; 00038 } 00039 00040 TGo4TaskHandlerAbortException::~TGo4TaskHandlerAbortException() 00041 { 00042 } 00043 00044 Int_t TGo4TaskHandlerAbortException::Handle() 00045 { 00046 if(fxCaller) 00047 { 00048 // we know the calling thread, stop it! 00049 fxCaller->GetThread()->Stop(); 00050 } 00051 else 00052 { 00053 // no caller specified, continue 00054 } 00055 00056 if (fxTaskHandler->IsAborting()) 00057 { 00058 // a concurrent exception is already performing, we do nothing 00059 TGo4Log::Debug(" TaskHandlerAbortException: taskhandler is already aborting "); 00060 return 0; 00061 } 00062 else 00063 { 00064 // just go on with the show 00065 fxTaskHandler->SetAborting(kTRUE); 00066 } // if (fxTaskHandler->IsAborting()) 00067 00068 if (fxThreadManager==0) 00069 { 00070 // error 00071 fxTaskHandler->SetAborting(kFALSE); 00072 return -1; 00073 } 00074 else 00075 { 00076 // continue 00077 } // if (fxThreadManager==0) 00078 00079 const char* taskname = fxTaskHandler->GetName(); 00080 TGo4Master* master=0; 00081 TGo4Slave* slave=0; 00082 TGo4Task* task = dynamic_cast<TGo4Task*> (fxThreadManager); 00083 if(task) 00084 { 00085 master=task->GetMaster(); 00086 slave=task->GetSlave(); 00087 } 00088 if(slave) 00089 { 00090 TGo4Log::Debug(" TaskHandlerAbortException for slave %s ... ",taskname); 00091 if(slave->IsServer()) 00092 { 00093 // note: we need a local command here (executed in local thread), 00094 // since this is running inside thread of taskhandler to be deleted! 00095 TGo4ComRemoveClient* removecommand = new TGo4ComRemoveClient; 00096 //removecommand->SetClient("currentclient"); 00097 removecommand->SetClient(taskname); 00098 removecommand->SetWaitForClient(kFALSE); 00099 TGo4Log::Debug(" TaskHandlerAbortException on slave server: revoming client %s",taskname); 00100 task->SubmitCommand(removecommand); 00101 } 00102 else 00103 { 00104 TGo4Log::Debug(" TaskHandlerAbortException: Terminating slave..."); 00105 fxThreadManager->SetBeingQuit(kTRUE); // flag for the application that we expect to be quit soon 00106 TGo4Log::Debug(" TaskHandlerAbortException set the being quit"); 00107 slave->TerminateFast(); 00108 } 00109 } 00110 else if(master) 00111 { 00112 TGo4Log::Debug(" TaskHandlerAbortException: Removing current slave... "); 00113 // note: we need a local command here (executed in local thread), since 00114 // this is running inside thread of taskhandler to be deleted! 00115 TGo4ComDisconnectSlave* discommand = new TGo4ComDisconnectSlave; 00116 discommand->SetSlave("currentclient"); 00117 discommand->SetWaitForSlave(kFALSE); 00118 TGo4Log::Debug(" TaskHandlerAbortException: Disconnecting current slave"); 00119 task->SubmitCommand(discommand); 00120 } 00121 else if(task) 00122 { 00123 // no master, no slave: old style connection, test client and server role 00124 if(fxTaskHandler->IsClientMode()) 00125 { 00126 // exception was raised by client task handler 00127 TGo4Log::Debug(" TaskHandlerAbortException: Quit client %s ... ",taskname); 00128 fxThreadManager->SetBeingQuit(kTRUE); // flag for the application that we expect to be quit soon 00129 task->TerminateFast(); 00130 } 00131 else 00132 { 00133 // exception was raised by server task handler 00134 TGo4ComRemoveClient* removecommand = new TGo4ComRemoveClient; 00135 removecommand->SetClient(taskname); 00136 removecommand->SetWaitForClient(kFALSE); 00137 TGo4Log::Debug(" TaskHandlerAbortException: Disconnecting client %s ... ",taskname); 00138 task->SubmitCommand(removecommand); 00139 // we cannot remove our own thread, so use local command thread 00140 } // if (fxTaskHandler->IsClientMode()) 00141 } 00142 else 00143 { 00144 // no task: something is very wrong! 00145 gApplication->Terminate(); 00146 } 00147 return 0; 00148 } 00149