Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / examples / Connection / blocking / SPIPE-connector.cpp
blob330defa5d63f994307972c66a1c6bcd09f6a63ff
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 ()
23 int
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.
29 if (iterations_ == 0)
31 this->display_menu ();
33 if (ACE_Event_Handler::register_stdin_handler (
34 this,
35 ACE_Reactor::instance (),
36 ACE_Thread_Manager::instance ()) == -1)
38 ACE_ERROR_RETURN ((LM_ERROR,
39 ACE_TEXT ("%p\n"),
40 ACE_TEXT ("register_stdin_handler")),
41 -1);
43 else
45 return 0;
48 else // If iterations_ has been set, send iterations_ buffers.
50 const char *buffer =
51 "Oh give me a home\n"
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)
62 continue;
65 this->peer ().close ();
66 ACE_Reactor::instance ()->end_reactor_event_loop();
67 return 0;
71 int
72 Peer_Handler::handle_input (ACE_HANDLE)
74 char buf[BUFSIZ];
76 ssize_t n = ACE_OS::read (ACE_STDIN,
77 buf,
78 sizeof buf);
80 if (n > 0)
82 if (this->peer ().send (buf, n) != n)
84 ACE_ERROR_RETURN ((LM_ERROR,
85 ACE_TEXT ("%p\n"),
86 ACE_TEXT ("write failed")),
87 -1);
89 else if (n == 0) // Explicitly close the connection.
91 if (this->peer ().close () == -1)
93 ACE_ERROR_RETURN ((LM_ERROR,
94 ACE_TEXT ("%p\n"),
95 ACE_TEXT ("close")),
96 1);
99 return -1;
101 else
103 this->display_menu ();
107 return 0;
111 Peer_Handler::handle_close (ACE_HANDLE,
112 ACE_Reactor_Mask)
114 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Shutting down\n")));
115 return 0;
118 ACE_HANDLE
119 Peer_Handler::get_handle () const
121 return this->peer ().get_handle ();
124 void
125 Peer_Handler::display_menu ()
127 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nplease enter input..: ")));
130 IPC_Client::IPC_Client ()
131 : iterations_ (0),
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)
147 return -1;
148 // Handle signals through the ACE_Reactor.
149 else if (ACE_Reactor::instance ()->register_handler
150 (SIGINT,
151 &this->done_handler_) == -1)
152 return -1;
154 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Opening %s\n"), rendezvous_));
156 Peer_Handler *ph;
158 ACE_NEW_RETURN (ph,
159 Peer_Handler (iterations_),
160 -1);
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,
169 0) == -1)
170 ACE_ERROR_RETURN ((LM_ERROR,
171 ACE_TEXT ("%p\n"),
172 ACE_TEXT ("connect")),
173 -1);
175 return 0;
179 IPC_Client::fini ()
181 return 0;
185 IPC_Client::svc ()
187 ACE_Reactor::instance ()->run_reactor_event_loop ();
188 return 0;
192 IPC_Client::handle_close (ACE_HANDLE,
193 ACE_Reactor_Mask)
195 return 0;
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; )
207 switch (c)
209 case 'r':
210 ACE_OS::strncpy (rendezvous_,
211 get_opt.opt_arg (),
212 sizeof (rendezvous_) / sizeof (ACE_TCHAR));
213 break;
214 case 'i':
215 iterations_ = ACE_OS::atoi (get_opt.opt_arg ());
216 break;
217 case 'u':
218 default:
219 ACE_ERROR_RETURN ((LM_ERROR,
220 ACE_TEXT ("usage: %n -i <iterations>\n")
221 ACE_TEXT ("-r <rendezvous>\n")),
222 -1);
226 return 0;
230 #endif /* SPIPE_CONNECTOR */