Double check if SM is running added. Some redundant output removed from SM
[u/mrichter/AliRoot.git] / MONITOR / MONITORzmq / AliCommunicationThread.cxx
CommitLineData
186c4b6e 1#include "AliCommunicationThread.h"
2#include "AliStorageEventManager.h"
3
4#include <iostream>
5#include <fstream>
6
7using namespace std;
8
9AliCommunicationThread::AliCommunicationThread(AliStorageClientThread *onlineReconstructionManager) :
10 fFinished(false),
11 fManager(onlineReconstructionManager),
12 fCommunicationThread(0)
13{
14 //create two-way communication thread
15 fCommunicationThread = new TThread("fCommunicationThread",Dispatch,(void*)this);
16 fCommunicationThread->Run();
17}
18
19AliCommunicationThread::~AliCommunicationThread()
20{
21 if(fCommunicationThread){delete fCommunicationThread;}
22}
23
24void AliCommunicationThread::Kill()
25{
26 if(fCommunicationThread)
27 {
28 fFinished=true;
29 fCommunicationThread->Join();
30 fCommunicationThread->Kill();
31 }
32}
33
34void AliCommunicationThread::CommunicationHandle()
35{
36 AliStorageEventManager *eventManager = AliStorageEventManager::GetEventManagerInstance();
37 storageSockets socket = CLIENT_COMMUNICATION_REP;
38 eventManager->CreateSocket(socket);
39
40 struct clientRequestStruct *request;
41 struct clientRequestStruct *response = new struct clientRequestStruct;
42
43 cout<<"CLIENT -- Communication stated"<<endl;
44
9cb99c70 45 // mutex mtx;
186c4b6e 46
47 while(!fFinished)
48 {
186c4b6e 49 request = eventManager->GetClientStruct(socket,5000);
50
51 if(request)
52 {
9cb99c70 53 // lock_guard<mutex> lock(mtx);
186c4b6e 54 cout<<"COMMUNICATION -- received request"<<endl;
55 switch(request->messageType)
56 {
57 case REQUEST_CONNECTION:
58 eventManager->Send((long)fManager->fConnectionStatus,socket);
59 break;
60 case REQUEST_RECEIVING:
61 eventManager->Send((long)fManager->fReceivingStatus,socket);
62 break;
63 case REQUEST_SAVING:
64 eventManager->Send((long)fManager->fSavingStatus,socket);
65 break;
66 case REQUEST_CURRENT_SIZE:
67 eventManager->Send((long)fManager->fCurrentStorageSize,socket);
68 break;
69 case REQUEST_GET_PARAMS:
70 response->maxStorageSize = fManager->fMaximumStorageSize;
71 response->maxOccupation = fManager->fStorageOccupationLevel;
72 response->removeEvents = fManager->fRemoveEventsPercentage;
73 response->eventsInChunk = fManager->fNumberOfEventsInFile;
74
75 eventManager->Send(response,socket);
76 break;
77 case REQUEST_SET_PARAMS:
78 SetStorageParams(request->maxStorageSize,
79 request->maxOccupation,
80 request->removeEvents,
81 request->eventsInChunk);
82
83 fManager->fMaximumStorageSize = request->maxStorageSize;
84 fManager->fStorageOccupationLevel = request->maxOccupation;
85 fManager->fRemoveEventsPercentage = request->removeEvents;
86 fManager->fNumberOfEventsInFile = request->eventsInChunk;
87
88 eventManager->Send(true,socket);
89 break;
90 default:break;
91 }
92 delete request;
93 }
e9b1f5d7 94 else{sleep(1);}
186c4b6e 95 }
96}
97
98void AliCommunicationThread::SetStorageParams(int maxStorageSize,int maxOccupation,int removeEvents,int eventsInChunk)
99{
100 cout<<maxStorageSize<<endl<<maxOccupation<<endl<<removeEvents<<endl<<eventsInChunk<<endl;
101
102 TThread::Lock();
103 ifstream configFile (GetConfigFilePath());
104 ofstream tmpFile("tmpFile.bla");
105
106 if (configFile.is_open())
107 {
108 string line;
109 string tmpLine;
110 int from,to;
111 while(configFile.good())
112 {
113 getline(configFile,line);
114 from = line.find("\"")+1;
115 to = line.find_last_of("\"");
116 tmpLine = line;
117 if(line.find("MAX_SIZE=")==0){
118 tmpLine = Form("MAX_SIZE=\"%d\"",maxStorageSize);
119 }
120 else if(line.find("MAX_OCCUPATION=")==0){
121 tmpLine = Form("MAX_OCCUPATION=\"%d\"",maxOccupation);
122 }
123 else if(line.find("REMOVE_PERCENT=")==0){
124 tmpLine = Form("REMOVE_PERCENT=\"%d\"",removeEvents);
125 }
126 else if(line.find("EVENTS_IN_FILE=")==0){
127 tmpLine = Form("EVENTS_IN_FILE=\"%d\"",eventsInChunk);
128 }
129 tmpLine += "\n";
130 tmpFile << tmpLine;
131 }
132 if(configFile.eof()){configFile.clear();}
133 configFile.close();
134 tmpFile.close();
135 rename("tmpFile.bla",GetConfigFilePath());
136 }
137 else{cout<<"CLIENT -- Unable to open config file"<<endl;}
138 TThread::UnLock();
9cb99c70 139}