1. Fixing memory leaks in alistorage
[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                       AliESDEvent *event = fDatabase->GetNextEvent(request->event);
90                       eventManager->Send(event,socket);
91                       delete event;
92                       break;
93                     }
94                   case REQUEST_GET_PREV_EVENT:
95                     {
96                       AliESDEvent *event = fDatabase->GetPrevEvent(request->event);
97                       eventManager->Send(event,socket);
98                       delete event;
99                       break;
100                     }
101                   case REQUEST_GET_LAST_EVENT:
102                     {
103                       AliESDEvent *event = fDatabase->GetLastEvent();
104                       eventManager->Send(event,socket);
105                       delete event;
106                       break;
107                     }
108                   case REQUEST_GET_FIRST_EVENT:
109                     {
110                       AliESDEvent *event = fDatabase->GetFirstEvent();
111                       eventManager->Send(event,socket);
112                       delete event;
113                       break;
114                     }
115                   case REQUEST_MARK_EVENT:
116                     {
117                       struct eventStruct *markData  = &(request->event);
118                       eventManager->Send(MarkEvent(*markData),socket);
119                       break;
120                     }
121                   default:break;
122                   }
123
124         }
125 }
126
127 bool AliStorageServerThread::MarkEvent(struct eventStruct event)
128 {
129   string pathToFile = fDatabase->GetFilePath(event);
130   TFile *tmpFile = new TFile(pathToFile.c_str(),"read");
131   if(!tmpFile)
132     {
133       cout<<"SERVER -- couldn't open temp file"<<endl;
134       return false;
135     }
136   AliESDEvent *eventToMark = (AliESDEvent*)tmpFile->Get(Form("event%d",event.eventNumber));
137   if(!eventToMark)
138     {
139       cout<<"SERVER -- couldn't find such event"<<endl;
140       if(tmpFile){delete tmpFile;}
141       return false;
142     }
143   cout<<"SERVER -- Marking event:"<<eventToMark->GetEventNumberInFile()<<endl;
144                 
145         TFile *permFile = new TFile(Form("%s/permEvents.root",fStoragePath.c_str()),"update");//open/create perm file
146         
147         if(!permFile)
148         {
149                 cout<<"SERVER -- Couldn't open perm file"<<endl;
150                 if(tmpFile){delete tmpFile;}
151                 if(eventToMark){delete eventToMark;}
152                 return false;
153         }
154
155         //create new directory for this run
156         TDirectory *currentRun;
157         if((currentRun = permFile->mkdir(Form("run%d",event.runNumber))))
158         {
159                 cout<<"SERVER -- creating new directory for this run"<<endl;
160                 currentRun->cd();
161         }
162         else
163         {
164                 cout<<"SERVER -- opening existing directory for this run"<<endl;
165                 permFile->cd(Form("run%d",event.runNumber));
166         }
167
168         //try to add record to the database
169         if(!fDatabase->MarkEvent(event))
170         {
171                 cout<<"SERVER -- could not mark event in the database"<<endl;
172                 if(tmpFile){delete tmpFile;}
173                 if(eventToMark){delete eventToMark;}
174                 if(permFile){delete permFile;}
175                 return false;
176         }
177
178         eventToMark->Write(Form("event%d",event.eventNumber));
179         permFile->Close();
180         tmpFile->Close();
181
182         if(tmpFile){delete tmpFile;}
183         if(eventToMark){delete eventToMark;}
184         if(permFile){delete permFile;}
185 //      if(currentRun)delete currentRun;//this line crashes if there is no permanent file yet
186         return true;
187 }
188