2 #include "ace/Get_Opt.h"
3 #include "ace/WFMO_Reactor.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_time.h"
7 #include "ace/OS_NS_unistd.h"
8 #include "ace/Truncate.h"
11 Handle_R_Stream::Handle_R_Stream ()
13 if (Handle_R_Stream::login_name == 0) {
14 #if !defined(ACE_LACKS_CUSERID)
15 Handle_R_Stream::login_name = ACE_OS::cuserid (Handle_R_Stream::login);
17 Handle_R_Stream::login[0] = '.';
18 Handle_R_Stream::login[1] = '\0';
19 Handle_R_Stream::login_name = Handle_R_Stream::login;
26 Handle_R_Stream::open (const ACE_INET_Addr &sia, int async)
28 if (this->ACE_SOCK_Acceptor::open (sia) == -1)
30 else if (async && this->ACE_SOCK_Acceptor::enable (ACE_SIGIO) == -1)
37 Handle_R_Stream::info (ACE_TCHAR **strp, size_t length) const
39 ACE_TCHAR buf[BUFSIZ];
42 if (this->get_local_addr (sa) == -1)
49 sa.get_port_number (),
51 ACE_TEXT("# tests remote stream\n"));
53 if (*strp == 0 && (*strp = ACE_OS::strdup (buf)) == 0)
59 ACE_OS::strncpy (*strp, buf, length);
62 return ACE_Utils::truncate_cast<int> (ACE_OS::strlen (buf));
66 Handle_R_Stream::init (int argc, ACE_TCHAR *argv[])
68 ACE_INET_Addr sis (Handle_R_Stream::DEFAULT_PORT);
69 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("p:"), 0);
71 for (int c; (c = get_opt ()) != -1; )
75 sis.set (ACE_OS::atoi (get_opt.opt_arg ()));
81 if (this->open (sis) == -1)
82 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1);
84 else if (ACE_Reactor::instance ()->register_handler
85 (this, ACE_Event_Handler::ACCEPT_MASK) == -1)
86 ACE_ERROR_RETURN ((LM_ERROR,
87 "registering service with ACE_Reactor\n"),
93 Handle_R_Stream::fini ()
95 return ACE_Reactor::instance ()->remove_handler
96 (this, ACE_Event_Handler::ACCEPT_MASK);
100 Handle_R_Stream::get_handle () const
102 return ACE_SOCK_Acceptor::get_handle ();
106 Handle_R_Stream::handle_input (ACE_HANDLE)
111 // Try to find out if the implementation of the reactor that we are
112 // using requires us to reset the event association for the newly
113 // created handle. This is because the newly created handle will
114 // inherit the properties of the listen handle, including its event
116 int reset_new_handle =
117 ACE_Reactor::instance ()->uses_event_associations ();
119 if (this->accept (this->new_remote_stream, // stream
123 reset_new_handle // reset new handler
127 ACE_DEBUG ((LM_INFO, "new_remote_stream fd = %d\n",
128 this->new_remote_stream.get_handle ()));
132 if (this->new_remote_stream.get_remote_addr (sa) == -1)
136 "accepted from host %s at port %d\n",
138 sa.get_port_number ()));
140 ACE_OS::puts (ACE_TEXT ("----------------------------------------"));
142 while ((bytes = this->new_remote_stream.recv (buf, sizeof buf)) > 0)
143 ACE_OS::write (ACE_STDOUT, buf, bytes);
145 ACE_OS::puts (ACE_TEXT ("----------------------------------------"));
147 time_t t = ACE_OS::time (0L);
148 ACE_TCHAR *cs = ACE_OS::ctime (&t);
150 if (this->new_remote_stream.send (4,
151 Handle_R_Stream::login_name,
152 ACE_OS::strlen (Handle_R_Stream::login_name),
154 ACE_OS::strlen (cs)) == -1)
157 if (this->new_remote_stream.close () == -1)
164 Handle_R_Stream::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
166 return this->ACE_SOCK_Acceptor::close ();