Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / protocols / tests / HTBP / Send_Recv_Tests / client.cpp
blob23a8d8378367a98d9319c9145db8e7a3cdf2083b
2 //=============================================================================
3 /**
4 * @file client.cpp
6 * receive methods, over HTBP. The test forks two processes or spawns
7 * two threads (depending upon the platform) and then executes client
8 * and server allowing them to connect and exchange data in ways
9 * designed to exercise the send and recv functions.
11 * Right now, it primarily tests the iov-like send and recv
12 * functions, but others should be added to completely cover the
13 * possible scenarios.
15 * @author Steve Huston <shuston@riverace.com>
17 //=============================================================================
20 #include "ace/HTBP/HTBP_Stream.h"
21 #include "ace/HTBP/HTBP_Session.h"
22 #include "ace/HTBP/HTBP_ID_Requestor.h"
23 #include "ace/HTBP/HTBP_Environment.h"
25 #include "ace/Thread.h"
26 #include "ace/Thread_Manager.h"
27 #include "ace/SOCK_Connector.h"
28 #include "ace/SOCK_Acceptor.h"
29 #include "ace/SOCK_Stream.h"
30 #include "ace/Get_Opt.h"
31 #include "ace/OS_NS_sys_socket.h"
33 // Change to non-zero if test fails
34 static int Test_Result = 0;
36 const size_t Test3_Send_Size = 4*1024;
37 const size_t Test3_Loops = 10;
38 const size_t Test3_Total_Size = Test3_Send_Size * Test3_Loops;
40 const ACE_TCHAR * remote_host = 0;
41 const ACE_TCHAR * config_file = 0;
42 unsigned remote_port = 8088;
44 int
45 parse_args (int argc, ACE_TCHAR *argv[])
47 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("p:h:c:"));
48 int c;
50 while ((c = get_opts ()) != -1)
51 switch (c)
53 case 'p':
54 remote_port = static_cast<unsigned>(ACE_OS::atoi (get_opts.opt_arg()));
55 break;
56 case 'h':
57 remote_host = get_opts.opt_arg ();
58 break;
59 case 'c':
60 config_file = get_opts.opt_arg ();
61 break;
62 case '?':
63 default:
64 ACE_ERROR_RETURN ((LM_ERROR,
65 ACE_TEXT ("usage: %s ")
66 ACE_TEXT ("-h remote_host ")
67 ACE_TEXT ("-p remote_port ")
68 ACE_TEXT ("-c config_file ")
69 ACE_TEXT ("\n"),
70 argv [0]),
71 -1);
73 // Indicates successful parsing of the command line
74 return 0;
77 int
78 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
80 ACE_OS::socket_init (ACE_WSOCK_VERSION);
82 if (parse_args(argc, argv) != 0)
83 return 1;
84 if (remote_host == 0)
85 ACE_ERROR_RETURN ((LM_ERROR,
86 ACE_TEXT ("Client: No remote host specified\n")),1);
88 ACE::HTBP::Environment env;
89 if (config_file != 0)
90 env.import_config (config_file);
92 ACE::HTBP::ID_Requestor req (&env);
93 ACE::HTBP::Addr local(ACE_TEXT_ALWAYS_CHAR(req.get_HTID()));
95 unsigned proxy_port = 0;
96 ACE_TString proxy_host;
98 if (env.get_proxy_port(proxy_port) != 0 ||
99 env.get_proxy_host(proxy_host) != 0)
101 ACE_DEBUG ((LM_DEBUG,
102 ACE_TEXT("(%P|%t) Client: ")
103 ACE_TEXT("no proxy address in ")
104 ACE_TEXT("config, using direct connect\n")));
105 proxy_port = remote_port;
106 proxy_host = remote_host;
109 ACE_INET_Addr proxy(proxy_port,proxy_host.c_str());
110 ACE::HTBP::Addr remote (remote_port,
111 ACE_TEXT_ALWAYS_CHAR(remote_host));
113 ACE::HTBP::Session session(remote,
114 local,
115 ACE::HTBP::Session::next_session_id(),
116 &proxy);
118 ACE::HTBP::Stream stream(&session);
120 ACE_DEBUG ((LM_DEBUG,
121 ACE_TEXT ("(%P|%t) Connecting to port %d\n"),
122 remote.get_port_number()));
124 ACE_DEBUG ((LM_DEBUG,
125 ACE_TEXT ("(%P|%t) connected to %C\n"),
126 remote.get_host_name ()));
128 ACE_DEBUG ((LM_DEBUG, "(%P) ***** client TEST 1 *****\n"));
130 //******************* TEST 1 ******************************
132 // Do a iovec sendv - send the 255 byte buffer in 5 chunks. The
133 // server will verify that the correct data is sent, and that there
134 // is no more and no less.
136 u_char buffer[255];
137 size_t i;
138 ssize_t len;
140 // The server will verify that this data pattern gets there intact.
142 for (i = 0; i < sizeof buffer; ++i)
143 buffer[i] = static_cast<u_char> (i);
145 iovec iov[5];
147 iov[0].iov_base = reinterpret_cast<char *> (&buffer[0]);
148 iov[0].iov_len = 50;
150 iov[1].iov_base = reinterpret_cast<char *> (&buffer[50]);
151 iov[1].iov_len = 25;
153 iov[2].iov_base = reinterpret_cast<char *> (&buffer[75]);
154 iov[2].iov_len = 150;
156 iov[3].iov_base = reinterpret_cast<char *> (&buffer[225]);
157 iov[3].iov_len = 29;
159 iov[4].iov_base = reinterpret_cast<char *> (&buffer[254]);
160 iov[4].iov_len = 1;
162 len = stream.sendv (iov, 5);
163 ACE_DEBUG ((LM_DEBUG,"(%P) after send, len = %d\n"));
164 if (len == -1)
166 ACE_ERROR ((LM_ERROR,
167 ACE_TEXT ("(%P|%t) %p\n"),
168 ACE_TEXT ("Test 1, sendv failed")));
169 Test_Result = 1;
171 else
172 if (len != 255)
174 ACE_ERROR ((LM_ERROR,
175 ACE_TEXT ("(%P|%t) %p\n"),
176 ACE_TEXT ("Test 1, len = %d != 255\n"), len));
177 Test_Result = 1;
180 // ACE_OS::sleep (10);
181 ACE_DEBUG ((LM_DEBUG, "(%P) ***** client TEST 2 *****\n"));
183 //******************* TEST 2 ******************************
185 // The same data is coming back - receive it using recv (size_t n,
186 // ...) and compare it to the original data.
188 u_char buffer2[255];
190 ssize_t total = 0;
191 do {
192 len = stream.recv (buffer2+total, 145 - total);
193 ACE_DEBUG ((LM_DEBUG,
194 ACE_TEXT("(%P) Test 2: want %d bytes, got %d\n"),
195 145 - total,len));
196 if (len == -1 || errno == EWOULDBLOCK)
197 ACE_OS::sleep (1);
198 else
199 total += len;
200 } while ((len == -1 && errno == EWOULDBLOCK) || total < 145);
202 if (total != 145)
203 Test_Result = 1;
205 len = stream.recv (buffer2 + total, 110);
206 ACE_DEBUG ((LM_DEBUG,
207 ACE_TEXT("(%P) Test 2: second read want 110 bytes, got %d\n"),
208 len));
210 if (len != 110)
211 Test_Result = 1;
213 for (i = 0; Test_Result == 0 && i < 255; i++)
214 if (buffer2[i] != buffer[i])
216 ACE_ERROR ((LM_ERROR,
217 ACE_TEXT ("(%P|%t) Test 2, rcvd byte %d is %d, not %d\n"),
218 i, buffer2[i], buffer[i]));
219 Test_Result = 1;
223 stream.close ();
225 return Test_Result;