2 #include "ace/Get_Opt.h"
3 #include "ace/Truncate.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_unistd.h"
9 Handle_Broadcast::~Handle_Broadcast ()
14 Handle_Broadcast::Handle_Broadcast ()
19 Handle_Broadcast::open (const ACE_INET_Addr &r, int async)
21 if (this->ACE_SOCK_Dgram::open (r) == -1)
23 else if (async && ACE_SOCK_Dgram::enable (ACE_SIGIO) == -1)
30 Handle_Broadcast::info (ACE_TCHAR **strp, size_t length) const
32 ACE_TCHAR buf[BUFSIZ];
35 if (this->get_local_addr (sa) == -1)
40 ACE_OS::sprintf (buf, ACE_TEXT("%d/"), sa.get_port_number ());
41 ACE_OS::strcat (buf, ACE_TEXT("udp # tests broadcasting\n"));
43 if (*strp == 0 && (*strp = ACE_OS::strdup (buf)) == 0)
49 ACE_OS::strncpy (*strp, buf, length);
52 return ACE_Utils::truncate_cast<int> (ACE_OS::strlen (buf));
56 Handle_Broadcast::init (int argc, ACE_TCHAR *argv[])
58 ACE_INET_Addr sba (Handle_Broadcast::DEFAULT_PORT);
59 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("p:"), 0);
61 for (int c; (c = get_opt ()) != -1; )
65 sba.set (ACE_OS::atoi (get_opt.opt_arg ()));
71 if (this->open (sba) == -1)
72 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("open")), -1);
73 else if (ACE_Reactor::instance ()->register_handler
74 (this, ACE_Event_Handler::ACCEPT_MASK) == -1)
75 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
76 ACE_TEXT ("registering service with ACE_Reactor")), -1);
81 Handle_Broadcast::fini ()
83 return ACE_Reactor::instance ()->remove_handler
84 (this, ACE_Event_Handler::ACCEPT_MASK);
88 Handle_Broadcast::get_handle () const
90 return this->ACE_SOCK_Dgram::get_handle ();
94 Handle_Broadcast::handle_input (ACE_HANDLE)
97 char buf[8 * 1024]; /* 8 k buffer */
98 ssize_t n = this->recv (buf, sizeof buf, sa);
104 ACE_TEXT ("received broadcast datagram from host %C\n"),
105 sa.get_host_name ()));
107 ACE_OS::puts (ACE_TEXT ("----------------------------------------"));
108 ACE_OS::write (ACE_STDOUT, buf, n);
110 if (buf[n - 1] != '\n')
113 ACE_OS::puts (ACE_TEXT ("----------------------------------------"));
119 Handle_Broadcast::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
121 return this->ACE_SOCK_Dgram::close ();