Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / protocols / tests / HTBP / Send_Large_Msg / server.cpp
blob2e43f9bfba4d25581f14efc4081e9158e747302f
1 #include "ace/Log_Msg.h"
2 #include "ace/Get_Opt.h"
4 #include "ace/HTBP/HTBP_Session.h"
5 #include "ace/HTBP/HTBP_Stream.h"
6 #include "ace/HTBP/HTBP_Addr.h"
8 #include "ace/SOCK_Acceptor.h"
9 #include "ace/SOCK_Stream.h"
10 #include "ace/OS_NS_stdio.h"
11 #include "ace/OS_NS_sys_socket.h"
12 #include "ace/os_include/os_netdb.h"
14 const size_t Send_Size = 4*1024;
15 const size_t Loops = 10;
16 const size_t Total_Size = Send_Size * Loops;
17 unsigned port = 8088;
18 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("o:p:"));
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[5000];
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::HTBP::Channel *channels[2];
68 ACE_SOCK_Acceptor acc(local,1);
69 ACE_DEBUG ((LM_DEBUG,
70 ACE_TEXT("(%P|%t) Server: ")
71 ACE_TEXT("server is ready\n")));
73 if (notifier_file != 0)
75 FILE *f = ACE_OS::fopen (notifier_file,ACE_TEXT("w+"));
76 const char *msg = "server ready";
77 ACE_OS::fwrite (msg,ACE_OS::strlen(msg),1,f);
78 ACE_OS::fclose (f);
81 acc.accept(sock[0]);
82 channels[0] = new ACE::HTBP::Channel (sock[0]);
83 ACE_DEBUG ((LM_DEBUG,
84 ACE_TEXT("(%P|%t) Server: ")
85 ACE_TEXT("Got sock[0], handle = %d\n"),
86 sock[0].get_handle()));
87 acc.accept(sock[1]);
88 channels[1] = new ACE::HTBP::Channel(sock[1]);
89 ACE_DEBUG ((LM_DEBUG,
90 ACE_TEXT("(%P|%t) Server: ")
91 ACE_TEXT("Got sock[1], handle = %d\n"),
92 sock[1].get_handle()));
93 int res = 0;
94 while ((res = channels[0]->pre_recv()) != 0)
96 ACE_DEBUG ((LM_DEBUG,
97 ACE_TEXT("(%P|%t) Server: ")
98 ACE_TEXT("res = %d. waiting 1 sec. %p\n"),
99 res,
100 ACE_TEXT("stream.pre_recv()")));
101 ACE_OS::sleep (1);
104 ACE_DEBUG ((LM_DEBUG,
105 ACE_TEXT("(%P|%t) Server: ")
106 ACE_TEXT("Read from channel2\n")));
107 while ((res = channels[1]->pre_recv()) != 0)
109 ACE_DEBUG ((LM_DEBUG,
110 ACE_TEXT("(%P|%t) Server: ")
111 ACE_TEXT("res = %d, waiting 1 sec. %p\n"),
112 res,
113 ACE_TEXT("stream2.pre_recv()")));
114 ACE_OS::sleep (1);
117 ACE::HTBP::Session *session = channels[0]->session();
118 ACE::HTBP::Stream stream (session);
120 ACE_DEBUG ((LM_DEBUG,
121 ACE_TEXT("(%P|%t) Server: ")
122 ACE_TEXT("using streams %d, %d. Got session = %x\n"),
123 sock[0].get_handle(),sock[1].get_handle(),session));
125 ssize_t got = 1;
126 ssize_t total_recv = 0;
128 while (got != 0)
130 errno = 0;
131 got = stream.recv (buffer, sizeof (buffer));
132 ACE_DEBUG ((LM_DEBUG,
133 ACE_TEXT("(%P|%t) Server: ")
134 ACE_TEXT("got = %b\n"), got));
136 if (got < 0)
137 break;
138 total_recv += got;
141 ACE_DEBUG ((LM_DEBUG,
142 ACE_TEXT("(%P|%t) Server: ")
143 ACE_TEXT("received %b\n"),total_recv));
146 ACE_OS::strcpy (buffer,"I hear you !");
147 ssize_t n = stream.send (buffer, ACE_OS::strlen (buffer)+1);
148 if (n == -1)
149 ACE_ERROR_RETURN ((LM_ERROR,
150 ACE_TEXT("(%P|%t) Server: ")
151 ACE_TEXT("%p\n"),
152 ACE_TEXT("stream.send")),-1);
154 ACE_DEBUG ((LM_DEBUG,
155 ACE_TEXT("(%P|%t) Server: ")
156 ACE_TEXT("send returned %b\n"),
157 n));
159 ACE_OS::sleep(1); // prevent test failure on windows when the connection
160 // closes too fast.
161 ACE_DEBUG ((LM_DEBUG,
162 ACE_TEXT("(%P|%t) Server: ")
163 ACE_TEXT("deleting channels[1]\n")));
164 delete channels[1];
165 ACE_DEBUG ((LM_DEBUG,
166 ACE_TEXT("(%P|%t) Server: ")
167 ACE_TEXT("deleting channels[0]\n")));
168 delete channels[0];
169 return 0;