Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / ASX / Event_Server / Transceiver / transceiver.cpp
bloba4c4173f5622e0de934e64e6a09ac7a0f5f57bff
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.
18 int
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; )
28 switch (c)
30 case 'C':
31 this->role_ = ACE_TEXT ("Consumer");
32 break;
33 case 'h':
34 this->host_name_ = get_opt.opt_arg ();
35 break;
36 case 'p':
37 this->port_number_ = ACE_OS::atoi (get_opt.opt_arg ());
38 break;
39 case 'S':
40 this->role_ = ACE_TEXT ("Supplier");
41 break;
42 default:
43 ACE_ERROR_RETURN ((LM_ERROR,
44 ACE_TEXT ("usage: %n [-CS] [-h host_name] [-p portnum]\n")),
45 -1);
46 /* NOTREACHED */
47 break;
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
54 // hack...
55 if (ACE_OS::strcmp (this->role_, ACE_TEXT ("Supplier")) == 0
56 && this->port_number_ == ACE_DEFAULT_SERVER_PORT)
57 this->port_number_++;
58 return 0;
61 int
62 Event_Transceiver::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
64 ACE_Reactor::instance ()->end_reactor_event_loop ();
65 return 0;
68 // Close down via SIGINT or SIGQUIT.
70 int
71 Event_Transceiver::handle_signal (int, siginfo_t *, ucontext_t *)
73 ACE_Reactor::instance ()->end_reactor_event_loop ();
74 return 0;
77 Event_Transceiver::Event_Transceiver ()
81 Event_Transceiver::Event_Transceiver (int argc, ACE_TCHAR *argv[])
83 if (this->parse_args (argc, argv) == -1)
84 ACE_ERROR ((LM_ERROR,
85 ACE_TEXT ("%p\n"),
86 ACE_TEXT ("parse_args")));
87 else
89 ACE_Sig_Set sig_set;
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
96 (sig_set,
97 this) == -1)
98 ACE_ERROR ((LM_ERROR,
99 ACE_TEXT ("%p\n"),
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,
109 ACE_TEXT ("%p\n"),
110 ACE_TEXT ("register_stdin_handler")));
112 // Address of the server.
113 ACE_INET_Addr server_addr (this->port_number_,
114 this->host_name_);
116 ACE_Connector<Event_Transceiver, ACE_SOCK_CONNECTOR> connector;
118 // We need a pointer here because connect takes a reference to a
119 // pointer!
120 Event_Transceiver *etp = this;
122 // Establish the connection to the Event Server.
123 if (connector.connect (etp,
124 server_addr) == -1)
126 ACE_ERROR ((LM_ERROR,
127 ACE_TEXT ("%p\n"),
128 this->host_name_));
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
140 // the socket.
141 if (ACE_Reactor::instance ()->register_handler
142 (this,
143 ACE_Event_Handler::READ_MASK) == -1)
144 ACE_ERROR_RETURN ((LM_ERROR,
145 ACE_TEXT ("%p\n"),
146 ACE_TEXT ("register_handler")),
147 -1);
148 return 0;
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 ();
157 else
158 return this->receiver ();
162 Event_Transceiver::transmitter ()
164 ACE_DEBUG ((LM_DEBUG,
165 ACE_TEXT ("(%P|%t) entering %s transmitter\n"),
166 this->role_));
168 char buf[BUFSIZ];
169 ssize_t n = ACE_OS::read (ACE_STDIN, buf, sizeof buf);
170 int result = 0;
172 if (n <= 0 || this->peer ().send_n (buf, n) != n)
173 result = -1;
175 ACE_DEBUG ((LM_DEBUG,
176 ACE_TEXT ("(%P|%t) leaving %s transmitter\n"),
177 this->role_));
178 return result;
182 Event_Transceiver::receiver ()
184 ACE_DEBUG ((LM_DEBUG,
185 ACE_TEXT ("(%P|%t) entering %s receiver\n"),
186 this->role_));
188 char buf[BUFSIZ];
190 ssize_t n = this->peer ().recv (buf, sizeof buf);
191 int result = 0;
193 if (n <= 0
194 || ACE_OS::write (ACE_STDOUT, buf, n) != n)
195 result = -1;
197 ACE_DEBUG ((LM_DEBUG,
198 ACE_TEXT ("(%P|%t) leaving %s receiver\n"),
199 this->role_));
200 return result;
204 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
206 if (ACE_Service_Config::open (argv[0]) == -1
207 && errno != ENOENT)
208 ACE_ERROR_RETURN ((LM_ERROR,
209 ACE_TEXT ("%p\n"),
210 ACE_TEXT ("open")),
211 -1);
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,
219 ACE_TEXT ("%p\n"),
220 ACE_TEXT ("Event_Transceiver constructor failed")),
221 -1);
224 // Run event loop until either the event server shuts down or we get
225 // a SIGINT.
226 ACE_Reactor::instance ()->run_reactor_event_loop ();
227 return 0;