summaryrefslogtreecommitdiff
path: root/examples/Reactor/WFMO_Reactor/test_removals.cpp
blob: 1c4b4acf687b9c278a6c3c7007b84eb1090b8684 (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
// $Id$
//
// ============================================================================
//
// = LIBRARY
//    examples
//
// = FILENAME
//    test_removals.cpp
//
// = DESCRIPTION
//
//    Tests the Reactor's ability to handle simultaneous events.  If
//    you pass anything on the command-line, then each handler
//    requests to be removed from the Reactor after each event.
//
// = AUTHOR
//    Tim Harrison
//    Irfan Pyarali
//
// ============================================================================

#include "ace/Reactor.h"
#include "ace/Service_Config.h"
#include "ace/Synch.h"

ACE_RCSID(WFMO_Reactor, test_removals, "$Id$")

class Event_Handler : public ACE_Event_Handler
// = TITLE
//    Generic Event Handler.
//
// = DESCRIPTION
//
//    Creates event.  Registers with Reactor.  Signals event.  If
//    created with -close_down- it returns -1 from handle signal.
{
public:
  Event_Handler (int event_number,
                 int close_down)
    : event_number_ (event_number),
    close_down_ (close_down)
    {
      if (ACE_Reactor::instance ()->register_handler (this,
                                                      this->event_.handle ()) == -1)
        ACE_ERROR ((LM_ERROR, "%p\tevent handler %d cannot be added to Reactor\n", "", event_number_));
      this->event_.signal ();
    }

  virtual int handle_signal (int index, siginfo_t *, ucontext_t *)
    {
      if (this->close_down_)
        return -1;
      else
        return 0;
    }

  virtual int handle_close (ACE_HANDLE, ACE_Reactor_Mask)
    {
      ACE_DEBUG ((LM_DEBUG, "event handler %d closed.\n", event_number_));
      delete this;
      return 0;
    }

  virtual ACE_HANDLE get_handle (void) const
    {
      return event_.handle ();
    }

private:
  int event_number_;
  // Our event number.

  int close_down_;
  // Shall we close down or not.

  ACE_Event event_;
  // Signaled to shut down the handler.
};

int
main (int argc, char *argv[])
{
  int close_down = argc > 1 ? 1 : 0;

  for (size_t i = 1; i <= ACE_Reactor::instance ()->size (); i++)
    new Event_Handler (i, close_down);

  int result = 0;
  ACE_Time_Value time (1);
  while (1)
    {
      result = ACE_Reactor::instance ()->handle_events (time);
      if (result == 0 && errno == ETIME)
        {
          ACE_DEBUG ((LM_DEBUG, "No more work left: timing out\n"));
          break;
        }
      if (result == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "%p.\n", "main"), -1);
    }
  return 0;
}