summaryrefslogtreecommitdiff
path: root/TAO/examples/Event_Comm/Consumer_Input_Handler.cpp
blob: 886d83e24f30975ba167281b5ff863bc2aaef566 (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
154
// $Id$

#include "Consumer_Input_Handler.h"
#include "Consumer_Handler.h"

ACE_RCSID(Consumer, Consumer_Input_Handler, "$Id$")

Consumer_Input_Handler::Consumer_Input_Handler (void)
  : receiver_handler_ (0),
    handle_ (0),
    consumer_initiated_shutdown_ (0)
{
  // No-Op.
}

Consumer_Input_Handler::~Consumer_Input_Handler (void)
{
  // No-Op.
}

int
Consumer_Input_Handler::consumer_initiated_shutdown (void)
{
  return this->consumer_initiated_shutdown_;
}

void
Consumer_Input_Handler::consumer_initiated_shutdown (int c)
{
  this->consumer_initiated_shutdown_ = c;
}

ACE_HANDLE
Consumer_Input_Handler::get_handle (void) const
{
  return this->handle_;
}

int
Consumer_Input_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
{
  ACE_DEBUG ((LM_DEBUG,
	      "closing down Consumer::Input_Handler\n"));

  Event_Comm::Consumer *receiver = this->receiver_handler_->receiver ();
  Event_Comm::Notifier *notifier = this->receiver_handler_->notifier ();

  if (this->consumer_initiated_shutdown ())
    {
      // Only try to unsubscribe if the Consumer initiated the
      // shutdown.  Otherwise, the Supplier initiated it and it has
      // probably gone away by now!
      TAO_TRY
        {
          // Gracefully shutdown the Receiver by removing it from the
          // Notifier's internal map.
	  if (notifier != 0)
	    notifier->unsubscribe (receiver, "", TAO_TRY_ENV);
	  TAO_CHECK_ENV;
        }
      TAO_CATCHANY
        {
          TAO_TRY_ENV.print_exception ("Consumer_Input_Handler::handle_close\n");
        }
      TAO_ENDTRY;
    }

  // Don't execute a callback here otherwise we'll recurse
  // indefinitely!
  if (this->receiver_handler_->reactor()->remove_handler
      (this,
       ACE_Event_Handler::READ_MASK | ACE_Event_Handler::DONT_CALL) == -1)
    ACE_ERROR ((LM_ERROR,
                "%p\n",
                "remove_handler"));

  return 0;
}

int Consumer_Input_Handler::initialize (Consumer_Handler *ch,
			      ACE_HANDLE handle)
{
  receiver_handler_ = ch;
  handle_ = handle;

  if (this->receiver_handler_->reactor()->register_handler
      (this,
       ACE_Event_Handler::READ_MASK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                "Consumer_Input_Handler::Input_Handler\n"), -1);
  return 0;
}

int
Consumer_Input_Handler::handle_input (ACE_HANDLE h)
{
  char buf[BUFSIZ];

  // Read up to BUFSIZ worth of data from ACE_HANDLE h.
  ssize_t n = ACE_OS::read (h, buf, sizeof buf - 1);

  if (n > 0)
    {
      // Null terminate the buffer, replacing the '\n' with '\0'.
      if (buf[n - 1] == '\n' || buf[n - 1] == EOF)
	buf[n - 1] = '\0';
      else
	buf[n] = '\0';
      ACE_DEBUG ((LM_DEBUG,
                  "notifying for event %s\n",
                  buf));

    }
  else
    {
      ACE_OS::strcpy (buf, "quit");
      ACE_DEBUG ((LM_DEBUG,
                  "shutting down Input_Handler\n"));
    }

  Event_Comm::Notifier *notifier =
    this->receiver_handler_->notifier ();

  ACE_ASSERT (notifier != 0);

  if (ACE_OS::strcmp (buf, "quit") == 0)
    {
      // Consumer wants to shutdown.
      this->consumer_initiated_shutdown (1);

      // Tell the main event loop to shutdown.
       this->receiver_handler_->close ();
    }
  else
    {
      TAO_TRY
        {
          Event_Comm::Event event;

          event.tag_ = ACE_OS::strdup (buf);

          notifier->push (event, TAO_TRY_ENV);
	  TAO_CHECK_ENV;
	}
      TAO_CATCHANY
        {
          TAO_TRY_ENV.print_exception("Unexpected exception\n");
        }
      TAO_ENDTRY;
    }

  /* NOTREACHED */
  return 0;
}