summaryrefslogtreecommitdiff
path: root/examples/Reactor/ReactorEx/test_console_input.cpp
blob: f76f678d9dfd441f8e4957be29b699786ac13bbe (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
// $Id$
//
// ============================================================================
//
// = LIBRARY
//    examples
// 
// = FILENAME
//    test_console_input.cpp
//
// = DESCRIPTION
//
//    This application tests the working of WFMO_Reactor when users
//    are interested in console input.
//
// = AUTHOR
//    Irfan Pyarali
// 
// ============================================================================

#include "ace/Reactor.h"

class Event_Handler : public ACE_Event_Handler
{
public:
  Event_Handler (ACE_Reactor &reactor);
  int handle_signal (int signum, siginfo_t * = 0, ucontext_t * = 0);  
  int handle_close (ACE_HANDLE handle,
		    ACE_Reactor_Mask close_mask);
};

Event_Handler::Event_Handler (ACE_Reactor &reactor)
{
  this->reactor (&reactor);

  if (this->reactor ()->register_handler (this,
					  ACE_STDIN) != 0)
    ACE_ERROR ((LM_ERROR, "Registration with Reactor could not be done\n"));		    
}

int 
Event_Handler::handle_signal (int signum, siginfo_t *, ucontext_t *)
{
  TCHAR buffer[BUFSIZ];
  int result = ACE_OS::read (ACE_STDIN, buffer, sizeof buffer);
  buffer[result] = '\0';

  if (result <= 0)
    {
      this->reactor ()->close ();
      ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_OS::read"), -1);
    }

  if (ACE_OS::strcmp (__TEXT("quit\r\n"), buffer) == 0)
    this->reactor ()->close ();

  ACE_DEBUG ((LM_DEBUG, "User input: %s", buffer));
  
  return 0;
}

int 
Event_Handler::handle_close (ACE_HANDLE handle,
			     ACE_Reactor_Mask close_mask)
{
  ACE_DEBUG ((LM_DEBUG, "Event_Handler removed from Reactor\n"));
  return 0;
}

int
main (int, char *[])
{
  ACE_Reactor reactor;
  Event_Handler handler (reactor);

  int result = 0;
  while (result != -1)
    result = reactor.handle_events ();

  return 0;
}