1. Fixing memory leaks in alistorage
[u/mrichter/AliRoot.git] / MONITOR / alistoragemanager / AliStorageAdministratorPanel.cxx
1 #include "AliStorageAdministratorPanel.h"
2 #include "AliStorageAdministratorPanelMarkEvent.h"
3 #include "AliStorageAdministratorPanelListEvents.h"
4 #include "AliStorageAdministratorPanelSetStorageParams.h"
5 #include "AliESDEvent.h"
6
7 #include <fstream>
8 #include <iostream>
9
10 #include <TGMenu.h>
11
12 using namespace std;
13
14 ClassImp(AliStorageAdministratorPanel);
15
16 #define WINDOWS_WIDTH 500
17 #define WINDOWS_HEIGHT 350
18
19 enum TOOLBUTTON{
20         TOOLBUTTON_START=1,
21         TOOLBUTTON_STOP,
22         TOOLBUTTON_PREFERENCES,
23         TOOLBUTTON_EXIT         
24 };
25
26 enum MENUBAR{
27         MENUBAR_CLIENT_SET_PARAMS=1,
28         MENUBAR_SERVER_LIST_EVENTS,
29         MENUBAR_SERVER_MARK_EVENT,
30         MENUBAR_SERVER_GET_EVENT,
31         MENUBAR_SERVER_GET_NEXT_EVENT,
32         MENUBAR_SERVER_GET_LAST_EVENT
33 };
34
35 enum FRAME{
36         CLIENT_FRAME=1,
37         SERVER_FRAME
38 };
39
40 AliStorageAdministratorPanel::AliStorageAdministratorPanel() :
41         TGMainFrame(gClient->GetRoot(), 400, 400),
42         fPanelQuited(false),
43         fConnectionLabel(0),
44         fDataLabel(0),
45         fSavingLabel(0),
46         fCurrentSizeLabel(0),
47         fMaxSizeLabel(0),
48         fMaxOccupationLabel(0),
49         fRemoveEventsLabel(0),
50         fEventsInChunkLabel(0),
51         fMaxStorageSize(0),
52         fMaxOccupation(0),
53         fRemoveEvents(0),
54         fEventsInChunk(0),
55         fCommunicationThread(0),
56         fCommunicationSocket(CLIENT_COMMUNICATION_REQ),
57         fServerSocket(SERVER_COMMUNICATION_REQ),
58         fEventManager(0)
59 {
60         InitWindow();
61     
62         //create event manager
63         fEventManager = AliStorageEventManager::GetEventManagerInstance();
64         fEventManager->CreateSocket(fServerSocket);
65         fEventManager->CreateSocket(fCommunicationSocket);
66
67         // start communication with client thread
68         fCommunicationThread = new TThread("fCommunicationThread",
69                                        Dispatch,(void*)this);
70         fCommunicationThread->Run();
71 }
72
73 AliStorageAdministratorPanel::~AliStorageAdministratorPanel()
74 {
75         cout<<"ADMIN -- AliStorageAdministratorPanel descructor called";
76         cout<<" --- OK"<<endl;
77 }
78
79 void AliStorageAdministratorPanel::CheckStateHandle()//ask client about its state
80 {
81         while(!fPanelQuited)
82         {
83                 CheckClientState(REQUEST_CONNECTION);
84                 sleep(1);
85                 CheckClientState(REQUEST_RECEIVING);
86                 sleep(1);
87                 CheckClientState(REQUEST_SAVING);
88                 sleep(1);
89                 CheckClientState(REQUEST_CURRENT_SIZE);
90                 sleep(1);
91                 CheckClientState(REQUEST_GET_PARAMS);
92                 sleep(1);
93         }
94 }
95
96 void AliStorageAdministratorPanel::CheckClientState(int option)
97 {
98         struct clientRequestStruct *request = new struct clientRequestStruct;
99         request->messageType = option;
100         if(!fEventManager->Send(request,fCommunicationSocket,10000))
101         {
102                 SetLabel(fConnectionLabel,STATUS_DOWN);
103                 SetLabel(fDataLabel,STATUS_DOWN);
104                 SetLabel(fSavingLabel,STATUS_DOWN);
105                 cout<<"ADMIN -- CLIENT IS DOWN"<<endl;
106                 return;
107         }
108
109         long response = -1;
110         struct clientRequestStruct *responseParams = NULL;
111         
112         if(option == REQUEST_GET_PARAMS)
113         {
114                 responseParams = fEventManager->GetClientStruct(fCommunicationSocket);
115         }
116         else
117         {
118                 response = fEventManager->GetLong(fCommunicationSocket);
119         }
120         switch(option)
121         {
122         case REQUEST_CONNECTION:
123                 SetLabel(fConnectionLabel,response);
124                 break;
125         case REQUEST_RECEIVING:
126                 SetLabel(fDataLabel,response);
127                 break;
128         case REQUEST_SAVING:
129                 SetLabel(fSavingLabel,response);
130                 break;
131         case REQUEST_CURRENT_SIZE:
132                 SetLabelValue(fCurrentSizeLabel,response,1);
133                 break;
134         case REQUEST_GET_PARAMS:
135                 fMaxStorageSize = responseParams->maxStorageSize;
136                 fMaxOccupation = responseParams->maxOccupation;
137                 fRemoveEvents = responseParams->removeEvents;
138                 fEventsInChunk = responseParams->eventsInChunk;
139                 
140                 SetLabelValue(fMaxSizeLabel,fMaxStorageSize,1);
141                 SetLabelValue(fMaxOccupationLabel,fMaxOccupation,2);
142                 SetLabelValue(fRemoveEventsLabel,fRemoveEvents,2);
143                 SetLabelValue(fEventsInChunkLabel,fEventsInChunk,3);
144                 break;
145         default:break;  
146         }
147         if(request){delete request;}
148 }
149
150 void AliStorageAdministratorPanel::InitWindow()
151 {
152         SetCleanup(kDeepCleanup);
153         // add main manubar on top of the window
154         SetupFixedMenuBar();
155         AddFrame(new TGHorizontal3DLine(this),
156                  new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
157         // add toolbar with pictured buttons
158         /*SetupToolbar();
159         AddFrame(new TGHorizontal3DLine(this),
160                  new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
161         // add dockable toolbar
162         SetupDockableMenuBar();
163         AddFrame(new TGHorizontal3DLine(this),
164                  new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
165         */
166         
167         // add frame with two segments for client and server
168         SetupThreadsFrame();
169         SetWindowName("ALICE Storage Manager");
170         MapSubwindows();
171         Resize(WINDOWS_WIDTH,WINDOWS_HEIGHT);
172         MapWindow();
173 }
174
175 void AliStorageAdministratorPanel::SetupThreadsFrame()
176 {
177         // create frame for both client and server
178         TGHorizontalFrame *threadsFrame = new TGHorizontalFrame(this);
179
180         //create client frame and add components to it
181         TGCompositeFrame *clientThreadFrame = new TGCompositeFrame(threadsFrame,CLIENT_FRAME);
182         fConnectionLabel = new TGLabel(clientThreadFrame,"======Waiting======");
183         fDataLabel = new TGLabel(clientThreadFrame,"======Waiting======");
184         fSavingLabel = new TGLabel(clientThreadFrame,"======Waiting======");
185         fCurrentSizeLabel = new TGLabel(clientThreadFrame,"=========Waiting=========");
186         fMaxSizeLabel = new TGLabel(clientThreadFrame,"==========Waiting=========");
187         fMaxOccupationLabel = new TGLabel(clientThreadFrame,"==========Waiting=========");
188         fRemoveEventsLabel = new TGLabel(clientThreadFrame,"==========Waiting=========");
189         fEventsInChunkLabel = new TGLabel(clientThreadFrame,"==========Waiting=========");
190         
191         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
192                                                 "ALICE Storage Client"),
193                                     new TGLayoutHints(kLHintsCenterX));
194
195         //connection label
196         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
197                                                 "Connection:"),
198                                     new TGLayoutHints(kLHintsCenterX));
199         clientThreadFrame->AddFrame(fConnectionLabel,
200                                     new TGLayoutHints(kLHintsCenterX));
201
202         //data receiving label
203         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
204                                                 "Receiving data:"),
205                                     new TGLayoutHints(kLHintsCenterX));
206         clientThreadFrame->AddFrame(fDataLabel,
207                                     new TGLayoutHints(kLHintsCenterX));
208
209         //saving label
210         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
211                                                 "Saving:"),
212                                     new TGLayoutHints(kLHintsCenterX));
213         clientThreadFrame->AddFrame(fSavingLabel,
214                                     new TGLayoutHints(kLHintsCenterX));
215
216         //current size label
217         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
218                                                 "Current storage size:"),
219                                     new TGLayoutHints(kLHintsCenterX));
220         clientThreadFrame->AddFrame(fCurrentSizeLabel,
221                                     new TGLayoutHints(kLHintsCenterX));
222
223         //max size label
224         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
225                                                 "Max storage size:"),
226                                     new TGLayoutHints(kLHintsCenterX));
227         clientThreadFrame->AddFrame(fMaxSizeLabel,
228                                     new TGLayoutHints(kLHintsCenterX));
229
230         //max occupation label
231         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
232                                                 "Max occupation percent:"),
233                                     new TGLayoutHints(kLHintsCenterX));
234         clientThreadFrame->AddFrame(fMaxOccupationLabel,
235                                     new TGLayoutHints(kLHintsCenterX));
236
237         //remove events label
238         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
239                                                 "Remove events percentage:"),
240                                     new TGLayoutHints(kLHintsCenterX));
241         clientThreadFrame->AddFrame(fRemoveEventsLabel,
242                                     new TGLayoutHints(kLHintsCenterX));
243
244         //events in chunk label
245         clientThreadFrame->AddFrame(new TGLabel(clientThreadFrame,
246                                                 "Number of events in chunk:"),
247                                     new TGLayoutHints(kLHintsCenterX));
248         clientThreadFrame->AddFrame(fEventsInChunkLabel,
249                                     new TGLayoutHints(kLHintsCenterX));
250
251         //create server frame and add components to it
252         TGCompositeFrame *serverThreadFrame = new TGCompositeFrame(threadsFrame,SERVER_FRAME);
253         serverThreadFrame->AddFrame(new TGLabel(serverThreadFrame,
254                                                  "ALICE Storage Server"),
255                                      new TGLayoutHints(kLHintsCenterX));
256
257         //add client and server frames to threads frame
258         threadsFrame->AddFrame(clientThreadFrame,
259                                 new TGLayoutHints(kLHintsLeft | kLHintsExpandX));
260         threadsFrame->AddFrame(new TGVertical3DLine(threadsFrame),
261                  new TGLayoutHints(kLHintsNormal | kLHintsExpandY));
262         threadsFrame->AddFrame(serverThreadFrame,
263                                 new TGLayoutHints(kLHintsRight | kLHintsExpandX));
264
265         //add threads frame to main window
266         AddFrame(threadsFrame,new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
267 }
268
269 void AliStorageAdministratorPanel::SetupFixedMenuBar()
270 {
271         //create popup menu for client
272         TGPopupMenu *clientPopup = new TGPopupMenu(fClient->GetRoot());
273         clientPopup->AddEntry("&Set storage params",MENUBAR_CLIENT_SET_PARAMS);
274         clientPopup->Associate(this);
275
276         //create popup menu for server
277         TGPopupMenu *serverPopup = new TGPopupMenu(fClient->GetRoot());
278         serverPopup->AddEntry("&Get Events List",MENUBAR_SERVER_LIST_EVENTS);
279         serverPopup->AddEntry("&Mark Event",MENUBAR_SERVER_MARK_EVENT);
280         serverPopup->AddEntry("&Get Event (test)",MENUBAR_SERVER_GET_EVENT);
281         serverPopup->AddEntry("&Get Next Event (test)",MENUBAR_SERVER_GET_NEXT_EVENT);
282         serverPopup->AddEntry("&Get Last Event (test)",MENUBAR_SERVER_GET_LAST_EVENT);
283         serverPopup->Associate(this);
284
285         //create menubar
286         TGMenuBar *menuBar = new TGMenuBar(this,1,1,kHorizontalFrame);
287         menuBar->AddPopup("&Client",clientPopup,
288                           new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
289         menuBar->AddPopup("&Server", serverPopup,
290                           new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
291
292         AddFrame(menuBar,new TGLayoutHints(kLHintsTop | kLHintsExpandX));
293 }
294
295 //handle GUI actions
296 Bool_t AliStorageAdministratorPanel::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
297 {
298         switch (GET_MSG(msg))
299         {
300
301         case kC_COMMAND:
302                 switch (GET_SUBMSG(msg))
303                 {
304                 case kCM_BUTTON://when button is pressed
305                         break;
306                 case kCM_MENUSELECT://when mouse is over menu entry
307                         break;
308                 case kCM_MENU://when menu entry was clicked
309                         switch (parm1)
310                         {
311                         case MENUBAR_CLIENT_SET_PARAMS:onClientSetParams();break;
312                         case MENUBAR_SERVER_LIST_EVENTS:onServerListEvents();break;
313                         case MENUBAR_SERVER_MARK_EVENT:onServerMarkEvent();break;
314                         case MENUBAR_SERVER_GET_EVENT:onServerGetEvent();break;
315                         case MENUBAR_SERVER_GET_NEXT_EVENT:onServerGetNextEvent();break;
316                         case MENUBAR_SERVER_GET_LAST_EVENT:onServerGetLastEvent();break;
317                         default:break;
318                         }
319                         break;
320                 default:break;
321                 }
322                 break;
323         default:break;
324         }
325
326         return false;
327 }
328
329 void AliStorageAdministratorPanel::onServerListEvents()
330 {
331         AliStorageAdministratorPanelListEvents *listEventsWindow =
332                 AliStorageAdministratorPanelListEvents::GetInstance();
333         if(listEventsWindow){cout<<"List events window created"<<endl;}
334 }
335
336 void AliStorageAdministratorPanel::onServerMarkEvent()
337 {
338         AliStorageAdministratorPanelMarkEvent *markEventWindow =
339                 AliStorageAdministratorPanelMarkEvent::GetInstance();
340         if(markEventWindow){cout<<"Mark event window created"<<endl;}
341 }
342
343 void AliStorageAdministratorPanel::onClientSetParams()
344 {
345         AliStorageAdministratorPanelSetStorageParams *setParamsWindow =
346                 AliStorageAdministratorPanelSetStorageParams::GetInstance();
347
348         setParamsWindow->Setup(fCommunicationSocket,
349                                fMaxStorageSize/1000000,
350                                fMaxOccupation,
351                                fRemoveEvents,
352                                fEventsInChunk);
353 }
354
355 void AliStorageAdministratorPanel::onServerGetEvent()
356 {
357         //this method is just for tests
358         int runNumber=197669;
359         int eventNumber=168;
360         
361         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
362         struct eventStruct mark;
363         mark.runNumber = runNumber;
364         mark.eventNumber = eventNumber;
365         requestMessage->messageType = REQUEST_GET_EVENT;
366         requestMessage->event = mark;
367
368         fEventManager->Send(requestMessage,fServerSocket);
369         AliESDEvent *resultEvent = fEventManager->GetEvent(fServerSocket);
370         
371         if(resultEvent)
372         {
373                 cout<<"ADMIN -- received event. Run no:"<<resultEvent->GetRunNumber()<<"\tEvent no in file:"<<resultEvent->GetEventNumberInFile()<<endl;
374         }
375         else
376         {
377                 cout<<"ADMIN -- received no event"<<endl;
378         }
379 }
380
381
382 void AliStorageAdministratorPanel::onServerGetNextEvent()
383 {
384         //this method is just for tests
385         int runNumber=197669;
386         int eventNumber=33;
387         
388         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
389         struct eventStruct mark;
390         mark.runNumber = runNumber;
391         mark.eventNumber = eventNumber;
392         requestMessage->messageType = REQUEST_GET_NEXT_EVENT;
393         requestMessage->event = mark;
394
395         fEventManager->Send(requestMessage,fServerSocket);
396         AliESDEvent* resultEvent= fEventManager->GetEvent(fServerSocket);       
397         if(resultEvent)
398         {
399                 cout<<"ADMIN -- received event. Run no:"<<resultEvent->GetRunNumber()<<"\tEvent no in file:"<<resultEvent->GetEventNumberInFile()<<endl;
400         }
401         else
402         {
403                 cout<<"ADMIN -- received no event"<<endl;
404         }
405 }
406
407
408 void AliStorageAdministratorPanel::onServerGetLastEvent()
409 {       
410         struct serverRequestStruct *requestMessage = new struct serverRequestStruct;
411         requestMessage->messageType = REQUEST_GET_LAST_EVENT;
412
413         fEventManager->Send(requestMessage,fServerSocket);
414         AliESDEvent* resultEvent= fEventManager->GetEvent(fServerSocket);
415
416         if(resultEvent)
417         {
418                 cout<<"ADMIN -- received event. Run no:"<<resultEvent->GetRunNumber()<<"\tEvent no in file:"<<resultEvent->GetEventNumberInFile()<<endl;
419         }
420         else
421         {
422                 cout<<"ADMIN -- received no event"<<endl;
423         }
424 }
425
426 void AliStorageAdministratorPanel::onExit()
427 {
428         cout<<"ADMIN -- Quiting ALICE Storage Admin Panel"<<endl;
429         fPanelQuited=true;
430         gSystem->ExitLoop();
431 }
432
433 void AliStorageAdministratorPanel::CloseWindow(){onExit();}
434
435
436 //methods to change labels:
437 void AliStorageAdministratorPanel::SetLabel(TGLabel *label, int option)
438 {
439         switch(option)
440         {
441         case STATUS_WAITING:
442                 label->SetText("Waiting");
443                 label->SetTextColor(kBlack);
444                 break;
445         case STATUS_OK:
446                 label->SetText("OK");
447                 label->SetTextColor(kBlack);
448                 break;
449         case STATUS_ERROR:
450                 label->SetText("ERROR");
451                 label->SetTextColor(kBlack);
452                 break;
453         case STATUS_DOWN:
454                 label->SetText("CLIENT IS DOWN");
455                 label->SetTextColor(kRed);
456                 break;
457         default:break;
458         }
459         gClient->HandleInput();
460 }
461
462 void AliStorageAdministratorPanel::SetLabelValue(TGLabel *label, long value,int option)
463 {
464         // options:
465         // 1 - MB
466         // 2 - percentage
467         // 3 - number
468
469         switch(option)
470         {
471         case 1:
472                 label->SetText(Form("%lu B (%.2f MB)",value,(float)value/(1000.*1000.)));
473                 break;
474         case 2:
475                 label->SetText(Form("%d %%",(int)value));
476                 break;
477         case 3:
478                 label->SetText(Form("%d",(int)value));
479                 break;
480         default:break;
481         }
482         label->SetTextColor(kBlack);
483         gClient->HandleInput();
484 }
485 //other methods that can be useful later
486
487 /*
488 void AliStorageAdministratorPanel::SetupToolbar()
489 {
490         TGToolBar* toolBar = new TGToolBar(this);
491         toolBar->AddButton(this,new TGPictureButton(toolBar,
492                                                      Form("%s/MONITOR/icons/start.png",
493                                                      gSystem->Getenv("ALICE_ROOT")),
494                                                      TOOLBUTTON_START ) );
495         toolBar->AddButton(this, new TGPictureButton(toolBar,
496                                                       Form("%s/MONITOR/icons/stop.png",
497                                                       gSystem->Getenv("ALICE_ROOT")),
498                                                       TOOLBUTTON_STOP) ); 
499         
500         AddFrame(toolBar, new TGLayoutHints(kLHintsNormal | kLHintsExpandX));
501 }
502
503 void AliStorageAdministratorPanel::SetupDockableMenuBar()
504 {
505         TGDockableFrame *menuDock = new TGDockableFrame(this);
506         AddFrame(menuDock,new TGLayoutHints(kLHintsExpandX,0,0,1,0));
507         menuDock->SetWindowName("ALICE Storage Dockable  MenuBar");
508         menuDock->EnableUndock(kTRUE);
509         menuDock->EnableHide(kTRUE);
510
511         TGPopupMenu *popup1 = new TGPopupMenu(fClient->GetRoot());
512         popup1->AddEntry("&Check client state",MENUBAR_1_OPTION_1);
513         popup1->AddSeparator();
514         popup1->AddEntry("&BBB",MENUBAR_1_OPTION_2);
515         popup1->AddEntry("&CCC",MENUBAR_1_OPTION_3);
516
517         popup1->DisableEntry(MENUBAR_1_OPTION_2);
518         popup1->Associate(this);
519
520         
521         TGMenuBar *menuBar = new TGMenuBar(menuDock,1,1,kHorizontalFrame);
522         menuBar->AddPopup("&File",
523                           popup1,
524                           new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
525
526         menuDock->AddFrame(menuBar,
527                            new TGLayoutHints(kLHintsTop | kLHintsExpandX));
528                            }*/