]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTHOMERProxyHandler.cxx
introducing variadic free HLT logging macros. When including AliHLTLoggingVariadicFre...
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTHOMERProxyHandler.cxx
CommitLineData
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// -- -- -- -- -- -- --
41#include "AliLog.h"
06272c83 42
43ClassImp(AliHLTHOMERProxyHandler)
44
45/*
46 * ---------------------------------------------------------------------------------
47 * Constructor / Destructor
48 * ---------------------------------------------------------------------------------
49 */
50
51//##################################################################################
52AliHLTHOMERProxyHandler::AliHLTHOMERProxyHandler() :
53 fRealm(kHLT),
54 fXmlRpcResponse(""),
55 fSourceList(NULL) {
56 // see header file for class documentation
57 // or
58 // refer to README to build package
59 // or
60 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
61}
62
63//##################################################################################
64AliHLTHOMERProxyHandler::~AliHLTHOMERProxyHandler() {
65 // see header file for class documentation
66
67}
68
69//##################################################################################
70Int_t AliHLTHOMERProxyHandler::Initialize() {
71 // see header file for class documentation
72
73 Int_t iResult = 0 ;
74
75 IdentifyRealm();
76
77 return iResult;
78}
79
80/*
81 * ---------------------------------------------------------------------------------
82 * Source List - public
83 * ---------------------------------------------------------------------------------
84 */
85
86//##################################################################################
87Int_t AliHLTHOMERProxyHandler::FillSourceList(TList *srcList) {
88 // see header file for class documentation
89
90 Int_t iResult = 0;
91
92 fSourceList = srcList;
93
94 iResult = RequestXmlRpcResponse();
95
96 if (!iResult)
97 iResult = ProcessXmlRpcResponse();
98
ae17e63c 99 if (iResult < 0) {
e728e5ba 100 AliError(Form("Filling SourceList failed."));
ae17e63c 101 }
06272c83 102
103 return iResult;
104}
105
106/*
107 * ---------------------------------------------------------------------------------
108 * Realms - private
109 * ---------------------------------------------------------------------------------
110 */
111
112//##################################################################################
113const Char_t *AliHLTHOMERProxyHandler::fgkHOMERProxyNode[] = {
114 "portal-dcs0.internal",
115 "alihlt-dcs0.cern.ch",
116 "alihlt-vobox0.cern.ch",
117 "alihlt-gw0.kip.uni-heidelberg.de",
118 "portal-dcs1.internal",
119 "alihlt-dcs1.cern.ch",
120 "alihlt-vobox1.cern.ch",
121 "alihlt-gw1.kip.uni-heidelberg.de"
122};
123
124//##################################################################################
125void AliHLTHOMERProxyHandler::IdentifyRealm() {
126 // see header file for class documentation
127
128 TString hostIP(gSystem->GetHostByName(gSystem->HostName()).GetHostAddress());
129
130 if ( hostIP.Contains("10.162.") )
131 fRealm = kHLT;
132 else if ( hostIP.Contains("10.160.") || hostIP.Contains("10.161.") )
133 fRealm = kACR;
134 else if ( hostIP.Contains("129.206.") )
135 fRealm = kKIP;
136 else
137 fRealm = kGPN;
138
139 return;
140}
141
142/*
143 * ---------------------------------------------------------------------------------
144 * Proxy Communication - private
145 * ---------------------------------------------------------------------------------
146 */
147
148//##################################################################################
149Int_t AliHLTHOMERProxyHandler::RequestXmlRpcResponse() {
150 // see header file for class documentation
151
152 Int_t iResult = 0;
153
154 // -- open socket
155 // ----------------
156
157 Int_t proxyPort = 19999;
158
159 TSocket *socket = new TSocket(fgkHOMERProxyNode[fRealm], proxyPort);
160 if ( ! socket->IsValid() ) {
e728e5ba 161 AliWarning(Form("Failed to create socket to %s:%d,",fgkHOMERProxyNode[fRealm], proxyPort));
162 AliWarning(Form("trying %s:%d now.", fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort));
06272c83 163
164 socket = new TSocket(fgkHOMERProxyNode[fRealm+kHOMERRealmsMax], proxyPort);
165 if ( ! socket->IsValid() ) {
e728e5ba 166 AliError(Form("Failed to create socket to %s:%d and %s:%d.",
167 fgkHOMERProxyNode[fRealm], proxyPort,
168 fgkHOMERProxyNode[fRealm+kHOMERRealmsMax],proxyPort));
169
06272c83 170 fRealm = -1;
171 return -1;
172 }
173 else
174 fRealm = fRealm+kHOMERRealmsMax;
175 }
176
177 // -- send request
178 // -----------------
179
180 Char_t reqMsg[] = "PUT / HTTP/1.1\r\n\
181User-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\
182Host: localhost:10000\r\n\
183Accept: */*\r\n\
184Content-type: text/xml\r\n\
185Content-Length: 68\r\n\
186\r\n<methodCall><methodName>getTcpDumpServices</methodName></methodCall>\r\n";
187
188 iResult = socket->SendRaw( reqMsg, strlen(reqMsg) );
189 if ( iResult < 1 ||
190 iResult != static_cast<Int_t>(strlen(reqMsg))) {
e728e5ba 191 AliError(Form("Error sending! -- send length %d -- msg length %d.", iResult, static_cast<Int_t>(strlen(reqMsg)) ));
06272c83 192 socket->Close();
193 return iResult;
194 }
195
196 // -- receive answer
197 // -------------------
198
199 const Int_t bufferSize = 1024;
200 Char_t buffer[bufferSize];
201
202 Bool_t isXmlRpc = kFALSE;
203
204 fXmlRpcResponse = "";
205
206 // -- loop for getting full xmlRPC response
207 while(1) {
208
209 Int_t bufferLength = 0;
210
211 // -- loop for reading until end of line
212 while (1) {
213
214 iResult = socket->RecvRaw(&buffer[bufferLength], 1);
215 if ( iResult < 0) {
e728e5ba 216 AliError(Form("Error reading form socket."));
06272c83 217 socket->Close();
218 return iResult;
219 }
220
221 // -- Checking for end of line
222 if ( buffer[bufferLength] == 10 ) {
223 buffer[bufferLength] = 0;
224 break;
225 }
226
227 ++bufferLength;
228 }
229
230 TString bufferString(buffer);
231
232 // -- Checking for start of XML response
233 if ( bufferString.BeginsWith("<?xml") )
234 isXmlRpc = kTRUE;
235
236 // -- Append the xml response
237 if (isXmlRpc) {
238 fXmlRpcResponse.Append(bufferString);
239 }
240
241 // -- Checking for end of XML response
242 if( ! bufferString.CompareTo("</methodResponse>") )
243 break;
244 }
245
246 // -- close socket
247 socket->Close();
248
249 return 0;
250}
251
252//##################################################################################
253Int_t AliHLTHOMERProxyHandler::ProcessXmlRpcResponse() {
254 // see header file for class documentation
255
256 Int_t iResult = 0;
257
258 // -- Parse XML RPC Response
259 // ---------------------------
260
261 TDOMParser xmlParser;
262 xmlParser.SetValidate(kFALSE);
263
e728e5ba 264 AliInfo(Form("XMLResponse: %s",fXmlRpcResponse.Data()));
06272c83 265
266 iResult = xmlParser.ParseBuffer(fXmlRpcResponse.Data(), fXmlRpcResponse.Length());
267 if ( iResult < 0 ) {
e728e5ba 268 AliError(Form("Parsing buffer with error: %s",
269 xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) ));
270
271
06272c83 272 return iResult;
273 }
274
275 TXMLNode * node = xmlParser.GetXMLDocument()->GetRootNode()->
276 GetChildren()->GetChildren()->GetChildren()->GetChildren();
277
278 if ( strcmp( node->GetNodeName(), "string" ) ) {
e728e5ba 279 AliError(Form("No node 'string' in XmlRpcResponse."));
06272c83 280 return -1;
281 }
282
283 // -- Parse Content
284 // ------------------
285
286 // -- Get Content
287 TString xmlContent(node->GetText() );
288
e728e5ba 289 AliInfo(Form("XMLContent: %s",xmlContent.Data()));
06272c83 290
291 iResult = xmlParser.ParseBuffer(xmlContent.Data(), xmlContent.Length());
292 if ( iResult < 0 ) {
e728e5ba 293 AliError(Form("Parsing buffer with error: %s",
294 xmlParser.GetParseCodeMessage(xmlParser.GetParseCode()) ));
295
06272c83 296 return iResult;
297 }
298
299
300 if ( !xmlParser.GetXMLDocument()->GetRootNode()->HasChildren() ) {
e728e5ba 301 AliWarning(Form("No Services active."));
ae17e63c 302 return 1;
06272c83 303 }
304
305 // -- Loop over all service nodes
306 TXMLNode* serviceNode = xmlParser.GetXMLDocument()->GetRootNode()->GetChildren();
307 TXMLNode* prevServiceNode = NULL;
308
309 do {
310 prevServiceNode = serviceNode;
311
312 // -- Add service to list
313 iResult = AddService( serviceNode->GetChildren() );
74682ab7 314 if ( iResult > 0 ) {
315 AliWarning(Form("Incomplete Service not added."));
316 iResult = 0;
317 }
06272c83 318 } while ( ( serviceNode = prevServiceNode->GetNextNode() ) && !iResult );
319
74682ab7 320
321
06272c83 322 return iResult;
323}
324
325/*
326 * ---------------------------------------------------------------------------------
327 * Source Resolving - private
328 * ---------------------------------------------------------------------------------
329 */
330
331//##################################################################################
332Int_t AliHLTHOMERProxyHandler::AddService(TXMLNode *innerNode) {
333 // see header file for class documentation
334
335 Int_t iResult = 0;
336
e728e5ba 337 AliInfo(Form(">> New service"));
06272c83 338
339 // -- Loop over all service properties and
340 // read them from the service tag
341 // -----------------------------------------
342
343 TString hostname = "";
344 Int_t port = 0;
345 TString dataType = "";
346 TString dataOrigin = "";
347 TString dataSpecification = "";
348
349 TXMLNode* prevInnerNode = NULL;
350
351 do {
352 prevInnerNode = innerNode;
353
354 if ( ! strcmp(innerNode->GetNodeName(), "text" ) )
355 continue;
356
e728e5ba 357 AliInfo(Form(" %s ++ %s", innerNode->GetNodeName(), innerNode->GetText() ));
06272c83 358
359 // -- hostname
360 if ( ! strcmp( innerNode->GetNodeName(), "address") )
361 hostname = innerNode->GetText();
362 else if ( ! strcmp( innerNode->GetNodeName(), "port") ) {
363 TString portS(innerNode->GetText());
364 if ( portS.IsDigit() )
365 port = portS.Atoi();
366 else {
e728e5ba 367 AliError(Form("Port %s is not a digit.", portS.Data()));
06272c83 368 iResult = -1;
369 }
370 }
371 else if ( ! strcmp( innerNode->GetNodeName(), "dataorigin") )
372 dataOrigin = innerNode->GetText();
373 else if ( ! strcmp( innerNode->GetNodeName(), "datatype") )
374 dataType = innerNode->GetText();
375 else if ( ! strcmp( innerNode->GetNodeName(), "dataspecification") )
376 dataSpecification = innerNode->GetText();
377
378 } while ( ( innerNode = prevInnerNode->GetNextNode() ) && !iResult );
379
380 // -- Check the service properties
381 // ---------------------------------
382
383 // -- Change hostame from service with proxy, if outside HLT
384 if ( fRealm != kHLT || fRealm != kHLT+kHOMERRealmsMax )
385 hostname = fgkHOMERProxyNode[fRealm];
386
387 // -- Check for completeness of the source properties
388 if ( hostname.IsNull() || !port || dataOrigin.IsNull() ||
e728e5ba 389 dataType.IsNull() || dataSpecification.IsNull() ) {
74682ab7 390 AliWarning(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 %s",
e728e5ba 391 hostname.Data(), port, dataOrigin.Data(), dataType.Data(), dataSpecification.Data()));
392
74682ab7 393 return 1;
06272c83 394 }
395
396 // -- Create new source
397 // ----------------------
398
399 AliHLTHOMERSourceDesc * source = new AliHLTHOMERSourceDesc();
4faff50e 400 source->SetService( hostname, port, dataOrigin, dataType, dataSpecification );
06272c83 401
402 fSourceList->Add( source );
403
e728e5ba 404 AliInfo(Form( "New Source added : %s", source->GetSourceName().Data()));
06272c83 405
406 return iResult;
407}
408
409