Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / protocols / tests / HTBP / ping / client.cpp
blob6ae5ff57fd2d762f31f45fb86d791200cb02cc5c
1 /**
2 * client for a basic connection establishment test using HTBP
3 */
5 #include "ace/ACE.h"
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;
21 int
22 parse_args (int argc, ACE_TCHAR *argv[])
24 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("p:h:c:d"));
25 int c;
27 while ((c = get_opts ()) != -1)
28 switch (c)
30 case 'p':
31 remote_port = static_cast<unsigned>(ACE_OS::atoi (get_opts.opt_arg()));
32 break;
33 case 'h':
34 remote_host = get_opts.opt_arg ();
35 break;
36 case 'c':
37 config_file = get_opts.opt_arg ();
38 break;
39 case 'd':
40 ACE::debug (true);
41 break;
42 case '?':
43 default:
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)")
50 ACE_TEXT ("\n"),
51 argv [0]),
52 -1);
54 // Indicates successful parsing of the command line
55 return 0;
58 int
59 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
61 ACE_OS::socket_init (ACE_WSOCK_VERSION);
63 if (parse_args (argc, argv) != 0)
64 return 1;
65 if (remote_host == 0)
66 ACE_ERROR_RETURN ((LM_ERROR,
67 ACE_TEXT ("Client: No remote host specified\n")),
68 1);
70 ACE::HTBP::Environment env;
71 if (config_file != 0)
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)
83 ACE_DEBUG ((LM_DEBUG,
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);
95 ACE_DEBUG ((LM_DEBUG,
96 ACE_TEXT ("(%P|%t) Client: connecting to %s\n"),
97 proxy_s));
98 ACE::HTBP::Session session (remote,
99 local,
100 ACE::HTBP::Session::next_session_id (),
101 &proxy);
102 ACE::HTBP::Stream stream (&session);
104 char buffer[1000];
105 ssize_t n = 0;
106 int retrycount = 10;
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);
112 if (n == -1)
113 ACE_ERROR_RETURN ((LM_ERROR,
114 ACE_TEXT ("(%P|%t) Client: ")
115 ACE_TEXT ("%p\n"),
116 ACE_TEXT("stream send")),
117 -1);
119 ACE_DEBUG ((LM_DEBUG,
120 ACE_TEXT ("(%P|%t) Client: ")
121 ACE_TEXT ("send returned %b\n"),
122 n));
124 retrycount = 10;
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)
131 && retrycount > 0)
133 retrycount--;
134 ACE_DEBUG ((LM_DEBUG,
135 ACE_TEXT ("(%P|%t) Client: ")
136 ACE_TEXT ("waiting for ack, %d tries left\n"),
137 retrycount));
138 ACE_OS::sleep (1);
140 ACE_DEBUG ((LM_DEBUG,
141 ACE_TEXT ("(%P|%t) Client: ")
142 ACE_TEXT ("After wait for ack, n = %b, retry = %d\n"),
143 n, retrycount));
145 retrycount = 10;
146 while ((n = stream.recv (buffer, 1000)) == -1
147 && (errno == EWOULDBLOCK || errno == ETIME)
148 && retrycount > 0)
150 retrycount--;
151 ACE_DEBUG ((LM_DEBUG,
152 ACE_TEXT ("(%P|%t) Client: ")
153 ACE_TEXT ("waiting for inbound data, %d tries left\n"),
154 retrycount));
155 ACE_OS::sleep (1);
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"),
162 ACE_TEXT ("recv")));
163 break;
166 buffer[n] = 0;
167 ACE_DEBUG ((LM_DEBUG,
168 ACE_TEXT ("(%P|%t) Client: Got: \"%C\"\n"),
169 buffer));
172 ACE::HTBP::Channel *ob = session.outbound ();
173 n = stream.send ("goodbye", 7);
174 if (n == -1)
175 ACE_ERROR_RETURN ((LM_ERROR,
176 ACE_TEXT ("(%P|%t) Client: %p\n"),
177 ACE_TEXT ("stream send")),
178 -1);
180 ACE_DEBUG ((LM_DEBUG,
181 ACE_TEXT ("(%P|%t) Client: ")
182 ACE_TEXT ("send returned %b\n"),
183 n));
185 retrycount = 10;
186 while ((n = ob->recv_ack ()) == -1
187 && (errno == EWOULDBLOCK || errno == ETIME)
188 && retrycount > 0)
190 retrycount--;
191 ACE_DEBUG ((LM_DEBUG,
192 ACE_TEXT ("(%P|%t) Client: ")
193 ACE_TEXT ("waiting for ack, %d tries left\n"),
194 retrycount));
195 ACE_OS::sleep (1);
197 ACE_DEBUG ((LM_DEBUG,
198 ACE_TEXT ("(%P|%t) Client: ")
199 ACE_TEXT ("After wait for ack, n = %b, retry = %d\n"),
200 n, retrycount));
201 stream.close ();
202 return 0;