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.8 2007/10/17 17:43:02 acolla
19 nextMessage removed from client
21 Revision 1.7 2007/10/16 15:02:20 jgrosseo
22 fixed bug if zero values collected
24 Revision 1.6 2007/10/16 14:37:16 jgrosseo
25 changing to AMANDA protocol version 2
27 Revision 1.5 2007/10/05 12:40:55 acolla
29 Result error code added to AliDCSClient data members (it was "lost" with the new implementation of TMap* GetAliasValues and GetDPValues).
31 Revision 1.4 2007/09/14 16:46:14 jgrosseo
32 1) Connect and Close are called before and after each query, so one can
33 keep the same AliDCSClient object.
34 2) The splitting of a query is moved to GetDPValues/GetAliasValues.
35 3) Splitting interval can be specified in constructor
37 Revision 1.3 2007/09/11 16:42:02 jgrosseo
38 starting modifying AliDCSClient to transparently switch between single and multi query
39 first step: same alidcsclient can be used for several queries
41 Revision 1.2 2007/06/09 13:01:09 jgrosseo
42 Switching to retrieval of several DCS DPs at a time (multiDPrequest)
44 Revision 1.1 2006/11/06 14:22:47 jgrosseo
45 major update (Alberto)
46 o) reading of run parameters from the logbook
47 o) online offline naming conversion
48 o) standalone DCSclient package
50 Revision 1.6 2006/10/02 16:38:39 jgrosseo
53 storing of objects that failed to be stored to the grid before
54 interfacing of shuttle status table in daq system
56 Revision 1.5 2006/08/15 10:50:00 jgrosseo
57 effc++ corrections (alberto)
59 Revision 1.4 2006/07/04 14:59:57 jgrosseo
60 revision of AliDCSValue: Removed wrapper classes, reduced storage size per value by factor 2
62 Revision 1.3 2006/06/12 09:11:16 jgrosseo
63 coding conventions (Alberto)
65 Revision 1.2 2006/03/07 07:52:34 hristov
66 New version (B.Yordanov)
68 Revision 1.3 2005/11/17 17:47:34 byordano
69 TList changed to TObjArray
71 Revision 1.2 2005/11/17 14:43:23 byordano
74 Revision 1.1.1.1 2005/10/28 07:33:58 hristov
75 Initial import as subdirectory in AliRoot
77 Revision 1.1.1.1 2005/09/12 22:11:40 byordano
80 Revision 1.3 2005/08/30 10:53:23 byordano
81 some more descriptions added
86 // This class represents the AliDCSClient.
87 // The client used for data retrieval from DCS server.
88 // There are two way for retrieving data from the server.
89 // 1) asking for DP (DataPoint) - usually changed frequently.
90 // 2) asking for Alias (Alias) - alias should be the same through whole
93 // There are two type of read operations:
94 // Asking for single alias/dp or asking for set of aliases/dp
96 // In case of ServerError the coresponding error code and
97 // error string (description) could be got by GetServerErrorCode() and
98 // GetServerErrorString()
101 #include "AliDCSClient.h"
102 #include "AliDCSValue.h"
106 #include <TObjArray.h>
108 #include <TObjString.h>
110 #include <TTimeStamp.h>
112 ClassImp(AliDCSClient)
114 const char* AliDCSClient::fgkBadStateString = "BadState";
115 const char* AliDCSClient::fgkInvalidParameterString = "InvalidParameter";
116 const char* AliDCSClient::fgkTimeoutString = "Timeout";
117 const char* AliDCSClient::fgkBadMessageString = "BadMessage";
118 const char* AliDCSClient::fgkCommErrorString = "CommunicationError";
119 const char* AliDCSClient::fgkServerErrorString = "ServerError";
120 const char* AliDCSClient::fgkUnknownDPString = "UnknownAlias/DP";
122 //______________________________________________________________________
123 AliDCSClient::AliDCSClient(const char* host, Int_t port, UInt_t timeout,
124 Int_t retries, Int_t multiSplit):
125 fSocket(NULL), fHost(host), fPort(port), fTimeout(timeout), fRetries(retries), fMultiSplit(multiSplit),
126 fServerErrorCode(AliDCSMessage::kNoneError), fServerError(""), fResultErrorCode(0)
129 // host: DCS server host
130 // port: DCS server port
131 // timeout: in case of communication error or socket read/write this
132 // timeout will be used before the next try is made.
133 // retries: the number of retries after which the connection is
134 // is considered as invalid and error is returned.
138 //______________________________________________________________________
139 AliDCSClient::~AliDCSClient()
146 //______________________________________________________________________
147 Bool_t AliDCSClient::Connect()
149 // connects to the AMANDA server
156 while (tries < fRetries)
158 fSocket = new TSocket(fHost, fPort);
160 if (fSocket && fSocket->IsValid())
162 AliDebug(1, Form("%s *** Connected to %s:%d", TTimeStamp(time(0)).AsString("s"), fHost.Data(), fPort));
163 fSocket->SetOption(kNoBlock, 1);
167 AliError(Form(" *** Connection to AMANDA server failed Tried <%d> times ...", tries+1));
168 if(tries<fRetries-1) AliInfo(Form(" *** Waiting %d seconds before next retry.", sleeptime));
173 gSystem->Sleep(sleeptime);
180 //______________________________________________________________________
181 Int_t AliDCSClient::SendBuffer(const char* buffer, Int_t size)
183 // send buffer containing the message to the DCS server
188 while (sentSize < size && tries < fRetries) {
190 Int_t sResult = fSocket->Select(TSocket::kWrite, fTimeout*1000); //timeout for TSocket::Select is in msec
193 AliDebug(1, Form("Timeout! tries <%d> ...", tries));
197 } else if (sResult < 0) {
198 AliDebug(1, Form("Communication error <%d>!",
199 fSocket->GetErrorCode()));
200 return AliDCSClient::fgkCommError;
203 sResult = fSocket->SendRaw(buffer + sentSize, size - sentSize,
209 AliDebug(1, Form("Communication error <%d>!",
210 fSocket->GetErrorCode()));
211 return AliDCSClient::fgkCommError;
215 if (tries == fRetries) {
216 return AliDCSClient::fgkTimeout;
222 //______________________________________________________________________
223 Int_t AliDCSClient::ReceiveBuffer(char* buffer, Int_t size)
225 // Receive message from the DCS server and fill buffer
227 Int_t receivedSize = 0;
230 while (receivedSize < size && tries < fRetries) {
232 Int_t sResult = fSocket->Select(TSocket::kRead, fTimeout*1000); //timeout for TSocket::Select is in msec
235 AliDebug(1, Form("Time when timing out: %s Timeout value: %d seconds",TTimeStamp(time(0)).AsString("s"),fTimeout));
236 AliDebug(1, Form("Timeout! tries <%d> ...", tries));
240 } else if (sResult < 0) {
241 AliDebug(1, Form("Communication error <%d>",
242 fSocket->GetErrorCode()));
243 return AliDCSClient::fgkCommError;
246 sResult = fSocket->RecvRaw(buffer + receivedSize,
247 size - receivedSize, kDontBlock);
250 receivedSize += sResult;
252 AliDebug(1, Form("Communication error <%d>",
253 fSocket->GetErrorCode()));
254 return AliDCSClient::fgkCommError;
258 if (tries == fRetries) {
259 return AliDCSClient::fgkTimeout;
265 //______________________________________________________________________
266 Int_t AliDCSClient::SendMessage(AliDCSMessage& message)
268 // send message to the DCS server
270 message.StoreToBuffer();
272 AliDebug(2, "Sending message.\n");
275 return SendBuffer(message.GetMessage(), message.GetMessageSize());
278 //______________________________________________________________________
279 Int_t AliDCSClient::ReceiveMessage(AliDCSMessage& message)
281 // receive message from the DCS server
283 char header[HEADER_SIZE];
287 if ((sResult = ReceiveBuffer(header, HEADER_SIZE)) < 0) {
288 AliDebug(1, Form("Can't receive message header! Reason: %s",
289 GetErrorString(sResult)));
293 if (!message.SetRawHeader(header)) {
294 return AliDCSClient::fgkBadMessage;
297 if ((sResult = ReceiveBuffer(message.GetBody(),
298 message.GetBodySize())) < 0) {
300 AliDebug(1, Form("Can't receive message body! Reason: %s",
301 GetErrorString(sResult)));
305 message.LoadFromBuffer();
307 AliDebug(2, "Message received.");
310 return HEADER_SIZE + sResult;
313 //______________________________________________________________________
314 Int_t AliDCSClient::GetValues(AliDCSMessage::RequestType reqType,
315 const char* reqString, UInt_t startTime, UInt_t endTime, TObjArray* result)
317 // get array of DCS values from the DCS server
318 // reqString: alias name
319 // startTime, endTime: start time and end time of the query
320 // result: contains the array of retrieved AliDCSValue's
324 if (!IsConnected()) {
325 AliError("Not connected!");
326 return AliDCSClient::fgkBadState;
330 AliDCSMessage requestMessage;
331 requestMessage.CreateRequestMessage(reqType, startTime, endTime,
334 if ((sResult = SendMessage(requestMessage)) < 0) {
335 AliError(Form("Can't send request message! Reason: %s",
336 GetErrorString(sResult)));
341 Int_t receivedValues = 0;
346 sResult = ReceiveValueSet(result, tmp);
349 receivedValues += sResult;
354 return receivedValues;
357 //______________________________________________________________________
358 TMap* AliDCSClient::GetValues(AliDCSMessage::RequestType reqType,
359 const TSeqCollection* list, UInt_t startTime, UInt_t endTime,
360 Int_t startIndex, Int_t endIndex)
362 // get array of DCS values from the DCS server
363 // list, startTime, endTime: list of dp/alias names, start time and end time of the query
365 // Result: map containing the array of alias names. It will be filled with
366 // the values retrieved for each alias
368 TMap* result = new TMap;
371 if (endIndex < 0 || endIndex > list->GetEntries())
372 endIndex = list->GetEntries();
374 for (Int_t subsetBegin = startIndex; subsetBegin < endIndex; subsetBegin += fMultiSplit)
380 AliError("Not connected!");
382 fResultErrorCode = fgkBadState;
386 Int_t subsetEnd = subsetBegin + fMultiSplit;
387 if (subsetEnd > endIndex)
388 subsetEnd = endIndex;
390 AliDCSMessage requestMessage;
391 if (fMultiSplit == 1)
395 TObjString* aRequest = (TObjString*) list->At(subsetBegin);
396 requestMessage.CreateRequestMessage(reqType, startTime, endTime, aRequest->String());
402 requestMessage.CreateMultiRequestMessage(reqType,
405 for (Int_t i=subsetBegin; i<subsetEnd; i++)
407 TObjString* aRequest = (TObjString*) list->At(i);
408 if (!requestMessage.AddRequestString(aRequest->String()))
411 fResultErrorCode = fgkBadMessage;
417 if ((fResultErrorCode = SendMessage(requestMessage)) < 0)
419 AliError(Form("Can't send request message! Reason: %s",
420 GetErrorString(fResultErrorCode)));
428 TObjArray* resultSet = new TObjArray();
429 resultSet->SetOwner(1);
431 Int_t ownerIndex = -1;
432 fResultErrorCode = ReceiveValueSet(resultSet, ownerIndex);
434 if (fResultErrorCode < 0)
436 if (fResultErrorCode == fgkUnknownDP)
438 AliError(Form("%s",fServerError.Data()));
441 AliError("Can't get values");
444 result->DeleteValues();
456 TObjString* aRequest = (TObjString*) list->At(ownerIndex + subsetBegin);
457 //AliInfo(Form("Received %d values for entry %d, that is %s", resultSet->GetEntries(),
458 // ownerIndex + subsetBegin, aRequest->String().Data()));
460 TObjArray* target = dynamic_cast<TObjArray*> (result->GetValue(aRequest));
463 target->AddAll(resultSet);
464 //AliInfo(Form("Now we have %d entries", target->GetEntries()));
465 resultSet->SetOwner(0);
469 result->Add(aRequest, resultSet);
475 TObjArray* example = (TObjArray*) result->GetValue(list->At(subsetBegin));
477 nValues = example->GetEntries();
478 AliInfo(Form("Retrieved entries %d..%d (total %d..%d); E.g. %s has %d values collected",
479 subsetBegin, subsetEnd-1, startIndex, endIndex-1, list->At(subsetBegin)->GetName(), nValues));
485 //______________________________________________________________________
486 Int_t AliDCSClient::ReceiveValueSet(TObjArray* result, Int_t& ownerIndex)
488 // receive set of values
490 AliDCSMessage message;
491 Int_t sResult = ReceiveMessage(message);
494 AliError(Form("Can't receive message! Reason: %s", GetErrorString(sResult)));
498 if (message.GetType() == AliDCSMessage::kResultSet)
500 // this was the last message
501 ownerIndex = message.GetOwnerIndex();
505 sResult = message.GetValues(result);
507 // AliDCSMessage nextMessage;
508 // nextMessage.CreateNextMessage();
510 // if ((fResultErrorCode = SendMessage(nextMessage)) < 0)
512 // AliError(Form("Can't send next message! Reason: %s",
513 // GetErrorString(fResultErrorCode)));
515 // return AliDCSClient::fgkCommError;
520 else if (message.GetType() == AliDCSMessage::kError)
522 fServerErrorCode = message.GetErrorCode();
523 fServerError = message.GetErrorString();
525 return AliDCSClient::fgkServerError;
528 else if (message.GetType() == AliDCSMessage::kUnknownDP)
530 fServerError = message.GetErrorString();
532 return AliDCSClient::fgkUnknownDP;
536 AliError("Bad message type received!");
537 return AliDCSClient::fgkBadMessage;
540 //______________________________________________________________________
541 Int_t AliDCSClient::GetDPValues(const char* dpName, UInt_t startTime,
542 UInt_t endTime, TObjArray* result)
545 // Reads a values from the server which correspond to this
546 // DataPoint (dpName) in time interval (startTime - endTime).
547 // result: Collection of AliDCSValue which contains the read values.
550 // If >= 0 , the number of values read.
551 // if < 0, the error code which has occured during the read.
554 return GetValues(AliDCSMessage::kDPName,
555 dpName, startTime, endTime, result);
558 //______________________________________________________________________
559 Int_t AliDCSClient::GetAliasValues(const char* alias, UInt_t startTime,
560 UInt_t endTime, TObjArray* result)
563 // Reads a values from the server which correspond to this
564 // alias (alias) in time interval (startTime - endTime).
565 // result: Collection of AliDCSValue which contains the read values.
568 // If >= 0 , the number of values read.
569 // if < 0, the error code which has occured during the read.
572 return GetValues(AliDCSMessage::kAlias,
573 alias, startTime, endTime, result);
576 //______________________________________________________________________
577 TMap* AliDCSClient::GetDPValues(const TSeqCollection* dpList, UInt_t startTime, UInt_t endTime,
578 Int_t startIndex, Int_t endIndex)
581 // For every entry (fron startIndex to endIndex) in dpList (which must be TObjString)
582 // reads a valueSet. The key represents particular DataPoint to be read.
583 // For all DataPoints time interval (startTime - endTime) is used.
584 // After the read, the correspoding value for every key is a
585 // TObjArray - collection of AliDCSValue, or result is an empty map in
589 // TMap of results, 0 in case of failure
592 return GetValues(AliDCSMessage::kDPName, dpList, startTime, endTime, startIndex, endIndex);
595 //______________________________________________________________________
596 TMap* AliDCSClient::GetAliasValues(const TSeqCollection* aliasList, UInt_t startTime, UInt_t endTime,
597 Int_t startIndex, Int_t endIndex)
600 // For every entry (fron startIndex to endIndex) in dpList (which must be TObjString)
601 // reads a valueSet. The key represents particular Alias to be read.
602 // For all aliases time interval (startTime - endTime) is used.
603 // After the read, the correspoding value for every key is a
604 // TObjArray - collection of AliDCSValue, or result is an empty map in
608 // TMap of results, 0 in case of failure
611 return GetValues(AliDCSMessage::kAlias, aliasList, startTime, endTime, startIndex, endIndex);
614 //______________________________________________________________________
615 Bool_t AliDCSClient::IsConnected()
618 // Returns kTRUE if there is a valid connection to the server.
622 return fSocket->IsValid();
628 //______________________________________________________________________
629 void AliDCSClient::Close()
632 // Close the connection.
636 AliDebug(1, Form("%s *** Closing connection to %s:%d", TTimeStamp(time(0)).AsString("s"), fHost.Data(), fPort));
643 //______________________________________________________________________
644 const char* AliDCSClient::GetErrorString(Int_t code)
647 // Returns a short string describing the error code.
648 // code: the error code.
653 case AliDCSClient::fgkBadState:
654 return AliDCSClient::fgkBadStateString;
656 case AliDCSClient::fgkInvalidParameter:
657 return AliDCSClient::fgkInvalidParameterString;
659 case AliDCSClient::fgkTimeout:
660 return AliDCSClient::fgkTimeoutString;
662 case AliDCSClient::fgkBadMessage:
663 return AliDCSClient::fgkBadMessageString;
665 case AliDCSClient::fgkCommError:
666 return AliDCSClient::fgkCommErrorString;
668 case AliDCSClient::fgkServerError:
669 return AliDCSClient::fgkServerErrorString;
671 case AliDCSClient::fgkUnknownDP:
672 return AliDCSClient::fgkUnknownDPString;
675 AliErrorGeneral("AliDCSClient::GetErrorString",
676 "Unknown error code!");
677 return "UnknownCode";