1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 Revision 1.6 2007/10/16 14:37:16 jgrosseo
19 changing to AMANDA protocol version 2
21 Revision 1.5 2007/10/05 12:40:55 acolla
23 Result error code added to AliDCSClient data members (it was "lost" with the new implementation of TMap* GetAliasValues and GetDPValues).
25 Revision 1.4 2007/09/14 16:46:14 jgrosseo
26 1) Connect and Close are called before and after each query, so one can
27 keep the same AliDCSClient object.
28 2) The splitting of a query is moved to GetDPValues/GetAliasValues.
29 3) Splitting interval can be specified in constructor
31 Revision 1.3 2007/09/11 16:42:02 jgrosseo
32 starting modifying AliDCSClient to transparently switch between single and multi query
33 first step: same alidcsclient can be used for several queries
35 Revision 1.2 2007/06/09 13:01:09 jgrosseo
36 Switching to retrieval of several DCS DPs at a time (multiDPrequest)
38 Revision 1.1 2006/11/06 14:22:47 jgrosseo
39 major update (Alberto)
40 o) reading of run parameters from the logbook
41 o) online offline naming conversion
42 o) standalone DCSclient package
44 Revision 1.6 2006/10/02 16:38:39 jgrosseo
47 storing of objects that failed to be stored to the grid before
48 interfacing of shuttle status table in daq system
50 Revision 1.5 2006/08/15 10:50:00 jgrosseo
51 effc++ corrections (alberto)
53 Revision 1.4 2006/07/04 14:59:57 jgrosseo
54 revision of AliDCSValue: Removed wrapper classes, reduced storage size per value by factor 2
56 Revision 1.3 2006/06/12 09:11:16 jgrosseo
57 coding conventions (Alberto)
59 Revision 1.2 2006/03/07 07:52:34 hristov
60 New version (B.Yordanov)
62 Revision 1.3 2005/11/17 17:47:34 byordano
63 TList changed to TObjArray
65 Revision 1.2 2005/11/17 14:43:23 byordano
68 Revision 1.1.1.1 2005/10/28 07:33:58 hristov
69 Initial import as subdirectory in AliRoot
71 Revision 1.1.1.1 2005/09/12 22:11:40 byordano
74 Revision 1.3 2005/08/30 10:53:23 byordano
75 some more descriptions added
80 // This class represents the AliDCSClient.
81 // The client used for data retrieval from DCS server.
82 // There are two way for retrieving data from the server.
83 // 1) asking for DP (DataPoint) - usually changed frequently.
84 // 2) asking for Alias (Alias) - alias should be the same through whole
87 // There are two type of read operations:
88 // Asking for single alias/dp or asking for set of aliases/dp
90 // In case of ServerError the coresponding error code and
91 // error string (description) could be got by GetServerErrorCode() and
92 // GetServerErrorString()
95 #include "AliDCSClient.h"
96 #include "AliDCSValue.h"
100 #include <TObjArray.h>
102 #include <TObjString.h>
105 ClassImp(AliDCSClient)
107 const char* AliDCSClient::fgkBadStateString = "BadState";
108 const char* AliDCSClient::fgkInvalidParameterString = "InvalidParameter";
109 const char* AliDCSClient::fgkTimeoutString = "Timeout";
110 const char* AliDCSClient::fgkBadMessageString = "BadMessage";
111 const char* AliDCSClient::fgkCommErrorString = "CommunicationError";
112 const char* AliDCSClient::fgkServerErrorString = "ServerError";
114 //______________________________________________________________________
115 AliDCSClient::AliDCSClient(const char* host, Int_t port, UInt_t timeout,
116 Int_t retries, Int_t multiSplit):
117 fSocket(NULL), fHost(host), fPort(port), fTimeout(timeout), fRetries(retries), fMultiSplit(multiSplit),
118 fServerErrorCode(AliDCSMessage::kNoneError), fServerError(""), fResultErrorCode(0)
121 // host: DCS server host
122 // port: DCS server port
123 // timeout: in case of communication error or socket read/write this
124 // timeout will be used before the next try is made.
125 // retries: the number of retries after which the connection is
126 // is considered as invalid and error is returned.
130 //______________________________________________________________________
131 AliDCSClient::~AliDCSClient()
138 //______________________________________________________________________
139 Bool_t AliDCSClient::Connect()
141 // connects to the AMANDA server
146 while (tries < fRetries)
148 fSocket = new TSocket(fHost, fPort);
149 if (fSocket->IsValid())
151 AliDebug(1, Form("Connected to %s:%d", fHost.Data(), fPort));
152 fSocket->SetOption(kNoBlock, 1);
156 AliDebug(1, Form("Connection timeout! tries <%d> ...", tries));
161 gSystem->Sleep(fTimeout);
168 //______________________________________________________________________
169 Int_t AliDCSClient::SendBuffer(const char* buffer, Int_t size)
171 // send buffer containing the message to the DCS server
176 while (sentSize < size && tries < fRetries) {
178 Int_t sResult = fSocket->Select(TSocket::kWrite, fTimeout);
181 AliDebug(1, Form("Timeout! tries <%d> ...", tries));
185 } else if (sResult < 0) {
186 AliDebug(1, Form("Communication error <%d>!",
187 fSocket->GetErrorCode()));
188 return AliDCSClient::fgkCommError;
191 sResult = fSocket->SendRaw(buffer + sentSize, size - sentSize,
197 AliDebug(1, Form("Communication error <%d>!",
198 fSocket->GetErrorCode()));
199 return AliDCSClient::fgkCommError;
203 if (tries == fRetries) {
204 return AliDCSClient::fgkTimeout;
210 //______________________________________________________________________
211 Int_t AliDCSClient::ReceiveBuffer(char* buffer, Int_t size)
213 // Receive message from the DCS server and fill buffer
215 Int_t receivedSize = 0;
218 while (receivedSize < size && tries < fRetries) {
220 Int_t sResult = fSocket->Select(TSocket::kRead, fTimeout);
223 AliDebug(1, Form("Timeout! tries <%d> ...", tries));
227 } else if (sResult < 0) {
228 AliDebug(1, Form("Communication error <%d>",
229 fSocket->GetErrorCode()));
230 return AliDCSClient::fgkCommError;
233 sResult = fSocket->RecvRaw(buffer + receivedSize,
234 size - receivedSize, kDontBlock);
237 receivedSize += sResult;
239 AliDebug(1, Form("Communication error <%d>",
240 fSocket->GetErrorCode()));
241 return AliDCSClient::fgkCommError;
245 if (tries == fRetries) {
246 return AliDCSClient::fgkTimeout;
252 //______________________________________________________________________
253 Int_t AliDCSClient::SendMessage(AliDCSMessage& message)
255 // send message to the DCS server
257 message.StoreToBuffer();
259 AliDebug(2, "Sending message.\n");
262 return SendBuffer(message.GetMessage(), message.GetMessageSize());
265 //______________________________________________________________________
266 Int_t AliDCSClient::ReceiveMessage(AliDCSMessage& message)
268 // receive message from the DCS server
270 char header[HEADER_SIZE];
274 if ((sResult = ReceiveBuffer(header, HEADER_SIZE)) < 0) {
275 AliDebug(1, Form("Can't receive message header! Reason: %s",
276 GetErrorString(sResult)));
280 if (!message.SetRawHeader(header)) {
281 return AliDCSClient::fgkBadMessage;
284 if ((sResult = ReceiveBuffer(message.GetBody(),
285 message.GetBodySize())) < 0) {
287 AliDebug(1, Form("Can't receive message body! Reason: %s",
288 GetErrorString(sResult)));
292 message.LoadFromBuffer();
294 AliDebug(2, "Message received.");
297 return HEADER_SIZE + sResult;
300 //______________________________________________________________________
301 Int_t AliDCSClient::GetValues(AliDCSMessage::RequestType reqType,
302 const char* reqString, UInt_t startTime, UInt_t endTime, TObjArray* result)
304 // get array of DCS values from the DCS server
305 // reqString: alias name
306 // startTime, endTime: start time and end time of the query
307 // result: contains the array of retrieved AliDCSValue's
311 if (!IsConnected()) {
312 AliError("Not connected!");
313 return AliDCSClient::fgkBadState;
317 AliDCSMessage requestMessage;
318 requestMessage.CreateRequestMessage(reqType, startTime, endTime,
321 if ((sResult = SendMessage(requestMessage)) < 0) {
322 AliError(Form("Can't send request message! Reason: %s",
323 GetErrorString(sResult)));
328 Int_t receivedValues = 0;
333 sResult = ReceiveValueSet(result, tmp);
336 receivedValues += sResult;
341 return receivedValues;
344 //______________________________________________________________________
345 TMap* AliDCSClient::GetValues(AliDCSMessage::RequestType reqType,
346 const TSeqCollection* list, UInt_t startTime, UInt_t endTime,
347 Int_t startIndex, Int_t endIndex)
349 // get array of DCS values from the DCS server
350 // list, startTime, endTime: list of dp/alias names, start time and end time of the query
352 // Result: map containing the array of alias names. It will be filled with
353 // the values retrieved for each alias
355 TMap* result = new TMap;
358 if (endIndex < 0 || endIndex > list->GetEntries())
359 endIndex = list->GetEntries();
361 for (Int_t subsetBegin = startIndex; subsetBegin < endIndex; subsetBegin += fMultiSplit)
367 AliError("Not connected!");
369 fResultErrorCode = fgkBadState;
373 Int_t subsetEnd = subsetBegin + fMultiSplit;
374 if (subsetEnd > endIndex)
375 subsetEnd = endIndex;
377 AliDCSMessage requestMessage;
378 if (fMultiSplit == 1)
382 TObjString* aRequest = (TObjString*) list->At(subsetBegin);
383 requestMessage.CreateRequestMessage(reqType, startTime, endTime, aRequest->String());
389 requestMessage.CreateMultiRequestMessage(reqType,
392 for (Int_t i=subsetBegin; i<subsetEnd; i++)
394 TObjString* aRequest = (TObjString*) list->At(i);
395 if (!requestMessage.AddRequestString(aRequest->String()))
398 fResultErrorCode = fgkBadMessage;
404 if ((fResultErrorCode = SendMessage(requestMessage)) < 0)
406 AliError(Form("Can't send request message! Reason: %s",
407 GetErrorString(fResultErrorCode)));
415 TObjArray* resultSet = new TObjArray();
416 resultSet->SetOwner(1);
418 Int_t ownerIndex = -1;
419 fResultErrorCode = ReceiveValueSet(resultSet, ownerIndex);
421 if (fResultErrorCode < 0)
423 AliError("Can't get values");
426 result->DeleteValues();
438 TObjString* aRequest = (TObjString*) list->At(ownerIndex + subsetBegin);
439 //AliInfo(Form("Received %d values for entry %d, that is %s", resultSet->GetEntries(),
440 // ownerIndex + subsetBegin, aRequest->String().Data()));
442 TObjArray* target = dynamic_cast<TObjArray*> (result->GetValue(aRequest));
445 target->AddAll(resultSet);
446 //AliInfo(Form("Now we have %d entries", target->GetEntries()));
447 resultSet->SetOwner(0);
451 result->Add(aRequest, resultSet);
457 TObjArray* example = (TObjArray*) result->GetValue(list->At(subsetBegin));
459 nValues = example->GetEntries();
460 AliInfo(Form("Retrieved entries %d..%d (total %d..%d); E.g. %s has %d values collected",
461 subsetBegin, subsetEnd-1, startIndex, endIndex-1, list->At(subsetBegin)->GetName(), nValues));
467 //______________________________________________________________________
468 Int_t AliDCSClient::ReceiveValueSet(TObjArray* result, Int_t& ownerIndex)
470 // receive set of values
472 AliDCSMessage message;
473 Int_t sResult = ReceiveMessage(message);
476 AliError(Form("Can't receive message! Reason: %s", GetErrorString(sResult)));
480 if (message.GetType() == AliDCSMessage::kResultSet)
482 // this was the last message
483 ownerIndex = message.GetOwnerIndex();
487 sResult = message.GetValues(result);
489 AliDCSMessage nextMessage;
490 nextMessage.CreateNextMessage();
492 if ((fResultErrorCode = SendMessage(nextMessage)) < 0)
494 AliError(Form("Can't send next message! Reason: %s",
495 GetErrorString(fResultErrorCode)));
497 return AliDCSClient::fgkCommError;
502 else if (message.GetType() == AliDCSMessage::kError)
504 fServerErrorCode = message.GetErrorCode();
505 fServerError = message.GetErrorString();
507 return AliDCSClient::fgkServerError;
510 AliError("Bad message type received!");
511 return AliDCSClient::fgkBadMessage;
514 //______________________________________________________________________
515 Int_t AliDCSClient::GetDPValues(const char* dpName, UInt_t startTime,
516 UInt_t endTime, TObjArray* result)
519 // Reads a values from the server which correspond to this
520 // DataPoint (dpName) in time interval (startTime - endTime).
521 // result: Collection of AliDCSValue which contains the read values.
524 // If >= 0 , the number of values read.
525 // if < 0, the error code which has occured during the read.
528 return GetValues(AliDCSMessage::kDPName,
529 dpName, startTime, endTime, result);
532 //______________________________________________________________________
533 Int_t AliDCSClient::GetAliasValues(const char* alias, UInt_t startTime,
534 UInt_t endTime, TObjArray* result)
537 // Reads a values from the server which correspond to this
538 // alias (alias) in time interval (startTime - endTime).
539 // result: Collection of AliDCSValue which contains the read values.
542 // If >= 0 , the number of values read.
543 // if < 0, the error code which has occured during the read.
546 return GetValues(AliDCSMessage::kAlias,
547 alias, startTime, endTime, result);
550 //______________________________________________________________________
551 TMap* AliDCSClient::GetDPValues(const TSeqCollection* dpList, UInt_t startTime, UInt_t endTime,
552 Int_t startIndex, Int_t endIndex)
555 // For every entry (fron startIndex to endIndex) in dpList (which must be TObjString)
556 // reads a valueSet. The key represents particular DataPoint to be read.
557 // For all DataPoints time interval (startTime - endTime) is used.
558 // After the read, the correspoding value for every key is a
559 // TObjArray - collection of AliDCSValue, or result is an empty map in
563 // TMap of results, 0 in case of failure
566 return GetValues(AliDCSMessage::kDPName, dpList, startTime, endTime, startIndex, endIndex);
569 //______________________________________________________________________
570 TMap* AliDCSClient::GetAliasValues(const TSeqCollection* aliasList, UInt_t startTime, UInt_t endTime,
571 Int_t startIndex, Int_t endIndex)
574 // For every entry (fron startIndex to endIndex) in dpList (which must be TObjString)
575 // reads a valueSet. The key represents particular Alias to be read.
576 // For all aliases time interval (startTime - endTime) is used.
577 // After the read, the correspoding value for every key is a
578 // TObjArray - collection of AliDCSValue, or result is an empty map in
582 // TMap of results, 0 in case of failure
585 return GetValues(AliDCSMessage::kAlias, aliasList, startTime, endTime, startIndex, endIndex);
588 //______________________________________________________________________
589 Bool_t AliDCSClient::IsConnected()
592 // Returns kTRUE if there is a valid connection to the server.
596 return fSocket->IsValid();
602 //______________________________________________________________________
603 void AliDCSClient::Close()
606 // Close the connection.
616 //______________________________________________________________________
617 const char* AliDCSClient::GetErrorString(Int_t code)
620 // Returns a short string describing the error code.
621 // code: the error code.
626 case AliDCSClient::fgkBadState:
627 return AliDCSClient::fgkBadStateString;
629 case AliDCSClient::fgkInvalidParameter:
630 return AliDCSClient::fgkInvalidParameterString;
632 case AliDCSClient::fgkTimeout:
633 return AliDCSClient::fgkTimeoutString;
635 case AliDCSClient::fgkBadMessage:
636 return AliDCSClient::fgkBadMessageString;
638 case AliDCSClient::fgkCommError:
639 return AliDCSClient::fgkCommErrorString;
641 case AliDCSClient::fgkServerError:
642 return AliDCSClient::fgkServerErrorString;
645 AliErrorGeneral("AliDCSClient::GetErrorString",
646 "Unknown error code!");
647 return "UnknownCode";