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
35 #include "TDOMParser.h"
38 // -- -- -- -- -- -- --
39 #include "AliHLTHOMERProxyHandler.h"
40 // -- -- -- -- -- -- --
42 ClassImp(AliHLTHOMERProxyHandler)
45 * ---------------------------------------------------------------------------------
46 * Constructor / Destructor
47 * ---------------------------------------------------------------------------------
50 //##################################################################################
51 AliHLTHOMERProxyHandler::AliHLTHOMERProxyHandler() :
55 // see header file for class documentation
57 // refer to README to build package
59 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
62 //##################################################################################
63 AliHLTHOMERProxyHandler::~AliHLTHOMERProxyHandler() {
64 // see header file for class documentation
68 //##################################################################################
69 Int_t AliHLTHOMERProxyHandler::Initialize() {
70 // see header file for class documentation
76 * ---------------------------------------------------------------------------------
77 * Source List - public
78 * ---------------------------------------------------------------------------------
81 //##################################################################################
82 Int_t AliHLTHOMERProxyHandler::FillSourceList(TList *srcList) {
83 // see header file for class documentation
87 fSourceList = srcList;
89 iResult = RequestXmlRpcResponse();
92 iResult = ProcessXmlRpcResponse();
95 HLTError("Filling SourceList failed.");
102 * ---------------------------------------------------------------------------------
104 * ---------------------------------------------------------------------------------
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",
114 "portal-dcs1.internal",
115 "alihlt-dcs1.cern.ch",
116 "alihlt-vobox1.cern.ch",
117 "alihlt-gw1.kip.uni-heidelberg.de",
121 //##################################################################################
122 void AliHLTHOMERProxyHandler::IdentifyRealm() {
123 // see header file for class documentation
125 TString hostIP(gSystem->GetHostByName(gSystem->HostName()).GetHostAddress());
127 HLTInfo(hostIP.Data());
129 if ( hostIP.Contains("10.162.") )
131 else if ( hostIP.Contains("10.160.") || hostIP.Contains("10.161.") )
133 else if ( hostIP.Contains("129.206.") )
135 else if ( hostIP.Contains("137.138")
136 || hostIP.Contains("128.141")
137 || hostIP.Contains("127.0.")
138 || hostIP.Contains("192.168")
151 * ---------------------------------------------------------------------------------
152 * Proxy Communication - private
153 * ---------------------------------------------------------------------------------
156 //##################################################################################
157 Int_t AliHLTHOMERProxyHandler::RequestXmlRpcResponse() {
158 // see header file for class documentation
165 Int_t proxyPort = 19999;
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));
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));
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));
194 fRealm = fRealm+kHOMERRealmsMax;
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\
204 Content-type: text/xml\r\n\
205 Content-Length: 68\r\n\
206 \r\n<methodCall><methodName>getTcpDumpServices</methodName></methodCall>\r\n";
208 iResult = socket->SendRaw( reqMsg, strlen(reqMsg) );
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)) ));
217 // -------------------
219 const Int_t bufferSize = 1024;
220 Char_t buffer[bufferSize];
222 Bool_t isXmlRpc = kFALSE;
224 fXmlRpcResponse = "";
226 // -- loop for getting full xmlRPC response
229 Int_t bufferLength = 0;
231 // -- loop for reading until end of line
234 iResult = socket->RecvRaw(&buffer[bufferLength], 1);
236 HLTError("Error reading form socket.");
241 // -- Checking for end of line
242 if ( buffer[bufferLength] == 10 ) {
243 buffer[bufferLength] = 0;
250 TString bufferString(buffer);
252 // -- Checking for start of XML response
253 if ( bufferString.BeginsWith("<?xml") )
256 // -- Append the xml response
258 fXmlRpcResponse.Append(bufferString);
261 // -- Checking for end of XML response
262 if( ! bufferString.CompareTo("</methodResponse>") )
272 //##################################################################################
273 Int_t AliHLTHOMERProxyHandler::ProcessXmlRpcResponse() {
274 // see header file for class documentation
278 // -- Parse XML RPC Response
279 // ---------------------------
281 TDOMParser xmlParser;
282 xmlParser.SetValidate(kFALSE);
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());
289 iResult = xmlParser.ParseBuffer(fXmlRpcResponse.Data(), fXmlRpcResponse.Length());
291 HLTError(Form("Parsing buffer with error: %s",
292 xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) ));
298 TXMLNode * node = xmlParser.GetXMLDocument()->GetRootNode()->
299 GetChildren()->GetChildren()->GetChildren()->GetChildren();
301 if ( strcmp( node->GetNodeName(), "string" ) ) {
302 HLTError("No node 'string' in XmlRpcResponse.");
307 // ------------------
310 TString xmlContent(node->GetText() );
312 HLTDebug(Form("XMLContent: %s",xmlContent.Data()));
314 iResult = xmlParser.ParseBuffer(xmlContent.Data(), xmlContent.Length());
316 HLTError(Form("Parsing buffer with error: %s",
317 xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) ));
322 if ( !xmlParser.GetXMLDocument()->GetRootNode()->HasChildren() ) {
323 HLTWarning("No Services active.");
327 // -- Loop over all service nodes
328 TXMLNode* serviceNode = xmlParser.GetXMLDocument()->GetRootNode()->GetChildren();
329 TXMLNode* prevServiceNode = NULL;
332 prevServiceNode = serviceNode;
334 // -- Add service to list
335 iResult = AddService( serviceNode->GetChildren() );
337 HLTWarning("Incomplete Service not added.");
340 } while ( ( serviceNode = prevServiceNode->GetNextNode() ) && !iResult );
348 * ---------------------------------------------------------------------------------
349 * Source Resolving - private
350 * ---------------------------------------------------------------------------------
353 //##################################################################################
354 Int_t AliHLTHOMERProxyHandler::AddService(TXMLNode *innerNode) {
355 // see header file for class documentation
359 HLTInfo(">> New service");
361 TXMLNode* serviceNode = innerNode;
363 // -- Loop over all service properties and
364 // read them from the service tag
365 // -----------------------------------------
367 TString hostname = "";
369 TString dataType = "";
370 TString dataOrigin = "";
371 TString dataSpecification = "";
373 TXMLNode* prevInnerNode = NULL;
375 // -- Retrieve hostname and port
376 // -------------------------------
379 prevInnerNode = innerNode;
381 if ( ! strcmp(innerNode->GetNodeName(), "text" ) )
385 if ( ! strcmp( innerNode->GetNodeName(), "address") ) {
386 HLTInfo(Form(" > %s ++ %s", innerNode->GetNodeName(), innerNode->GetText() ));
387 hostname = innerNode->GetText();
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() )
395 HLTError(Form("Port %s is not a digit.", portS.Data()));
399 } while ( ( innerNode = prevInnerNode->GetNextNode() ) && !iResult );
402 // -- Change hostame from service with proxy, if outside HLT
403 if ( fRealm != kHLT && fRealm != kHLT+kHOMERRealmsMax )
404 hostname = fgkHOMERProxyNode[fRealm];
407 // -- Get Data Specifications from blocks
408 // ----------------------------------------
411 prevInnerNode = serviceNode;
413 if ( strcmp( serviceNode->GetNodeName(), "blocks") )
416 TXMLNode* blocks = serviceNode->GetChildren();
419 HLTError("No blocks present");
423 TXMLNode* blockNode = blocks->GetNextNode();
424 TXMLNode* prevBlockNode = NULL;
427 HLTError("No block present in the blocks tag");
434 prevBlockNode = blockNode;
436 if ( strcmp( blockNode->GetNodeName(), "block") )
439 TXMLNode *dataNode = blockNode->GetChildren();
440 TXMLNode *prevDataNode = NULL;
443 HLTError("No data specification tags present in block tag.");
449 prevDataNode = dataNode;
451 if ( ! strcmp(dataNode->GetNodeName(), "text" ) )
454 HLTInfo(Form(" %s ++ %s", dataNode->GetNodeName(), dataNode->GetText() ));
456 if ( ! strcmp( dataNode->GetNodeName(), "dataorigin") ) {
457 dataOrigin = dataNode->GetText();
459 else if ( ! strcmp( dataNode->GetNodeName(), "datatype") ) {
460 dataType = dataNode->GetText();
462 else if ( ! strcmp( dataNode->GetNodeName(), "dataspecification") ) {
463 dataSpecification = dataNode->GetText();
465 } while ( ( dataNode = prevDataNode->GetNextNode() ) && !iResult );
469 // -- Check the service properties
470 // ---------------------------------
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()));
481 // -- Create new source
482 // ----------------------
484 AliHLTHOMERSourceDesc * source = new AliHLTHOMERSourceDesc();
485 source->SetService( hostname, port, dataOrigin, dataType, dataSpecification );
487 fSourceList->Add( source );
489 HLTInfo(Form( "New Source added : %s", source->GetSourceName().Data()));
491 } while ( ( blockNode = prevBlockNode->GetNextNode() ) && !iResult );
496 } while ( ( serviceNode = prevInnerNode->GetNextNode() ) && !iResult );