Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / protocols / tests / HTBP / ping / server.cpp
blobe0a52fba7eb37443234d0d76e85c3e3a313e4b7e
1 /**
2 * server for a basic connection establishment test using HTBP
3 */
5 #include "ace/Log_Msg.h"
7 #include "ace/HTBP/HTBP_Session.h"
8 #include "ace/HTBP/HTBP_Stream.h"
9 #include "ace/HTBP/HTBP_Addr.h"
11 #include "ace/SOCK_Acceptor.h"
12 #include "ace/SOCK_Stream.h"
13 #include "ace/Get_Opt.h"
14 #include "ace/OS_NS_stdio.h"
15 #include "ace/OS_NS_sys_socket.h"
16 #include "ace/os_include/os_netdb.h"
18 unsigned port = 8088;
19 const ACE_TCHAR *notifier_file = 0;
20 int
21 parse_args (int argc, ACE_TCHAR *argv[])
23 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("p:o:"));
24 int c;
26 while ((c = get_opts ()) != -1)
27 switch (c)
29 case 'o':
30 notifier_file = get_opts.opt_arg();
31 break;
32 case 'p':
33 port = static_cast<unsigned>(ACE_OS::atoi (get_opts.opt_arg()));
34 break;
35 case '?':
36 default:
37 ACE_ERROR_RETURN ((LM_ERROR,
38 "usage: %s "
39 "-p port "
40 "\n",
41 argv [0]),
42 -1);
44 // Indicates successful parsing of the command line
45 return 0;
48 int
49 ACE_TMAIN (int argc , ACE_TCHAR *argv[])
51 char buffer[1000];
52 ssize_t n = 0;
53 ACE_OS::socket_init (ACE_WSOCK_VERSION);
55 if (parse_args(argc, argv) != 0)
56 return 1;
58 ACE_TCHAR host[MAXHOSTNAMELEN+1];
59 if (ACE_OS::hostname (host, MAXHOSTNAMELEN) != 0)
60 ACE_ERROR_RETURN ((LM_ERROR,
61 ACE_TEXT ("(%P|%t) Server failure: %p\n"),
62 ACE_TEXT ("hostname")),
63 1);
65 ACE_INET_Addr local (port, host);
66 ACE_SOCK_Stream sock[2];
67 ACE_SOCK_Acceptor acc(local, 1);
68 local.addr_to_string (host, MAXHOSTNAMELEN);
69 ACE_DEBUG ((LM_DEBUG,
70 ACE_TEXT ("(%P|%t) Server is ready on %s\n"),
71 host));
72 if (notifier_file != 0)
74 FILE *f = ACE_OS::fopen (notifier_file,ACE_TEXT("w+"));
75 const char *msg = "server ready";
76 ACE_OS::fwrite (msg,ACE_OS::strlen(msg),1,f);
77 ACE_OS::fclose (f);
79 acc.accept (sock[0]);
80 ACE::HTBP::Channel channel1(sock[0]);
81 ACE_DEBUG ((LM_DEBUG,
82 ACE_TEXT ("(%P|%t) Server: ")
83 ACE_TEXT ("Got sock[0], handle = %d\n"),
84 sock[0].get_handle()));
85 acc.accept (sock[1]);
86 ACE::HTBP::Channel channel2 (sock[1]);
87 ACE_DEBUG ((LM_DEBUG,
88 ACE_TEXT ("(%P|%t) Server: ")
89 ACE_TEXT ("Got sock[1], handle = %d\n"),
90 sock[1].get_handle()));
91 int res = 0;
92 while ((res = channel1.pre_recv ()) != 0)
94 ACE_DEBUG ((LM_DEBUG,
95 ACE_TEXT ("(%P|%t) Server: ")
96 ACE_TEXT ("res = %d. waiting 1 sec. %p\n"),
97 res,
98 ACE_TEXT ("stream.pre_recv()")));
99 ACE_OS::sleep (1);
102 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Read from channel2\n")));
103 while ((res = channel2.pre_recv()) != 0)
105 ACE_DEBUG ((LM_DEBUG,
106 ACE_TEXT ("(%P|%t) Server: ")
107 ACE_TEXT ("res = %d, waiting 1 sec. %p\n"),
108 res,
109 ACE_TEXT ("stream2.pre_recv()")));
110 ACE_OS::sleep (1);
113 ACE::HTBP::Session *session = channel1.session();
114 ACE::HTBP::Stream stream (session);
116 ACE_DEBUG ((LM_DEBUG,
117 ACE_TEXT ("(%P|%t) Server: ")
118 ACE_TEXT ("using streams %d, %d. Got session = %@\n"),
119 sock[0].get_handle(),
120 sock[1].get_handle(),
121 session));
123 for (int i = 0; i >= 0; i++)
125 int retrycount = 10;
126 while ((n = stream.recv(buffer,1000)) == -1
127 && (errno == EWOULDBLOCK || errno == ETIME)
128 && retrycount > 0)
130 retrycount--;
131 ACE_DEBUG ((LM_DEBUG,
132 ACE_TEXT ("(%P|%t) Server: ")
133 ACE_TEXT ("waiting for inbound data, %d tries left\n"),
134 retrycount));
135 ACE_OS::sleep(1);
137 if (retrycount == 0 || n < 0)
138 break;
140 buffer[n] = 0;
142 ACE_DEBUG ((LM_DEBUG,
143 ACE_TEXT ("(%P|%t) Server: ")
144 ACE_TEXT ("Got: \"%C\"\n"), buffer));
146 if (ACE_OS::strstr (buffer,"goodbye") != 0)
147 break;
149 ACE_OS::sprintf (buffer,"I hear you %d",i);
150 n = stream.send (buffer,ACE_OS::strlen(buffer)+1);
151 if (n == -1)
152 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
153 ACE_TEXT ("stream.send")),
154 -1);
156 ACE_DEBUG ((LM_DEBUG,
157 ACE_TEXT ("(%P|%t) Server: Send returned %d\n"), n));
159 int got[2] = {-1,-1};
160 while (got[0] == -1 || got[1] == -1)
162 if (got[0] == -1)
164 if ((got[0] = (res =channel1.pre_recv())) == -1)
165 ACE_DEBUG ((LM_DEBUG,
166 ACE_TEXT ("(%P|%t) Server: ")
167 ACE_TEXT ("res = %d, waiting 1 sec. %p\n"),
168 got[0],
169 ACE_TEXT ("channel1.pre_recv()")));
171 if (got[1] == -1)
173 if ((got[1] = (res =channel2.pre_recv())) == -1)
174 ACE_DEBUG ((LM_DEBUG,
175 ACE_TEXT ("(%P|%t) Server: ")
176 ACE_TEXT ("res = %d, waiting 1 sec. %p\n"),
177 got[1],
178 ACE_TEXT ("channel2.pre_recv()")));
180 if (got[0] == -1 || got[1] == -1)
181 ACE_OS::sleep (1);
184 ACE_OS::sleep(1); // prevent test failure on windows when the connection
185 // closes too fast.
187 stream.close();
188 acc.close();
190 return 0;