2 * client for a reactor based connection establishment test using HTBP
5 #include "ace/Log_Msg.h"
6 #include "ace/Get_Opt.h"
7 #include "ace/OS_NS_sys_socket.h"
9 #include "ace/HTBP/HTBP_Session.h"
10 #include "ace/HTBP/HTBP_Stream.h"
11 #include "ace/HTBP/HTBP_Addr.h"
12 #include "ace/HTBP/HTBP_ID_Requestor.h"
13 #include "ace/HTBP/HTBP_Environment.h"
15 const ACE_TCHAR
* remote_host
= 0;
16 const ACE_TCHAR
* config_file
= 0;
17 unsigned remote_port
= 8088;
20 parse_args (int argc
, ACE_TCHAR
*argv
[])
22 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("p:h:c:"));
25 while ((c
= get_opts ()) != -1)
29 remote_port
= static_cast<unsigned>(ACE_OS::atoi (get_opts
.opt_arg()));
32 remote_host
= get_opts
.opt_arg ();
35 config_file
= get_opts
.opt_arg ();
39 ACE_ERROR_RETURN ((LM_ERROR
,
40 ACE_TEXT ("usage: %s ")
41 ACE_TEXT ("-h remote_host ")
42 ACE_TEXT ("-p remote_port ")
43 ACE_TEXT ("-c config_file ")
48 // Indicates successful parsing of the command line
53 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
55 ACE_OS::socket_init (ACE_WSOCK_VERSION
);
57 if (parse_args(argc
, argv
) != 0)
60 ACE_ERROR_RETURN ((LM_ERROR
,
61 ACE_TEXT ("Client: No remote host specified\n")),1);
63 ACE::HTBP::Environment env
;
65 env
.import_config (config_file
);
67 ACE::HTBP::ID_Requestor
req (&env
);
68 ACE::HTBP::Addr
local (ACE_TEXT_ALWAYS_CHAR(req
.get_HTID()));
70 unsigned proxy_port
= 0;
71 ACE_TString proxy_host
;
73 if (env
.get_proxy_port(proxy_port
) != 0 ||
74 env
.get_proxy_host(proxy_host
) != 0)
77 ACE_TEXT ("(%P|%t) Client: ")
78 ACE_TEXT("no proxy address in ")
79 ACE_TEXT("config, using direct connect\n")));
80 proxy_port
= remote_port
;
81 proxy_host
= remote_host
;
84 ACE_INET_Addr
proxy (proxy_port
, proxy_host
.c_str ());
85 ACE::HTBP::Addr
remote (remote_port
, ACE_TEXT_ALWAYS_CHAR (remote_host
));
87 ACE_TCHAR local_s
[512], remote_s
[512], proxy_s
[512];
88 remote
.addr_to_string (remote_s
, 512);
89 local
.addr_to_string (local_s
, 512);
90 proxy
.addr_to_string (proxy_s
, 512);
92 ACE_TEXT ("(%P|%t) Client starting session for ")
93 ACE_TEXT ("remote %s, local %s, proxy %s\n"),
97 ACE::HTBP::Session
session (remote
,
99 ACE::HTBP::Session::next_session_id(),
101 ACE::HTBP::Stream
stream (&session
);
106 for (int i
= 0; i
< 3; i
++)
108 ACE::HTBP::Channel
*ch
= session
.outbound();
109 ACE_OS::sprintf (buffer
,"Do you hear me? %d",i
);
110 n
= stream
.send (buffer
,ACE_OS::strlen(buffer
)+1);
112 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT("%p\n"),
113 ACE_TEXT("stream send")),-1);
115 ACE_DEBUG ((LM_DEBUG
,
116 ACE_TEXT ("(%P|%t) Client: ")
117 ACE_TEXT("send returned %d\n"),n
));
120 while ((n
= ch
->recv_ack()) == -1
121 && (errno
== EWOULDBLOCK
|| errno
== ETIME
)
125 ACE_DEBUG ((LM_DEBUG
,
126 ACE_TEXT ("(%P|%t) Client: ")
127 ACE_TEXT("waiting for ack, %d tries left\n"),
131 ACE_DEBUG ((LM_DEBUG
,
132 ACE_TEXT ("(%P|%t) Client: ")
133 ACE_TEXT("After wait for ack, n = %d, retry = %d\n"),
134 n
,retrycount
,ACE_ERRNO_GET
));
137 while ((n
= stream
.recv(buffer
,1000)) == -1
138 && (errno
== EWOULDBLOCK
|| errno
== ETIME
)
142 ACE_DEBUG ((LM_DEBUG
,
143 ACE_TEXT ("(%P|%t) Client: ")
144 ACE_TEXT("waiting for inbound data, %d tries left\n"),
148 if (retrycount
== 0 || n
< 0)
150 ACE_DEBUG ((LM_DEBUG
,
151 ACE_TEXT ("(%P|%t) Client: ")
152 ACE_TEXT("bailing after wait, %p\n"),
159 ACE_DEBUG ((LM_DEBUG
,
160 ACE_TEXT ("(%P|%t) Client: ")
161 ACE_TEXT("Got: \"%s\"\n"),
164 ACE::HTBP::Channel
*ch
= session
.outbound();
166 ACE_ERROR_RETURN ((LM_ERROR
,
167 ACE_TEXT ("(%P|%t) Client: ")
168 ACE_TEXT("session's outbound channel is null!\n")),1);
169 n
= stream
.send ("goodbye",7);
171 ACE_ERROR_RETURN ((LM_ERROR
,
172 ACE_TEXT ("(%P|%t) Client: ")
174 ACE_TEXT("stream send")),-1);
176 ACE_DEBUG ((LM_DEBUG
,
177 ACE_TEXT ("(%P|%t) Client: ")
178 ACE_TEXT("send returned %d\n"),n
));
182 (n
= ch
->recv_ack()) == -1
183 && (errno
== EWOULDBLOCK
|| errno
== ETIME
)
187 ACE_DEBUG ((LM_DEBUG
,
188 ACE_TEXT ("(%P|%t) Client: ")
189 ACE_TEXT("waiting for ack, %d tries left\n"),
193 ACE_DEBUG ((LM_DEBUG
,
194 ACE_TEXT ("(%P|%t) Client: ")
195 ACE_TEXT("After wait for ack, n = %d, retry = %d\n"),
196 n
,retrycount
,ACE_ERRNO_GET
));