summaryrefslogtreecommitdiff
path: root/examples/APG/Reactor/Timer_State_Data.cpp
blob: ff94555337256819fed3f046a619ea32372fc86a (plain)
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
/**
 * $Id$
 *
 * Reactor examples
 *
 * Timers & state data
 */

#include "ace/OS_NS_time.h"
#include "ace/Log_Msg.h"
#include "ace/Reactor.h"
#include "ace/Event_Handler.h"

// Listing 0 code/ch07
class TemperatureSensor
{
public:
  TemperatureSensor (const char *location)
    : location_(location),
      count_(0),
      temperature_(0.0)
    // ...
  { }

  const char *location () const
  {
    return this->location_;
  }

  int querySensor (void)
  {
    // ...
    return ++this->count_;
  }

  float temperature (void) const
  {
    return this->temperature_;
  }

private:
  const char *location_;
  int count_;
  float temperature_;
  // ...
};
// Listing 0

// Listing 1 code/ch07
class TemperatureQueryHandler : public ACE_Event_Handler
{
public:
  TemperatureQueryHandler ()
    : ACE_Event_Handler(),
      counter_(0),
      averageTemperature_(0.0)
    // ...
  {
  }

  int handle_timeout (const ACE_Time_Value &current_time,
                      const void *arg)
  {
    time_t epoch = ((timespec_t)current_time).tv_sec;

    const TemperatureSensor *const_sensor =
      ACE_reinterpret_cast (const TemperatureSensor *, arg);
    TemperatureSensor *sensor =
      ACE_const_cast (TemperatureSensor *, const_sensor);

    int queryCount = sensor->querySensor ();
    this->updateAverageTemperature (sensor);

    ACE_DEBUG ((LM_INFO,
                ACE_TEXT ("%s\t")
                ACE_TEXT ("%d/%d\t")
                ACE_TEXT ("%.2f/%.2f\t")
                ACE_TEXT ("%s\n"),
                sensor->location (),
                ++this->counter_,
                queryCount,
                this->averageTemperature_,
                sensor->temperature (),
                ACE_OS::ctime(&epoch)));
    return 0;
  }

private:
  void updateAverageTemperature (TemperatureSensor *)
  {
    // ...
  }

  int counter_;
  float averageTemperature_;
};
// Listing 1

// Create a SIGINT handler so that we can exit
// the program politely
class SigintHandler : public ACE_Event_Handler
{
public:
  int handle_signal (int signum, siginfo_t * = 0,
                     ucontext_t * = 0)
  {
    if (signum == SIGINT)
      {
        ACE_Reactor::instance ()->end_reactor_event_loop ();
      }
    return 0;
  }
};

int ACE_TMAIN (int, ACE_TCHAR *[])
{
  // Listing 2 code/ch07
  TemperatureQueryHandler *temperatureMonitor =
    new TemperatureQueryHandler ();
  // Listing 2

  ACE_Time_Value initialDelay (3);
  ACE_Time_Value intervalOne (5);
  // Listing 3 code/ch07
  TemperatureSensor *sensorOne =
    new TemperatureSensor ("Kitchen");
  
  ACE_Reactor::instance ()->schedule_timer (temperatureMonitor,
                                            sensorOne,
                                            initialDelay,
                                            intervalOne);
  // Listing 3

  ACE_Time_Value intervalTwo (4);
  // Listing 4 code/ch07
  TemperatureSensor *sensorTwo =
    new TemperatureSensor ("Foyer");
  
  ACE_Reactor::instance ()->schedule_timer (temperatureMonitor,
                                            sensorTwo,
                                            initialDelay,
                                            intervalTwo);
  // Listing 4

  SigintHandler *handleExit = new SigintHandler ();

  ACE_Reactor::instance ()->register_handler (SIGINT,
                                              handleExit);

  ACE_Reactor::instance ()->run_reactor_event_loop ();

  return 0;
}