eventlist.cpp
上传用户:ets1996
上传日期:2014-09-30
资源大小:353k
文件大小:7k
源码类别:

SNMP编程

开发平台:

Visual C++

  1. /*_############################################################################
  2.   _## 
  3.   _##  eventlist.cpp  
  4.   _##
  5.   _##  SNMP++v3.2.22
  6.   _##  -----------------------------------------------
  7.   _##  Copyright (c) 2001-2007 Jochen Katz, Frank Fock
  8.   _##
  9.   _##  This software is based on SNMP++2.6 from Hewlett Packard:
  10.   _##  
  11.   _##    Copyright (c) 1996
  12.   _##    Hewlett-Packard Company
  13.   _##  
  14.   _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  15.   _##  Permission to use, copy, modify, distribute and/or sell this software 
  16.   _##  and/or its documentation is hereby granted without fee. User agrees 
  17.   _##  to display the above copyright notice and this license notice in all 
  18.   _##  copies of the software and any documentation of the software. User 
  19.   _##  agrees to assume all liability for the use of the software; 
  20.   _##  Hewlett-Packard and Jochen Katz make no representations about the 
  21.   _##  suitability of this software for any purpose. It is provided 
  22.   _##  "AS-IS" without warranty of any kind, either express or implied. User 
  23.   _##  hereby grants a royalty-free license to any and all derivatives based
  24.   _##  upon this software code base. 
  25.   _##  
  26.   _##  Stuttgart, Germany, Wed May  2 23:22:30 CEST 2007 
  27.   _##  
  28.   _##########################################################################*/
  29. /*===================================================================
  30.   Copyright (c) 1999
  31.   Hewlett-Packard Company
  32.   ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  33.   Permission to use, copy, modify, distribute and/or sell this software
  34.   and/or its documentation is hereby granted without fee. User agrees
  35.   to display the above copyright notice and this license notice in all
  36.   copies of the software and any documentation of the software. User
  37.   agrees to assume all liability for the use of the software; Hewlett-Packard
  38.   makes no representations about the suitability of this software for any
  39.   purpose. It is provided "AS-IS" without warranty of any kind,either express
  40.   or implied. User hereby grants a royalty-free license to any and all
  41.   derivatives based upon this software code base.
  42.       E V E N T L I S T . C P P
  43.       CEventList  CLASS DEFINITION
  44.       COPYRIGHT HEWLETT PACKARD COMPANY 1999
  45.       INFORMATION NETWORKS DIVISION
  46.       NETWORK MANAGEMENT SECTION
  47.       DESIGN + AUTHOR:
  48.         Tom Murray
  49.       LANGUAGE:
  50.         ANSI C++
  51.       OPERATING SYSTEMS:
  52.         DOS/WINDOWS 3.1
  53.         BSD UNIX
  54.       DESCRIPTION:
  55.         Queue for holding all event sources (snmp messages, user
  56.         defined input sources, user defined timeouts, etc)
  57.       COMPILER DIRECTIVES:
  58.         UNIX - For UNIX build
  59. =====================================================================*/
  60. char event_list_version[]="@(#) SNMP++ $Id: eventlist.cpp 232 2006-03-10 20:43:44Z katz $";
  61. //----[ snmp++ includes ]----------------------------------------------
  62. #include "snmp_pp/config_snmp_pp.h"
  63. #include "snmp_pp/v3.h"
  64. #include "snmp_pp/eventlist.h" // queue for holding all event sources
  65. #include "snmp_pp/msgqueue.h" // queue for holding snmp event sources
  66. #include "snmp_pp/notifyqueue.h" // queue for holding trap callbacks
  67. #include "snmp_pp/snmperrs.h"
  68. #ifdef SNMP_PP_NAMESPACE
  69. namespace Snmp_pp {
  70. #endif
  71. //----[ CSNMPMessageQueueElt class ]--------------------------------------
  72. CEventList::CEventListElt::CEventListElt(CEvents *events,
  73.  CEventListElt *next,
  74.  CEventListElt *previous):
  75.   m_events(events), m_Next(next), m_previous(previous)
  76. {
  77.   /* Finish insertion into doubly linked list */
  78.   if (m_Next)     m_Next->m_previous = this;
  79.   if (m_previous) m_previous->m_Next = this;
  80. }
  81. CEventList::CEventListElt::~CEventListElt()
  82. {
  83.   /* Do deletion form doubly linked list */
  84.   if (m_Next)     m_Next->m_previous = m_previous;
  85.   if (m_previous) m_previous->m_Next = m_Next;
  86.   if (m_events)   delete m_events;
  87. }
  88. //----[ CEventList class ]--------------------------------------
  89. CEventList::~CEventList()
  90. {
  91.   CEventListElt *leftOver;
  92.   /* walk the list deleting any elements still on the queue */
  93.   lock();
  94.   while ((leftOver = m_head.GetNext()))
  95.     delete leftOver;
  96.   unlock();
  97. }
  98. CEvents * CEventList::AddEntry(CEvents *events) REENTRANT ({
  99.     /*---------------------------------------------------------*/
  100.     /* Insert entry at head of list, done automagically by the */
  101.     /* constructor function, so don't use the return value.    */
  102.     /*---------------------------------------------------------*/
  103.   (void) new CEventListElt(events, m_head.GetNext(), &m_head);
  104.   m_msgCount++;
  105.   return events;
  106. })
  107. int CEventList::GetNextTimeout(msec &sendTime) REENTRANT ({
  108.   CEventListElt *msgEltPtr = m_head.GetNext();
  109.   msec tmpTime(sendTime);
  110.   sendTime.SetInfinite(); // set sendtime out into future
  111.   while (msgEltPtr) {
  112.     if (msgEltPtr->GetEvents()->GetCount() &&
  113. !msgEltPtr->GetEvents()->GetNextTimeout(tmpTime)) {
  114.       if (sendTime > tmpTime)
  115. sendTime = tmpTime;
  116.     }
  117.     msgEltPtr = msgEltPtr->GetNext();
  118.   }
  119.  return 0;
  120. })
  121. void CEventList::GetFdSets(int &maxfds, fd_set &readfds, fd_set &writefds,
  122.    fd_set &exceptfds) REENTRANT ({
  123.   CEventListElt *msgEltPtr = m_head.GetNext();
  124.   maxfds = 0;
  125.   FD_ZERO(&readfds);
  126.   FD_ZERO(&writefds);
  127.   FD_ZERO(&exceptfds);
  128.   while (msgEltPtr) {
  129.     if (msgEltPtr->GetEvents()->GetCount()) {
  130.       msgEltPtr->GetEvents()->GetFdSets(maxfds, readfds, writefds, exceptfds);
  131.     }
  132.     msgEltPtr = msgEltPtr->GetNext();
  133.   }
  134. })
  135. int CEventList::HandleEvents(const int maxfds,
  136.      const fd_set &readfds,
  137.      const fd_set &writefds,
  138.      const fd_set &exceptfds)
  139. {
  140.   lock();
  141.   CEventListElt *msgEltPtr = m_head.GetNext();
  142.   int status = SNMP_CLASS_SUCCESS;
  143.   while (msgEltPtr){
  144.     if (msgEltPtr->GetEvents()->GetCount()) {
  145.       unlock();
  146.       status = msgEltPtr->GetEvents()->HandleEvents(maxfds, readfds, writefds,
  147.     exceptfds);
  148.       lock();
  149.     }
  150.     msgEltPtr = msgEltPtr->GetNext();
  151.   }
  152.   unlock();
  153.   return status;
  154. }
  155. int CEventList::DoRetries(const msec &sendtime) REENTRANT ({
  156.   CEventListElt *msgEltPtr = m_head.GetNext();
  157.   int status = SNMP_CLASS_SUCCESS;
  158.   while (msgEltPtr){
  159.     if (msgEltPtr->GetEvents()->GetCount()) {
  160.       status = msgEltPtr->GetEvents()->DoRetries(sendtime);
  161.     }
  162.     msgEltPtr = msgEltPtr->GetNext();
  163.   }
  164.   return status;
  165. })
  166. int CEventList::Done() REENTRANT ({
  167.   CEventListElt *msgEltPtr = m_head.GetNext();
  168.   int status = SNMP_CLASS_SUCCESS;
  169.   if (m_done) {
  170.     m_done--;
  171.     return 1;
  172.   }
  173.   while (msgEltPtr){
  174.     if (msgEltPtr->GetEvents()->GetCount()) {
  175.       status = msgEltPtr->GetEvents()->Done();
  176.       if (status)
  177. break;
  178.     }
  179.     msgEltPtr = msgEltPtr->GetNext();
  180.   }
  181.   return status;
  182. })
  183. #ifdef SNMP_PP_NAMESPACE
  184. }; // end of namespace Snmp_pp
  185. #endif