"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/client/libnxclient/events.cpp" (23 Feb 2021, 4335 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "events.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** NetXMS - Network Management System
    3 ** Client Library
    4 ** Copyright (C) 2003-2020 Victor Kirhenshtein
    5 **
    6 ** This program is free software; you can redistribute it and/or modify
    7 ** it under the terms of the GNU Lesser General Public License as published by
    8 ** the Free Software Foundation; either version 3 of the License, or
    9 ** (at your option) any later version.
   10 **
   11 ** This program is distributed in the hope that it will be useful,
   12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 ** GNU General Public License for more details.
   15 **
   16 ** You should have received a copy of the GNU Lesser General Public License
   17 ** along with this program; if not, write to the Free Software
   18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   19 **
   20 ** File: events.cpp
   21 **
   22 **/
   23 
   24 #include "libnxclient.h"
   25 
   26 /**
   27  * Controller constructor
   28  */
   29 EventController::EventController(NXCSession *session) : Controller(session)
   30 {
   31    m_eventTemplates = NULL;
   32    m_eventTemplateLock = MutexCreate();
   33 }
   34 
   35 /**
   36  * Controller destructor
   37  */
   38 EventController::~EventController()
   39 {
   40    delete m_eventTemplates;
   41    MutexDestroy(m_eventTemplateLock);
   42 }
   43 
   44 /**
   45  * Synchronize event templates
   46  */
   47 UINT32 EventController::syncEventTemplates()
   48 {
   49    ObjectArray<EventTemplate> *list = new ObjectArray<EventTemplate>(256, 256, Ownership::True);
   50    UINT32 rcc = getEventTemplates(list);
   51    if (rcc != RCC_SUCCESS)
   52    {
   53       delete list;
   54       return rcc;
   55    }
   56 
   57    MutexLock(m_eventTemplateLock);
   58    delete m_eventTemplates;
   59    m_eventTemplates = list;
   60    MutexUnlock(m_eventTemplateLock);
   61    return RCC_SUCCESS;
   62 }
   63 
   64 /**
   65  * Get configured event templates
   66  */
   67 UINT32 EventController::getEventTemplates(ObjectArray<EventTemplate> *templates)
   68 {
   69    NXCPMessage msg;
   70    msg.setCode(CMD_LOAD_EVENT_DB);
   71    msg.setId(m_session->createMessageId());
   72 
   73    if (!m_session->sendMessage(&msg))
   74       return RCC_COMM_FAILURE;
   75 
   76    UINT32 rcc = m_session->waitForRCC(msg.getId());
   77    if (rcc != RCC_SUCCESS)
   78       return rcc;
   79 
   80    while(true)
   81    {
   82       NXCPMessage *response = m_session->waitForMessage(CMD_EVENT_DB_RECORD, msg.getId());
   83       if (response != NULL)
   84       {
   85          if (response->isEndOfSequence())
   86          {
   87             delete response;
   88             break;
   89          }
   90          templates->add(new EventTemplate(response));
   91          delete response;
   92       }
   93       else
   94       {
   95          rcc = RCC_TIMEOUT;
   96          break;
   97       }
   98    }
   99    return rcc;
  100 }
  101 
  102 /**
  103  * Get event name by code
  104  */
  105 TCHAR *EventController::getEventName(UINT32 code, TCHAR *buffer, size_t bufferSize)
  106 {
  107    MutexLock(m_eventTemplateLock);
  108    if (m_eventTemplates != NULL)
  109    {
  110       for(int i = 0; i < m_eventTemplates->size(); i++)
  111       {
  112          EventTemplate *t = m_eventTemplates->get(i);
  113          if (t->getCode() == code)
  114          {
  115             nx_strncpy(buffer, t->getName(), bufferSize);
  116             MutexUnlock(m_eventTemplateLock);
  117             return buffer;
  118          }
  119       }
  120    }
  121    MutexUnlock(m_eventTemplateLock);
  122    nx_strncpy(buffer, _T("<unknown>"), bufferSize);
  123    return buffer;
  124 }
  125 
  126 /**
  127  * Send event to server
  128  */
  129 UINT32 EventController::sendEvent(UINT32 code, const TCHAR *name, UINT32 objectId, int argc, TCHAR **argv, const TCHAR *userTag)
  130 {
  131    NXCPMessage msg;
  132 
  133    msg.setCode(CMD_TRAP);
  134    msg.setId(m_session->createMessageId());
  135    msg.setField(VID_EVENT_CODE, code);
  136    msg.setField(VID_EVENT_NAME, name);
  137    msg.setField(VID_OBJECT_ID, objectId);
  138     msg.setField(VID_USER_TAG, CHECK_NULL_EX(userTag));
  139    msg.setField(VID_NUM_ARGS, (UINT16)argc);
  140    for(int i = 0; i < argc; i++)
  141       msg.setField(VID_EVENT_ARG_BASE + i, argv[i]);
  142 
  143    if (!m_session->sendMessage(&msg))
  144       return RCC_COMM_FAILURE;
  145    return m_session->waitForRCC(msg.getId());
  146 }
  147 
  148 /**
  149  * Event template constructor
  150  */
  151 EventTemplate::EventTemplate(NXCPMessage *msg)
  152 {
  153    m_code = msg->getFieldAsUInt32(VID_EVENT_CODE);
  154    msg->getFieldAsString(VID_NAME, m_name, MAX_EVENT_NAME);
  155    m_severity = msg->getFieldAsInt32(VID_SEVERITY);
  156    m_flags = msg->getFieldAsUInt32(VID_FLAGS);
  157    m_messageTemplate = msg->getFieldAsString(VID_MESSAGE);
  158    m_description = msg->getFieldAsString(VID_DESCRIPTION);
  159 }
  160 
  161 /**
  162  * Event template destructor
  163  */
  164 EventTemplate::~EventTemplate()
  165 {
  166    MemFree(m_messageTemplate);
  167    MemFree(m_description);
  168 }