2 * A simple client program using ACE_Svc_Handler and ACE_Connector.
5 #include "ace/OS_NS_stdio.h"
6 #include "ace/OS_NS_errno.h"
7 #include "ace/OS_NS_string.h"
8 #include "ace/OS_NS_sys_time.h"
11 // Listing 2 code/ch07
12 int Client::open (void *p
)
14 ACE_Time_Value
iter_delay (2); // Two seconds
15 if (super::open (p
) == -1)
17 this->notifier_
.reactor (this->reactor ());
18 this->msg_queue ()->notification_strategy (&this->notifier_
);
19 this->iterations_
= 0;
20 return this->reactor ()->schedule_timer
21 (this, 0, ACE_Time_Value::zero
, iter_delay
);
25 // Listing 3 code/ch07
26 int Client::handle_input (ACE_HANDLE
)
29 ssize_t recv_cnt
= this->peer ().recv (buf
, sizeof (buf
) - 1);
32 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%.*C"),
33 static_cast<int> (recv_cnt
),
38 if (recv_cnt
== 0 || ACE_OS::last_error () != EWOULDBLOCK
)
40 this->reactor ()->end_reactor_event_loop ();
47 // Listing 4 code/ch07
48 int Client::handle_timeout(const ACE_Time_Value
&, const void *)
50 if (++this->iterations_
>= ITERATIONS
)
52 this->peer ().close_writer ();
56 ACE_Message_Block
*mb
= 0;
57 ACE_NEW_RETURN (mb
, ACE_Message_Block (128), -1);
58 int nbytes
= ACE_OS::sprintf
59 (mb
->wr_ptr (), "Iteration %d\n", this->iterations_
);
60 ACE_ASSERT (nbytes
> 0);
61 mb
->wr_ptr (static_cast<size_t> (nbytes
));
67 // Listing 5 code/ch07
68 int Client::handle_output (ACE_HANDLE
)
70 ACE_Message_Block
*mb
= 0;
71 ACE_Time_Value
nowait (ACE_OS::gettimeofday ());
72 while (-1 != this->getq (mb
, &nowait
))
75 this->peer ().send (mb
->rd_ptr (), mb
->length ());
78 ACE_TEXT ("(%P|%t) %p\n"),
81 mb
->rd_ptr (static_cast<size_t> (send_cnt
));
82 if (mb
->length () > 0)
89 if (this->msg_queue ()->is_empty ())
90 this->reactor ()->cancel_wakeup
91 (this, ACE_Event_Handler::WRITE_MASK
);
93 this->reactor ()->schedule_wakeup
94 (this, ACE_Event_Handler::WRITE_MASK
);
99 // Listing 6 code/ch07
100 int ACE_TMAIN (int, ACE_TCHAR
*[])
102 ACE_INET_Addr
port_to_connect (ACE_TEXT ("HAStatus"), ACE_LOCALHOST
);
103 ACE_Connector
<Client
, ACE_SOCK_CONNECTOR
> connector
;
105 Client
*pc
= &client
;
106 if (connector
.connect (pc
, port_to_connect
) == -1)
107 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
108 ACE_TEXT ("connect")), 1);
110 ACE_Reactor::instance ()->run_reactor_event_loop ();
115 // Listing 7 code/ch07