AliRoot Core  v5-06-30 (35d6c57)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliStorageServerThread.cxx
Go to the documentation of this file.
2 #include "AliStorageTypes.h"
3 #include "AliESDEvent.h"
4 #include "AliCDBManager.h"
5 #include "AliCDBEntry.h"
6 #include "AliCDBPath.h"
7 #include "AliOnlineReconstructionUtil.h"
8 #include "AliTriggerConfiguration.h"
9 #include "AliTriggerClass.h"
10 
11 #include <iostream>
12 #include <fstream>
13 
14 #include <TEnv.h>
15 #include <TFile.h>
16 #include <TThread.h>
17 
18 using namespace std;
19 
21 fDatabase(0),
22 fStoragePath("")
23 {
25 
26  TThread::Lock();
27  //load parameters from config file
28  ifstream configFile (GetConfigFilePath());
29  if (configFile.is_open())
30  {
31  string line;
32  int from,to;
33  while(configFile.good())
34  {
35  getline(configFile,line);
36  from = line.find("\"")+1;
37  to = line.find_last_of("\"");
38  if(line.find("STORAGE_PATH=")==0){
39  fStoragePath=line.substr(from,to-from);
40  }
41  }
42  if(configFile.eof()){configFile.clear();}
43  configFile.close();
44  }
45  else{cout<<"SERVER -- Unable to open config file"<<endl;}
46  TThread::UnLock();
47 
48  //start communication on socket
49  cout<<"Starting server's communication"<<endl;
51 }
52 
54 {
55  cout<<"SERVER -- AliStorageServerThread destructor called";
56  if (fDatabase) {delete fDatabase;}
57  cout<<" --- OK"<<endl;
58 }
59 
61 {
62  AliZMQManager *eventManager = AliZMQManager::GetInstance();
63  storageSockets socket = SERVER_COMMUNICATION_REP;
64  eventManager->CreateSocket(socket);
65 
66  struct serverRequestStruct *request;
67 
68  bool receiveStatus = false;
69  bool sendStatus = false;
70 
71  while(1)
72  {
73  cout<<"Server waiting for requests"<<endl;
74  do{ // try to receive requests until success
75  receiveStatus = eventManager->Get(request,socket);
76  } while(receiveStatus == false);
77 
78  cout<<"Server received request:"<<request->messageType<<endl;
79  switch(request->messageType)
80  {
81  case REQUEST_LIST_EVENTS:
82  {
83  cout<<"SERVER -- received request for list of events"<<endl;
84  struct listRequestStruct list;
85  list.runNumber[0] = request->runNumber[0];
86  list.runNumber[1] = request->runNumber[1];
87  list.eventNumber[0] = request->eventNumber[0];
88  list.eventNumber[1] = request->eventNumber[1];
89  list.marked[0] = request->marked[0];
90  list.marked[1] = request->marked[1];
91  list.multiplicity[0] = request->multiplicity[0];
92  list.multiplicity[1] = request->multiplicity[1];
93  strcpy(list.system[0],request->system[0]);
94  strcpy(list.system[1],request->system[1]);
95  strcpy(list.triggerClass,request->triggerClass);
96 
97  vector<serverListStruct> result = fDatabase->GetList(list);
98  cout<<"SERVER -- got list from database"<<endl;
99  sendStatus = eventManager->Send(result,socket);
100  if(sendStatus){cout<<"SERVER -- list was sent"<<endl;}
101  else{cout<<"SERVER -- couldn't send list"<<endl;}
102  break;
103  }
104  case REQUEST_GET_EVENT:
105  {
106  cout<<"get event"<<endl;
107  struct eventStruct ev;
108  ev.runNumber = request->eventsRunNumber;
109  ev.eventNumber = request->eventsEventNumber;
110 
111  AliESDEvent *event = fDatabase->GetEvent(ev);
112  sendStatus = eventManager->Send(event,socket);
113  delete event;
114  break;
115  }
116  case REQUEST_GET_NEXT_EVENT:
117  {
118  cout<<"NEXT EVENT request received"<<endl;
119 
120  struct eventStruct ev;
121  ev.runNumber = request->eventsRunNumber;
122  ev.eventNumber = request->eventsEventNumber;
123 
124  AliESDEvent *event = fDatabase->GetNextEvent(ev);
125  sendStatus = eventManager->Send(event,socket);
126  delete event;
127  break;
128  }
129  case REQUEST_GET_PREV_EVENT:
130  {
131  cout<<"PREV request"<<endl;
132 
133  struct eventStruct ev;
134  ev.runNumber = request->eventsRunNumber;
135  ev.eventNumber = request->eventsEventNumber;
136 
137  AliESDEvent *event = fDatabase->GetPrevEvent(ev);
138  sendStatus = eventManager->Send(event,socket);
139  delete event;
140  break;
141  }
142  case REQUEST_GET_LAST_EVENT:
143  {
144  cout<<"LAST request"<<endl;
145  AliESDEvent *event = fDatabase->GetLastEvent();
146  sendStatus = eventManager->Send(event,socket);
147  delete event;
148  break;
149  }
150  case REQUEST_GET_FIRST_EVENT:
151  {
152  cout<<"FIRST request"<<endl;
153  AliESDEvent *event = fDatabase->GetFirstEvent();
154  sendStatus = eventManager->Send(event,socket);
155  delete event;
156  break;
157  }
158  case REQUEST_MARK_EVENT:
159  {
160  cout<<"MARK request"<<endl;
161  struct eventStruct ev;
162  ev.runNumber = request->eventsRunNumber;
163  ev.eventNumber = request->eventsEventNumber;
164 
165  struct eventStruct *markData = &(ev);
166  sendStatus = eventManager->Send(MarkEvent(*markData),socket);
167  break;
168  }
169  case REQUEST_GET_TRIGGER_LIST:
170  {
171  cout<<"Get trigger list request"<<endl;
172  vector<string100> classes = GetTriggerClasses();
173  cout<<"SERVER -- got set from database"<<endl;
174  sendStatus = eventManager->Send(classes,socket);
175  break;
176  }
177  default:
178  {
179  cout<<"SERVER -- unknown request message"<<endl;
180  sendStatus = false;
181  break;
182  }
183  }
184  if(sendStatus == false)
185  {
186  eventManager->RecreateSocket(socket);// if couldn't send, recreate socket to be able to receive messages (currently socket is in SEND state)
187  }
188  delete request;
189  }
190 }
191 
192 bool AliStorageServerThread::MarkEvent(struct eventStruct event)
193 {
194  string pathToFile = fDatabase->GetFilePath(event);
195  TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
196  if(!tmpFile)
197  {
198  cout<<"SERVER -- couldn't open temp file"<<endl;
199  return false;
200  }
201 
202  tmpFile->cd(Form("run%d",event.runNumber));
203 
204  AliESDEvent *eventToMark = (AliESDEvent*)gDirectory->Get(Form("event%d",event.eventNumber));
205  if(!eventToMark)
206  {
207  cout<<"SERVER -- couldn't find such event"<<endl;
208  if(tmpFile){tmpFile->Close();delete tmpFile;}
209  return false;
210  }
211  cout<<"SERVER -- Marking event:"<<eventToMark->GetEventNumberInFile()<<endl;
212 
213  TFile *permFile = new TFile(Form("%s/permEvents.root",fStoragePath.c_str()),"update");//open/create perm file
214 
215  if(!permFile)
216  {
217  cout<<"SERVER -- Couldn't open perm file"<<endl;
218  if(tmpFile){tmpFile->Close();delete tmpFile;}
219  if(eventToMark){delete eventToMark;}
220  return false;
221  }
222 
223  //create new directory for this run
224  TDirectory *currentRun;
225  if((currentRun = permFile->mkdir(Form("run%d",event.runNumber))))
226  {
227  cout<<"SERVER -- creating new directory for this run"<<endl;
228  currentRun->cd();
229  }
230  else
231  {
232  cout<<"SERVER -- opening existing directory for this run"<<endl;
233  permFile->cd(Form("run%d",event.runNumber));
234  }
235 
236  //try to add record to the database
237  if(!fDatabase->MarkEvent(event))
238  {
239  cout<<"SERVER -- could not mark event in the database"<<endl;
240  if(tmpFile){delete tmpFile;}
241  if(eventToMark){delete eventToMark;}
242  if(permFile){delete permFile;}
243  return false;
244  }
245 
246  eventToMark->Write(Form("event%d",event.eventNumber));
247  permFile->Close();
248  tmpFile->Close();
249 
250  if(tmpFile){delete tmpFile;}
251  if(eventToMark){delete eventToMark;}
252  if(permFile){delete permFile;}
253  // if(currentRun)delete currentRun;//this line crashes if there is no permanent file yet
254  return true;
255 }
256 
258 {
259  set<string> setResult;
260 
261  // craete CDB manager:
262  TEnv settings;
263  settings.ReadFile(AliOnlineReconstructionUtil::GetPathToServerConf(), kEnvUser);
264  const char *cdbPath = settings.GetValue("cdb.defaultStorage", "");
265  AliCDBManager *man = AliCDBManager::Instance();
266  man->SetDefaultStorage(cdbPath);
267 
268  // get list of runs stored in Storage's database:
269  vector<int> runs = fDatabase->GetListOfRuns();
270 
271  // get trigger classes for all runs:
272  AliCDBEntry *cdbEntry;
273  AliTriggerConfiguration *cfg;
274  TObjArray trarr;
275  AliCDBPath path("GRP/CTP/Config");
276 
277  AliTriggerClass* trgclass;
278 
279  for(int i=0;i<runs.size();i++)
280  {
281  man->SetRun(runs[i]);
282  cdbEntry = man->Get(path);
283  cfg = (AliTriggerConfiguration*)cdbEntry->GetObject();
284  trarr = cfg->GetClasses();
285 
286  for (int j=0;j<trarr.GetEntriesFast();j++)
287  {
288  trgclass = (AliTriggerClass*)trarr.At(j);
289  if(strcmp(trgclass->GetName(),"NONE")!=0)
290  {
291  setResult.insert(trgclass->GetName());
292  }
293  else
294  {
295  cout<<"Trigger classes not defined for run "<<runs[i]<<endl;
296  cout<<"This may indicate problems with OCDB"<<endl;
297  }
298  }
299  }
300 
301  vector<string100> result;
302  set<string>::iterator it;
303 
304  for (it = setResult.begin(); it != setResult.end(); ++it)
305  {
306  string str = *it;
307  string100 cls(str.c_str());
308  result.push_back(cls);
309  }
310  return result;
311 }
312 
313 
314 
315 
316 
317 
318 
std::vector< string100 > GetTriggerClasses()
#define TObjArray
static AliZMQManager * GetInstance()
bool MarkEvent(struct eventStruct event)
AliESDEvent * GetFirstEvent()
ZMQ communication manager.
Definition: AliZMQManager.h:57
AliESDEvent * GetLastEvent()
AliStorageDatabase * fDatabase
void CreateSocket(storageSockets socket)
std::vector< RunInfo > runs
bool Send(std::vector< serverListStruct > list, storageSockets socket)
std::vector< int > GetListOfRuns()
AliESDEvent * GetNextEvent(struct eventStruct event)
void RecreateSocket(storageSockets socket)
std::vector< serverListStruct > GetList(struct listRequestStruct listStruct)
bool MarkEvent(struct eventStruct event)
AliESDEvent * GetEvent(struct eventStruct event)
std::string GetFilePath(struct eventStruct event)
AliESDEvent * GetPrevEvent(struct eventStruct event)
bool Get(std::vector< serverListStruct > *&result, storageSockets socket)