Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #if defined(__clang__) && __clang_major__ >= 19
83 # pragma clang diagnostic push
84 # pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
85 #endif
86  //Now try pinging the server
87  clnt_stat ret = clnt_call(m_client, NULLPROC, (xdrproc_t)xdr_void, NULL,
88  (xdrproc_t)xdr_void, NULL, GetTimeout());
89 #if defined(__clang__) && __clang_major__ >= 19
90 # pragma clang diagnostic pop
91 #endif
92 #if __GNUC__ >= 8
93 # pragma GCC diagnostic pop
94 #endif
95 
96  //And check for the result
97  if (ret != RPC_SUCCESS){
98  ATH_MSG_WARNING( "Failed calling the server on host " << m_hostName << " : " << clnt_sperrno(ret) );
99  //Also destroy the client in this case - otherwise we might be stranded
100  //with an invalid client object
101  ReleaseClient().ignore();
102  //and return failure
103  return StatusCode::FAILURE;
104  }
105 
106  //Be verbose on success
107  ATH_MSG_VERBOSE( "Successfully connected to server on host " << m_hostName );
108 
109  return StatusCode::SUCCESS;
110  }
111 
115  struct timeval ExternalONCRPCServerSvc::GetTimeout(){
116  //Declare return value
117  struct timeval timeout = { 0, 0 };
118  //Catch for negatives
119  if (m_timeOut < 0) return timeout;
120  //Get seconds as integer part
121  timeout.tv_sec = static_cast<long>(m_timeOut);
122  //Get microseconds as fractional part
123  timeout.tv_usec = static_cast<long>((m_timeOut - timeout.tv_sec)*1e6);
124  //Finally return it
125  return timeout;
126  }
127 
132  //Destroy the client
133  if (m_client) clnt_destroy(m_client);
134  //And set pointer to NULL
135  m_client=NULL;
136  //For now always return success
137  return StatusCode::SUCCESS;
138  }
139 
145 
146  ATH_MSG_VERBOSE( "Finalize()" );
147 
151  ReleaseClient().ignore();
152 
153  return StatusCode::SUCCESS;
154  }
155 
160  StatusCode ExternalONCRPCServerSvc::UpdateEventForStream( const EventStreamID& evtStreamID, const std::string & eventStr) {
161 
162  ATH_MSG_VERBOSE( "UpdateEventForStream()" );
163 
164  //Check that the event stream id is valid
165  if (!evtStreamID.isValid()){
166  ATH_MSG_ERROR( "Invalid event stream identifier - cannot add event" );
167  return StatusCode::FAILURE;
168  }
169 
170  //Now create an event C-struct that we can send to the server
171  //NOTE: the struct has to be properly initialized
172  Event event;
173  event.isAvailable = false ; event.isIdentical = false ; event.isCompressed = false ;
174  event.RunNumber = evtStreamID.RunNumber();
175  event.EventNumber=evtStreamID.EventNumber();
176  event.StreamName=strdup(evtStreamID.StreamName().c_str());
177  event.NBytes = strlen(eventStr.c_str())+1;
178  event.EventData = strdup(eventStr.c_str());
179 
180  ATH_MSG_VERBOSE( "Created event structure for event " << event.EventNumber
181  << " from run " << event.RunNumber
182  << " to be put in stream " << event.StreamName
183  << " with " << event.NBytes << " bytes" );
184 
185  //Try to get a client
186  if (GetClient().isFailure()) {
187  ATH_MSG_WARNING( "Failed obtaining a client for sever on host " << m_hostName );
188  ATH_MSG_WARNING( " while updating stream " << evtStreamID.StreamName()
189  << " with event " << evtStreamID.EventNumber()
190  << " from run " << evtStreamID.RunNumber() );
191  return StatusCode::FAILURE;
192  }
193 
194  //Call the client to update the event
195  bool isSuccess = false;
196  clnt_stat ret = clnt_call(m_client, ONCRPC_SETEVENT_PROC, (xdrproc_t)xdr_event, (caddr_t)&event,
197  (xdrproc_t)xdr_bool,(caddr_t)&isSuccess, GetTimeout());
198 
199  //And check for the result
200  if (ret != RPC_SUCCESS){
201  ATH_MSG_WARNING( "Failed calling the server on host " << m_hostName << " : " << clnt_sperrno(ret) );
202  ATH_MSG_WARNING( " while updating stream " << evtStreamID.StreamName()
203  << " with event " << evtStreamID.EventNumber()
204  << " from run " << evtStreamID.RunNumber() );
205  return StatusCode::FAILURE;
206  }
207 
208  //Check if the server managed to update the event from return value
209  if (!isSuccess){
210  ATH_MSG_WARNING( "Server on host " << m_hostName << " returned failure " );
211  ATH_MSG_WARNING( " while updating stream " << evtStreamID.StreamName()
212  << " with event " << evtStreamID.EventNumber()
213  << " from run " << evtStreamID.RunNumber() );
214  return StatusCode::FAILURE;
215  }
216 
217  //Be verbose on success
218  ATH_MSG_DEBUG( "Server on host " << m_hostName << " returned success " );
219  ATH_MSG_DEBUG( " while updating stream " << evtStreamID.StreamName()
220  << " with event " << evtStreamID.EventNumber()
221  << " from run " << evtStreamID.RunNumber() );
222 
223  //Finally free the memory allocated for the event structure
224  xdr_free((xdrproc_t)xdr_event,(caddr_t)&event);
225 
226  return StatusCode::SUCCESS;
227  }
228 
229 } //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)
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:115
ONCRPCXDRProcs.h
JiveXML::EventStreamID
For the client-server communication, each event is uniquely identified by the run number,...
Definition: EventStream.h:19
JiveXML::EventStreamID::StreamName
const std::string & StreamName() const
Definition: EventStream.h:43
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:22
ONCRPC_SETEVENT_PROC
#define ONCRPC_SETEVENT_PROC
Definition: ONCRPCServer.h:35
JiveXML::ExternalONCRPCServerSvc::m_timeOut
double m_timeOut
Definition: ExternalONCRPCServerSvc.h:56
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
JiveXML::ExternalONCRPCServerSvc::ReleaseClient
StatusCode ReleaseClient()
Destroy the private client handle.
Definition: ExternalONCRPCServerSvc.cxx:131
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:160
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:144
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
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:36
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
JiveXML::ExternalONCRPCServerSvc::m_hostName
std::string m_hostName
Definition: ExternalONCRPCServerSvc.h:53