]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/src/eventlist.cpp
Initial adding
[ssmd.git] / 3rdparty / snmp++ / src / eventlist.cpp
1 /*_############################################################################
2   _## 
3   _##  eventlist.cpp  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 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, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 /*===================================================================
30
31   Copyright (c) 1999
32   Hewlett-Packard Company
33
34   ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
35   Permission to use, copy, modify, distribute and/or sell this software
36   and/or its documentation is hereby granted without fee. User agrees
37   to display the above copyright notice and this license notice in all
38   copies of the software and any documentation of the software. User
39   agrees to assume all liability for the use of the software; Hewlett-Packard
40   makes no representations about the suitability of this software for any
41   purpose. It is provided "AS-IS" without warranty of any kind,either express
42   or implied. User hereby grants a royalty-free license to any and all
43   derivatives based upon this software code base.
44
45       E V E N T L I S T . C P P
46
47       CEventList  CLASS DEFINITION
48
49       COPYRIGHT HEWLETT PACKARD COMPANY 1999
50
51       INFORMATION NETWORKS DIVISION
52
53       NETWORK MANAGEMENT SECTION
54
55       DESIGN + AUTHOR:        Tom Murray
56
57       DESCRIPTION:
58         Queue for holding all event sources (snmp messages, user
59         defined input sources, user defined timeouts, etc)
60 =====================================================================*/
61 char event_list_version[]="@(#) SNMP++ $Id: eventlist.cpp 1542 2009-05-29 11:38:48Z katz $";
62
63 //----[ snmp++ includes ]----------------------------------------------
64
65 #include "snmp_pp/config_snmp_pp.h"
66 #include "snmp_pp/v3.h"
67 #include "snmp_pp/eventlist.h"          // queue for holding all event sources
68 #include "snmp_pp/msgqueue.h"           // queue for holding snmp event sources
69 #include "snmp_pp/notifyqueue.h"        // queue for holding trap callbacks
70 #include "snmp_pp/snmperrs.h"
71
72 #ifdef SNMP_PP_NAMESPACE
73 namespace Snmp_pp {
74 #endif
75
76 //----[ CSNMPMessageQueueElt class ]--------------------------------------
77
78 CEventList::CEventListElt::CEventListElt(CEvents *events,
79                                          CEventListElt *next,
80                                          CEventListElt *previous):
81   m_events(events), m_Next(next), m_previous(previous)
82 {
83   /* Finish insertion into doubly linked list */
84   if (m_Next)     m_Next->m_previous = this;
85   if (m_previous) m_previous->m_Next = this;
86 }
87
88 CEventList::CEventListElt::~CEventListElt()
89 {
90   /* Do deletion form doubly linked list */
91   if (m_Next)     m_Next->m_previous = m_previous;
92   if (m_previous) m_previous->m_Next = m_Next;
93   if (m_events)   delete m_events;
94 }
95
96
97 //----[ CEventList class ]--------------------------------------
98
99
100 CEventList::~CEventList()
101 {
102   CEventListElt *leftOver;
103
104   /* walk the list deleting any elements still on the queue */
105   lock();
106   while ((leftOver = m_head.GetNext()))
107     delete leftOver;
108   unlock();
109 }
110
111 CEvents * CEventList::AddEntry(CEvents *events) REENTRANT ({
112     /*---------------------------------------------------------*/
113     /* Insert entry at head of list, done automagically by the */
114     /* constructor function, so don't use the return value.    */
115     /*---------------------------------------------------------*/
116   (void) new CEventListElt(events, m_head.GetNext(), &m_head);
117   m_msgCount++;
118
119   return events;
120 })
121
122 int CEventList::GetNextTimeout(msec &sendTime) REENTRANT ({
123
124   CEventListElt *msgEltPtr = m_head.GetNext();
125   msec tmpTime(sendTime);
126
127   sendTime.SetInfinite();       // set sendtime out into future
128   while (msgEltPtr) {
129     if (msgEltPtr->GetEvents()->GetCount() &&
130         !msgEltPtr->GetEvents()->GetNextTimeout(tmpTime)) {
131       if (sendTime > tmpTime)
132         sendTime = tmpTime;
133     }
134
135     msgEltPtr = msgEltPtr->GetNext();
136   }
137  return 0;
138 })
139
140 #ifdef HAVE_POLL_SYSCALL
141
142 int CEventList::GetFdCount()
143 {
144   SnmpSynchronize _synchronize(*this); // instead of REENTRANT()
145   int count = 0;
146   CEventListElt *msgEltPtr = m_head.GetNext();
147
148   while (msgEltPtr)
149   {
150     count += msgEltPtr->GetEvents()->GetFdCount();
151     msgEltPtr = msgEltPtr->GetNext();
152   }
153   return count;
154 }
155
156 bool CEventList::GetFdArray(struct pollfd *readfds, int &remaining)
157 {
158   SnmpSynchronize _synchronize(*this); // instead of REENTRANT()
159   CEventListElt *msgEltPtr = m_head.GetNext();
160
161   while (msgEltPtr)
162   {
163       int old_remaining = remaining;
164       if (msgEltPtr->GetEvents()->GetFdArray(readfds, remaining) == false)
165           return false;
166       readfds += (old_remaining - remaining);
167       msgEltPtr = msgEltPtr->GetNext();
168   }
169   return true;
170 }
171
172 int CEventList::HandleEvents(const struct pollfd *readfds, const int fds)
173 {
174   lock();
175   CEventListElt *msgEltPtr = m_head.GetNext();
176   int status = SNMP_CLASS_SUCCESS;
177   while (msgEltPtr)
178   {
179     if (msgEltPtr->GetEvents()->GetCount())
180     {
181       unlock();
182       status = msgEltPtr->GetEvents()->HandleEvents(readfds, fds);
183       lock();
184     }
185     msgEltPtr = msgEltPtr->GetNext();
186   }
187   unlock();
188   return status;
189 }
190
191 #else
192
193 void CEventList::GetFdSets(int &maxfds, fd_set &readfds, fd_set &writefds,
194                            fd_set &exceptfds) REENTRANT ({
195
196   CEventListElt *msgEltPtr = m_head.GetNext();
197
198   maxfds = 0;
199   FD_ZERO(&readfds);
200   FD_ZERO(&writefds);
201   FD_ZERO(&exceptfds);
202   while (msgEltPtr) {
203     if (msgEltPtr->GetEvents()->GetCount()) {
204       msgEltPtr->GetEvents()->GetFdSets(maxfds, readfds, writefds, exceptfds);
205     }
206     msgEltPtr = msgEltPtr->GetNext();
207   }
208 })
209
210 int CEventList::HandleEvents(const int maxfds,
211                              const fd_set &readfds,
212                              const fd_set &writefds,
213                              const fd_set &exceptfds)
214 {
215   lock();
216   CEventListElt *msgEltPtr = m_head.GetNext();
217   int status = SNMP_CLASS_SUCCESS;
218   while (msgEltPtr){
219     if (msgEltPtr->GetEvents()->GetCount()) {
220       unlock();
221       status = msgEltPtr->GetEvents()->HandleEvents(maxfds, readfds, writefds,
222                                                     exceptfds);
223       lock();
224     }
225     msgEltPtr = msgEltPtr->GetNext();
226   }
227   unlock();
228   return status;
229 }
230
231 #endif // HAVE_POLL_SYSCALL
232
233 int CEventList::DoRetries(const msec &sendtime) REENTRANT ({
234
235   CEventListElt *msgEltPtr = m_head.GetNext();
236   int status = SNMP_CLASS_SUCCESS;
237   while (msgEltPtr){
238     if (msgEltPtr->GetEvents()->GetCount()) {
239       status = msgEltPtr->GetEvents()->DoRetries(sendtime);
240     }
241     msgEltPtr = msgEltPtr->GetNext();
242   }
243   return status;
244 })
245
246 int CEventList::Done() REENTRANT ({
247
248   CEventListElt *msgEltPtr = m_head.GetNext();
249   int status = SNMP_CLASS_SUCCESS;
250
251   if (m_done) {
252     m_done--;
253     return 1;
254   }
255
256   while (msgEltPtr){
257     if (msgEltPtr->GetEvents()->GetCount()) {
258       status = msgEltPtr->GetEvents()->Done();
259       if (status)
260         break;
261     }
262     msgEltPtr = msgEltPtr->GetNext();
263   }
264   return status;
265 })
266
267 #ifdef SNMP_PP_NAMESPACE
268 }; // end of namespace Snmp_pp
269 #endif