1 // Test program for the event transceiver. This program can play the
2 // role of either Consumer or Supplier. You can terminate this
3 // program by typing ^C....
5 #include "ace/OS_main.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/Service_Config.h"
8 #include "ace/SOCK_Connector.h"
9 #include "ace/Connector.h"
10 #include "ace/Get_Opt.h"
11 #include "ace/Signal.h"
12 #include "ace/OS_NS_unistd.h"
14 #include "transceiver.h"
16 // Handle the command-line arguments.
19 Event_Transceiver::parse_args (int argc
, ACE_TCHAR
*argv
[])
21 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT ("Ch:p:S"));
23 this->port_number_
= ACE_DEFAULT_SERVER_PORT
;
24 this->host_name_
= ACE_DEFAULT_SERVER_HOST
;
25 this->role_
= ACE_TEXT ("Supplier");
27 for (int c
; (c
= get_opt ()) != -1; )
31 this->role_
= ACE_TEXT ("Consumer");
34 this->host_name_
= get_opt
.opt_arg ();
37 this->port_number_
= ACE_OS::atoi (get_opt
.opt_arg ());
40 this->role_
= ACE_TEXT ("Supplier");
43 ACE_ERROR_RETURN ((LM_ERROR
,
44 ACE_TEXT ("usage: %n [-CS] [-h host_name] [-p portnum]\n")),
50 // Increment by 1 if we're the supplier to mirror the default
51 // behavior of the Event_Server (which sets the Consumer port to
52 // ACE_DEFAULT_SERVER_PORT and the Supplier port to
53 // ACE_DEFAULT_SERVER_PORT + 1). Note that this is kind of a
55 if (ACE_OS::strcmp (this->role_
, ACE_TEXT ("Supplier")) == 0
56 && this->port_number_
== ACE_DEFAULT_SERVER_PORT
)
62 Event_Transceiver::handle_close (ACE_HANDLE
, ACE_Reactor_Mask
)
64 ACE_Reactor::instance ()->end_reactor_event_loop ();
68 // Close down via SIGINT or SIGQUIT.
71 Event_Transceiver::handle_signal (int, siginfo_t
*, ucontext_t
*)
73 ACE_Reactor::instance ()->end_reactor_event_loop ();
77 Event_Transceiver::Event_Transceiver ()
81 Event_Transceiver::Event_Transceiver (int argc
, ACE_TCHAR
*argv
[])
83 if (this->parse_args (argc
, argv
) == -1)
86 ACE_TEXT ("parse_args")));
91 sig_set
.sig_add (SIGINT
);
92 sig_set
.sig_add (SIGQUIT
);
94 // Register to handle the SIGINT and SIGQUIT signals.
95 if (ACE_Reactor::instance ()->register_handler
100 ACE_TEXT ("register_handler")));
102 // We need to register <this> here before we're connected since
103 // otherwise <get_handle> will return the connection socket
104 // handle for the peer.
105 else if (ACE_Event_Handler::register_stdin_handler (this,
106 ACE_Reactor::instance (),
107 ACE_Thread_Manager::instance ()) == -1)
108 ACE_ERROR ((LM_ERROR
,
110 ACE_TEXT ("register_stdin_handler")));
112 // Address of the server.
113 ACE_INET_Addr
server_addr (this->port_number_
,
116 ACE_Connector
<Event_Transceiver
, ACE_SOCK_CONNECTOR
> connector
;
118 // We need a pointer here because connect takes a reference to a
120 Event_Transceiver
*etp
= this;
122 // Establish the connection to the Event Server.
123 if (connector
.connect (etp
,
126 ACE_ERROR ((LM_ERROR
,
129 ACE_Reactor::instance()->remove_handler (sig_set
);
130 ACE_Event_Handler::remove_stdin_handler (ACE_Reactor::instance(),
131 ACE_Thread_Manager::instance());
137 Event_Transceiver::open (void *)
139 // Register ourselves to be notified when there's data to read on
141 if (ACE_Reactor::instance ()->register_handler
143 ACE_Event_Handler::READ_MASK
) == -1)
144 ACE_ERROR_RETURN ((LM_ERROR
,
146 ACE_TEXT ("register_handler")),
152 Event_Transceiver::handle_input (ACE_HANDLE handle
)
154 // Determine whether we play the role of a consumer or a supplier.
155 if (handle
== ACE_STDIN
)
156 return this->transmitter ();
158 return this->receiver ();
162 Event_Transceiver::transmitter ()
164 ACE_DEBUG ((LM_DEBUG
,
165 ACE_TEXT ("(%P|%t) entering %s transmitter\n"),
169 ssize_t n
= ACE_OS::read (ACE_STDIN
, buf
, sizeof buf
);
172 if (n
<= 0 || this->peer ().send_n (buf
, n
) != n
)
175 ACE_DEBUG ((LM_DEBUG
,
176 ACE_TEXT ("(%P|%t) leaving %s transmitter\n"),
182 Event_Transceiver::receiver ()
184 ACE_DEBUG ((LM_DEBUG
,
185 ACE_TEXT ("(%P|%t) entering %s receiver\n"),
190 ssize_t n
= this->peer ().recv (buf
, sizeof buf
);
194 || ACE_OS::write (ACE_STDOUT
, buf
, n
) != n
)
197 ACE_DEBUG ((LM_DEBUG
,
198 ACE_TEXT ("(%P|%t) leaving %s receiver\n"),
204 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
206 if (ACE_Service_Config::open (argv
[0]) == -1
208 ACE_ERROR_RETURN ((LM_ERROR
,
213 // Create and initialize the transceiver.
214 Event_Transceiver
transceiver (argc
, argv
);
216 // Demonstrate how we can check if a constructor failed...
217 if (ACE_LOG_MSG
->op_status () == -1)
218 ACE_ERROR_RETURN ((LM_ERROR
,
220 ACE_TEXT ("Event_Transceiver constructor failed")),
224 // Run event loop until either the event server shuts down or we get
226 ACE_Reactor::instance ()->run_reactor_event_loop ();