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 std::promise< int > m_receivedSignal ATLAS_THREAD_SAFE
int m_portNumber
EventStreamMap m_eventStreamMap
std::timed_mutex 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 37 of file JiveXMLServer.h.

Constructor & Destructor Documentation

◆ JiveXMLServer()

JiveXMLServer::JiveXMLServer ( int port = 0)

Constructor.

Definition at line 35 of file JiveXMLServer.cxx.

35 :
36 m_portNumber(port){
37
38 //Make sure ServerThread does not start unexpectedly
39 m_runServerThread = false ;
40
41 //And then start it
42 StartServingThread().ignore();
43
44 //Also register the signal handlers
47 }
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 52 of file JiveXMLServer.cxx.

52 {
53
54 //Just stop the serving thread
55 StopServingThread().ignore();
56 }
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 281 of file JiveXMLServer.cxx.

281 {
282
283 //Obtain an exclusive access lock
284 std::scoped_lock lock(m_accessLock);
285
286 // Search the entry in the map
287 if (auto MapItr = m_eventStreamMap.find(evtStreamID); MapItr != m_eventStreamMap.end()) {
288 return MapItr->second;
289 }
290
291 return {};
292 }
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
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 265 of file JiveXMLServer.cxx.

265 {
266
267 //Obtain an exclusive access lock
268 std::scoped_lock lock(m_accessLock);
269
270 // Search the entry in the map
271 if (auto MapItr = m_eventStreamMap.find(StreamName); MapItr != m_eventStreamMap.end()) {
272 return MapItr->first;
273 }
274
275 return EventStreamID{""};
276 }

◆ 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 79 of file JiveXMLServer.h.

79{ 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 240 of file JiveXMLServer.cxx.

240 {
245 return 3;
246 }

◆ 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 251 of file JiveXMLServer.cxx.

251 {
252
253 //Obtain an exclusive access lock
254 std::scoped_lock lock(m_accessLock);
255
256 return m_eventStreamMap
257 | std::views::keys
258 | std::views::transform(&EventStreamID::StreamName)
259 | std::ranges::to<std::vector>();
260 }
const std::string & StreamName() const
Definition EventStream.h:43

◆ 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 309 of file JiveXMLServer.cxx.

309 {
310 //set to fixed value for now
311 return MSG::DEBUG;
312 }

◆ 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 297 of file JiveXMLServer.cxx.

297 {
298 //Deliver message to the proper stream
299 if (level <= MSG::DEBUG) ERS_REPORT_IMPL( ers::debug, ers::Message, msg, level);
300 if (level == MSG::INFO) ERS_REPORT_IMPL( ers::info, ers::Message, msg, );
301 if (level == MSG::WARNING) ERS_REPORT_IMPL( ers::warning, ers::Message, msg, );
302 if (level == MSG::ERROR) ERS_REPORT_IMPL( ers::error, ers::Message, msg, );
303 if (level >= MSG::FATAL) ERS_REPORT_IMPL( ers::fatal, ers::Message, msg, );
304 }
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 177 of file JiveXMLServer.cxx.

177 {
178 //call the signal handler, so we will also reach post condition
179 signalHandler(-1);
180 }

◆ signalHandler()

void JiveXMLServer::signalHandler ( int signum)
staticprivate

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

Definition at line 155 of file JiveXMLServer.cxx.

155 {
156 //Store signal and notify thread
157 m_receivedSignal.set_value(signal);
158 }

◆ 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 64 of file JiveXMLServer.cxx.

64 {
65
66 ERS_DEBUG(MSG::VERBOSE,"StartServingThread()");
67
68 //The arguments passed on to the server - create new object on the heap that
69 //is persistent through the lifetime of the thread
71
72 //set runServer flag to true, so the thread will start
73 m_runServerThread = true ;
74
75 //create thread itself
76 if ( ( pthread_create(&m_ServerThreadHandle, NULL , &JiveXML::ONCRPCServerThread, (void *) args )) != 0){
77
78 //Create thread failed
79 ERS_WARNING("Thread creation failed");
80 return StatusCode::FAILURE;
81 }
82
83 return StatusCode::SUCCESS;
84
85 }
#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 93 of file JiveXMLServer.cxx.

93 {
94
95 ERS_DEBUG(MSG::VERBOSE,"StopServingThread()");
96
101
102 //Create a client - this will already cause an update of the file
103 //descriptors on the sockets
104 CLIENT* client = clnt_create("localhost", ONCRPCSERVERPROG,ONCRPCSERVERVERS, "tcp");
105 if (!client){
106 ERS_ERROR("Unable to create shutdown client for local server");
107 return StatusCode::FAILURE;
108 }
109
110 // Next unset the runServerThread flag, which will cause the loop to stop
111 // This needs to happen after client creation, otherwise the next call won't
112 // be serverd anymore
113 m_runServerThread = false ;
114
115 // Now issue the call with a timeout
116 struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0;
117// xdr_void is defined inconsistently in xdr.h and gets a warning from gcc8.
118#if __GNUC__ >= 8
119# pragma GCC diagnostic push
120# pragma GCC diagnostic ignored "-Wcast-function-type"
121#endif
122#if defined(__clang__) && __clang_major__ >= 19
123# pragma clang diagnostic push
124# pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
125#endif
126 clnt_call(client, NULLPROC, (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_void, NULL, timeout);
127#if defined(__clang__) && __clang_major__ >= 19
128# pragma clang diagnostic pop
129#endif
130#if __GNUC__ >= 8
131# pragma GCC diagnostic pop
132#endif
133
134 // A pointer to the return value of the thread
135 void* ret = NULL;
136 // wait till the server thread has finished
137 ERS_INFO("Waiting for server thread to terminate ...");
138 pthread_join(m_ServerThreadHandle, &ret);
139 ERS_INFO(" ... finished server thread");
140
141 //check if there was a return value
142 if (ret){
143 //Get the return value
144 unsigned long NRequests = *(unsigned long*)ret;
145 ERS_DEBUG(MSG::DEBUG,"Server thread stopped after handling " << NRequests << " requests");
146 } else
147 ERS_WARNING("Server thread stopped unexpectedly");
148
149 return StatusCode::SUCCESS;
150 }
#define ERS_ERROR(message)
#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 186 of file JiveXMLServer.cxx.

186 {
187
188 ERS_DEBUG(MSG::VERBOSE,"UpdateEventForStream");
189
190 //Check that the event stream id is valid
191 if (!evtStreamID.isValid()){
192 ERS_ERROR("Invalid event stream identifier - cannot add event");
193 return StatusCode::FAILURE;
194 }
195
196 //Make sure we don't have already exceeded the maximum number of streams
197 if (m_eventStreamMap.size() > NSTREAMMAX ){
198 ERS_ERROR("Reached max. allowed number of streams " << NSTREAMMAX << " - cannot add event");
199 return StatusCode::FAILURE;
200 }
201
202 //Make sure the event is not larger than the allowed maximal size
203 if (event.length() > NBYTESMAX ){
204 ERS_ERROR("Event is larger than allowed max. of " << NBYTESMAX << " bytes - cannot add event");
205 return StatusCode::FAILURE;
206 }
207
208 //Make sure we are the only one accessing the data right now, by trying to
209 //obtain a lock. If the lock cannot be obtained after a certain time, an
210 //error is reported
211
212 //Try to obtain the lock within 30 seconds
213 using namespace std::chrono_literals;
214 std::unique_lock lock(m_accessLock, 30s);
215
216 if ( !lock ){
217 ERS_ERROR("Unable to obtain access lock to update event");
218 return StatusCode::FAILURE;
219 }
220
221 //Using std::map::operator[] and std::map::insert() will create a new event
222 //if it did not exist, otherwise just replace the existing entry (making a
223 //copy of the std::string) but would not update the key which holds new
224 //event/run number. Therefore delete existing entry first.
225
226 m_eventStreamMap.erase(evtStreamID);
227 m_eventStreamMap.insert(EventStreamPair(evtStreamID,event));
228
229 ERS_DEBUG(MSG::DEBUG, "Updated stream " << evtStreamID.StreamName()
230 << " with event Nr. " << evtStreamID.EventNumber()
231 << " from run Nr. " << evtStreamID.RunNumber());
232
233 return StatusCode::SUCCESS;
234 }
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 165 of file JiveXMLServer.cxx.

165 {
166 auto signal = m_receivedSignal.get_future();
167 //just wait for a signal
168 signal.wait();
169 //Tell why the lock was released
170 ERS_INFO("Reached post-condition after received signal " << signal.get() );
171 }

Member Data Documentation

◆ ATLAS_THREAD_SAFE [1/2]

std::timed_mutex m_accessLock JiveXML::JiveXMLServer::ATLAS_THREAD_SAFE
mutableprivate

Definition at line 106 of file JiveXMLServer.h.

◆ ATLAS_THREAD_SAFE [2/2]

std::promise<int> m_receivedSignal JiveXML::JiveXMLServer::ATLAS_THREAD_SAFE
inlinestaticprivate

Definition at line 96 of file JiveXMLServer.h.

◆ m_eventStreamMap

EventStreamMap JiveXML::JiveXMLServer::m_eventStreamMap
private

Definition at line 103 of file JiveXMLServer.h.

◆ m_portNumber

int JiveXML::JiveXMLServer::m_portNumber
private

Definition at line 100 of file JiveXMLServer.h.

◆ m_runServerThread

bool JiveXML::JiveXMLServer::m_runServerThread
private

Definition at line 113 of file JiveXMLServer.h.

◆ m_ServerThreadHandle

pthread_t JiveXML::JiveXMLServer::m_ServerThreadHandle
private

Definition at line 109 of file JiveXMLServer.h.


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