]>
Commit | Line | Data |
---|---|---|
06272c83 | 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" | |
e728e5ba | 40 | // -- -- -- -- -- -- -- |
06272c83 | 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 | ||
72 | Int_t iResult = 0 ; | |
73 | ||
74 | IdentifyRealm(); | |
75 | ||
76 | return iResult; | |
77 | } | |
78 | ||
79 | /* | |
80 | * --------------------------------------------------------------------------------- | |
81 | * Source List - public | |
82 | * --------------------------------------------------------------------------------- | |
83 | */ | |
84 | ||
85 | //################################################################################## | |
86 | Int_t AliHLTHOMERProxyHandler::FillSourceList(TList *srcList) { | |
87 | // see header file for class documentation | |
88 | ||
89 | Int_t iResult = 0; | |
90 | ||
91 | fSourceList = srcList; | |
92 | ||
93 | iResult = RequestXmlRpcResponse(); | |
94 | ||
95 | if (!iResult) | |
96 | iResult = ProcessXmlRpcResponse(); | |
97 | ||
ae17e63c | 98 | if (iResult < 0) { |
b6bb7ca3 | 99 | HLTError(Form("Filling SourceList failed.")); |
ae17e63c | 100 | } |
06272c83 | 101 | |
102 | return iResult; | |
103 | } | |
104 | ||
105 | /* | |
106 | * --------------------------------------------------------------------------------- | |
107 | * Realms - private | |
108 | * --------------------------------------------------------------------------------- | |
109 | */ | |
110 | ||
111 | //################################################################################## | |
112 | const Char_t *AliHLTHOMERProxyHandler::fgkHOMERProxyNode[] = { | |
113 | "portal-dcs0.internal", | |
114 | "alihlt-dcs0.cern.ch", | |
115 | "alihlt-vobox0.cern.ch", | |
116 | "alihlt-gw0.kip.uni-heidelberg.de", | |
af7671cd | 117 | "localhost", |
06272c83 | 118 | "portal-dcs1.internal", |
119 | "alihlt-dcs1.cern.ch", | |
120 | "alihlt-vobox1.cern.ch", | |
cfa641b1 | 121 | "alihlt-gw1.kip.uni-heidelberg.de", |
122 | "localhost" | |
06272c83 | 123 | }; |
124 | ||
125 | //################################################################################## | |
126 | void AliHLTHOMERProxyHandler::IdentifyRealm() { | |
127 | // see header file for class documentation | |
128 | ||
129 | TString hostIP(gSystem->GetHostByName(gSystem->HostName()).GetHostAddress()); | |
130 | ||
131 | if ( hostIP.Contains("10.162.") ) | |
132 | fRealm = kHLT; | |
133 | else if ( hostIP.Contains("10.160.") || hostIP.Contains("10.161.") ) | |
134 | fRealm = kACR; | |
135 | else if ( hostIP.Contains("129.206.") ) | |
136 | fRealm = kKIP; | |
af7671cd | 137 | else if ( hostIP.Contains("137.138") |
138 | || hostIP.Contains("128.141") | |
139 | || hostIP.Contains("127.0.") | |
cfa641b1 | 140 | ) |
06272c83 | 141 | fRealm = kGPN; |
cfa641b1 | 142 | else { |
143 | fRealm = kLoc; | |
144 | } | |
145 | ||
06272c83 | 146 | return; |
147 | } | |
148 | ||
149 | /* | |
150 | * --------------------------------------------------------------------------------- | |
151 | * Proxy Communication - private | |
152 | * --------------------------------------------------------------------------------- | |
153 | */ | |
154 | ||
155 | //################################################################################## | |
156 | Int_t AliHLTHOMERProxyHandler::RequestXmlRpcResponse() { | |
157 | // see header file for class documentation | |
158 | ||
159 | Int_t iResult = 0; | |
160 | ||
161 | // -- open socket | |
162 | // ---------------- | |
163 | ||
164 | Int_t proxyPort = 19999; | |
165 | ||
166 | TSocket *socket = new TSocket(fgkHOMERProxyNode[fRealm], proxyPort); | |
167 | if ( ! socket->IsValid() ) { | |
b6bb7ca3 | 168 | HLTWarning(Form("Failed to create socket to %s:%d,",fgkHOMERProxyNode[fRealm], proxyPort)); |
169 | HLTWarning(Form("trying %s:%d now.", fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort)); | |
06272c83 | 170 | |
171 | socket = new TSocket(fgkHOMERProxyNode[fRealm+kHOMERRealmsMax], proxyPort); | |
172 | if ( ! socket->IsValid() ) { | |
b6bb7ca3 | 173 | HLTError(Form("Failed to create socket to %s:%d and %s:%d.", |
e728e5ba | 174 | fgkHOMERProxyNode[fRealm], proxyPort, |
175 | fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort)); | |
176 | ||
06272c83 | 177 | fRealm = -1; |
178 | return -1; | |
179 | } | |
180 | else | |
181 | fRealm = fRealm+kHOMERRealmsMax; | |
182 | } | |
183 | ||
184 | // -- send request | |
185 | // ----------------- | |
186 | ||
187 | Char_t reqMsg[] = "PUT / HTTP/1.1\r\n\ | |
188 | 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\ | |
189 | Host: localhost:10000\r\n\ | |
190 | Accept: */*\r\n\ | |
191 | Content-type: text/xml\r\n\ | |
192 | Content-Length: 68\r\n\ | |
193 | \r\n<methodCall><methodName>getTcpDumpServices</methodName></methodCall>\r\n"; | |
194 | ||
195 | iResult = socket->SendRaw( reqMsg, strlen(reqMsg) ); | |
196 | if ( iResult < 1 || | |
197 | iResult != static_cast<Int_t>(strlen(reqMsg))) { | |
b6bb7ca3 | 198 | HLTError(Form("Error sending! -- send length %d -- msg length %d.", iResult, static_cast<Int_t>(strlen(reqMsg)) )); |
06272c83 | 199 | socket->Close(); |
200 | return iResult; | |
201 | } | |
202 | ||
203 | // -- receive answer | |
204 | // ------------------- | |
205 | ||
206 | const Int_t bufferSize = 1024; | |
207 | Char_t buffer[bufferSize]; | |
208 | ||
209 | Bool_t isXmlRpc = kFALSE; | |
210 | ||
211 | fXmlRpcResponse = ""; | |
212 | ||
213 | // -- loop for getting full xmlRPC response | |
214 | while(1) { | |
215 | ||
216 | Int_t bufferLength = 0; | |
217 | ||
218 | // -- loop for reading until end of line | |
219 | while (1) { | |
220 | ||
221 | iResult = socket->RecvRaw(&buffer[bufferLength], 1); | |
222 | if ( iResult < 0) { | |
b6bb7ca3 | 223 | HLTError(Form("Error reading form socket.")); |
06272c83 | 224 | socket->Close(); |
225 | return iResult; | |
226 | } | |
227 | ||
228 | // -- Checking for end of line | |
229 | if ( buffer[bufferLength] == 10 ) { | |
230 | buffer[bufferLength] = 0; | |
231 | break; | |
232 | } | |
233 | ||
234 | ++bufferLength; | |
235 | } | |
236 | ||
237 | TString bufferString(buffer); | |
238 | ||
239 | // -- Checking for start of XML response | |
240 | if ( bufferString.BeginsWith("<?xml") ) | |
241 | isXmlRpc = kTRUE; | |
242 | ||
243 | // -- Append the xml response | |
244 | if (isXmlRpc) { | |
245 | fXmlRpcResponse.Append(bufferString); | |
246 | } | |
247 | ||
248 | // -- Checking for end of XML response | |
249 | if( ! bufferString.CompareTo("</methodResponse>") ) | |
250 | break; | |
251 | } | |
252 | ||
253 | // -- close socket | |
254 | socket->Close(); | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
259 | //################################################################################## | |
260 | Int_t AliHLTHOMERProxyHandler::ProcessXmlRpcResponse() { | |
261 | // see header file for class documentation | |
262 | ||
263 | Int_t iResult = 0; | |
264 | ||
265 | // -- Parse XML RPC Response | |
266 | // --------------------------- | |
267 | ||
268 | TDOMParser xmlParser; | |
269 | xmlParser.SetValidate(kFALSE); | |
270 | ||
a82a31af | 271 | HLTDebug(Form("XMLResponse: %s",fXmlRpcResponse.Data())); |
272 | ||
06272c83 | 273 | iResult = xmlParser.ParseBuffer(fXmlRpcResponse.Data(), fXmlRpcResponse.Length()); |
274 | if ( iResult < 0 ) { | |
b6bb7ca3 | 275 | HLTError(Form("Parsing buffer with error: %s", |
e728e5ba | 276 | xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) )); |
277 | ||
278 | ||
06272c83 | 279 | return iResult; |
280 | } | |
281 | ||
282 | TXMLNode * node = xmlParser.GetXMLDocument()->GetRootNode()-> | |
283 | GetChildren()->GetChildren()->GetChildren()->GetChildren(); | |
284 | ||
285 | if ( strcmp( node->GetNodeName(), "string" ) ) { | |
b6bb7ca3 | 286 | HLTError(Form("No node 'string' in XmlRpcResponse.")); |
06272c83 | 287 | return -1; |
288 | } | |
289 | ||
290 | // -- Parse Content | |
291 | // ------------------ | |
292 | ||
293 | // -- Get Content | |
294 | TString xmlContent(node->GetText() ); | |
295 | ||
9e78371d | 296 | HLTDebug(Form("XMLContent: %s",xmlContent.Data())); |
06272c83 | 297 | |
298 | iResult = xmlParser.ParseBuffer(xmlContent.Data(), xmlContent.Length()); | |
299 | if ( iResult < 0 ) { | |
b6bb7ca3 | 300 | HLTError(Form("Parsing buffer with error: %s", |
e728e5ba | 301 | xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) )); |
302 | ||
06272c83 | 303 | return iResult; |
304 | } | |
305 | ||
06272c83 | 306 | if ( !xmlParser.GetXMLDocument()->GetRootNode()->HasChildren() ) { |
b6bb7ca3 | 307 | HLTWarning(Form("No Services active.")); |
ae17e63c | 308 | return 1; |
06272c83 | 309 | } |
310 | ||
311 | // -- Loop over all service nodes | |
312 | TXMLNode* serviceNode = xmlParser.GetXMLDocument()->GetRootNode()->GetChildren(); | |
313 | TXMLNode* prevServiceNode = NULL; | |
314 | ||
315 | do { | |
316 | prevServiceNode = serviceNode; | |
317 | ||
318 | // -- Add service to list | |
319 | iResult = AddService( serviceNode->GetChildren() ); | |
74682ab7 | 320 | if ( iResult > 0 ) { |
b6bb7ca3 | 321 | HLTWarning(Form("Incomplete Service not added.")); |
74682ab7 | 322 | iResult = 0; |
323 | } | |
06272c83 | 324 | } while ( ( serviceNode = prevServiceNode->GetNextNode() ) && !iResult ); |
325 | ||
74682ab7 | 326 | |
327 | ||
06272c83 | 328 | return iResult; |
329 | } | |
330 | ||
331 | /* | |
332 | * --------------------------------------------------------------------------------- | |
333 | * Source Resolving - private | |
334 | * --------------------------------------------------------------------------------- | |
335 | */ | |
336 | ||
337 | //################################################################################## | |
338 | Int_t AliHLTHOMERProxyHandler::AddService(TXMLNode *innerNode) { | |
339 | // see header file for class documentation | |
340 | ||
341 | Int_t iResult = 0; | |
342 | ||
b6bb7ca3 | 343 | HLTInfo(Form(">> New service")); |
06272c83 | 344 | |
69f2a104 | 345 | TXMLNode* serviceNode = innerNode; |
346 | ||
06272c83 | 347 | // -- Loop over all service properties and |
348 | // read them from the service tag | |
349 | // ----------------------------------------- | |
350 | ||
351 | TString hostname = ""; | |
352 | Int_t port = 0; | |
353 | TString dataType = ""; | |
354 | TString dataOrigin = ""; | |
355 | TString dataSpecification = ""; | |
356 | ||
357 | TXMLNode* prevInnerNode = NULL; | |
358 | ||
69f2a104 | 359 | // -- Retrieve hostname and port |
360 | // ------------------------------- | |
361 | ||
06272c83 | 362 | do { |
363 | prevInnerNode = innerNode; | |
364 | ||
365 | if ( ! strcmp(innerNode->GetNodeName(), "text" ) ) | |
366 | continue; | |
69f2a104 | 367 | |
06272c83 | 368 | // -- hostname |
69f2a104 | 369 | if ( ! strcmp( innerNode->GetNodeName(), "address") ) { |
370 | HLTInfo(Form(" > %s ++ %s", innerNode->GetNodeName(), innerNode->GetText() )); | |
06272c83 | 371 | hostname = innerNode->GetText(); |
69f2a104 | 372 | } |
06272c83 | 373 | else if ( ! strcmp( innerNode->GetNodeName(), "port") ) { |
69f2a104 | 374 | HLTInfo(Form(" > %s ++ %s", innerNode->GetNodeName(), innerNode->GetText() )); |
06272c83 | 375 | TString portS(innerNode->GetText()); |
376 | if ( portS.IsDigit() ) | |
377 | port = portS.Atoi(); | |
378 | else { | |
b6bb7ca3 | 379 | HLTError(Form("Port %s is not a digit.", portS.Data())); |
06272c83 | 380 | iResult = -1; |
381 | } | |
382 | } | |
06272c83 | 383 | } while ( ( innerNode = prevInnerNode->GetNextNode() ) && !iResult ); |
384 | ||
06272c83 | 385 | |
386 | // -- Change hostame from service with proxy, if outside HLT | |
b325d632 | 387 | if ( fRealm != kHLT && fRealm != kHLT+kHOMERRealmsMax ) |
06272c83 | 388 | hostname = fgkHOMERProxyNode[fRealm]; |
389 | ||
e728e5ba | 390 | |
69f2a104 | 391 | // -- Get Data Specifications from blocks |
392 | // ---------------------------------------- | |
393 | ||
394 | do { | |
395 | prevInnerNode = serviceNode; | |
396 | ||
397 | if ( strcmp( serviceNode->GetNodeName(), "blocks") ) | |
398 | continue; | |
399 | ||
400 | TXMLNode* blocks = serviceNode->GetChildren(); | |
401 | ||
402 | if ( ! blocks ) { | |
403 | HLTError(Form("No blocks present")); | |
404 | return 1; | |
405 | } | |
406 | ||
407 | TXMLNode* blockNode = blocks->GetNextNode(); | |
408 | TXMLNode* prevBlockNode = NULL; | |
409 | ||
410 | if ( ! blockNode ) { | |
411 | HLTError(Form("No block present in the blocks tag")); | |
412 | return 1; | |
413 | } | |
414 | ||
415 | // -- blocks loop | |
416 | ||
417 | do { | |
418 | prevBlockNode = blockNode; | |
419 | ||
420 | if ( strcmp( blockNode->GetNodeName(), "block") ) | |
421 | continue; | |
422 | ||
423 | TXMLNode *dataNode = blockNode->GetChildren(); | |
424 | TXMLNode *prevDataNode = NULL; | |
425 | ||
426 | if ( ! dataNode ) { | |
427 | HLTError(Form("No data specification tags present in block tag.")); | |
428 | return 1; | |
429 | } | |
430 | // -- data spec loop | |
431 | ||
432 | do { | |
433 | prevDataNode = dataNode; | |
434 | ||
435 | if ( ! strcmp(dataNode->GetNodeName(), "text" ) ) | |
436 | continue; | |
437 | ||
438 | HLTInfo(Form(" %s ++ %s", dataNode->GetNodeName(), dataNode->GetText() )); | |
439 | ||
440 | if ( ! strcmp( dataNode->GetNodeName(), "dataorigin") ) { | |
441 | dataOrigin = dataNode->GetText(); | |
442 | } | |
443 | else if ( ! strcmp( dataNode->GetNodeName(), "datatype") ) { | |
444 | dataType = dataNode->GetText(); | |
445 | } | |
446 | else if ( ! strcmp( dataNode->GetNodeName(), "dataspecification") ) { | |
447 | dataSpecification = dataNode->GetText(); | |
448 | } | |
449 | } while ( ( dataNode = prevDataNode->GetNextNode() ) && !iResult ); | |
450 | ||
451 | // -- data spec loop | |
452 | ||
453 | // -- Check the service properties | |
454 | // --------------------------------- | |
455 | ||
456 | // -- Check for completeness of the source properties | |
457 | if ( hostname.IsNull() || !port || dataOrigin.IsNull() || | |
458 | dataType.IsNull() || dataSpecification.IsNull() ) { | |
459 | 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", | |
460 | hostname.Data(), port, dataOrigin.Data(), dataType.Data(), dataSpecification.Atoi())); | |
461 | ||
462 | return 1; | |
463 | } | |
06272c83 | 464 | |
69f2a104 | 465 | // -- Create new source |
466 | // ---------------------- | |
467 | ||
468 | AliHLTHOMERSourceDesc * source = new AliHLTHOMERSourceDesc(); | |
469 | source->SetService( hostname, port, dataOrigin, dataType, dataSpecification ); | |
470 | ||
471 | fSourceList->Add( source ); | |
472 | ||
473 | HLTInfo(Form( "New Source added : %s", source->GetSourceName().Data())); | |
06272c83 | 474 | |
69f2a104 | 475 | } while ( ( blockNode = prevBlockNode->GetNextNode() ) && !iResult ); |
06272c83 | 476 | |
06272c83 | 477 | |
69f2a104 | 478 | // -- blocks loop |
479 | ||
480 | } while ( ( serviceNode = prevInnerNode->GetNextNode() ) && !iResult ); | |
06272c83 | 481 | |
482 | return iResult; | |
483 | } | |
484 | ||
485 |