Merging STORAGE-dev to master
[u/mrichter/AliRoot.git] / MONITOR / alistoragemanager / AliStorageServerThread.cxx
CommitLineData
5eb34a26 1#include "AliStorageServerThread.h"
2#include "AliStorageTypes.h"
3#include "AliESDEvent.h"
4
5eb34a26 5#include <iostream>
6#include <fstream>
7
8#include <TFile.h>
9#include <TThread.h>
10
11using namespace std;
5eb34a26 12
13AliStorageServerThread::AliStorageServerThread() :
5eb34a26 14 fDatabase(0),
15 fStoragePath("")
16{
17 TThread::Lock();
18 fDatabase = new AliStorageDatabase();
19 //load parameters from config file
164d3d29 20 ifstream configFile (GetConfigFilePath());
5eb34a26 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
5eb34a26 50 StartCommunication();
51}
52
53AliStorageServerThread::~AliStorageServerThread()
54{
164d3d29 55 cout<<"SERVER -- AliStorageServerThread destructor called";
5eb34a26 56 cout<<" --- OK"<<endl;
57}
58
59void AliStorageServerThread::StartCommunication()
60{
164d3d29 61 AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
62 storageSockets socket = SERVER_COMMUNICATION_REP;
63 eventManager->CreateSocket(socket);
64
65 struct serverRequestStruct *request;
5eb34a26 66
67 while(1)
68 {
164d3d29 69 request = eventManager->GetServerStruct(socket);
5eb34a26 70
164d3d29 71 switch(request->messageType)
5eb34a26 72 {
73 case REQUEST_LIST_EVENTS:
74 {
164d3d29 75 vector<serverListStruct> result = fDatabase->GetList(request->list);
76 eventManager->Send(result,socket);
5eb34a26 77 break;
78 }
79 case REQUEST_GET_EVENT:
80 {
164d3d29 81 AliESDEvent *event = fDatabase->GetEvent(request->event);
82 eventManager->Send(event,socket);
5eb34a26 83 delete event;
84 break;
85 }
86 case REQUEST_GET_NEXT_EVENT:
87 {
164d3d29 88 AliESDEvent *event = fDatabase->GetNextEvent(request->event);
89 eventManager->Send(event,socket);
5eb34a26 90 delete event;
91 break;
92 }
93 case REQUEST_GET_LAST_EVENT:
94 {
95 AliESDEvent *event = fDatabase->GetLastEvent();
164d3d29 96 eventManager->Send(event,socket);
5eb34a26 97 delete event;
98 break;
99 }
100 case REQUEST_MARK_EVENT:
101 {
164d3d29 102 struct eventStruct *markData = &(request->event);
103 eventManager->Send(MarkEvent(*markData),socket);
5eb34a26 104 break;
105 }
106 default:break;
107 }
5eb34a26 108 }
109}
110
111bool AliStorageServerThread::MarkEvent(struct eventStruct event)
112{
113 string pathToFile = fDatabase->GetFilePath(event);
114 TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
115 if(!tmpFile)
116 {
117 cout<<"SERVER -- couldn't open temp file"<<endl;
118 return false;
119 }
120 AliESDEvent *eventToMark = (AliESDEvent*)tmpFile->Get(Form("event%d",event.eventNumber));
121 if(!eventToMark)
122 {
123 cout<<"SERVER -- couldn't find such event"<<endl;
124 if(tmpFile){delete tmpFile;}
125 return false;
126 }
127 cout<<"SERVER -- Marking event:"<<eventToMark->GetEventNumberInFile()<<endl;
128
129 TFile *permFile = new TFile(Form("%s/permEvents.root",fStoragePath.c_str()),"update");//open/create perm file
130
131 if(!permFile)
132 {
133 cout<<"SERVER -- Couldn't open perm file"<<endl;
134 if(tmpFile){delete tmpFile;}
135 if(eventToMark){delete eventToMark;}
136 return false;
137 }
138
139 //create new directory for this run
140 TDirectory *currentRun;
141 if((currentRun = permFile->mkdir(Form("run%d",event.runNumber))))
142 {
143 cout<<"SERVER -- creating new directory for this run"<<endl;
144 currentRun->cd();
145 }
146 else
147 {
148 cout<<"SERVER -- opening existing directory for this run"<<endl;
149 permFile->cd(Form("run%d",event.runNumber));
150 }
151
152 //try to add record to the database
153 if(!fDatabase->MarkEvent(event))
154 {
155 cout<<"SERVER -- could not mark event in the database"<<endl;
156 if(tmpFile){delete tmpFile;}
157 if(eventToMark){delete eventToMark;}
158 if(permFile){delete permFile;}
159 return false;
160 }
161
162 eventToMark->Write(Form("event%d",event.eventNumber));
163 permFile->Close();
164 tmpFile->Close();
165
166 if(tmpFile){delete tmpFile;}
167 if(eventToMark){delete eventToMark;}
168 if(permFile){delete permFile;}
169// if(currentRun)delete currentRun;//this line crashes if there is no permanent file yet
170 return true;
171}
172