ATLAS Offline Software
Loading...
Searching...
No Matches
JiveXML::JiveXMLServer Class Reference

#include <JiveXMLServer.h>

Inheritance diagram for JiveXML::JiveXMLServer:
Collaboration diagram for JiveXML::JiveXMLServer:

Public Member Functions

 JiveXMLServer (int port=0)
 Constructor.
virtual ~JiveXMLServer ()
 Destructor.
IEventReceiver methods
virtual StatusCode UpdateEventForStream (const EventStreamID &evtStreamID, const std::string &event) override
 Put this event as new current event for stream given by name.
IEventServer methods
virtual std::vector< std::string > GetStreamNames () const override
 get the names of all the streams
virtual const EventStreamID GetEventStreamID (const std::string &streamName) const override
 get the current EventStreamID for a particular stream
virtual const std::string GetEvent (const EventStreamID &evtStreamID) const override
 get the current event for a particular stream
virtual int GetState () const override
 get the Status of the application
virtual void Message (const MSG::Level level, const std::string &msg) const override
 This function is exposed to allow using ERS messaging service from other threads.
virtual MSG::Level LogLevel () const override
 Get the logging level.

Event serving thread control

static OWLSemaphore m_lock ATLAS_THREAD_SAFE
static std::atomic< int > m_receivedSignal {0}
int m_portNumber
EventStreamMap m_eventStreamMap
pthread_mutex_t m_accessLock ATLAS_THREAD_SAFE
pthread_t m_ServerThreadHandle
bool m_runServerThread
StatusCode StartServingThread ()
 Start the serving thread.
StatusCode StopServingThread ()
 Stop the serving thread.
virtual bool GetRunServerFlag () const override
 The server thread will stop once this flag is set to false.
virtual void ServerThreadStopped () override
 Callback whenever the server thread is stopped.
void Wait ()
 Wait for the server finish.
static void signalHandler (int signum)
 When the signal handler is called, switch the lock to the post condition.

Detailed Description

Definition at line 38 of file JiveXMLServer.h.

Constructor & Destructor Documentation

◆ JiveXMLServer()

JiveXMLServer::JiveXMLServer ( int port = 0)

Constructor.

Definition at line 41 of file JiveXMLServer.cxx.

41 :
42 m_portNumber(port){
43
44 //Make sure ServerThread does not start unexpectedly
45 m_runServerThread = false ;
46
47 //And then start it
48 StartServingThread().ignore();
49
50 //Also register the signal handlers
53 }
static void signalHandler(int signum)
When the signal handler is called, switch the lock to the post condition.
StatusCode StartServingThread()
Start the serving thread.

◆ ~JiveXMLServer()

JiveXMLServer::~JiveXMLServer ( )
virtual

Destructor.

Definition at line 58 of file JiveXMLServer.cxx.

58 {
59
60 //Just stop the serving thread
61 StopServingThread().ignore();
62 }
StatusCode StopServingThread()
Stop the serving thread.

Member Function Documentation

◆ GetEvent()

const std::string JiveXMLServer::GetEvent ( const EventStreamID & evtStreamID) const
overridevirtual

get the current event for a particular stream

Return the event for a given stream.

Implements JiveXML::IEventServer.

Definition at line 364 of file JiveXMLServer.cxx.

364 {
365
366 //Obtain an exclusive access lock
367 int retVal = pthread_mutex_lock(&m_accessLock);
368 if ( retVal != 0 ){
369 ERS_ERROR("Unable to obtain access lock to get event: " << fmterror(retVal));
370 return std::string("");
371 }
372
373
374 // Search the entry in the map
375 EventStreamMap::const_iterator MapItr = m_eventStreamMap.find(evtStreamID);
376
377 //Initialize with an empty event stream
378 std::string event;
379
380 //If the element is found, get a copy of the found event string
381 if ( MapItr != m_eventStreamMap.end()){
382 event = std::string((*MapItr).second);
383 }
384
385 //Release the lock
386 retVal = pthread_mutex_unlock(&m_accessLock);
387 if ( retVal != 0 )
388 ERS_ERROR("Unable to release access lock after getting stream event: " << fmterror(retVal));
389
390 return event;
391 }
#define ERS_ERROR(message)
EventStreamMap m_eventStreamMap

◆ GetEventStreamID()

const EventStreamID JiveXMLServer::GetEventStreamID ( const std::string & streamName) const
overridevirtual

get the current EventStreamID for a particular stream

Return the EventStreamID for the last event of a given stream.

Implements JiveXML::IEventServer.

Definition at line 332 of file JiveXMLServer.cxx.

332 {
333
334 //Obtain an exclusive access lock
335 int retVal = pthread_mutex_lock(&m_accessLock);
336 if ( retVal != 0 ){
337 ERS_ERROR("Unable to obtain access lock to get stream ID: " << fmterror(retVal));
338 return EventStreamID("");
339 }
340
341 // Search the entry in the map
342 EventStreamMap::const_iterator MapItr = m_eventStreamMap.find(EventStreamID(StreamName));
343
344 //Initialize with an invalid event stream identifier
345 EventStreamID streamID = EventStreamID("");
346
347 //If the element is found, get a copy of the found event stream identifier
348 if ( MapItr != m_eventStreamMap.end()){
349 streamID = EventStreamID((*MapItr).first);
350 }
351
352 //Release the lock
353 retVal = pthread_mutex_unlock(&m_accessLock);
354 if ( retVal != 0 )
355 ERS_ERROR("Unable to release access lock after getting stream ID: " << fmterror(retVal));
356
357 return streamID;
358
359 }

◆ GetRunServerFlag()

virtual bool JiveXML::JiveXMLServer::GetRunServerFlag ( ) const
inlineoverridevirtual

The server thread will stop once this flag is set to false.

Implements JiveXML::IEventServer.

Definition at line 80 of file JiveXMLServer.h.

80{ return m_runServerThread; };

◆ GetState()

int JiveXMLServer::GetState ( ) const
overridevirtual

get the Status of the application

Return the current athena state.

at the moement return a fixed value - will change this to some timeout condition soon.

Implements JiveXML::IEventServer.

Definition at line 286 of file JiveXMLServer.cxx.

286 {
291 return 3;
292 }

◆ GetStreamNames()

std::vector< std::string > JiveXMLServer::GetStreamNames ( ) const
overridevirtual

get the names of all the streams

Return an array with all the stream names.

Implements JiveXML::IEventServer.

Definition at line 297 of file JiveXMLServer.cxx.

297 {
298
299 //Create a vector that can be returned
300 std::vector<std::string> StreamNames;
301
302 //Obtain an exclusive access lock
303 int retVal = pthread_mutex_lock(&m_accessLock);
304 if ( retVal != 0 ){
305 ERS_ERROR("Unable to obtain access lock to get stream names: " << fmterror(retVal));
306 return StreamNames;
307 }
308
309 // Iterate over map to get entries
310 EventStreamMap::const_iterator MapItr = m_eventStreamMap.begin();
311 for ( ; MapItr != m_eventStreamMap.end(); ++MapItr){
312
313 //Get the EventStreamID object
314 EventStreamID EvtStrID = (*MapItr).first;
315
316 //Add the name of this EventStreamID to the list of stream names
317 StreamNames.push_back(EvtStrID.StreamName());
318 }
319
320 //Release the lock
321 retVal = pthread_mutex_unlock(&m_accessLock);
322 if ( retVal != 0 )
323 ERS_ERROR("Unable to release access lock after getting stream names: " << fmterror(retVal));
324
325 //Return the list of names
326 return StreamNames;
327 }

◆ LogLevel()

MSG::Level JiveXMLServer::LogLevel ( ) const
overridevirtual

Get the logging level.

Currently only used to suppress client hostname lookup if not in debug mode.

Implements JiveXML::IMessage.

Definition at line 408 of file JiveXMLServer.cxx.

408 {
409 //set to fixed value for now
410 return MSG::DEBUG;
411 }

◆ Message()

void JiveXMLServer::Message ( const MSG::Level level,
const std::string & msg ) const
overridevirtual

This function is exposed to allow using ERS messaging service from other threads.

Deliver a message - possibly from another thread - to ERS;.

Implements JiveXML::IMessage.

Definition at line 396 of file JiveXMLServer.cxx.

396 {
397 //Deliver message to the proper stream
398 if (level <= MSG::DEBUG) ERS_REPORT_IMPL( ers::debug, ers::Message, msg, level);
399 if (level == MSG::INFO) ERS_REPORT_IMPL( ers::info, ers::Message, msg, );
400 if (level == MSG::WARNING) ERS_REPORT_IMPL( ers::warning, ers::Message, msg, );
401 if (level == MSG::ERROR) ERS_REPORT_IMPL( ers::error, ers::Message, msg, );
402 if (level >= MSG::FATAL) ERS_REPORT_IMPL( ers::fatal, ers::Message, msg, );
403 }
MsgStream & msg
Definition testRead.cxx:32

◆ ServerThreadStopped()

void JiveXMLServer::ServerThreadStopped ( )
overridevirtual

Callback whenever the server thread is stopped.

When the server thread stopped, we will also call the the signal handler with the special value -1.

Implements JiveXML::IServer.

Definition at line 199 of file JiveXMLServer.cxx.

199 {
200 //call the signal handler, so we will also reach post condition
201 signalHandler(-1);
202 }

◆ signalHandler()

void JiveXMLServer::signalHandler ( int signum)
staticprivate

When the signal handler is called, switch the lock to the post condition.

Definition at line 176 of file JiveXMLServer.cxx.

176 {
177 //Store signal
179 //finish semaphore lock
180 m_lock.post();
181 }
static std::atomic< int > m_receivedSignal

◆ StartServingThread()

StatusCode JiveXMLServer::StartServingThread ( )

Start the serving thread.

Create the server by.

  • passing a this-pointer as an argument
  • setting the thread-running flag
  • starting the server thread

Definition at line 70 of file JiveXMLServer.cxx.

70 {
71
72 ERS_DEBUG(MSG::VERBOSE,"StartServingThread()");
73
74 //Initialize access lock mechanism to ensure that the data map is never
75 //accessed by more than one thread at the same time. NULL means default
76 int retVal = pthread_mutex_init(&m_accessLock,NULL);
77 if (retVal != 0){
78 ERS_WARNING("Unable to initialize access lock while starting server: " << fmterror(retVal));
79 return StatusCode::FAILURE;
80 }
81
82 //The arguments passed on to the server - create new object on the heap that
83 //is persistent through the lifetime of the thread
85
86 //set runServer flag to true, so the thread will start
87 m_runServerThread = true ;
88
89 //create thread itself
90 if ( ( pthread_create(&m_ServerThreadHandle, NULL , &JiveXML::ONCRPCServerThread, (void *) args )) != 0){
91
92 //Create thread failed
93 ERS_WARNING("Thread creation failed");
94 return StatusCode::FAILURE;
95 }
96
97 return StatusCode::SUCCESS;
98
99 }
#define ERS_WARNING(message)
struct ServerThreadArguments_t ServerThreadArguments
Arguments handed over fromt the main (Athena) thread to the server thread.
void * ONCRPCServerThread(void *args)
This is the actual server thread, which takes above arguments.

◆ StopServingThread()

StatusCode JiveXMLServer::StopServingThread ( )

Stop the serving thread.

Stop the server by.

  • unsetting the thread-running flag
  • waiting till the server thread has finished
  • destroying the thread handle

Ping the server which will cause another request Otherwise the server won't update its loop condition

Definition at line 107 of file JiveXMLServer.cxx.

107 {
108
109 ERS_DEBUG(MSG::VERBOSE,"StopServingThread()");
110
115
116 //Create a client - this will already cause an update of the file
117 //descriptors on the sockets
118 CLIENT* client = clnt_create("localhost", ONCRPCSERVERPROG,ONCRPCSERVERVERS, "tcp");
119 if (!client){
120 ERS_ERROR("Unable to create shutdown client for local server");
121 return StatusCode::FAILURE;
122 }
123
124 // Next unset the runServerThread flag, which will cause the loop to stop
125 // This needs to happen after client creation, otherwise the next call won't
126 // be serverd anymore
127 m_runServerThread = false ;
128
129 // Now issue the call with a timeout
130 struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0;
131// xdr_void is defined inconsistently in xdr.h and gets a warning from gcc8.
132#if __GNUC__ >= 8
133# pragma GCC diagnostic push
134# pragma GCC diagnostic ignored "-Wcast-function-type"
135#endif
136#if defined(__clang__) && __clang_major__ >= 19
137# pragma clang diagnostic push
138# pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
139#endif
140 clnt_call(client, NULLPROC, (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_void, NULL, timeout);
141#if defined(__clang__) && __clang_major__ >= 19
142# pragma clang diagnostic pop
143#endif
144#if __GNUC__ >= 8
145# pragma GCC diagnostic pop
146#endif
147
148 // A pointer to the return value of the thread
149 void* ret = NULL;
150 // wait till the server thread has finished
151 ERS_INFO("Waiting for server thread to terminate ...");
152 pthread_join(m_ServerThreadHandle, &ret);
153 ERS_INFO(" ... finished server thread");
154
155 //check if there was a return value
156 if (ret){
157 //Get the return value
158 unsigned long NRequests = *(unsigned long*)ret;
159 ERS_DEBUG(MSG::DEBUG,"Server thread stopped after handling " << NRequests << " requests");
160 } else
161 ERS_WARNING("Server thread stopped unexpectedly");
162
163 //Destroy the access lock
164 int retVal = pthread_mutex_destroy(&m_accessLock);
165 if (retVal != 0){
166 ERS_WARNING("Unable to destroy access lock after stopping server: " << fmterror(retVal));
167 return StatusCode::FAILURE;
168 }
169
170 return StatusCode::SUCCESS;
171 }
#define ONCRPCSERVERVERS
#define ONCRPCSERVERPROG

◆ UpdateEventForStream()

StatusCode JiveXMLServer::UpdateEventForStream ( const EventStreamID & evtStreamID,
const std::string & event )
overridevirtual

Put this event as new current event for stream given by name.

Get one event and put it as the new event for the this stream, which is identified by EventStreamID.

Implements JiveXML::IEventReceiver.

Definition at line 208 of file JiveXMLServer.cxx.

208 {
209
210 ERS_DEBUG(MSG::VERBOSE,"UpdateEventForStream");
211
212 //Check that the event stream id is valid
213 if (!evtStreamID.isValid()){
214 ERS_ERROR("Invalid event stream identifier - cannot add event");
215 return StatusCode::FAILURE;
216 }
217
218 //Make sure we don't have already exceeded the maximum number of streams
219 if (m_eventStreamMap.size() > NSTREAMMAX ){
220 ERS_ERROR("Reached max. allowed number of streams " << NSTREAMMAX << " - cannot add event");
221 return StatusCode::FAILURE;
222 }
223
224 //Make sure the event is not larger than the allowed maximal size
225 if (event.length() > NBYTESMAX ){
226 ERS_ERROR("Event is larger than allowed max. of " << NBYTESMAX << " bytes - cannot add event");
227 return StatusCode::FAILURE;
228 }
229
230 //Make sure we are the only one accessing the data right now, by trying to
231 //obtain a lock. If the lock can not be obtained after a certain time, an
232 //error is reported
233
234 //Timeout of 30 second and 0 nanoseconds
235 struct timespec timeout = { 30, 0 };
236 //Try to obtain the lock
237 int retVal = pthread_mutex_timedlock(&m_accessLock, &timeout);
238 if ( retVal != 0 ){
239 ERS_ERROR("Unable to obtain access lock to update event: " << fmterror(retVal));
240 return StatusCode::FAILURE;
241 }
242
243 //Using try/catch to ensure the mutex gets unlocked in any case
244 try {
245
246 //Using std::map::operator[] and std::map::insert() will create a new event
247 //if it did not exist, otherwise just replace the existing entry (making a
248 //copy of the std::string) but would not update the key which holds new
249 //event/run number. Therefore delete existing entry first.
250
251 //Delete old entry if there is one
252 EventStreamMap::iterator OldEvtItr = m_eventStreamMap.find(evtStreamID);
253 if (OldEvtItr != m_eventStreamMap.end())
254 m_eventStreamMap.erase(OldEvtItr);
255
256 //Now add the new event
257 m_eventStreamMap.insert(EventStreamPair(evtStreamID,event));
258
259 } catch ( const std::exception& e ) {
260 ERS_ERROR("Exception caught while updating event for stream "
261 << evtStreamID.StreamName() << ": " << e.what());
262 //Also release the lock in this case
263 pthread_mutex_unlock(&m_accessLock);
264 //before we return
265 return StatusCode::FAILURE;
266 }
267
268 //Finally release the lock again
269 retVal = pthread_mutex_unlock(&m_accessLock);
270 if ( retVal != 0 ){
271 ERS_ERROR("Unable to release access lock after updating event: " << fmterror(retVal));
272 return StatusCode::FAILURE;
273 }
274
275 ERS_DEBUG(MSG::DEBUG, "Updated stream " << evtStreamID.StreamName()
276 << " with event Nr. " << evtStreamID.EventNumber()
277 << " from run Nr. " << evtStreamID.RunNumber());
278
279 return StatusCode::SUCCESS;
280 }
const unsigned int NBYTESMAX
std::pair< const EventStreamID, const std::string > EventStreamPair
A map that stores events according to their EventStreamID Due to the way EventStreamID is build,...
Definition EventStream.h:86
const unsigned int NSTREAMMAX

◆ Wait()

void JiveXMLServer::Wait ( )

Wait for the server finish.

Wait for the server thread to finish.

This can be because a) we received a SIGTERM or SIGINT signal b) the server thread stopped by itself

Definition at line 188 of file JiveXMLServer.cxx.

188 {
189 //just wait for the lock
190 m_lock.wait();
191 //Tell why the lock was released
192 ERS_INFO("Reached post-condition after received signal " << m_receivedSignal );
193 }

Member Data Documentation

◆ ATLAS_THREAD_SAFE [1/2]

OWLSemaphore m_lock JiveXML::JiveXMLServer::ATLAS_THREAD_SAFE
inlinestaticprivate

Definition at line 97 of file JiveXMLServer.h.

◆ ATLAS_THREAD_SAFE [2/2]

pthread_mutex_t m_accessLock JiveXML::JiveXMLServer::ATLAS_THREAD_SAFE
mutableprivate

Definition at line 110 of file JiveXMLServer.h.

◆ m_eventStreamMap

EventStreamMap JiveXML::JiveXMLServer::m_eventStreamMap
private

Definition at line 107 of file JiveXMLServer.h.

◆ m_portNumber

int JiveXML::JiveXMLServer::m_portNumber
private

Definition at line 104 of file JiveXMLServer.h.

◆ m_receivedSignal

std::atomic<int> JiveXML::JiveXMLServer::m_receivedSignal {0}
inlinestaticprivate

Definition at line 100 of file JiveXMLServer.h.

100{0};

◆ m_runServerThread

bool JiveXML::JiveXMLServer::m_runServerThread
private

Definition at line 117 of file JiveXMLServer.h.

◆ m_ServerThreadHandle

pthread_t JiveXML::JiveXMLServer::m_ServerThreadHandle
private

Definition at line 113 of file JiveXMLServer.h.


The documentation for this class was generated from the following files: