arzmq-sys 0.6.3

Low-level bindings to the zeromq library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*********************************************************************
 *
 * AUTHORIZATION TO USE AND DISTRIBUTE
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: 
 *
 * (1) source code distributions retain this paragraph in its entirety, 
 *  
 * (2) distributions including binary code include this paragraph in
 *     its entirety in the documentation or other materials provided 
 *     with the distribution, and 
 *
 * (3) all advertising materials mentioning features or use of this 
 *     software display the following acknowledgment:
 * 
 *      "This product includes software written and developed 
 *       by Brian Adamson, Justin Dean, and Joe Macker of the Naval 
 *       Research Laboratory (NRL)." 
 *         
 *  The name of NRL, the name(s) of NRL  employee(s), or any entity
 *  of the United States Government may not be used to endorse or
 *  promote  products derived from this software, nor does the 
 *  inclusion of the NRL written and developed software  directly or
 *  indirectly suggest NRL or United States  Government endorsement
 *  of this product.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 ********************************************************************/
 
 
#ifndef _PROTO_TIMER
#define _PROTO_TIMER

#include "protoDebug.h"
#include "protoDefs.h"  // for ProtoSystemTime()
#include "protoTime.h"
#include "protoTree.h"  // for ProtoSortedTree
#include <math.h>

#define _SORTED_TIMERS 1

/**
 * @class ProtoTimer
 *
 * @brief This is a generic timer class which will notify a ProtoTimer::Listener upon timeout.
 *
 * A list of ProtoTimers is managed by the ProtoTimerMgr class after they are "activated".
 * The ProtoDispatcher's "main loop" calls ProtoTimerMgr::OnSystemTimeout at regular intervals
 * to determine if any timer objects have timed out.  If so, the callback function
 * associated with the ProtoTimer::Listener is invoked.  
 *
 */
class ProtoTimer 
#ifdef _SORTED_TIMERS
    : public ProtoSortedTree::Item
#endif // _SORTED_TIMERS
{
    friend class ProtoTimerMgr;
    
    public:
        ProtoTimer();
        ~ProtoTimer();
        
 		/**
		 * This function sets what class instance (timer listener) and method will be 
         * invoked upon a timer timeout..
         *
         * Note there are two forms for the listener method here.  The newer form has a "void"
         * return type and requires less consideration for its implementation.  The older 
         * (likely to be deprecated) form has a "bool" return type that is used to indicate
         * whether the ProtoTimer instance (and likely its listener) are any longer valid
         * (and implies whether or not it should be reinstalled if it is a repeating timer).
         * The current ProtoTimerMgr automates the detection/management of this case, thus
         * the newer, easier-to-use form.  The old form is retained simply for backwards
         * compatibility.
		 *
		 * IMPORTANT: For the "legacy" listener method form and _older_ Protolib versions, the 
         * 'bool' return value of this function MUST be set appropriately.  When returning "true" 
         * the 'repeat_count' will be decremented and the timer will be reactivated or 
         *  deactivated accordingly. So, if the timer is rescheduled, canceled, or deleted during 
         * the listener method invocation, the method MUST return "false'.  Again, NOTE that 
         * the _current_ Protolib automatically tracks timer change/deletion and the return value
         * is now ignored.

		 * NOTE: For VC++ 6.0 Debug builds, you _cannot_ use pre-compiled
         * headers with this template code.  Also, "/ZI" or "/Z7" compile options 
         * must NOT be specified.  (or else VC++ 6.0 experiences an "internal compiler error")
		 *
		 * @param theListener a pointer to the "listening" object
		 * @param timeoutHandler a pointer to the listener's callback function.
		 *
		 */
         
         
        // This is our new "ProtoTimer::SetListener()" where the listener timeoutHandler
        // has a "void" return type instead of the old "bool" since we don't care about
        // the return type anymore. (yay!) 
        template <class LTYPE>
        bool SetListener(LTYPE* theListener, void(LTYPE::*timeoutHandler)(ProtoTimer&))
        {
            if (NULL != listener) delete listener;
            listener = theListener ? new LISTENER_TYPE<LTYPE>(theListener, timeoutHandler) : NULL;
            return (NULL == listener) ? (NULL != theListener) : true;
        }
        
        //  This is the old "ProtoTimer::SetListener()" we keep for backwards-compatibility
        //  WARNING: This _will_ be deprecated in the future
		template <class LTYPE>
        bool SetListener(LTYPE* theListener, bool(LTYPE::*timeoutHandler)(ProtoTimer&))
        {
            if (NULL != listener) delete listener;
            listener = theListener ? new OLD_LISTENER_TYPE<LTYPE>(theListener, timeoutHandler) : NULL;
            return (NULL == listener) ? (NULL != theListener) : true;
        }
       
        /**
         * This method sets the interval (in seconds) after which the
         * timer will "fire" (i.e., the listener/method specified by 
         * ProtoTimer::SetListener() is invoked).
         *
         * @param theInterval timer interval in seconds
         */ 
        // Our ProtoTime class currently only handles 1 usec granularity (for non-zero timeouts)
        // so we enforce that constraint here.
        void SetInterval(double theInterval) 
            {interval = (theInterval >= 1.0e-06) ? theInterval : ((theInterval > 0.0) ? 1.0e-06 : 0.0);}
            
        double GetInterval() const {return interval;}
		/**
        * Timer repetition (0 =  one shot, -1 = infinite repetition)
        *
		* @param numRepeat number of timer repetitions (0 is one-shot)
		*/
		void SetRepeat(int numRepeat) 
            {repeat = numRepeat;}
        int GetRepeat() const 
            {return repeat;}
        
        /// Provided for "advanced" timer monitoring/control
        void ResetRepeat() 
            {repeat_count = repeat;}
       /// Provided for "advanced" timer monitoring/control
		void DecrementRepeatCount() 
            { if (repeat_count > 0) repeat_count--; }
       /// Provided for "advanced" timer monitoring/control
  
		int GetRepeatCount() {return repeat_count;}
       /// Provided for "advanced" timer monitoring/control
  
		/// This must be used with wisdom!
        void SetRepeatCount(int repeatCount)
            {repeat_count = repeatCount;}
        
        // Activity status/control
        /**
		* Returns true if timer is associated with a ProtoTimerMgr
		*/
		bool IsActive() const {return (NULL != mgr);}
        double GetTimeRemaining() const;
        const ProtoTime GetTimeout() const {return timeout;}
        void Reset()
        {
            ResetRepeat();
            if (IsActive()) Reschedule();
        }
        bool Reschedule();
        void Scale(double factor);
        void Deactivate();
        
        // Ancillary
        void SetUserData(const void* userData) {user_data = userData;}
        const void* GetUserData() {return user_data;}
        
        /**
		* Installer commands
        */
		enum Command {INSTALL, MODIFY, REMOVE};
#ifdef _SORTED_TIMERS        
        void UpdateKey()
            {timeout_key.SetValue(timeout);}
        void InvalidateKey()
            {timeout_key.Invalidate();}
        bool KeyIsValid() const
            {return timeout_key.IsValid();}
        
        // ProtoSortedTree::Item required overrides
        const char* GetKey() const
            {return timeout_key.GetKey();}
        unsigned int GetKeysize() const
            {return timeout_key.GetKeysize();}
#endif // _SORTED_TIMERS
                 
    private:
		/**
		* Invokes the "listener's" callback function.
		*
		* TODO: Update with new timer functionality.
		*
		* @retval Returns the result of the listener's callback function if
		* a listener exists for the timer.
		*/
        void DoTimeout() 
            {if (NULL !=  listener) listener->on_timeout(*this);}
            
		/**
		* @class Listener
		*
		* @brief Listener base class
		*/
        class Listener
        {
            public:
				/// virtual destructor
                virtual ~Listener() {}
                /// virtual on_timeout function
				virtual void on_timeout(ProtoTimer& theTimer) = 0;
        };
        /**
		* @class LISTENER_TYPE
		*
		* @brief Template for Listener classes. (This will be deprecated)
		*/
        template <class LTYPE>
        class LISTENER_TYPE : public Listener
        {
            public:
				/**
				* Listener constructor
				*
				* @param theListener pointer to the "listening" object
				* @param timeoutHandler *pointer to the Listener's callback function.
				*/
                LISTENER_TYPE(LTYPE* theListener, void(LTYPE::*timeoutHandler)(ProtoTimer&))
                    : listener(theListener), timeout_handler(timeoutHandler) {}
				/**
				* @retval Returns the result of the Listeners timeout handler.
                */
				void on_timeout(ProtoTimer& theTimer) 
                    {(listener->*timeout_handler)(theTimer);}
                /**
				* Duplicates the Listener member and returns a pointer to the new
				* object.
				*/
				Listener* duplicate()
                    {return (static_cast<Listener*>(new LISTENER_TYPE<LTYPE>(listener, timeout_handler)));}
            private:
                LTYPE* listener;
                void (LTYPE::*timeout_handler)(ProtoTimer&);
        };  // end class ProtoTimer::OLD_LISTENER_TYPE
        
		/**
		* @class OldListener
		*
		* @brief OldListener base class (will be deprecated)
		*/
        class OldListener : public Listener
        {
            public:
				/// virtual destructor
                virtual ~OldListener() {}
                /// virtual on_timeout function
				void on_timeout(ProtoTimer& theTimer)
                        {old_on_timeout(theTimer);}
            private:
                virtual bool old_on_timeout(ProtoTimer& theTimer) = 0;   
        };
        
        /**
		* @class OLD_LISTENER_TYPE
		*
		* @brief Template for Listener classes. (This will be deprecated)
		*/
        template <class LTYPE>
        class OLD_LISTENER_TYPE : public OldListener
        {
            public:
				/**
				* Listener contstructor
				*
				* @param theListener pointer to the "listening" object
				* @param timeoutHandler *pointer to the Listener's callback function.
				*/
                OLD_LISTENER_TYPE(LTYPE* theListener, bool(LTYPE::*timeoutHandler)(ProtoTimer&))
                    : listener(theListener), timeout_handler(timeoutHandler) {}
				/**
				* @retval Returns the result of the Listeners timeout handler.
                */
				bool old_on_timeout(ProtoTimer& theTimer) 
                    {return (listener->*timeout_handler)(theTimer);}
                /**
				* Duplicates the Listener member and returns a pointer to the new
				* object.
				*/
				OldListener* duplicate()
                    {return (static_cast<OldListener*>(new OLD_LISTENER_TYPE<LTYPE>(listener, timeout_handler)));}
            private:
                LTYPE* listener;
                bool (LTYPE::*timeout_handler)(ProtoTimer&);
        };  // end class ProtoTimer::OLD_LISTENER_TYPE
        
        Listener*                   listener;
        
        double                      interval;                
        int                         repeat;                  
        int                         repeat_count;            
        
        const void*                 user_data;
        ProtoTime                   timeout;    
        bool                        is_precise; 
        class ProtoTimerMgr*        mgr; 
#ifdef _SORTED_TIMERS      
        ProtoTime::Key              timeout_key;            
#else                            
        ProtoTimer*                 prev;                    
        ProtoTimer*                 next; 
#endif  // if/else _SORTED_TIMERS                   
};  // end class ProtoTimer

#ifdef _SORTED_TIMERS
class ProtoTimerList : public ProtoListTemplate<ProtoTimer> {};
class ProtoTimerTable : public ProtoSortedTreeTemplate<ProtoTimer> {};
#endif // _SORTED_TIMERS

/** 
 * @class ProtoTimerMgr
 *
 * @brief This class manages ProtoTimer instances when they are "activated". 
 * The ProtoDispatcher(see below) derives from this to manage ProtoTimers 
 * for an application. (The ProtoSimAgent base class contains a ProtoTimerMgr 
 * to similarly manage timers for a simulation instance). 
 */
class ProtoTimerMgr
{
    friend class ProtoTimer;
    
    public:
		/// Default constructor
        ProtoTimerMgr();
		/// Default destructor
        virtual ~ProtoTimerMgr();
        
        // ProtoTimer activation/deactivation
        virtual void ActivateTimer(ProtoTimer& theTimer);
        virtual void DeactivateTimer(ProtoTimer& theTimer);
        
        /**
        * @retval Returns "true" if there are any active timers
        */
        bool IsActive() const
            {return (NULL != GetShortHead());}
        
        /**
		* @retval Returns any time remaining for the active short timer or -1
		*/
        double GetTimeRemaining() const 
        {
            ProtoTimer* next = GetShortHead();
            return ((NULL != next) ? next->GetTimeRemaining() : -1.0);
        }
        
        /// Call this when the timer mgr's one-shot system timer fires
        void OnSystemTimeout();
        
        /// Special call to allow early dispatch of timeouts
        void DoSystemTimeout()
        {
            bool updateStatus = update_pending;
            update_pending = true;
            OnSystemTimeout();
            update_pending = updateStatus;
        }
        
        virtual void GetSystemTime(struct timeval& currentTime);

#ifdef _SORTED_TIMERS        
        // These are for testing
        ProtoTimerTable& GetTimerTable() {return timer_table;}
        ProtoTimerList& GetTimerList() {return timer_list;}
        ProtoTimerTable& GetLongTable() {return long_timer_table;}
#endif // _SORTED_TIMERS
                    
    protected:
        /// System timer association/management definitions and routines
        virtual bool UpdateSystemTimer(ProtoTimer::Command command,
                                       double              delay) = 0;// {return true;}
            
    private:
        // Methods used internally 
        void ReactivateTimer(ProtoTimer& theTimer, const ProtoTime& now);
        void InsertLongTimer(ProtoTimer& theTimer);
        bool InsertLongTimerReverse(ProtoTimer& theTimer);
        void RemoveLongTimer(ProtoTimer& theTimer);
        void InsertShortTimer(ProtoTimer& theTimer);
        bool InsertShortTimerReverse(ProtoTimer& theTimer);
        void RemoveShortTimer(ProtoTimer& theTimer);
        void Update();

#ifdef _SORTED_TIMERS        
        ProtoTimer* GetShortHead() const
        {
            ProtoTimer* next = timer_list.GetHead();
            return (NULL != next) ? next : timer_table.GetHead();
        }
        ProtoTimer* GetLongHead() const {return long_timer_table.GetHead();}
#else
        ProtoTimer* GetShortHead() const {return short_head;}
        ProtoTimer* GetLongHead() const {return long_head;} 
#endif // if/else _SORTED_TIMERS
        
        bool GetNextTimeout(ProtoTime& nextTimeout) const
        {
            ProtoTimer* next = GetShortHead();
            if (NULL != next)
            {
                nextTimeout = next->timeout;
                return true;
            }
            else
            {
                return false;
            }
        }
        void GetPulseTime(ProtoTime& pulseTime) const
        {
            pulseTime = pulse_mark;
            pulseTime += (1.0 - pulse_timer.GetTimeRemaining());
        }
        bool OnPulseTimeout(ProtoTimer& theTimer);
        
        static const double PRECISION_TIME_THRESHOLD;
        
        void GetCurrentSystemTime(struct timeval& currentTime)
        {
#if (defined(SIMULATE) && defined(NS2))
            GetSystemTime(currentTime);
#else
            ProtoSystemTime(currentTime);
#endif // if/else (SIMULATE && NS2)
        }
        
        // We need this to support some of our SIM code so it can
        // find its simulation context
        void GetCurrentProtoTime(ProtoTime& currentTime)
        {
#if (defined(SIMULATE) && defined(NS2))
           GetSystemTime(currentTime.AccessTimeVal());
#else
           currentTime.GetCurrentTime();
#endif // if/else (SIMULATE && NS2)
        }
       
        // Member variables
        bool            update_pending;
        bool            timeout_scheduled;
        ProtoTime       scheduled_timeout;
        ProtoTimer      pulse_timer;  // one second pulse timer
        ProtoTime       pulse_mark;

#ifdef _SORTED_TIMERS   
        unsigned int    timer_list_count;
        ProtoTimerList  timer_list;
        ProtoTimerTable timer_table;
        ProtoTimerTable long_timer_table;
#else                                 
        ProtoTimer*     long_head;
        ProtoTimer*     long_tail;
        ProtoTimer*     short_head;
        ProtoTimer*     short_tail;
#endif // if/else _SORTED_TIMERS
        ProtoTimer*     invoked_timer;  // timer whose listener is being invoked
};  // end class ProtoTimerMgr

#endif // _PROTO_TIMER