2 * client for a basic connection establishment test using HTBP
6 #include "ace/Log_Msg.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/OS_NS_sys_socket.h"
9 #include "ace/os_include/os_netdb.h"
11 #include "ace/HTBP/HTBP_Session.h"
12 #include "ace/HTBP/HTBP_Stream.h"
13 #include "ace/HTBP/HTBP_Addr.h"
14 #include "ace/HTBP/HTBP_ID_Requestor.h"
15 #include "ace/HTBP/HTBP_Environment.h"
17 const ACE_TCHAR
* remote_host
= 0;
18 const ACE_TCHAR
* config_file
= 0;
19 unsigned int remote_port
= 8088;
22 parse_args (int argc
, ACE_TCHAR
*argv
[])
24 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT ("p:h:c:d"));
27 while ((c
= get_opts ()) != -1)
31 remote_port
= static_cast<unsigned>(ACE_OS::atoi (get_opts
.opt_arg()));
34 remote_host
= get_opts
.opt_arg ();
37 config_file
= get_opts
.opt_arg ();
44 ACE_ERROR_RETURN ((LM_ERROR
,
45 ACE_TEXT ("usage: %s ")
46 ACE_TEXT ("-h remote_host ")
47 ACE_TEXT ("-p remote_port ")
48 ACE_TEXT ("-c config_file ")
49 ACE_TEXT ("-d (turn on extra logging)")
54 // Indicates successful parsing of the command line
59 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
61 ACE_OS::socket_init (ACE_WSOCK_VERSION
);
63 if (parse_args (argc
, argv
) != 0)
66 ACE_ERROR_RETURN ((LM_ERROR
,
67 ACE_TEXT ("Client: No remote host specified\n")),
70 ACE::HTBP::Environment env
;
72 env
.import_config (config_file
);
74 ACE::HTBP::ID_Requestor
req (&env
);
75 ACE::HTBP::Addr
local (ACE_TEXT_ALWAYS_CHAR (req
.get_HTID ()));
77 unsigned int proxy_port
= 0;
78 ACE_TString proxy_host
;
80 if (env
.get_proxy_port (proxy_port
) != 0 ||
81 env
.get_proxy_host (proxy_host
) != 0)
84 ACE_TEXT ("(%P|%t) Client: ")
85 ACE_TEXT("no proxy address in ")
86 ACE_TEXT("config, using direct connect\n")));
87 proxy_port
= remote_port
;
88 proxy_host
= remote_host
;
91 ACE_INET_Addr
proxy (proxy_port
, proxy_host
.c_str ());
92 ACE::HTBP::Addr
remote (remote_port
, ACE_TEXT_ALWAYS_CHAR (remote_host
));
93 ACE_TCHAR proxy_s
[MAXHOSTNAMELEN
+1];
94 proxy
.addr_to_string (proxy_s
, MAXHOSTNAMELEN
);
96 ACE_TEXT ("(%P|%t) Client: connecting to %s\n"),
98 ACE::HTBP::Session
session (remote
,
100 ACE::HTBP::Session::next_session_id (),
102 ACE::HTBP::Stream
stream (&session
);
107 for (int i
= 0; i
< 3; i
++)
109 ACE_OS::sprintf (buffer
, "Do you hear me? %d", i
);
110 ACE::HTBP::Channel
*ob
= session
.outbound ();
111 n
= stream
.send (buffer
, ACE_OS::strlen (buffer
)+1);
113 ACE_ERROR_RETURN ((LM_ERROR
,
114 ACE_TEXT ("(%P|%t) Client: ")
116 ACE_TEXT("stream send")),
119 ACE_DEBUG ((LM_DEBUG
,
120 ACE_TEXT ("(%P|%t) Client: ")
121 ACE_TEXT ("send returned %b\n"),
125 ACE_DEBUG ((LM_DEBUG
,
126 ACE_TEXT ("(%P|%t) Client: ")
127 ACE_TEXT ("after send, outbound = %@, ob = %@\n"),
128 session
.outbound (), ob
));
129 while ((n
= ob
->recv_ack ()) == -1
130 && (errno
== EWOULDBLOCK
|| errno
== ETIME
)
134 ACE_DEBUG ((LM_DEBUG
,
135 ACE_TEXT ("(%P|%t) Client: ")
136 ACE_TEXT ("waiting for ack, %d tries left\n"),
140 ACE_DEBUG ((LM_DEBUG
,
141 ACE_TEXT ("(%P|%t) Client: ")
142 ACE_TEXT ("After wait for ack, n = %b, retry = %d\n"),
146 while ((n
= stream
.recv (buffer
, 1000)) == -1
147 && (errno
== EWOULDBLOCK
|| errno
== ETIME
)
151 ACE_DEBUG ((LM_DEBUG
,
152 ACE_TEXT ("(%P|%t) Client: ")
153 ACE_TEXT ("waiting for inbound data, %d tries left\n"),
157 if (retrycount
== 0 || n
< 0)
159 ACE_DEBUG ((LM_DEBUG
,
160 ACE_TEXT ("(%P|%t) Client: ")
161 ACE_TEXT ("bailing after wait, %p\n"),
167 ACE_DEBUG ((LM_DEBUG
,
168 ACE_TEXT ("(%P|%t) Client: Got: \"%C\"\n"),
172 ACE::HTBP::Channel
*ob
= session
.outbound ();
173 n
= stream
.send ("goodbye", 7);
175 ACE_ERROR_RETURN ((LM_ERROR
,
176 ACE_TEXT ("(%P|%t) Client: %p\n"),
177 ACE_TEXT ("stream send")),
180 ACE_DEBUG ((LM_DEBUG
,
181 ACE_TEXT ("(%P|%t) Client: ")
182 ACE_TEXT ("send returned %b\n"),
186 while ((n
= ob
->recv_ack ()) == -1
187 && (errno
== EWOULDBLOCK
|| errno
== ETIME
)
191 ACE_DEBUG ((LM_DEBUG
,
192 ACE_TEXT ("(%P|%t) Client: ")
193 ACE_TEXT ("waiting for ack, %d tries left\n"),
197 ACE_DEBUG ((LM_DEBUG
,
198 ACE_TEXT ("(%P|%t) Client: ")
199 ACE_TEXT ("After wait for ack, n = %b, retry = %d\n"),