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