31 #ifdef SERVER_MONITORING
46 CAStatusManager *CAStatusManager::ms_pStatusManager = NULL;
47 state_t ***CAStatusManager::ms_pAllStates = NULL;
48 event_t ***CAStatusManager::ms_pAllEvents = NULL;
52 if(ms_pAllEvents == NULL)
56 if(ms_pAllStates == NULL)
60 if(ms_pStatusManager == NULL)
62 ms_pStatusManager =
new CAStatusManager();
70 "CAStatusManager: doing cleanup\n");
80 if(ms_pStatusManager != NULL)
82 delete ms_pStatusManager;
83 ms_pStatusManager = NULL;
85 if(ms_pAllStates != NULL)
89 if(ms_pAllEvents != NULL)
97 if(ms_pStatusManager != NULL)
99 return ms_pStatusManager->transition(e_type, s_type);
104 "StatusManager: cannot handle event %d/%d "
105 "because there is no StatusManager deployed\n",
111 CAStatusManager::CAStatusManager()
114 m_pCurrentStates = NULL;
115 m_pCurrentStatesInfo = NULL;
116 m_pStatusLock = NULL;
117 m_pStatusSocket = NULL;
118 m_pListenAddr = NULL;
119 m_pMonitoringThread = NULL;
120 m_pPreparedStatusMessage = NULL;
121 m_bTryListen =
false;
127 m_pCurrentStates[i] =
131 m_pCurrentStates[i]->st_description);
140 m_pMonitoringThread =
new CAThread((
UINT8*)
"Monitoring Thread");
141 m_pMonitoringThread->setMainLoop(serveMonitoringRequests);
142 m_pMonitoringThread->start(
this);
147 "StatusManager: an error occured while initializing the"
148 " server monitoring socket\n");
155 (m_pCurrentStatesInfo[i]).dsi_stateType,
156 (
UINT32)(m_pCurrentStates[i])->st_type);
158 (m_pCurrentStatesInfo[i]).dsi_stateDesc,
159 (
UINT8*)(m_pCurrentStates[i])->st_description);
161 (m_pCurrentStatesInfo[i]).dsi_stateLevel,
162 (
UINT8*)(STATUS_LEVEL_NAMES[(m_pCurrentStates[i])->st_stateLevel]));
166 CAStatusManager::~CAStatusManager()
169 if(m_pMonitoringThread != NULL)
171 if(m_pStatusSocket != NULL)
173 if( !(m_pStatusSocket->isClosed()) )
175 m_pStatusSocket->close();
179 delete m_pMonitoringThread;
182 "CAStatusManager: The monitoring thread is no more.\n");
184 m_pMonitoringThread = NULL;
186 if(m_pStatusLock != NULL)
188 delete m_pStatusLock;
189 m_pStatusLock = NULL;
191 if(m_pCurrentStates != NULL)
195 m_pCurrentStates[i] = NULL;
197 delete[] m_pCurrentStates;
198 m_pCurrentStates = NULL;
200 if(m_pStatusSocket != NULL)
202 delete m_pStatusSocket;
203 m_pStatusSocket = NULL;
205 if(m_pListenAddr != NULL)
207 delete m_pListenAddr;
208 m_pListenAddr = NULL;
210 if(m_pPreparedStatusMessage != NULL)
212 m_pPreparedStatusMessage->release();
213 m_pPreparedStatusMessage = NULL;
216 SINT32 CAStatusManager::initSocket()
221 if(m_pStatusSocket == NULL)
226 if( !(m_pStatusSocket->isClosed()) )
229 "StatusManager: server monitoring socket already connected.\n");
233 ret = m_pStatusSocket->create();
237 "StatusManager: could not create server monitoring socket.\n");
240 m_pStatusSocket->setReuseAddr(
true);
244 char *hostname =
"localhost";
246 bool userdefined =
false;
260 ret = m_pListenAddr->setAddr((
UINT8 *) hostname, port);
266 "StatusManager: could not initialize specified listener interface:"
267 " invalid host %s\n", hostname);
270 hostname =
"localhost";
272 "StatusManager: trying %s.\n", hostname);
274 ret = m_pListenAddr->setAddr((
UINT8 *) hostname, port);
278 "StatusManager: setting up listener interface %s:%d for "
279 "server monitoring failed\n",
288 "StatusManager: setting up listener interface %s:%d for "
289 "server monitoring failed\n",
294 ret = m_pStatusSocket->listen(*m_pListenAddr);
302 "StatusManager: not able to init server socket %s:%d "
303 "for server monitoring. %s failed because: %s\n",
312 "StatusManager: listen to monitoring socket on %s:%d\n",
323 if( (m_pStatusLock == NULL) ||
324 (m_pCurrentStates == NULL) )
327 "StatusManager: fatal error\n");
333 "StatusManager: received event for an invalid status type: %d\n", s_type);
336 if((e_type >= EVENT_COUNT[s_type]) || (e_type <
FIRST_EVENT))
339 "StatusManager: received an invalid event: %d\n", e_type);
347 m_pStatusLock->lock();
348 if(m_pCurrentStates[s_type]->st_transitions == NULL)
350 m_pStatusLock->unlock();
352 "StatusManager: current state is corrupt\n");
355 transitionToNextState = m_pCurrentStates[s_type]->st_transitions[e_type];
356 if(transitionToNextState >= STATE_COUNT[s_type])
358 m_pStatusLock->unlock();
360 "StatusManager: transition to invalid state %d\n", transitionToNextState);
365 prev = m_pCurrentStates[s_type];
366 m_pCurrentStates[s_type] = ms_pAllStates[s_type][transitionToNextState];
367 m_pCurrentStates[s_type]->
st_prev = prev;
368 m_pCurrentStates[s_type]->
st_cause = ms_pAllEvents[s_type][e_type];
372 (m_pCurrentStatesInfo[s_type]).dsi_stateType,
373 (
UINT32)(m_pCurrentStates[s_type])->st_type);
375 (m_pCurrentStatesInfo[s_type]).dsi_stateDesc,
376 (
UINT8*)(m_pCurrentStates[s_type])->st_description);
378 (m_pCurrentStatesInfo[s_type]).dsi_stateLevel,
379 (
UINT8*)(STATUS_LEVEL_NAMES[(m_pCurrentStates[s_type])->st_stateLevel]));
382 "StatusManager: status %s: "
383 "transition from state %d (%s) "
384 "to state %d (%s) caused by event %d (%s)\n",
385 STATUS_NAMES[s_type],
387 m_pCurrentStates[s_type]->st_type, m_pCurrentStates[s_type]->st_description,
388 e_type, (ms_pAllEvents[s_type][e_type])->ev_description);
391 m_pStatusLock->unlock();
396 SINT32 CAStatusManager::initStatusMessage()
402 DOMElement *status_dom_element = NULL;
408 (m_pCurrentStatesInfo[i]).dsi_stateType =
413 (m_pCurrentStatesInfo[i]).dsi_stateLevel =
418 (m_pCurrentStatesInfo[i]).dsi_stateDesc =
423 status_dom_element->appendChild((m_pCurrentStatesInfo[i]).dsi_stateType);
424 status_dom_element->appendChild((m_pCurrentStatesInfo[i]).dsi_stateLevel);
425 status_dom_element->appendChild((m_pCurrentStatesInfo[i]).dsi_stateDesc);
426 elemRoot->appendChild(status_dom_element);
428 m_pPreparedStatusMessage->appendChild(elemRoot);
434 CAMsg::printMsg(LOG_DEBUG,
"the status message template looks like this: %s \n",debugout);
443 CAStatusManager *statusManager = (CAStatusManager*) param;
445 if(statusManager == NULL)
448 "Monitoring Thread: fatal error, exiting.\n");
455 if(statusManager->m_pStatusSocket != NULL)
457 if(statusManager->m_pStatusSocket->isClosed())
460 "Monitoring Thread: server socket closed, leaving loop.\n");
467 "Monitoring Thread: server socket disposed, leaving loop.\n");
471 if(statusManager->m_pStatusSocket->accept(monitoringRequestSocket) ==
E_SUCCESS)
476 statusManager->m_pStatusLock->lock();
478 statusManager->m_pPreparedStatusMessage,
479 (
UINT8*)xmlStatusMessage,
480 &xmlStatusMessageLength);
481 statusManager->m_pStatusLock->unlock();
487 size_t http_prefix_length = strlen(http_prefix);
489 char statusMessageResponse[http_prefix_length+xmlStatusMessageLength+1];
490 strncpy(statusMessageResponse, http_prefix, http_prefix_length);
491 strncpy((statusMessageResponse+http_prefix_length), xmlStatusMessage, xmlStatusMessageLength);
492 statusMessageResponse[xmlStatusMessageLength+http_prefix_length]=0;
495 CAMsg::printMsg(LOG_DEBUG,
"the status message looks like this: %s \n",statusMessageResponse);
498 if(monitoringRequestSocket.
send((
UINT8*)statusMessageResponse,
499 (http_prefix_length+xmlStatusMessageLength)) < 0)
502 "StatusManager: error: could not send status message.\n");
504 monitoringRequestSocket.
close();
510 "StatusManager: error: could not process monitoring request: %s\n",
GET_NET_ERROR_STR(errnum));
522 void CAStatusManager::initStates()
534 ms_pAllStates[i][j] =
new state_t;
546 void CAStatusManager::deleteStates()
554 if(ms_pAllStates[i][j] != NULL)
556 if(ms_pAllStates[i][j]->st_transitions != NULL)
558 delete[] ms_pAllStates[i][j]->st_transitions;
559 ms_pAllStates[i][j]->st_transitions = NULL;
561 delete ms_pAllStates[i][j];
562 ms_pAllStates[i][j] = NULL;
565 delete[] ms_pAllStates[i];
566 ms_pAllStates[i] = NULL;
568 delete[] ms_pAllStates;
569 ms_pAllStates = NULL;
572 void CAStatusManager::initEvents()
579 ms_pAllEvents[i] =
new event_t*[EVENT_COUNT[i]];
582 ms_pAllEvents[i][j] =
new event_t;
590 void CAStatusManager::deleteEvents()
598 delete ms_pAllEvents[i][j];
599 ms_pAllEvents[i][j] = NULL;
601 delete[] ms_pAllEvents[i];
602 ms_pAllEvents[i] = NULL;
604 delete[] ms_pAllEvents;
605 ms_pAllEvents = NULL;
617 va_start(ap, transitionCount);
618 for(i = 0; i < transitionCount; i++)
620 specifiedEventTypes[i] = (
event_type_t) va_arg(ap,
int);
621 specifiedTransitions[i] = (
transition_t) va_arg(ap,
int);
630 if(transitionCount > (EVENT_COUNT[s_type]))
638 for(i = 0; i < transitionCount; i++)
640 if((specifiedEventTypes[i] >= EVENT_COUNT[s_type]) ||
641 (specifiedEventTypes[i] < 0))
645 "StatusManager: definition of an invalid state transition (invalid event %d).\n",
646 specifiedEventTypes[i]);
649 if((specifiedTransitions[i] >= STATE_COUNT[s_type]) ||
654 "StatusManager: definition of an invalid state transition (invalid state %d).\n",
655 specifiedTransitions[i]);
658 transitions[specifiedEventTypes[i]] = specifiedTransitions[i];
SINT32 setDOMElementValue(DOMElement *pElem, SINT32 value)
XERCES_CPP_NAMESPACE::DOMDocument * createDOMDocument()
Parses a timestamp in JDBC timestamp escape format (as it comes from the BI) and outputs the value in...
DOMElement * createDOMElement(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const name)
Creates a new DOMElement with the given name which belongs to the DOMDocument owernDoc.
#define GET_NET_ERROR_STR(x)
#define THREAD_RETURN_ERROR
#define THREAD_RETURN_SUCCESS
static CACmdLnOptions * getOptions()
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
This class represents a socket address for Internet (IP) connections.
virtual SINT32 send(const UINT8 *buff, UINT32 len)
Sends some data over the network.
static SINT32 dumpToMem(const DOMNode *node, UINT8 *buff, UINT32 *size)
Dumps the node and all childs into buff.
#define HTTP_ANSWER_PREFIX_MAX_LENGTH
transition_t * defineTransitions(status_type_t s_type, int transitionCount,...)
a convenience function for easily defining state transitions
#define MONITORING_SERVER_PORT
#define XML_STATUS_MESSAGE_MAX_SIZE
enum state_type transition_t
#define DOM_ELEMENT_STATUS_MESSAGE_NAME
#define FINISH_STATE_DEFINITIONS(state_array)
helper macros for defining states and events:
#define HTTP_ANSWER_PREFIX_FORMAT
enum state_type state_type_t
#define DOM_ELEMENT_STATE_LEVEL_NAME
enum event_type event_type_t
#define FINISH_EVENT_DEFINITIONS(event_array)
#define DOM_ELEMENT_STATE_DESCRIPTION_NAME
enum status_type status_type_t
#define DOM_ELEMENT_STATE_NAME
void init()
do necessary initialisations of libraries etc.
void cleanup()
do necessary cleanups of libraries etc.