Beispiel #1
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 sesssion = %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;
}
Beispiel #2
0
int
Accept_Handler::handle_input (ACE_HANDLE h)
{
  ACE::HTBP::Channel **ch = 0;
  if (h == acceptor_.get_handle())
    {
      ACE_SOCK_Stream *sock = new ACE_SOCK_Stream;
      acceptor_.accept(*sock);
      ch = channels_[0] == 0 ? &channels_[0] :& channels_[1];
      *ch = new ACE::HTBP::Channel(*sock);
      this->reactor()->register_handler (sock->get_handle(),
                                         this,
                                         ACE_Event_Handler::READ_MASK);
      return 0;
    }
  for (int i = 0; i < 2; i++)
    if (channels_[i] && channels_[i]->get_handle() == h)
      {
        ch = &channels_[i];
        break;
      }
  if (ch == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%P|%t) Server Accept_Handler::handle_input, ")
                       ACE_TEXT ("unknown handle %d\n") ,h),
                      -1);
  int result = (*ch)->pre_recv();
  if (result == 0)
    {
      this->reactor()->remove_handler (h,
                                      ACE_Event_Handler::READ_MASK |
                                      ACE_Event_Handler::DONT_CALL);

      (*ch)->register_notifier(this->reactor());
      ACE::HTBP::Session *session = (*ch)->session();

      ACE::HTBP::Stream *stream = new ACE::HTBP::Stream(session);
      ACE_Event_Handler *handler = session->handler();

      if (handler == 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) Server Accept_Handler::handle_input ")
                      ACE_TEXT ("Creating new stream handler for %d\n"),
                      stream->get_handle()));
          Stream_Handler *sh = new Stream_Handler(*stream);
          session->handler (sh);
        }
      else
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%P|%t) Server Accept_Handler::handle_input ")
                    ACE_TEXT ("There is already a handler for %d\n"),
                    stream->get_handle()));

      if ((*ch)->state() == ACE::HTBP::Channel::Data_Queued)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) Server Accept_Handler::handle_input\n"),
                      ACE_TEXT ("Issuing notification on handler\n")));
          this->reactor()->notify (session->handler(),
                                   ACE_Event_Handler::READ_MASK);
        }

      *ch = 0;
    }
  return 0;
}
Beispiel #3
0
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{

  ACE_OS::socket_init (ACE_WSOCK_VERSION);

  if (parse_args(argc, argv) != 0)
    return 1;
  if (remote_host == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("Client: No remote host specified\n")),1);

  ACE::HTBP::Environment env;
  if (config_file != 0)
    env.import_config (config_file);

  ACE::HTBP::ID_Requestor req (&env);
  ACE::HTBP::Addr local (ACE_TEXT_ALWAYS_CHAR(req.get_HTID()));

  unsigned proxy_port = 0;
  ACE_TString proxy_host;

  if (env.get_proxy_port(proxy_port) != 0 ||
      env.get_proxy_host(proxy_host) != 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) Client: ")
                  ACE_TEXT("no proxy address in ")
                  ACE_TEXT("config, using direct connect\n")));
      proxy_port = remote_port;
      proxy_host = remote_host;
    }

  ACE_INET_Addr proxy (proxy_port, proxy_host.c_str ());
  ACE::HTBP::Addr remote (remote_port, ACE_TEXT_ALWAYS_CHAR (remote_host));

  ACE_TCHAR local_s[512], remote_s[512], proxy_s[512];
  remote.addr_to_string (remote_s, 512);
  local.addr_to_string (local_s, 512);
  proxy.addr_to_string (proxy_s, 512);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Client starting session for ")
              ACE_TEXT ("remote %s, local %s, proxy %s\n"),
              remote_s,
              local_s,
              proxy_s));
  ACE::HTBP::Session session (remote,
                              local,
                              ACE::HTBP::Session::next_session_id(),
                              &proxy);
  ACE::HTBP::Stream stream (&session);

  char buffer[1000];
  ssize_t n = 0;
  int retrycount = 10;
  for (int i = 0; i < 3; i++)
    {
      ACE::HTBP::Channel *ch = session.outbound();
      ACE_OS::sprintf (buffer,"Do you hear me? %d",i);
      n = stream.send (buffer,ACE_OS::strlen(buffer)+1);
      if (n == -1)
        ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("%p\n"),
                           ACE_TEXT("stream send")),-1);

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) Client: ")
                  ACE_TEXT("send returned %d\n"),n));

      retrycount = 10;
      while ((n = ch->recv_ack()) == -1
             && (errno == EWOULDBLOCK || errno == ETIME)
             && retrycount > 0)
        {
          retrycount--;
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) Client: ")
                      ACE_TEXT("waiting for ack, %d tries left\n"),
                      retrycount));
          ACE_OS::sleep (1);
        }
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) Client: ")
                  ACE_TEXT("After wait for ack, n = %d, retry = %d\n"),
                  n,retrycount,ACE_ERRNO_GET));

      retrycount = 10;
      while ((n = stream.recv(buffer,1000)) == -1
             && (errno == EWOULDBLOCK || errno == ETIME)
             && retrycount > 0)
        {
          retrycount--;
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) Client: ")
                      ACE_TEXT("waiting for inbound data, %d tries left\n"),
                      retrycount));
          ACE_OS::sleep(1);
        }
      if (retrycount == 0 || n < 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) Client: ")
                      ACE_TEXT("bailing after wait, %p\n"),
                      ACE_TEXT("recv")));
          break;
        }

      buffer[n] = 0;

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) Client: ")
                  ACE_TEXT("Got: \"%s\"\n"),
                  buffer));
    }
  ACE::HTBP::Channel *ch = session.outbound();
  if (ch == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%P|%t) Client: ")
                       ACE_TEXT("session's outbound channel is null!\n")),1);
  n = stream.send ("goodbye",7);
  if (n == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("(%P|%t) Client: ")
                       ACE_TEXT("%p\n"),
                       ACE_TEXT("stream send")),-1);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Client: ")
              ACE_TEXT("send returned %d\n"),n));

  retrycount = 10;
  while (ch &&
         (n = ch->recv_ack()) == -1
         && (errno == EWOULDBLOCK || errno == ETIME)
         && retrycount > 0)
    {
      retrycount--;
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) Client: ")
                  ACE_TEXT("waiting for ack, %d tries left\n"),
                  retrycount));
      ACE_OS::sleep (1);
    }
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Client: ")
              ACE_TEXT("After wait for ack, n = %d, retry = %d\n"),
              n,retrycount,ACE_ERRNO_GET));

  return 0;
}
Beispiel #4
0
int
ACE_TMAIN (int argc , ACE_TCHAR *argv[])
{
    char buffer[1000];
    ssize_t n = 0;
    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_SOCK_Acceptor acc(local, 1);
    local.addr_to_string (host, MAXHOSTNAMELEN);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("(%P|%t) Server is ready on %s\n"),
                host));
    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]);
    ACE::HTBP::Channel channel1(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]);
    ACE::HTBP::Channel channel2 (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 = channel1.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 ("Read from channel2\n")));
    while ((res = channel2.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 = channel1.session();
    ACE::HTBP::Stream stream (session);

    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("(%P|%t) Server: ")
                ACE_TEXT ("using streams %d, %d. Got session = %@\n"),
                sock[0].get_handle(),
                sock[1].get_handle(),
                session));

    for (int i = 0; i >= 0; i++)
    {
        int retrycount = 10;
        while ((n = stream.recv(buffer,1000)) == -1
                && (errno == EWOULDBLOCK || errno == ETIME)
                && retrycount > 0)
        {
            retrycount--;
            ACE_DEBUG ((LM_DEBUG,
                        ACE_TEXT ("(%P|%t) Server: ")
                        ACE_TEXT ("waiting for inbound data, %d tries left\n"),
                        retrycount));
            ACE_OS::sleep(1);
        }
        if (retrycount == 0 || n < 0)
            break;

        buffer[n] = 0;

        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%P|%t) Server: ")
                    ACE_TEXT ("Got: \"%C\"\n"), buffer));

        if (ACE_OS::strstr (buffer,"goodbye") != 0)
            break;

        ACE_OS::sprintf (buffer,"I hear you %d",i);
        n = stream.send (buffer,ACE_OS::strlen(buffer)+1);
        if (n == -1)
            ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
                               ACE_TEXT ("stream.send")),
                              -1);

        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%P|%t) Server: Send returned %d\n"), n));

        int got[2] = {-1,-1};
        while (got[0] == -1 || got[1] == -1)
        {
            if (got[0] == -1)
            {
                if ((got[0] = (res =channel1.pre_recv())) == -1)
                    ACE_DEBUG ((LM_DEBUG,
                                ACE_TEXT ("(%P|%t) Server: ")
                                ACE_TEXT ("res = %d, waiting 1 sec. %p\n"),
                                got[0],
                                ACE_TEXT ("channel1.pre_recv()")));
            }
            if (got[1] == -1)
            {
                if ((got[1] = (res =channel2.pre_recv())) == -1)
                    ACE_DEBUG ((LM_DEBUG,
                                ACE_TEXT ("(%P|%t) Server: ")
                                ACE_TEXT ("res = %d, waiting 1 sec. %p\n"),
                                got[1],
                                ACE_TEXT ("channel2.pre_recv()")));
            }
            if (got[0] == -1 || got[1] == -1)
                ACE_OS::sleep (1);
        }
    }
    ACE_OS::sleep(1); // prevent test failure on windows when the connection
    // closes too fast.

    stream.close();
    acc.close();

    return 0;
}