ATLAS Offline Software
ExternalONCRPCServerSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "JiveXML/ONCRPCServer.h"
8 
9 #include <rpc/rpc.h>
10 
11 namespace JiveXML {
12 
13 
17  ExternalONCRPCServerSvc::ExternalONCRPCServerSvc( const std::string& name, ISvcLocator* sl) :
18  base_class ( name, sl ),
19  m_client(NULL)
20  {
21  //declare properties
22  declareProperty("Hostname",m_hostName="localhost","The name of the host the external server is running on [ default: \"localhost\" ]");
23  declareProperty("Timeout",m_timeOut=10,"Timeout when calling the server in seconds [ default: 10 ]");
24  }
25 
30  ATH_MSG_VERBOSE( "Destructor() " );
31  }
32 
38  ATH_MSG_VERBOSE( "Initialize()" );
39 
40  //Initialize Service base
41  if (Service::initialize().isFailure()) return StatusCode::FAILURE;
42 
43  //Initialize message stream level
44  msg().setLevel(outputLevel());
45  ATH_MSG_VERBOSE( "Output level is set to " << (int)msg().level() );
46 
50  GetClient().ignore();
51 
52  return StatusCode::SUCCESS;
53  }
54 
61 
62  ATH_MSG_VERBOSE( "GetClient()" );
63 
64  //Try to create client if not there yet
65 #ifndef __APPLE__
66  if (!m_client) m_client = clnt_create(m_hostName.c_str(), ONCRPCSERVERPROG,ONCRPCSERVERVERS, "tcp");
67 #else
68  char proto[] = "tcp";
69  if (!m_client) m_client = clnt_create((char*)m_hostName.c_str(), ONCRPCSERVERPROG,ONCRPCSERVERVERS, proto);
70 #endif
71  //Fail if we still don't have a client
72  if (!m_client){
73  ATH_MSG_WARNING( "Unable to create client for server on host " << m_hostName << " using TCP/IP " );
74  return StatusCode::FAILURE;
75  }
76 
77 // xdr_void is defined inconsistently in xdr.h and gets a warning from gcc8.
78 #if __GNUC__ >= 8
79 # pragma GCC diagnostic push
80 # pragma GCC diagnostic ignored "-Wcast-function-type"
81 #endif
82  //Now try pinging the server
83  clnt_stat ret = clnt_call(m_client, NULLPROC, (xdrproc_t)xdr_void, NULL,
84  (xdrproc_t)xdr_void, NULL, GetTimeout());
85 #if __GNUC__ >= 8
86 # pragma GCC diagnostic pop
87 #endif
88 
89  //And check for the result
90  if (ret != RPC_SUCCESS){
91  ATH_MSG_WARNING( "Failed calling the server on host " << m_hostName << " : " << clnt_sperrno(ret) );
92  //Also destroy the client in this case - otherwise we might be stranded
93  //with an invalid client object
94  ReleaseClient().ignore();
95  //and return failure
96  return StatusCode::FAILURE;
97  }
98 
99  //Be verbose on success
100  ATH_MSG_VERBOSE( "Successfully connected to server on host " << m_hostName );
101 
102  return StatusCode::SUCCESS;
103  }
104 
108  struct timeval ExternalONCRPCServerSvc::GetTimeout(){
109  //Declare return value
110  struct timeval timeout = { 0, 0 };
111  //Catch for negatives
112  if (m_timeOut < 0) return timeout;
113  //Get seconds as integer part
114  timeout.tv_sec = static_cast<long>(m_timeOut);
115  //Get microseconds as fractional part
116  timeout.tv_usec = static_cast<long>((m_timeOut - timeout.tv_sec)*1e6);
117  //Finally return it
118  return timeout;
119  }
120 
125  //Destroy the client
126  if (m_client) clnt_destroy(m_client);
127  //And set pointer to NULL
128  m_client=NULL;
129  //For now always return success
130  return StatusCode::SUCCESS;
131  }
132 
138 
139  ATH_MSG_VERBOSE( "Finalize()" );
140 
144  ReleaseClient().ignore();
145 
146  return StatusCode::SUCCESS;
147  }
148 
153  StatusCode ExternalONCRPCServerSvc::UpdateEventForStream( const EventStreamID& evtStreamID, const std::string & eventStr) {
154 
155  ATH_MSG_VERBOSE( "UpdateEventForStream()" );
156 
157  //Check that the event stream id is valid
158  if (!evtStreamID.isValid()){
159  ATH_MSG_ERROR( "Invalid event stream identifier - cannot add event" );
160  return StatusCode::FAILURE;
161  }
162 
163  //Now create an event C-struct that we can send to the server
164  //NOTE: the struct has to be properly initialized
165  Event event;
166  event.isAvailable = false ; event.isIdentical = false ; event.isCompressed = false ;
167  event.RunNumber = evtStreamID.RunNumber();
168  event.EventNumber=evtStreamID.EventNumber();
169  event.StreamName=strdup(evtStreamID.StreamName().c_str());
170  event.NBytes = strlen(eventStr.c_str())+1;
171  event.EventData = strdup(eventStr.c_str());
172 
173  ATH_MSG_VERBOSE( "Created event structure for event " << event.EventNumber
174  << " from run " << event.RunNumber
175  << " to be put in stream " << event.StreamName
176  << " with " << event.NBytes << " bytes" );
177 
178  //Try to get a client
179  if (GetClient().isFailure()) {
180  ATH_MSG_WARNING( "Failed obtaining a client for sever on host " << m_hostName );
181  ATH_MSG_WARNING( " while updating stream " << evtStreamID.StreamName()
182  << " with event " << evtStreamID.EventNumber()
183  << " from run " << evtStreamID.RunNumber() );
184  return StatusCode::FAILURE;
185  }
186 
187  //Call the client to update the event
188  bool isSuccess = false;
189  clnt_stat ret = clnt_call(m_client, ONCRPC_SETEVENT_PROC, (xdrproc_t)xdr_event, (caddr_t)&event,
190  (xdrproc_t)xdr_bool,(caddr_t)&isSuccess, GetTimeout());
191 
192  //And check for the result
193  if (ret != RPC_SUCCESS){
194  ATH_MSG_WARNING( "Failed calling the server on host " << m_hostName << " : " << clnt_sperrno(ret) );
195  ATH_MSG_WARNING( " while updating stream " << evtStreamID.StreamName()
196  << " with event " << evtStreamID.EventNumber()
197  << " from run " << evtStreamID.RunNumber() );
198  return StatusCode::FAILURE;
199  }
200 
201  //Check if the server managed to update the event from return value
202  if (!isSuccess){
203  ATH_MSG_WARNING( "Server on host " << m_hostName << " returned failure " );
204  ATH_MSG_WARNING( " while updating stream " << evtStreamID.StreamName()
205  << " with event " << evtStreamID.EventNumber()
206  << " from run " << evtStreamID.RunNumber() );
207  return StatusCode::FAILURE;
208  }
209 
210  //Be verbose on success
211  ATH_MSG_DEBUG( "Server on host " << m_hostName << " returned success " );
212  ATH_MSG_DEBUG( " while updating stream " << evtStreamID.StreamName()
213  << " with event " << evtStreamID.EventNumber()
214  << " from run " << evtStreamID.RunNumber() );
215 
216  //Finally free the memory allocated for the event structure
217  xdr_free((xdrproc_t)xdr_event,(caddr_t)&event);
218 
219  return StatusCode::SUCCESS;
220  }
221 
222 } //namespace
JiveXML::ExternalONCRPCServerSvc::GetClient
StatusCode GetClient()
Obtain a client handle by.
Definition: ExternalONCRPCServerSvc.cxx:60
JiveXML::EventStreamID::EventNumber
unsigned long EventNumber() const
Definition: EventStream.h:41
initialize
void initialize()
Definition: run_EoverP.cxx:894
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
JiveXML::ExternalONCRPCServerSvc::ExternalONCRPCServerSvc
ExternalONCRPCServerSvc(const std::string &name, ISvcLocator *sl)
Default constructor.
Definition: ExternalONCRPCServerSvc.cxx:17
JiveXML::ExternalONCRPCServerSvc::initialize
virtual StatusCode initialize() override
Gaudi default methods.
Definition: ExternalONCRPCServerSvc.cxx:37
ONCRPCServer.h
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
ret
T ret(T t)
Definition: rootspy.cxx:260
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
JiveXML::ExternalONCRPCServerSvc::m_client
CLIENT * m_client
Definition: ExternalONCRPCServerSvc.h:59
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
JiveXML::xdr_event
bool_t xdr_event(XDR *xdrsp, Event *event)
De-/Encoding of Event_t.
Definition: ONCRPCXDRProcs.cxx:51
JiveXML::ExternalONCRPCServerSvc
This athena service will provide a link to an external ONCRPC server, e.g running as standalone appli...
Definition: ExternalONCRPCServerSvc.h:23
JiveXML::EventStreamID::isValid
bool isValid() const
Definition: EventStream.h:55
JiveXML::ExternalONCRPCServerSvc::GetTimeout
struct timeval GetTimeout()
Convert timeout double in seconds to struct timeval.
Definition: ExternalONCRPCServerSvc.cxx:108
ONCRPCXDRProcs.h
JiveXML::EventStreamID
For the client-server communication, each event is uniquely identified by the run number,...
Definition: EventStream.h:19
ONCRPCSERVERPROG
#define ONCRPCSERVERPROG
Definition: ONCRPCServer.h:30
JiveXML::ExternalONCRPCServerSvc::~ExternalONCRPCServerSvc
virtual ~ExternalONCRPCServerSvc()
Destructor.
Definition: ExternalONCRPCServerSvc.cxx:29
JiveXML
This header is shared inbetween the C-style server thread and the C++ Athena ServerSvc.
Definition: BadLArRetriever.cxx:21
ONCRPC_SETEVENT_PROC
#define ONCRPC_SETEVENT_PROC
Definition: ONCRPCServer.h:35
JiveXML::ExternalONCRPCServerSvc::m_timeOut
double m_timeOut
Definition: ExternalONCRPCServerSvc.h:56
JiveXML::EventStreamID::StreamName
std::string StreamName() const
Definition: EventStream.h:43
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
JiveXML::ExternalONCRPCServerSvc::ReleaseClient
StatusCode ReleaseClient()
Destroy the private client handle.
Definition: ExternalONCRPCServerSvc.cxx:124
JiveXML::ExternalONCRPCServerSvc::UpdateEventForStream
virtual StatusCode UpdateEventForStream(const EventStreamID &evtStreamID, const std::string &event) override
Put this event as new current event for stream given by name.
Definition: ExternalONCRPCServerSvc.cxx:153
python.TrigPSCPythonDbSetup.outputLevel
outputLevel
Definition: TrigPSCPythonDbSetup.py:30
ExternalONCRPCServerSvc.h
ONCRPCSERVERVERS
#define ONCRPCSERVERVERS
Definition: ONCRPCServer.h:31
JiveXML::ExternalONCRPCServerSvc::finalize
virtual StatusCode finalize() override
Finalize - called once at the end.
Definition: ExternalONCRPCServerSvc.cxx:137
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
JiveXML::Event_t
Definition: ONCRPCServer.h:66
JiveXML::EventStreamID::RunNumber
unsigned int RunNumber() const
Definition: EventStream.h:42
python.TrigInDetArtSteps.timeout
timeout
Definition: TrigInDetArtSteps.py:35
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
JiveXML::ExternalONCRPCServerSvc::m_hostName
std::string m_hostName
Definition: ExternalONCRPCServerSvc.h:53