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