00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00019 #include <errno.h>
00020 #include "TMessage.h"
00021 #include "TSocket.h"
00022 #include "TSystem.h"
00023 #include "TStopwatch.h"
00024 #include "TPluginManager.h"
00025 #include "TROOT.h"
00026
00027 #include "MinosObjectMap/MomNavigator.h"
00028 #include "Persistency/PerInputStream.h"
00029 #include "Persistency/PerFileManager.h"
00030 #include "Persistency/PerFile.h"
00031 #include "MessageService/MsgService.h"
00032
00033 #include "Dispatcher/DDSChildServer.h"
00034 #include "Dispatcher/DDSSubscription.h"
00035 #include "Dispatcher/DDSFileHandler.h"
00036
00037 #include "Record/RecMinos.h"
00038
00039 std::ostream& operator << (std::ostream& ms, DDSChildServer* cs)
00040 { return cs->Print(ms); }
00041
00042 ClassImp(DDSChildServer)
00043
00044
00045
00046
00047 CVSID("$Id: DDSChildServer.cxx,v 1.34 2007/08/10 05:14:02 schubert Exp $");
00048
00049
00050
00051
00052 DDSChildServer::DDSChildServer(Int_t sockfd, Int_t maxinactive) : fTSocket(0),
00053 fSubscription(0), fFileHandler(0), fShutdown(false), fPid(0), fMom(0),
00054 fMessageIn(0), fMaxInactive(maxinactive) {
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 fPid = gSystem -> GetPid();
00072
00073
00074 fTSocket = new TSocket(sockfd);
00075
00076 if (!fTSocket || !fTSocket -> IsValid()) {
00077
00078 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00079 << ": Unable to create child server socket with socket descriptor "
00080 << sockfd << "." << endl;
00081 if (fTSocket) delete fTSocket; fTSocket = 0;
00082 fShutdown = true;
00083 }
00084 else {
00085 fServerAddress = fTSocket -> GetLocalInetAddress();
00086 fClientAddress = fTSocket -> GetInetAddress();
00087 }
00088
00089 fInputStreamManager.SetUpdateMode(true);
00090 fInputStreamManager.SetPrintOpt("brief");
00091
00092
00093
00094
00095
00096 if ( gROOT -> GetVersionInt() < 51600 )
00097 gROOT -> GetPluginManager() -> RemoveHandler("TFile","^root:");
00098
00099 fSubscription = new DDSSubscription();
00100 fFileHandler = new DDSFileHandler(fSubscription->GetDataSource(),
00101 fSubscription->IsOffLine());
00102
00103 }
00104
00105 DDSChildServer::~DDSChildServer() {
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 fInputStreamManager.CloseStream();
00116 if ( IsValid() ) delete fTSocket; fTSocket = 0;
00117 if ( fSubscription ) delete fSubscription; fSubscription = 0;
00118 if ( fFileHandler ) delete fFileHandler; fFileHandler = 0;
00119 if ( fMom ) delete fMom; fMom = 0;
00120 if ( fMessageIn ) delete fMessageIn; fMessageIn = 0;
00121
00122 }
00123
00124 void DDSChildServer::Get() {
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 if (!fMom) fMom = new MomNavigator();
00137
00138
00139 fInputStreamManager.Get(fMom);
00140
00141
00142 TIter riter = fMom->FragmentIter();
00143 RecMinos* record = 0;
00144 while ( (record = dynamic_cast<RecMinos*>(riter.Next())) ) {
00145 Registry* temptags = new Registry(record->GetTempTags());
00146 fMom->AdoptFragment(temptags);
00147 }
00148
00149 fMessageOut.Reset(DDS::kOk);
00150 fMessageOut.WriteObject(fMom);
00151 return;
00152
00153 }
00154
00155 void DDSChildServer::GoToFile() {
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 if ( !fFileHandler || !fFileHandler -> IsValid()) {
00167 fMessageOut.Reset(DDS::kFileError);
00168 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00169 << " GoToFile called but DDSFileHandler is InValid." << endl;
00170 return;
00171 }
00172
00173
00174 char filename[512];
00175 (*fMessageIn) >> filename;
00176 std::string newfilename;
00177 if ( !strncmp(filename,"next",4) ) {
00178 MSG("DDS",Msg::kDebug) << "CS_" << fPid << " GoToNextFile requested."
00179 << endl;
00180 newfilename = fFileHandler->GoToNextFile();
00181 }
00182 else if ( !strncmp(filename,"symlink",7) ) {
00183 MSG("DDS",Msg::kDebug) << "CS_" << fPid << " GoToSymLinkFile requested."
00184 << endl;
00185 newfilename = fFileHandler->GoToSymLinkFile();
00186 }
00187 else {
00188 MSG("DDS",Msg::kDebug) << "CS_" << fPid << " GoToFile " << filename
00189 << " requested." << endl;
00190 newfilename = fFileHandler->GoToFile(filename);
00191 }
00192
00193 if ( !newfilename.empty() ) {
00194 MSG("DDS",Msg::kVerbose) << "CS_" << fPid
00195 << " PerInputStreamManager status before closing old file:\n"
00196 << fInputStreamManager;
00197 fInputStreamManager.CloseFile();
00198 MSG("DDS",Msg::kDebug) << "CS_" << fPid << " Advancing to " << newfilename
00199 << "." << endl;
00200 if(!fInputStreamManager.SetFile("*",newfilename,Per::kRead)) {
00201 fMessageOut.Reset(DDS::kFileError);
00202 }
00203 else {
00204 fMessageOut.Reset(DDS::kOk);
00205 fMessageOut << newfilename.c_str();
00206 MSG("DDS",Msg::kVerbose) << "CS_" << fPid
00207 << " PerInputStreamManager status after opening new file:\n"
00208 << fInputStreamManager;
00209 }
00210 }
00211 else {
00212 MSG("DDS",Msg::kDebug) << "CS_" << fPid
00213 << " Failed to retrieve new filename." << endl;
00214 fMessageOut.Reset(DDS::kFileError);
00215 }
00216
00217 return;
00218
00219 }
00220
00221 bool DDSChildServer::IsClientConnected() const {
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 TSystem::ResetErrno();
00233 if (!fTSocket || !fTSocket->IsValid()) return false;
00234
00235 Int_t n;
00236 UInt_t len;
00237 Int_t savevalue;
00238 fTSocket->GetOption(kNoBlock,savevalue);
00239 fTSocket->SetOption(kNoBlock,1);
00240 Int_t socketdescriptor = fTSocket->GetDescriptor();
00241 n = gSystem->RecvRaw(socketdescriptor, &len, sizeof(UInt_t),kPeek);
00242 fTSocket->SetOption(kNoBlock,savevalue);
00243
00244 if ( n <= 0 && n != -4) return false;
00245 return true;
00246
00247 }
00248
00249 void DDSChildServer::Next() {
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 if ( !fSubscription ) {
00264 fMessageOut.Reset(DDS::kError);
00265 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00266 << " Next called but no subscription has been submitted." << endl;
00267 return;
00268 }
00269
00270 if ( !fFileHandler || !fFileHandler -> IsValid()) {
00271 fMessageOut.Reset(DDS::kFileError);
00272 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00273 << " Next called but DDSFileHandler is InValid." << endl;
00274 return;
00275 }
00276
00277
00278 if (!fMom) fMom = new MomNavigator();
00279
00280
00281 UInt_t waittime;
00282 (*fMessageIn) >> waittime;
00283 UInt_t advanceby;
00284 (*fMessageIn) >> advanceby;
00285 TStopwatch timer; timer.Start();
00286 bool isNewSet = false;
00287
00288 Int_t keepUpWindow = fSubscription -> GetKeepUpWindow();
00289 while ( timer.RealTime() < waittime ) {
00290 timer.Continue();
00291 fMom->Clear();
00292
00293 DDS::EKeepUpMode keepupmode = fSubscription -> GetKeepUpMode();
00294 if ( (keepupmode == DDS::kFileKeepUp || keepupmode == DDS::kRecordKeepUp)
00295 && fFileHandler->NewFileAvailable() ) {
00296
00297 std::string newfilename = fFileHandler->GoToSymLinkFile();
00298 MSG("DDS",Msg::kDebug) << "CS_" << fPid
00299 << " Next detected NewFileAvailable and keepupmode is "
00300 << DDS::AsString(keepupmode) << ", called GoToSymLinkFile." << endl;
00301 if ( !newfilename.empty() ) {
00302 MSG("DDS",Msg::kVerbose) << "CS_" << fPid
00303 << " PerInputStreamManager status before closing old file:\n"
00304 << fInputStreamManager;
00305 fInputStreamManager.CloseFile();
00306 MSG("DDS",Msg::kDebug) << "CS_" << fPid << " Advancing to "
00307 << newfilename << "." << endl;
00308 if(!fInputStreamManager.SetFile("*",newfilename,Per::kRead)) {
00309 fMessageOut.Reset(DDS::kFileError);
00310 return;
00311 }
00312 else {
00313 MSG("DDS",Msg::kVerbose) << "CS_" << fPid
00314 << " PerInputStreamManager status after opening new file:\n"
00315 << fInputStreamManager;
00316 }
00317 }
00318 else {
00319 MSG("DDS",Msg::kDebug) << "CS_" << fPid
00320 << " Failed to retrieve new filename." << endl;
00321 }
00322 }
00323 if ( !fInputStreamManager.IsValidSelectionString() ) {
00324 MSG("DDS",Msg::kDebug) << "CS_" << fPid
00325 << " Has invalid subscription selection string." << endl;
00326 fMessageOut.Reset(DDS::kInvalidSelection);
00327 return;
00328 }
00329
00330 if ( fInputStreamManager.Next(0,advanceby) > 0 ) {
00331 isNewSet = true;
00332 if ( keepupmode != DDS::kRecordKeepUp ) {
00333
00334 this -> Get();
00335 return;
00336 }
00337 else {
00338 Int_t lastTime = (Int_t)
00339 (fInputStreamManager.GetLastEntryVld().GetTimeStamp().GetSec());
00340 Int_t currentTime = (Int_t)
00341 (fInputStreamManager.GetCurrentVld().GetTimeStamp().GetSec());
00342 if ( lastTime - currentTime <= keepUpWindow ) {
00343 this -> Get();
00344 return;
00345 }
00346 }
00347 }
00348 else {
00349
00350 if ( fFileHandler->NewFileAvailable() &&
00351 !fInputStreamManager.IsFileEnd() ) {
00352 std::string currentfilename = "";
00353 const PerStreamManager::StreamMap& sm_mgr
00354 = fInputStreamManager.GetStreamMap();
00355 PerStreamManager::StreamMapConstItr itr_mgr = sm_mgr.begin();
00356 const PerInputStream* instream = 0;
00357 if ( itr_mgr != sm_mgr.end() ) instream
00358 = dynamic_cast<PerInputStream*>(itr_mgr->second);
00359 if (instream) currentfilename = instream->GetFullFilePathName();
00360 PerFileManager& filemgr = PerFileManager::Instance();
00361 if ( !((filemgr.GetOpenedFile(currentfilename))->HasFileEndKey()) ) {
00362 std::string symlinktargetname = fFileHandler->GetSymLinkTargetName();
00363 if ( symlinktargetname != currentfilename ) {
00364 MSG("DDS",Msg::kDebug) << "CS_" << fPid << " Current file "
00365 << currentfilename << "\n not closed but symlink currentfile "
00366 << symlinktargetname << "\n has moved on."
00367 << " Assume abort, finish current file, and move to next file."
00368 << endl;
00369 fInputStreamManager.SetFileEnd();
00370 }
00371 }
00372 }
00373 else if ( fInputStreamManager.IsFileEnd() ){
00374
00375
00376 std::string fullfilepathname="";
00377 if ( keepupmode == DDS::kAll) {
00378 fullfilepathname = fFileHandler->GoToNextFile();
00379 if ( !fullfilepathname.empty() ) {
00380 MSG("DDS",Msg::kDebug) << "CS_" << fPid
00381 << " Next call to GoToNextFile retrieved file\n\t"
00382 << fullfilepathname << "." << endl;
00383 }
00384 }
00385 else if ( fFileHandler->NewFileAvailable() ) {
00386 fullfilepathname = fFileHandler->GoToSymLinkFile();
00387 MSG("DDS",Msg::kDebug) << "CS_" << fPid
00388 << " Next call to GoToSymLinkFile retrieved filename\n\t"
00389 << fullfilepathname << "." << endl;
00390 }
00391 if (!fullfilepathname.empty()) {
00392 MSG("DDS",Msg::kVerbose) << "CS_" << fPid
00393 << " PerInputStreamManager status before closing old file:\n"
00394 << fInputStreamManager;
00395 fInputStreamManager.CloseFile();
00396 if(!fInputStreamManager.SetFile("*",fullfilepathname,Per::kRead)) {
00397 fMessageOut.Reset(DDS::kFileError);
00398 return;
00399 }
00400 MSG("DDS",Msg::kVerbose) << "CS_" << fPid
00401 << " PerInputStreamManager status after opening new file:\n"
00402 << fInputStreamManager;
00403 }
00404 else {
00405 if ( keepupmode == DDS::kRecordKeepUp && isNewSet ) {
00406
00407 fInputStreamManager.Previous(0,advanceby);
00408 this -> Get();
00409 return;
00410 }
00411
00412 gSystem->Sleep(1000);
00413
00414
00415 if ( !IsClientConnected() ) {
00416 fMessageOut.Reset(DDS::kSocketError);
00417 return;
00418 }
00419 }
00420 }
00421 else {
00422 if ( keepupmode == DDS::kRecordKeepUp && isNewSet ) {
00423
00424 this -> Get();
00425 return;
00426 }
00427
00428 gSystem->Sleep(1000);
00429
00430
00431 if ( !IsClientConnected() ) {
00432 fMessageOut.Reset(DDS::kSocketError);
00433 return;
00434 }
00435 }
00436 }
00437 }
00438 timer.Stop();
00439
00440
00441
00442 if ( !fInputStreamManager.IsFileEnd() ) {
00443 fMessageOut.Reset(DDS::kTimeoutNewRecord);
00444 }
00445 else {
00446 fMessageOut.Reset(DDS::kTimeoutNewFile);
00447 }
00448
00449 return;
00450
00451 }
00452
00453 std::ostream& DDSChildServer::Print(std::ostream& ms) const {
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463 if ( IsValid() ) {
00464 ms << "CS_" << fPid << ": connected to client at "
00465
00466 << fClientAddress.GetHostName() <<"/"<< fClientAddress.GetHostAddress()
00467 << "(port " << fClientAddress.GetPort() << ")." << endl;
00468 }
00469 else {
00470 ms << "CS_" << fPid << ": childserver socket is not connected." << endl;
00471 }
00472
00473 return ms;
00474
00475 }
00476
00477 Int_t DDSChildServer::Run() {
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 Int_t rc = 0;
00512
00513 TStopwatch timer;
00514 timer.Start();
00515
00516 while ( !fShutdown ) {
00517
00518 fTSocket -> SetOption(kNoBlock,1);
00519 Int_t rettype = fTSocket->Recv(fMessageIn);
00520 fTSocket -> SetOption(kNoBlock,0);
00521 if ( rettype != -4 ) {
00522 timer.Reset();
00523 if ( rettype > 0 ) {
00524 switch ( fMessageIn -> What() ) {
00525
00526 case DDS::kGoToFile:
00527
00528 GoToFile();
00529 break;
00530
00531 case DDS::kNext:
00532
00533 Next();
00534 break;
00535
00536 case DDS::kShutdown:
00537
00538 Shutdown();
00539 break;
00540
00541 case DDS::kSubscribe:
00542
00543 Subscribe();
00544 break;
00545
00546 default:
00547 MSG("DDS",Msg::kWarning)<< "CS_"<< fPid << ": Unknown client message: "
00548 << fMessageIn -> What() << " received.\n"
00549 << "Unable to process client request." << endl;
00550 fMessageOut.Reset(DDS::kMessageUnknown);
00551 break;
00552 }
00553
00554 if ( fMessageIn ) delete fMessageIn; fMessageIn = 0;
00555
00556 }
00557 else {
00558
00559 fShutdown = true;
00560 delete fTSocket; fTSocket = 0;
00561 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00562 << ": Socket error detected on TSocket::Recv." <<
00563 "\n ChildServer will be shutdown." << endl;
00564 rc = 1;
00565 }
00566
00567 if ( fTSocket && IsClientConnected() ) {
00568
00569 if ( fTSocket -> Send(fMessageOut) < 0 ) {
00570
00571 if (errno == EPIPE) {
00572
00573 fShutdown = true;
00574 delete fTSocket; fTSocket = 0;
00575 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00576 << ": Socket error detected on TSocket::Send."
00577 << "\n ChildServer will be shutdown." << endl;
00578 rc = 1;
00579 }
00580 }
00581 }
00582 else if (!fShutdown) {
00583
00584 fShutdown = true;
00585 delete fTSocket; fTSocket = 0;
00586 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00587 << ": Socket error detected indicating client has disconnected.\n"
00588 << " ChildServer will be shutdown."<< endl;
00589 rc = 1;
00590 }
00591 }
00592 else {
00593 if ( timer.RealTime() > fMaxInactive ) {
00594 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00595 << ": Inactivity time limit of "
00596 << fMaxInactive << "(sec) reached.\n"
00597 << "ChildServer will be shutdown." << endl;
00598 Shutdown();
00599 fMessageOut.Reset(DDS::kInactive);
00600 rc = 2;
00601 if ( fTSocket && IsClientConnected() ) fTSocket->Send(fMessageOut);
00602 }
00603 else {
00604 timer.Continue();
00605 gSystem->Sleep(10);
00606 }
00607 }
00608 }
00609
00610 return rc;
00611
00612 }
00613
00614 void DDSChildServer::Shutdown() {
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626 fShutdown = true;
00627 MSG("DDS",Msg::kVerbose) << "CS_" << fPid
00628 << " PerInputStreamManager status before shutdown:\n"
00629 << fInputStreamManager;
00630 fInputStreamManager.CloseStream();
00631 fMessageOut.Reset(DDS::kOk);
00632
00633 return;
00634
00635 }
00636
00637 void DDSChildServer::Subscribe() {
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650 if ( fSubscription) delete fSubscription; fSubscription = 0;
00651
00652
00653 fSubscription = dynamic_cast<DDSSubscription*>
00654 (fMessageIn -> ReadObject(fMessageIn->GetClass()));
00655 if ( !fSubscription ) {
00656 MSG("DDS",Msg::kWarning) << "CS_" << fPid
00657 << "\nSubscribe failed to receive expected DDSSubscription from client."
00658 << endl;
00659 fMessageOut.Reset(DDS::kError);
00660 }
00661 else {
00662
00663 MSG("DDS",Msg::kDebug) << "CS_" << fPid
00664 << " Subscribe received subscription:\n"
00665 << fSubscription;
00666
00667 fMessageOut.Reset(DDS::kOk);
00668 if (fFileHandler && ( (fSubscription -> GetDataSource())
00669 !=(fFileHandler -> GetDataSource())
00670 || (fSubscription -> IsOffLine())
00671 !=(fFileHandler -> IsOffLine()) ) ) {
00672 delete fFileHandler; fFileHandler=0;
00673 }
00674 if (!fFileHandler)
00675 fFileHandler = new DDSFileHandler(fSubscription -> GetDataSource(),
00676 fSubscription -> IsOffLine());
00677
00678 std::string currentFileName = fFileHandler->GetCurrentFileName();
00679
00680
00681 PerInputStream* instream;
00682 const DDSSubscription::StreamMap& sm_sub = fSubscription->GetStreamMap();
00683 for (DDSSubscription::StreamMapConstItr itr_sub = sm_sub.begin();
00684 itr_sub!= sm_sub.end();++itr_sub) {
00685 string streamName = (itr_sub->first).Data();
00686 if ( !(instream = dynamic_cast<PerInputStream*>
00687 (fInputStreamManager.GetOpenedStream(streamName))) ) {
00688
00689 instream = fInputStreamManager.OpenStream(streamName,streamName);
00690 if (!currentFileName.empty())instream -> SetFile(currentFileName);
00691 }
00692 if ( instream ) instream -> SetSelection((itr_sub->second).Data());
00693 }
00694
00695 fInputStreamManager.SetMaxSyncDelay(fSubscription -> GetMaxSyncDelay());
00696
00697
00698 const PerStreamManager::StreamMap& sm_mgr
00699 = fInputStreamManager.GetStreamMap();
00700 for ( PerStreamManager::StreamMapConstItr itr_mgr = sm_mgr.begin();
00701 itr_mgr!= sm_mgr.end(); ++itr_mgr) {
00702 std::string streamName = itr_mgr->first;
00703 DDSSubscription::StreamMapConstItr itr_sub
00704 = sm_sub.find(streamName.c_str());
00705 if (itr_sub == sm_sub.end())fInputStreamManager.CloseStream(streamName);
00706 }
00707 }
00708
00709 return;
00710
00711 }
00712