1 #if !defined (SPIPE_CONNECTOR_C)
3 #define SPIPE_CONNECTOR_C
5 #include "ace/OS_NS_string.h"
6 #include "ace/SPIPE_Addr.h"
7 #include "ace/SPIPE_Connector.h"
8 #include "ace/Get_Opt.h"
9 #include "ace/Truncate.h"
10 #include "ace/OS_NS_unistd.h"
11 #include "SPIPE-connector.h"
14 Peer_Handler::Peer_Handler (int iterations
)
15 : iterations_ (iterations
)
19 Peer_Handler::~Peer_Handler ()
24 Peer_Handler::open (void *)
26 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("activating %d\n"), this->get_handle ()));
28 // If iterations_ has not been set, read from stdin.
31 this->display_menu ();
33 if (ACE_Event_Handler::register_stdin_handler (
35 ACE_Reactor::instance (),
36 ACE_Thread_Manager::instance ()) == -1)
38 ACE_ERROR_RETURN ((LM_ERROR
,
40 ACE_TEXT ("register_stdin_handler")),
48 else // If iterations_ has been set, send iterations_ buffers.
52 "Where the buffalo roam,\n"
53 "And the deer and the antelope play.\n"
54 "Where seldom is heard\n"
55 "A discouraging word,\n"
56 "And the skies are not cloudy all day.\n";
57 int length
= ACE_Utils::truncate_cast
<int> (ACE_OS::strlen (buffer
));
59 while (iterations_
-- > 0
60 && this->peer ().send_n (buffer
, length
) == length
)
65 this->peer ().close ();
66 ACE_Reactor::instance ()->end_reactor_event_loop();
72 Peer_Handler::handle_input (ACE_HANDLE
)
76 ssize_t n
= ACE_OS::read (ACE_STDIN
,
82 if (this->peer ().send (buf
, n
) != n
)
84 ACE_ERROR_RETURN ((LM_ERROR
,
86 ACE_TEXT ("write failed")),
89 else if (n
== 0) // Explicitly close the connection.
91 if (this->peer ().close () == -1)
93 ACE_ERROR_RETURN ((LM_ERROR
,
103 this->display_menu ();
111 Peer_Handler::handle_close (ACE_HANDLE
,
114 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Shutting down\n")));
119 Peer_Handler::get_handle () const
121 return this->peer ().get_handle ();
125 Peer_Handler::display_menu ()
127 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nplease enter input..: ")));
130 IPC_Client::IPC_Client ()
132 done_handler_ (ACE_Sig_Handler_Ex (ACE_Reactor::end_event_loop
))
134 ACE_OS::strcpy (rendezvous_
, ACE_TEXT ("acepipe"));
137 IPC_Client::~IPC_Client ()
141 // Dynamic linking hooks.
144 IPC_Client::init (int argc
, ACE_TCHAR
*argv
[])
146 if (this->parse_args (argc
, argv
) == -1)
148 // Handle signals through the ACE_Reactor.
149 else if (ACE_Reactor::instance ()->register_handler
151 &this->done_handler_
) == -1)
154 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Opening %s\n"), rendezvous_
));
159 Peer_Handler (iterations_
),
162 // Connect to the peer, reusing the local addr if necessary.
163 if (this->connect (ph
,
164 ACE_SPIPE_Addr (rendezvous_
),
165 ACE_Synch_Options::defaults
,
166 ACE_sap_any_cast (ACE_SPIPE_Addr
&),
168 O_RDWR
| FILE_FLAG_OVERLAPPED
,
170 ACE_ERROR_RETURN ((LM_ERROR
,
172 ACE_TEXT ("connect")),
187 ACE_Reactor::instance ()->run_reactor_event_loop ();
192 IPC_Client::handle_close (ACE_HANDLE
,
199 IPC_Client::parse_args (int argc
, ACE_TCHAR
*argv
[])
201 ACE_LOG_MSG
->open (argv
[0]);
203 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT ("ui:r:"));
205 for (int c
; (c
= get_opt ()) != -1; )
210 ACE_OS::strncpy (rendezvous_
,
212 sizeof (rendezvous_
) / sizeof (ACE_TCHAR
));
215 iterations_
= ACE_OS::atoi (get_opt
.opt_arg ());
219 ACE_ERROR_RETURN ((LM_ERROR
,
220 ACE_TEXT ("usage: %n -i <iterations>\n")
221 ACE_TEXT ("-r <rendezvous>\n")),
230 #endif /* SPIPE_CONNECTOR */