1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// $Id: server.cpp 91826 2010-09-17 09:11:31Z johnnyw $
#include "ace/Log_Msg.h"
#include "ace/Get_Opt.h"
#include "ace/HTBP/HTBP_Session.h"
#include "ace/HTBP/HTBP_Stream.h"
#include "ace/HTBP/HTBP_Addr.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_sys_socket.h"
#include "ace/os_include/os_netdb.h"
const size_t Send_Size = 4*1024;
const size_t Loops = 10;
const size_t Total_Size = Send_Size * Loops;
unsigned port = 8088;
const ACE_TCHAR *notifier_file = 0;
int
parse_args (int argc, ACE_TCHAR *argv[])
{
ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:p:"));
int c;
while ((c = get_opts ()) != -1)
switch (c)
{
case 'o':
notifier_file = get_opts.opt_arg();
break;
case 'p':
port = static_cast<unsigned>(ACE_OS::atoi (get_opts.opt_arg()));
break;
case '?':
default:
ACE_ERROR_RETURN ((LM_ERROR,
"usage: %s "
"-p port "
"\n",
argv [0]),
-1);
}
// Indicates successful parsing of the command line
return 0;
}
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
char buffer[5000];
ACE_OS::socket_init (ACE_WSOCK_VERSION);
if (parse_args (argc, argv) != 0)
return 1;
ACE_TCHAR host[MAXHOSTNAMELEN+1];
if (ACE_OS::hostname (host,MAXHOSTNAMELEN) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("(%P|%t) Server failure: %p\n"),
ACE_TEXT ("hostname")),
1);
ACE_INET_Addr local (port, host);
ACE_SOCK_Stream sock[2];
ACE::HTBP::Channel *channels[2];
ACE_SOCK_Acceptor acc(local,1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("server is ready\n")));
if (notifier_file != 0)
{
FILE *f = ACE_OS::fopen (notifier_file,ACE_TEXT("w+"));
const char *msg = "server ready";
ACE_OS::fwrite (msg,ACE_OS::strlen(msg),1,f);
ACE_OS::fclose (f);
}
acc.accept(sock[0]);
channels[0] = new ACE::HTBP::Channel (sock[0]);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("Got sock[0], handle = %d\n"),
sock[0].get_handle()));
acc.accept(sock[1]);
channels[1] = new ACE::HTBP::Channel(sock[1]);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("Got sock[1], handle = %d\n"),
sock[1].get_handle()));
int res = 0;
while ((res = channels[0]->pre_recv()) != 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("res = %d. waiting 1 sec. %p\n"),
res,
ACE_TEXT("stream.pre_recv()")));
ACE_OS::sleep (1);
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("Read from channel2\n")));
while ((res = channels[1]->pre_recv()) != 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("res = %d, waiting 1 sec. %p\n"),
res,
ACE_TEXT("stream2.pre_recv()")));
ACE_OS::sleep (1);
}
ACE::HTBP::Session *session = channels[0]->session();
ACE::HTBP::Stream stream (session);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("using streams %d, %d. Got session = %x\n"),
sock[0].get_handle(),sock[1].get_handle(),session));
ssize_t got = 1;
ssize_t total_recv = 0;
while (got != 0)
{
errno = 0;
got = stream.recv (buffer, sizeof (buffer));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("got = %b\n"), got));
if (got < 0)
break;
total_recv += got;
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("received %b\n"),total_recv));
ACE_OS::strcpy (buffer,"I hear you !");
ssize_t n = stream.send (buffer, ACE_OS::strlen (buffer)+1);
if (n == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("%p\n"),
ACE_TEXT("stream.send")),-1);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("send returned %b\n"),
n));
ACE_OS::sleep(1); // prevent test failure on windows when the connection
// closes too fast.
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("deleting channels[1]\n")));
delete channels[1];
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT("(%P|%t) Server: ")
ACE_TEXT("deleting channels[0]\n")));
delete channels[0];
return 0;
}
|