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. *
7 //* Primary Authors: Jochen Thaeder <thaeder@kip.uni-heidelberg.de> *
8 //* for The ALICE HLT Project. *
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 //**************************************************************************
19 /** @file AliHLTHOMERProxyHandler.cxx
20 @author Jochen Thaeder
22 @brief HOMER proxy handler for HomerManger
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
31 #include "TDOMParser.h"
34 // -- -- -- -- -- -- --
35 #include "AliHLTHOMERProxyHandler.h"
36 // -- -- -- -- -- -- --
38 ClassImp(AliHLTHOMERProxyHandler)
41 * ---------------------------------------------------------------------------------
42 * Constructor / Destructor
43 * ---------------------------------------------------------------------------------
46 //##################################################################################
47 AliHLTHOMERProxyHandler::AliHLTHOMERProxyHandler() :
51 // see header file for class documentation
53 // refer to README to build package
55 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
58 //##################################################################################
59 AliHLTHOMERProxyHandler::~AliHLTHOMERProxyHandler() {
60 // see header file for class documentation
64 //##################################################################################
65 Int_t AliHLTHOMERProxyHandler::Initialize() {
66 // see header file for class documentation
72 * ---------------------------------------------------------------------------------
73 * Source List - public
74 * ---------------------------------------------------------------------------------
77 //##################################################################################
78 Int_t AliHLTHOMERProxyHandler::FillSourceList(TList *srcList) {
79 // see header file for class documentation
83 fSourceList = srcList;
85 iResult = RequestXmlRpcResponse();
88 iResult = ProcessXmlRpcResponse();
91 HLTError("Filling SourceList failed.");
98 * ---------------------------------------------------------------------------------
100 * ---------------------------------------------------------------------------------
103 //##################################################################################
104 const Char_t *AliHLTHOMERProxyHandler::fgkHOMERProxyNode[] = {
105 "portal-dcs0.internal",
106 "alihlt-dcs0.cern.ch",
107 "alihlt-vobox0.cern.ch",
108 "alihlt-gw0.kip.uni-heidelberg.de",
110 "portal-dcs1.internal",
111 "alihlt-dcs1.cern.ch",
112 "alihlt-vobox1.cern.ch",
113 "alihlt-gw1.kip.uni-heidelberg.de",
117 //##################################################################################
118 void AliHLTHOMERProxyHandler::IdentifyRealm() {
119 // see header file for class documentation
121 TString hostIP(gSystem->GetHostByName(gSystem->HostName()).GetHostAddress());
123 HLTInfo(hostIP.Data());
125 if ( hostIP.Contains("10.162.") )
127 else if ( hostIP.Contains("10.160.") || hostIP.Contains("10.161.") )
129 else if ( hostIP.Contains("129.206.") )
131 else if ( hostIP.Contains("137.138")
132 || hostIP.Contains("128.141")
133 || hostIP.Contains("127.0.")
134 || hostIP.Contains("192.168")
147 * ---------------------------------------------------------------------------------
148 * Proxy Communication - private
149 * ---------------------------------------------------------------------------------
152 //##################################################################################
153 Int_t AliHLTHOMERProxyHandler::RequestXmlRpcResponse() {
154 // see header file for class documentation
161 Int_t proxyPort = 19999;
163 TSocket *socket = new TSocket(fgkHOMERProxyNode[fRealm], proxyPort);
164 if ( ! socket->IsValid() ) {
165 HLTWarning(Form("Failed to create socket to %s:%d,",fgkHOMERProxyNode[fRealm], proxyPort));
166 HLTWarning(Form("trying %s:%d now.", fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort));
168 socket = new TSocket(fgkHOMERProxyNode[fRealm+kHOMERRealmsMax], proxyPort);
169 if ( ! socket->IsValid() ) {
170 HLTError(Form("Failed to create socket to %s:%d and %s:%d.",
171 fgkHOMERProxyNode[fRealm], proxyPort,
172 fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort));
174 HLTWarning(Form("trying %s:%d now.",
175 fgkHOMERProxyNode[kLoc],proxyPort));
176 socket = new TSocket(fgkHOMERProxyNode[kLoc], proxyPort);
177 if ( ! socket->IsValid() ) {
178 HLTError(Form("Failed to create socket to %s:%d , %s:%d and %s:%d.",
179 fgkHOMERProxyNode[fRealm], proxyPort,
180 fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort,
181 fgkHOMERProxyNode[kLoc], proxyPort));
190 fRealm = fRealm+kHOMERRealmsMax;
196 Char_t reqMsg[] = "POST / HTTP/1.1\r\n\
197 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\
198 Host: localhost:10000\r\n\
200 Content-type: text/xml\r\n\
201 Content-Length: 68\r\n\
202 \r\n<methodCall><methodName>getTcpDumpServices</methodName></methodCall>\r\n";
204 iResult = socket->SendRaw( reqMsg, strlen(reqMsg) );
206 iResult != static_cast<Int_t>(strlen(reqMsg))) {
207 HLTError(Form("Error sending! -- send length %d -- msg length %d.", iResult, static_cast<Int_t>(strlen(reqMsg)) ));
213 // -------------------
215 const Int_t bufferSize = 1024;
216 Char_t buffer[bufferSize];
218 Bool_t isXmlRpc = kFALSE;
220 fXmlRpcResponse = "";
222 // -- loop for getting full xmlRPC response
225 Int_t bufferLength = 0;
227 // -- loop for reading until end of line
230 iResult = socket->RecvRaw(&buffer[bufferLength], 1);
232 HLTError("Error reading form socket.");
237 // -- Checking for end of line
238 if ( buffer[bufferLength] == 10 ) {
239 buffer[bufferLength] = 0;
246 TString bufferString(buffer);
248 // -- Checking for start of XML response
249 if ( bufferString.BeginsWith("<?xml") )
252 // -- Append the xml response
254 fXmlRpcResponse.Append(bufferString);
257 // -- Checking for end of XML response
258 if( ! bufferString.CompareTo("</methodResponse>") )
268 //##################################################################################
269 Int_t AliHLTHOMERProxyHandler::ProcessXmlRpcResponse() {
270 // see header file for class documentation
274 // -- Parse XML RPC Response
275 // ---------------------------
277 TDOMParser xmlParser;
278 xmlParser.SetValidate(kFALSE);
280 //NOTE Have to use a temporary variable for printing the XML responce,
281 // because Form might overrun its internal buffer and crash for large strings.
282 TString infoMsg = Form("XMLResponse: %s",fXmlRpcResponse.Data());
283 HLTInfo(infoMsg.Data());
285 iResult = xmlParser.ParseBuffer(fXmlRpcResponse.Data(), fXmlRpcResponse.Length());
287 HLTError(Form("Parsing buffer with error: %s",
288 xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) ));
294 TXMLNode * node = xmlParser.GetXMLDocument()->GetRootNode()->
295 GetChildren()->GetChildren()->GetChildren()->GetChildren();
297 if ( strcmp( node->GetNodeName(), "string" ) ) {
298 HLTError("No node 'string' in XmlRpcResponse.");
303 // ------------------
306 TString xmlContent(node->GetText() );
308 HLTDebug(Form("XMLContent: %s",xmlContent.Data()));
310 iResult = xmlParser.ParseBuffer(xmlContent.Data(), xmlContent.Length());
312 HLTError(Form("Parsing buffer with error: %s",
313 xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) ));
318 if ( !xmlParser.GetXMLDocument()->GetRootNode()->HasChildren() ) {
319 HLTWarning("No Services active.");
323 // -- Loop over all service nodes
324 TXMLNode* serviceNode = xmlParser.GetXMLDocument()->GetRootNode()->GetChildren();
325 TXMLNode* prevServiceNode = NULL;
328 prevServiceNode = serviceNode;
330 // -- Add service to list
331 iResult = AddService( serviceNode->GetChildren() );
333 HLTWarning("Incomplete Service not added.");
336 } while ( ( serviceNode = prevServiceNode->GetNextNode() ) && !iResult );
344 * ---------------------------------------------------------------------------------
345 * Source Resolving - private
346 * ---------------------------------------------------------------------------------
349 //##################################################################################
350 Int_t AliHLTHOMERProxyHandler::AddService(TXMLNode *innerNode) {
351 // see header file for class documentation
355 HLTInfo(">> New service");
357 TXMLNode* serviceNode = innerNode;
359 // -- Loop over all service properties and
360 // read them from the service tag
361 // -----------------------------------------
363 TString hostname = "";
365 TString dataType = "";
366 TString dataOrigin = "";
367 TString dataSpecification = "";
369 TXMLNode* prevInnerNode = NULL;
371 // -- Retrieve hostname and port
372 // -------------------------------
375 prevInnerNode = innerNode;
377 if ( ! strcmp(innerNode->GetNodeName(), "text" ) )
381 if ( ! strcmp( innerNode->GetNodeName(), "address") ) {
382 HLTInfo(Form(" > %s ++ %s", innerNode->GetNodeName(), innerNode->GetText() ));
383 hostname = innerNode->GetText();
385 else if ( ! strcmp( innerNode->GetNodeName(), "port") ) {
386 HLTInfo(Form(" > %s ++ %s", innerNode->GetNodeName(), innerNode->GetText() ));
387 TString portS(innerNode->GetText());
388 if ( portS.IsDigit() )
391 HLTError(Form("Port %s is not a digit.", portS.Data()));
395 } while ( ( innerNode = prevInnerNode->GetNextNode() ) && !iResult );
398 // -- Change hostame from service with proxy, if outside HLT
399 if ( fRealm != kHLT && fRealm != kHLT+kHOMERRealmsMax )
400 hostname = fgkHOMERProxyNode[fRealm];
403 // -- Get Data Specifications from blocks
404 // ----------------------------------------
407 prevInnerNode = serviceNode;
409 if ( strcmp( serviceNode->GetNodeName(), "blocks") )
412 TXMLNode* blocks = serviceNode->GetChildren();
415 HLTError("No blocks present");
419 TXMLNode* blockNode = blocks->GetNextNode();
420 TXMLNode* prevBlockNode = NULL;
423 HLTError("No block present in the blocks tag");
430 prevBlockNode = blockNode;
432 if ( strcmp( blockNode->GetNodeName(), "block") )
435 TXMLNode *dataNode = blockNode->GetChildren();
436 TXMLNode *prevDataNode = NULL;
439 HLTError("No data specification tags present in block tag.");
445 prevDataNode = dataNode;
447 if ( ! strcmp(dataNode->GetNodeName(), "text" ) )
450 HLTInfo(Form(" %s ++ %s", dataNode->GetNodeName(), dataNode->GetText() ));
452 if ( ! strcmp( dataNode->GetNodeName(), "dataorigin") ) {
453 dataOrigin = dataNode->GetText();
455 else if ( ! strcmp( dataNode->GetNodeName(), "datatype") ) {
456 dataType = dataNode->GetText();
458 else if ( ! strcmp( dataNode->GetNodeName(), "dataspecification") ) {
459 dataSpecification = dataNode->GetText();
461 } while ( ( dataNode = prevDataNode->GetNextNode() ) && !iResult );
465 // -- Check the service properties
466 // ---------------------------------
468 // -- Check for completeness of the source properties
469 if ( hostname.IsNull() || !port || dataOrigin.IsNull() ||
470 dataType.IsNull() || dataSpecification.IsNull() ) {
471 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",
472 hostname.Data(), port, dataOrigin.Data(), dataType.Data(), dataSpecification.Atoi()));
477 // -- Create new source
478 // ----------------------
480 AliHLTHOMERSourceDesc * source = new AliHLTHOMERSourceDesc();
481 source->SetService( hostname, port, dataOrigin, dataType, dataSpecification );
483 fSourceList->Add( source );
485 HLTInfo(Form( "New Source added : %s", source->GetSourceName().Data()));
487 } while ( ( blockNode = prevBlockNode->GetNextNode() ) && !iResult );
492 } while ( ( serviceNode = prevInnerNode->GetNextNode() ) && !iResult );