nidas  v1.2-1520
Looper.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 
28 #ifndef NIDAS_CORE_LOOPER_H
29 #define NIDAS_CORE_LOOPER_H
30 
31 #include "LooperClient.h"
32 
33 #include <nidas/util/Thread.h>
35 #include <nidas/util/IOException.h>
37 
38 #include <sys/time.h>
39 #include <sys/select.h>
40 #include <assert.h>
41 
42 #include <list>
43 
44 namespace nidas { namespace core {
45 
51 class Looper : public nidas::util::Thread {
52 public:
53 
54  Looper();
55 
65  void addClient(LooperClient *clnt,unsigned int msecPeriod,
66  unsigned int msecOffset)
68 
72  void removeClient(LooperClient *clnt);
73 
77  virtual int run() throw(nidas::util::Exception);
78 
82  static int gcd(unsigned int a, unsigned int b);
83 
84 private:
85 
86  void setupClientMaps();
87 
89 
90  std::list<LooperClient*> _clients;
91 
92  std::map<LooperClient*, unsigned int> _clientPeriods;
93  std::map<LooperClient*, unsigned int> _clientOffsets;
94 
95  std::map<LooperClient*, unsigned int> _clientDivs;
96  std::map<LooperClient*, unsigned int> _clientMods;
97 
98  unsigned int _sleepMsec;
99 
100 };
101 
102 }} // namespace nidas namespace core
103 
104 #endif
nidas::util::Mutex _clientMutex
Definition: Looper.h:88
Interface of a client of Looper.
Definition: LooperClient.h:38
void setupClientMaps()
Definition: Looper.cc:97
virtual int run()
Thread function.
Definition: Looper.cc:156
Looper is a Thread that periodically loops, calling the LooperClient::looperNotify() method of Looper...
Definition: Looper.h:51
static int gcd(unsigned int a, unsigned int b)
Utility function for finding greatest common divisor.
Definition: Looper.cc:91
void removeClient(LooperClient *clnt)
Remove a client from the Looper.
Definition: Looper.cc:65
unsigned int _sleepMsec
Definition: Looper.h:98
std::map< LooperClient *, unsigned int > _clientOffsets
Definition: Looper.h:93
std::map< LooperClient *, unsigned int > _clientPeriods
Definition: Looper.h:92
Definition: Exception.h:35
Definition: Thread.h:80
std::map< LooperClient *, unsigned int > _clientMods
Definition: Looper.h:96
Looper()
Definition: Looper.cc:36
void addClient(LooperClient *clnt, unsigned int msecPeriod, unsigned int msecOffset)
Add a client to the Looper whose LooperClient::looperNotify() method should be called every msec numb...
Definition: Looper.cc:45
std::list< LooperClient * > _clients
Definition: Looper.h:90
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
std::map< LooperClient *, unsigned int > _clientDivs
Definition: Looper.h:95
Definition: InvalidParameterException.h:35