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