Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / protocols / tests / HTBP / Reactor_Tests / client.cpp
blobade9454a8e57dce9fffbec7b75b15ba0e04c93b0
1 /**
2 * client for a reactor based connection establishment test using HTBP
3 */
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;
19 int
20 parse_args (int argc, ACE_TCHAR *argv[])
22 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("p:h:c:"));
23 int c;
25 while ((c = get_opts ()) != -1)
26 switch (c)
28 case 'p':
29 remote_port = static_cast<unsigned>(ACE_OS::atoi (get_opts.opt_arg()));
30 break;
31 case 'h':
32 remote_host = get_opts.opt_arg ();
33 break;
34 case 'c':
35 config_file = get_opts.opt_arg ();
36 break;
37 case '?':
38 default:
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 ")
44 ACE_TEXT ("\n"),
45 argv [0]),
46 -1);
48 // Indicates successful parsing of the command line
49 return 0;
52 int
53 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
55 ACE_OS::socket_init (ACE_WSOCK_VERSION);
57 if (parse_args(argc, argv) != 0)
58 return 1;
59 if (remote_host == 0)
60 ACE_ERROR_RETURN ((LM_ERROR,
61 ACE_TEXT ("Client: No remote host specified\n")),1);
63 ACE::HTBP::Environment env;
64 if (config_file != 0)
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)
76 ACE_DEBUG ((LM_DEBUG,
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);
91 ACE_DEBUG ((LM_DEBUG,
92 ACE_TEXT ("(%P|%t) Client starting session for ")
93 ACE_TEXT ("remote %s, local %s, proxy %s\n"),
94 remote_s,
95 local_s,
96 proxy_s));
97 ACE::HTBP::Session session (remote,
98 local,
99 ACE::HTBP::Session::next_session_id(),
100 &proxy);
101 ACE::HTBP::Stream stream (&session);
103 char buffer[1000];
104 ssize_t n = 0;
105 int retrycount = 10;
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);
111 if (n == -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));
119 retrycount = 10;
120 while ((n = ch->recv_ack()) == -1
121 && (errno == EWOULDBLOCK || errno == ETIME)
122 && retrycount > 0)
124 retrycount--;
125 ACE_DEBUG ((LM_DEBUG,
126 ACE_TEXT ("(%P|%t) Client: ")
127 ACE_TEXT("waiting for ack, %d tries left\n"),
128 retrycount));
129 ACE_OS::sleep (1);
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));
136 retrycount = 10;
137 while ((n = stream.recv(buffer,1000)) == -1
138 && (errno == EWOULDBLOCK || errno == ETIME)
139 && retrycount > 0)
141 retrycount--;
142 ACE_DEBUG ((LM_DEBUG,
143 ACE_TEXT ("(%P|%t) Client: ")
144 ACE_TEXT("waiting for inbound data, %d tries left\n"),
145 retrycount));
146 ACE_OS::sleep(1);
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"),
153 ACE_TEXT("recv")));
154 break;
157 buffer[n] = 0;
159 ACE_DEBUG ((LM_DEBUG,
160 ACE_TEXT ("(%P|%t) Client: ")
161 ACE_TEXT("Got: \"%s\"\n"),
162 buffer));
164 ACE::HTBP::Channel *ch = session.outbound();
165 if (ch == 0)
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);
170 if (n == -1)
171 ACE_ERROR_RETURN ((LM_ERROR,
172 ACE_TEXT ("(%P|%t) Client: ")
173 ACE_TEXT("%p\n"),
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));
180 retrycount = 10;
181 while (ch &&
182 (n = ch->recv_ack()) == -1
183 && (errno == EWOULDBLOCK || errno == ETIME)
184 && retrycount > 0)
186 retrycount--;
187 ACE_DEBUG ((LM_DEBUG,
188 ACE_TEXT ("(%P|%t) Client: ")
189 ACE_TEXT("waiting for ack, %d tries left\n"),
190 retrycount));
191 ACE_OS::sleep (1);
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));
198 return 0;