List event tab embedded in left panel of ED + other minor changes.
[u/mrichter/AliRoot.git] / MONITOR / alistoragemanager / AliStorageServerThread.cxx
1 #include "AliStorageServerThread.h"
2 #include "AliStorageTypes.h"
3 #include "AliESDEvent.h"
4
5 #include <iostream>
6 #include <fstream>
7
8 #include <TFile.h>
9 #include <TThread.h>
10
11 using namespace std;
12
13 AliStorageServerThread::AliStorageServerThread() :
14         fDatabase(0),
15         fStoragePath("")
16 {
17         TThread::Lock();
18         fDatabase = new AliStorageDatabase();
19         //load parameters from config file
20         ifstream configFile (GetConfigFilePath());
21
22         
23         if (configFile.is_open())
24         {
25                 string line;
26                 int from,to;
27                 while(configFile.good())
28                 {
29                         getline(configFile,line);
30                         from = line.find("\"")+1;
31                         to = line.find_last_of("\"");
32                         if(line.find("STORAGE_PATH=")==0)
33                         {
34                                 fStoragePath=line.substr(from,to-from);
35                         }
36                 }
37                 if(configFile.eof())
38                 {
39                         configFile.clear();
40                 }
41                 configFile.close();
42         }
43         else
44         {
45                 cout<<"SERVER -- Unable to open config file"<<endl;
46         }
47         TThread::UnLock();
48
49         //start communication on socket
50         StartCommunication();
51 }
52
53 AliStorageServerThread::~AliStorageServerThread()
54 {
55         cout<<"SERVER -- AliStorageServerThread destructor called";     
56         if (fDatabase) {delete fDatabase;}
57         cout<<" --- OK"<<endl;
58 }
59
60 void AliStorageServerThread::StartCommunication()
61 {
62         AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
63         storageSockets socket = SERVER_COMMUNICATION_REP;
64         eventManager->CreateSocket(socket);
65
66         struct serverRequestStruct *request;
67         
68         while(1)
69         {
70                 request = eventManager->GetServerStruct(socket);
71                 
72                 switch(request->messageType)
73                   {
74                   case REQUEST_LIST_EVENTS:
75                     {
76                       vector<serverListStruct> result = fDatabase->GetList(request->list);
77                       eventManager->Send(result,socket);
78                       break;
79                     }
80                   case REQUEST_GET_EVENT:
81                     {
82                       AliESDEvent *event = fDatabase->GetEvent(request->event);
83                       eventManager->Send(event,socket);
84                       delete event;
85                       break;
86                     }
87                   case REQUEST_GET_NEXT_EVENT:
88                     {
89                 cout<<"NEXT EVENT request received"<<endl;
90                       AliESDEvent *event = fDatabase->GetNextEvent(request->event);
91                       eventManager->Send(event,socket);
92                       delete event;
93                       break;
94                     }
95                   case REQUEST_GET_PREV_EVENT:
96                     {
97                       AliESDEvent *event = fDatabase->GetPrevEvent(request->event);
98                       eventManager->Send(event,socket);
99                       delete event;
100                       break;
101                     }
102                   case REQUEST_GET_LAST_EVENT:
103                     {
104                       AliESDEvent *event = fDatabase->GetLastEvent();
105                       eventManager->Send(event,socket);
106                       delete event;
107                       break;
108                     }
109                   case REQUEST_GET_FIRST_EVENT:
110                     {
111                       AliESDEvent *event = fDatabase->GetFirstEvent();
112                       eventManager->Send(event,socket);
113                       delete event;
114                       break;
115                     }
116                   case REQUEST_MARK_EVENT:
117                     {
118                       struct eventStruct *markData  = &(request->event);
119                       eventManager->Send(MarkEvent(*markData),socket);
120                       break;
121                     }
122                   default:break;
123                   }
124
125         }
126 }
127
128 bool AliStorageServerThread::MarkEvent(struct eventStruct event)
129 {
130   string pathToFile = fDatabase->GetFilePath(event);
131   TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
132   if(!tmpFile)
133     {
134       cout<<"SERVER -- couldn't open temp file"<<endl;
135       return false;
136     }
137   AliESDEvent *eventToMark = (AliESDEvent*)tmpFile->Get(Form("event%d",event.eventNumber));
138   if(!eventToMark)
139     {
140       cout<<"SERVER -- couldn't find such event"<<endl;
141       if(tmpFile){delete tmpFile;}
142       return false;
143     }
144   cout<<"SERVER -- Marking event:"<<eventToMark->GetEventNumberInFile()<<endl;
145                 
146         TFile *permFile = new TFile(Form("%s/permEvents.root",fStoragePath.c_str()),"update");//open/create perm file
147         
148         if(!permFile)
149         {
150                 cout<<"SERVER -- Couldn't open perm file"<<endl;
151                 if(tmpFile){delete tmpFile;}
152                 if(eventToMark){delete eventToMark;}
153                 return false;
154         }
155
156         //create new directory for this run
157         TDirectory *currentRun;
158         if((currentRun = permFile->mkdir(Form("run%d",event.runNumber))))
159         {
160                 cout<<"SERVER -- creating new directory for this run"<<endl;
161                 currentRun->cd();
162         }
163         else
164         {
165                 cout<<"SERVER -- opening existing directory for this run"<<endl;
166                 permFile->cd(Form("run%d",event.runNumber));
167         }
168
169         //try to add record to the database
170         if(!fDatabase->MarkEvent(event))
171         {
172                 cout<<"SERVER -- could not mark event in the database"<<endl;
173                 if(tmpFile){delete tmpFile;}
174                 if(eventToMark){delete eventToMark;}
175                 if(permFile){delete permFile;}
176                 return false;
177         }
178
179         eventToMark->Write(Form("event%d",event.eventNumber));
180         permFile->Close();
181         tmpFile->Close();
182
183         if(tmpFile){delete tmpFile;}
184         if(eventToMark){delete eventToMark;}
185         if(permFile){delete permFile;}
186 //      if(currentRun)delete currentRun;//this line crashes if there is no permanent file yet
187         return true;
188 }
189