]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTHOMERProxyHandler.cxx
bugfix: suppress points without associated track point in residual calculation
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTHOMERProxyHandler.cxx
1 //-*- Mode: C++ -*-
2 // $Id: AliHLTHOMERProxyHandler.cxx  $
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Jochen Thaeder <thaeder@kip.uni-heidelberg.de>        *
8 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
10 //* Permission to use, copy, modify and distribute this software and its   *
11 //* documentation strictly for non-commercial purposes is hereby granted   *
12 //* without fee, provided that the above copyright notice appears in all   *
13 //* copies and that both the copyright notice and this permission notice   *
14 //* appear in the supporting documentation. The authors make no claims     *
15 //* about the suitability of this software for any purpose. It is          *
16 //* provided "as is" without express or implied warranty.                  *
17 //**************************************************************************
18
19 /** @file   AliHLTHOMERProxyHandler.cxx
20     @author Jochen Thaeder
21     @date
22     @brief  HOMER proxy handler for HomerManger
23 */
24
25 // see header file for class documentation
26 // or
27 // refer to README to build package
28 // or
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
31 #if __GNUC__>= 3
32    using namespace std;
33 #endif
34
35 #include "TDOMParser.h"
36 #include "TSocket.h"
37 #include "TSystem.h"
38 // -- -- -- -- -- -- -- 
39 #include "AliHLTHOMERProxyHandler.h"
40 // -- -- -- -- -- -- -- 
41
42 ClassImp(AliHLTHOMERProxyHandler)
43
44 /*
45  * ---------------------------------------------------------------------------------
46  *                            Constructor / Destructor
47  * ---------------------------------------------------------------------------------
48  */
49
50 //##################################################################################
51 AliHLTHOMERProxyHandler::AliHLTHOMERProxyHandler() :
52   fRealm(kHLT),
53   fXmlRpcResponse(""),
54   fSourceList(NULL) {
55   // see header file for class documentation
56   // or
57   // refer to README to build package
58   // or
59   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
60 }
61
62 //##################################################################################
63 AliHLTHOMERProxyHandler::~AliHLTHOMERProxyHandler() {
64   // see header file for class documentation
65
66 }
67
68 //##################################################################################
69 Int_t AliHLTHOMERProxyHandler::Initialize() {
70   // see header file for class documentation
71   IdentifyRealm();
72   return 0;
73 }
74
75 /*
76  * ---------------------------------------------------------------------------------
77  *                             Source List - public
78  * ---------------------------------------------------------------------------------
79  */
80
81 //##################################################################################
82 Int_t AliHLTHOMERProxyHandler::FillSourceList(TList *srcList) {
83   // see header file for class documentation
84
85   Int_t iResult = 0;
86
87   fSourceList = srcList;
88
89   iResult = RequestXmlRpcResponse();
90
91   if (!iResult)
92     iResult = ProcessXmlRpcResponse();
93
94   if (iResult < 0) {
95     HLTError("Filling SourceList failed.");
96   }
97
98   return iResult;
99 }
100
101 /*
102  * ---------------------------------------------------------------------------------
103  *                        Realms - private
104  * ---------------------------------------------------------------------------------
105  */
106
107 //##################################################################################
108 const Char_t *AliHLTHOMERProxyHandler::fgkHOMERProxyNode[] = { 
109   "portal-dcs0.internal", 
110   "alihlt-dcs0.cern.ch",
111   "alihlt-vobox0.cern.ch",
112   "alihlt-gw0.kip.uni-heidelberg.de",
113   "localhost",
114   "portal-dcs1.internal", 
115   "alihlt-dcs1.cern.ch",
116   "alihlt-vobox1.cern.ch",
117   "alihlt-gw1.kip.uni-heidelberg.de",
118   "localhost"
119 };
120
121 //##################################################################################
122 void AliHLTHOMERProxyHandler::IdentifyRealm() {
123   // see header file for class documentation
124
125   TString hostIP(gSystem->GetHostByName(gSystem->HostName()).GetHostAddress());
126   
127   HLTInfo(hostIP.Data());
128
129   if ( hostIP.Contains("10.162.") )
130     fRealm = kHLT;
131   else if ( hostIP.Contains("10.160.") || hostIP.Contains("10.161.") )
132     fRealm = kACR;
133   else if ( hostIP.Contains("129.206.") )
134     fRealm = kKIP;
135   else  if ( hostIP.Contains("137.138") 
136              || hostIP.Contains("128.141") 
137              || hostIP.Contains("127.0.") 
138              || hostIP.Contains("192.168")
139              )
140     fRealm = kGPN;
141   else {
142     fRealm = kLoc;
143   }
144
145   //fRealm = kLoc;
146
147   return;
148 }
149
150 /*
151  * ---------------------------------------------------------------------------------
152  *                        Proxy Communication - private
153  * ---------------------------------------------------------------------------------
154  */
155
156 //##################################################################################
157 Int_t AliHLTHOMERProxyHandler::RequestXmlRpcResponse() {
158   // see header file for class documentation
159
160   Int_t iResult = 0;
161
162   // -- open socket
163   // ----------------
164
165   Int_t proxyPort = 19999;
166
167   TSocket *socket = new TSocket(fgkHOMERProxyNode[fRealm], proxyPort);
168   if ( ! socket->IsValid() ) {
169     HLTWarning(Form("Failed to create socket to %s:%d,",fgkHOMERProxyNode[fRealm], proxyPort));
170     HLTWarning(Form("trying %s:%d now.", fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort));
171
172     socket = new TSocket(fgkHOMERProxyNode[fRealm+kHOMERRealmsMax], proxyPort);
173     if ( ! socket->IsValid() ) {
174       HLTError(Form("Failed to create socket to %s:%d and %s:%d.",
175                     fgkHOMERProxyNode[fRealm], proxyPort,
176                     fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort));
177
178       HLTWarning(Form("trying %s:%d now.",
179                       fgkHOMERProxyNode[kLoc],proxyPort));
180       socket = new TSocket(fgkHOMERProxyNode[kLoc], proxyPort);
181       if ( ! socket->IsValid() ) {
182         HLTError(Form("Failed to create socket to %s:%d , %s:%d and %s:%d.",
183                       fgkHOMERProxyNode[fRealm], proxyPort,
184                       fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort,
185                       fgkHOMERProxyNode[kLoc], proxyPort));
186         fRealm = -1;
187         return -1;
188       }
189       else {
190         fRealm = kLoc;
191       }
192     }
193     else
194       fRealm = fRealm+kHOMERRealmsMax;
195   }
196
197   // -- send request
198   // -----------------
199
200   Char_t reqMsg[] = "POST / HTTP/1.1\r\n\
201 User-Agent: curl/7.18.0 (x86_64-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1\r\n\
202 Host: localhost:10000\r\n\
203 Accept: */*\r\n\
204 Content-type: text/xml\r\n\
205 Content-Length: 68\r\n\
206 \r\n<methodCall><methodName>getTcpDumpServices</methodName></methodCall>\r\n";
207
208   iResult = socket->SendRaw( reqMsg, strlen(reqMsg) );
209   if ( iResult < 1 || 
210        iResult !=  static_cast<Int_t>(strlen(reqMsg))) {
211     HLTError(Form("Error sending! -- send length %d  -- msg length %d.", iResult, static_cast<Int_t>(strlen(reqMsg)) ));
212     socket->Close();
213     return iResult;
214   }
215   
216   // -- receive answer
217   // -------------------
218
219   const Int_t bufferSize = 1024;
220   Char_t buffer[bufferSize];
221
222   Bool_t isXmlRpc = kFALSE;
223
224   fXmlRpcResponse = "";
225
226   // -- loop for getting full xmlRPC response
227   while(1) {
228
229     Int_t bufferLength = 0;
230     
231     // -- loop for reading until end of line
232     while (1) {
233
234       iResult = socket->RecvRaw(&buffer[bufferLength], 1);
235       if ( iResult < 0) {
236         HLTError("Error reading form socket.");
237         socket->Close();
238         return iResult;
239       }
240             
241       // -- Checking for  end of line
242       if ( buffer[bufferLength] == 10 ) {
243         buffer[bufferLength] = 0;
244         break;
245       }
246       
247       ++bufferLength;
248     }
249
250     TString bufferString(buffer);
251
252     // -- Checking for start of XML response
253     if ( bufferString.BeginsWith("<?xml") )
254       isXmlRpc = kTRUE;
255
256     // -- Append the xml response
257     if (isXmlRpc) {
258       fXmlRpcResponse.Append(bufferString);
259     }
260
261     // -- Checking for end of XML response
262     if( ! bufferString.CompareTo("</methodResponse>") ) 
263       break;
264   }
265   
266   // -- close socket
267   socket->Close();
268
269   return 0;
270 }
271
272 //##################################################################################
273 Int_t AliHLTHOMERProxyHandler::ProcessXmlRpcResponse() {
274   // see header file for class documentation
275
276   Int_t iResult = 0;
277
278   // -- Parse XML RPC Response
279   // ---------------------------
280
281   TDOMParser xmlParser;
282   xmlParser.SetValidate(kFALSE);
283
284   //NOTE Have to use a temporary variable for printing the XML responce,
285   // because Form might overrun its internal buffer and crash for large strings.
286   TString infoMsg = Form("XMLResponse: %s",fXmlRpcResponse.Data());
287   HLTInfo(infoMsg.Data());
288   
289   iResult = xmlParser.ParseBuffer(fXmlRpcResponse.Data(), fXmlRpcResponse.Length());
290   if ( iResult < 0 ) {
291     HLTError(Form("Parsing buffer with error: %s", 
292                   xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) ));
293     
294
295     return iResult;
296   }
297
298   TXMLNode * node = xmlParser.GetXMLDocument()->GetRootNode()->
299     GetChildren()->GetChildren()->GetChildren()->GetChildren();
300   
301   if ( strcmp( node->GetNodeName(), "string" ) ) {
302     HLTError("No node 'string' in XmlRpcResponse.");
303     return -1;
304   }
305
306   // -- Parse Content
307   // ------------------
308
309   // -- Get Content
310   TString xmlContent(node->GetText() );
311
312   HLTDebug(Form("XMLContent: %s",xmlContent.Data()));
313
314   iResult = xmlParser.ParseBuffer(xmlContent.Data(), xmlContent.Length());
315   if ( iResult < 0 ) {
316     HLTError(Form("Parsing buffer with error: %s", 
317                   xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) ));
318
319     return iResult;
320   }
321   
322   if ( !xmlParser.GetXMLDocument()->GetRootNode()->HasChildren() ) {
323     HLTWarning("No Services active.");
324     return 1;
325   }
326
327   // -- Loop over all service nodes
328   TXMLNode* serviceNode = xmlParser.GetXMLDocument()->GetRootNode()->GetChildren();
329   TXMLNode* prevServiceNode = NULL;
330   
331   do {
332     prevServiceNode = serviceNode;
333
334     // -- Add service to list
335     iResult = AddService( serviceNode->GetChildren() );
336     if ( iResult > 0 ) {
337       HLTWarning("Incomplete Service not added.");
338       iResult = 0;
339     }
340   } while ( ( serviceNode = prevServiceNode->GetNextNode() ) && !iResult );
341
342
343
344   return iResult;
345 }
346
347 /*
348  * ---------------------------------------------------------------------------------
349  *                            Source Resolving - private
350  * ---------------------------------------------------------------------------------
351  */
352
353 //##################################################################################
354 Int_t AliHLTHOMERProxyHandler::AddService(TXMLNode *innerNode) {
355   // see header file for class documentation
356
357   Int_t iResult = 0;
358
359   HLTInfo(">> New service");    
360
361   TXMLNode* serviceNode = innerNode;
362
363   // -- Loop over all service properties and 
364   //    read them from the service tag
365   // -----------------------------------------
366
367   TString hostname          = "";
368   Int_t   port              = 0;
369   TString dataType          = "";
370   TString dataOrigin        = "";
371   TString dataSpecification = "";
372
373   TXMLNode* prevInnerNode = NULL;
374
375   // -- Retrieve hostname and port
376   // -------------------------------
377
378   do {
379     prevInnerNode = innerNode;
380     
381     if ( ! strcmp(innerNode->GetNodeName(), "text" ) )
382       continue;
383         
384     // -- hostname
385     if ( ! strcmp( innerNode->GetNodeName(), "address") ) {
386       HLTInfo(Form("  > %s ++ %s", innerNode->GetNodeName(), innerNode->GetText() ));
387       hostname = innerNode->GetText();
388     }
389     else if ( ! strcmp( innerNode->GetNodeName(), "port") ) {
390       HLTInfo(Form("  > %s ++ %s", innerNode->GetNodeName(), innerNode->GetText() ));
391       TString portS(innerNode->GetText());
392       if ( portS.IsDigit() )
393         port = portS.Atoi();
394       else {
395         HLTError(Form("Port %s is not a digit.", portS.Data()));
396         iResult = -1;
397       }
398     }
399   } while ( ( innerNode = prevInnerNode->GetNextNode() ) && !iResult );
400
401
402   // -- Change hostame from service with proxy, if outside HLT
403   if ( fRealm != kHLT && fRealm != kHLT+kHOMERRealmsMax )
404     hostname = fgkHOMERProxyNode[fRealm];
405
406
407   // -- Get Data Specifications from blocks
408   // ----------------------------------------
409
410   do {
411     prevInnerNode = serviceNode;
412
413     if ( strcmp( serviceNode->GetNodeName(), "blocks") )
414       continue;
415  
416     TXMLNode* blocks = serviceNode->GetChildren();
417
418     if ( ! blocks ) {
419       HLTError("No blocks present");
420       return 1;
421     }
422       
423     TXMLNode* blockNode = blocks->GetNextNode();
424     TXMLNode* prevBlockNode = NULL;
425
426     if ( ! blockNode ) {
427       HLTError("No block present in the blocks tag");
428       return 1;
429     }
430       
431     // -- blocks loop 
432     
433     do {
434       prevBlockNode = blockNode;
435       
436       if ( strcmp( blockNode->GetNodeName(), "block") )
437         continue;
438
439       TXMLNode *dataNode = blockNode->GetChildren();
440       TXMLNode *prevDataNode = NULL;
441
442       if ( ! dataNode ) {
443         HLTError("No data specification tags present in block tag.");
444         return 1;
445       }
446       // -- data spec loop
447       
448       do {
449         prevDataNode = dataNode;
450
451         if ( ! strcmp(dataNode->GetNodeName(), "text" ) )
452           continue;
453
454         HLTInfo(Form(" %s ++ %s", dataNode->GetNodeName(), dataNode->GetText() ));      
455
456         if ( ! strcmp( dataNode->GetNodeName(), "dataorigin") ) {
457           dataOrigin = dataNode->GetText();
458         }
459         else if ( ! strcmp( dataNode->GetNodeName(), "datatype") ) {
460           dataType = dataNode->GetText();
461         }
462         else if ( ! strcmp( dataNode->GetNodeName(), "dataspecification") ) {
463           dataSpecification = dataNode->GetText();    
464         }
465       } while ( ( dataNode = prevDataNode->GetNextNode() ) && !iResult );
466       
467       // -- data spec loop
468
469       // -- Check the service properties
470       // ---------------------------------
471       
472       // -- Check for completeness of the source properties
473       if ( hostname.IsNull() || !port || dataOrigin.IsNull() ||
474            dataType.IsNull() || dataSpecification.IsNull() ) {
475         HLTWarning(Form("Service provides not all values:\n\thostname\t\t %s\n\tport\t\t\t %d\n\tdataorigin\t\t %s\n\tdatatype\t\t %s\n\tdataspecification\t 0x%08X", 
476                         hostname.Data(), port, dataOrigin.Data(), dataType.Data(), dataSpecification.Atoi()));
477         
478         return 1;
479       }
480
481       // -- Create new source
482       // ----------------------
483       
484       AliHLTHOMERSourceDesc * source = new AliHLTHOMERSourceDesc();
485       source->SetService( hostname, port, dataOrigin, dataType, dataSpecification );
486       
487       fSourceList->Add( source );
488       
489       HLTInfo(Form( "New Source added : %s", source->GetSourceName().Data()));
490
491     } while ( ( blockNode = prevBlockNode->GetNextNode() ) && !iResult );
492
493
494     // -- blocks loop
495     
496   } while ( ( serviceNode = prevInnerNode->GetNextNode() ) && !iResult );
497   
498   return iResult;
499 }
500
501