Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Reactor / HAStatus-AC.cpp
blob7579b51261a593eee463c73a78bd249f9057457c
1 #include "ace/OS_NS_errno.h"
2 #include "ace/OS_NS_sys_time.h"
3 #include "ace/os_include/os_netdb.h"
4 #include "ClientService.h"
6 // Listing 1 code/ch07
7 #include "ace/Log_Msg.h"
8 #include "ace/INET_Addr.h"
9 #include "ace/SOCK_Acceptor.h"
10 #include "ace/Reactor.h"
11 #include "ace/Acceptor.h"
13 typedef ACE_Acceptor<ClientService, ACE_SOCK_ACCEPTOR>
14 ClientAcceptor;
15 // Listing 1
17 // Listing 4 code/ch07
18 int
19 ClientService::open (void *p)
21 if (super::open (p) == -1)
22 return -1;
24 ACE_TCHAR peer_name[MAXHOSTNAMELEN];
25 ACE_INET_Addr peer_addr;
26 if (this->peer ().get_remote_addr (peer_addr) == 0 &&
27 peer_addr.addr_to_string (peer_name, MAXHOSTNAMELEN) == 0)
28 ACE_DEBUG ((LM_DEBUG,
29 ACE_TEXT ("(%P|%t) Connection from %s\n"),
30 peer_name));
31 return 0;
33 // Listing 4
35 // Listing 5 code/ch07
36 int
37 ClientService::handle_input (ACE_HANDLE)
39 const size_t INPUT_SIZE = 4096;
40 char buffer[INPUT_SIZE];
41 ssize_t recv_cnt, send_cnt;
43 recv_cnt = this->peer ().recv (buffer, sizeof(buffer));
44 if (recv_cnt <= 0)
46 ACE_DEBUG ((LM_DEBUG,
47 ACE_TEXT ("(%P|%t) Connection closed\n")));
48 return -1;
51 send_cnt =
52 this->peer ().send (buffer,
53 static_cast<size_t> (recv_cnt));
54 if (send_cnt == recv_cnt)
55 return 0;
56 if (send_cnt == -1 && ACE_OS::last_error () != EWOULDBLOCK)
57 ACE_ERROR_RETURN ((LM_ERROR,
58 ACE_TEXT ("(%P|%t) %p\n"),
59 ACE_TEXT ("send")),
60 0);
61 if (send_cnt == -1)
62 send_cnt = 0;
63 ACE_Message_Block *mb = 0;
64 size_t remaining =
65 static_cast<size_t> ((recv_cnt - send_cnt));
66 ACE_NEW_RETURN (mb, ACE_Message_Block (remaining), -1);
67 mb->copy (&buffer[send_cnt], remaining);
68 int output_off = this->msg_queue ()->is_empty ();
69 ACE_Time_Value nowait (ACE_OS::gettimeofday ());
70 if (this->putq (mb, &nowait) == -1)
72 ACE_ERROR ((LM_ERROR,
73 ACE_TEXT ("(%P|%t) %p; discarding data\n"),
74 ACE_TEXT ("enqueue failed")));
75 mb->release ();
76 return 0;
78 if (output_off)
79 return this->reactor ()->register_handler
80 (this, ACE_Event_Handler::WRITE_MASK);
81 return 0;
83 // Listing 5
85 // Listing 6 code/ch07
86 int
87 ClientService::handle_output (ACE_HANDLE)
89 ACE_Message_Block *mb = 0;
90 ACE_Time_Value nowait (ACE_OS::gettimeofday ());
91 while (-1 != this->getq (mb, &nowait))
93 ssize_t send_cnt =
94 this->peer ().send (mb->rd_ptr (), mb->length ());
95 if (send_cnt == -1)
96 ACE_ERROR ((LM_ERROR,
97 ACE_TEXT ("(%P|%t) %p\n"),
98 ACE_TEXT ("send")));
99 else
100 mb->rd_ptr (static_cast<size_t> (send_cnt));
101 if (mb->length () > 0)
103 this->ungetq (mb);
104 break;
106 mb->release ();
108 return (this->msg_queue ()->is_empty ()) ? -1 : 0;
110 // Listing 6
112 // Listing 7 code/ch07
114 ClientService::handle_close (ACE_HANDLE h, ACE_Reactor_Mask mask)
116 if (mask == ACE_Event_Handler::WRITE_MASK)
117 return 0;
118 return super::handle_close (h, mask);
120 // Listing 7
122 // Listing 2 code/ch07
123 int ACE_TMAIN (int, ACE_TCHAR *[])
125 ACE_INET_Addr port_to_listen ("HAStatus");
126 ClientAcceptor acceptor;
127 if (acceptor.open (port_to_listen,
128 ACE_Reactor::instance (),
129 ACE_NONBLOCK) == -1)
130 return 1;
132 ACE_Reactor::instance ()->run_reactor_event_loop ();
134 return (0);
136 // Listing 2
138 // Listing 8 code/ch07
139 // Listing 8