コード例 #1
0
ファイル: Peer.cpp プロジェクト: manut/ACE
int
Peer_Factory::info (ACE_TCHAR **strp, size_t length) const
{
  ACE_TCHAR buf[BUFSIZ];
  ACE_TCHAR consumer_addr_str[BUFSIZ];
  ACE_TCHAR supplier_addr_str[BUFSIZ];

  ACE_INET_Addr addr;

  if (this->consumer_acceptor_.acceptor ().get_local_addr (addr) == -1)
    return -1;
  else if (addr.addr_to_string (consumer_addr_str,
                                sizeof addr) == -1)
    return -1;
  else if (this->supplier_acceptor_.acceptor ().get_local_addr (addr) == -1)
    return -1;
  else if (addr.addr_to_string (supplier_addr_str,
                                sizeof addr) == -1)
    return -1;

  ACE_OS::strcpy (buf, ACE_TEXT ("peerd\t C:"));
  ACE_OS::strcat (buf, consumer_addr_str);
  ACE_OS::strcat (buf, ACE_TEXT ("|S:"));
  ACE_OS::strcat (buf, supplier_addr_str);
  ACE_OS::strcat
    (buf, ACE_TEXT ("/tcp # Gateway traffic generator and data sink\n"));

  if (*strp == 0 && (*strp = ACE_OS::strdup (buf)) == 0)
    return -1;
  else
    ACE_OS::strncpy (*strp, buf, length);
  return ACE_OS::strlen (buf);
}
コード例 #2
0
ファイル: FlReactor_Test.cpp プロジェクト: azraelly/knetwork
int
Connection_Handler::open (void*)
{
  if (this->box_ != 0)
    {
      ACE_INET_Addr from;

      this->peer ().get_remote_addr (from);
      const int bufsiz = 128;
      char buf[bufsiz];

      from.addr_to_string (buf, bufsiz, 0);

      static char msg[256];
      ACE_OS::sprintf (msg, "connection from <%s>\n", buf);

      this->box_->label (msg);
      this->box_->redraw ();
    }

  if (this->w_ != 0)
    {
      this->w_->incr_sides ();
    }

  return this->peer ().enable (ACE_NONBLOCK);
}
コード例 #3
0
// Open a RAPI QoS session [dest IP, dest port, Protocol ID].
int
ACE_RAPI_Session::open (ACE_INET_Addr dest_addr,
                        ACE_Protocol_ID protocol_id)
{
  char buf [BUFSIZ];
  dest_addr.addr_to_string (buf,
                            BUFSIZ);
  ACELIB_DEBUG ((LM_DEBUG,
              "In RAPI SESSION OPEN %s\n",
              buf));

  this->dest_addr_ = dest_addr;
  this->protocol_id_ = protocol_id;

  // Open a RAPI session. Note "this" is being passed as an argument to
  // the callback function. The callback function uses this argument to
  // update the QoS of this session based on the RSVP event it receives.

  if ((this->session_id_ = rapi_session((struct sockaddr *) dest_addr.get_addr (),
                                        protocol_id,
                                        0,
                                        rsvp_callback,
                                        (void *) this,
                                        &rsvp_error)) == NULL_SID)
    ACELIB_ERROR_RETURN ((LM_ERROR,
                       "rapi_session () call fails. Error\n"),
                      -1);
  else
    ACELIB_DEBUG ((LM_DEBUG,
                "rapi_session () call succeeds. "
                "Session ID = %d\n",
                this->session_id_));

  return 0;
}
コード例 #4
0
ssize_t
RtpsUdpSendStrategy::send_single_i(const iovec iov[], int n,
                                   const ACE_INET_Addr& addr)
{
#ifdef ACE_HAS_IPV6
  ACE_SOCK_Dgram& sock = link_->socket_for(addr.get_type());
#define USE_SOCKET sock
#else
#define USE_SOCKET link_->unicast_socket()
#endif
#ifdef ACE_LACKS_SENDMSG
  char buffer[UDP_MAX_MESSAGE_SIZE];
  char *iter = buffer;
  for (int i = 0; i < n; ++i) {
    if (iter - buffer + iov[i].iov_len > UDP_MAX_MESSAGE_SIZE) {
      ACE_ERROR((LM_ERROR, "(%P|%t) RtpsUdpSendStrategy::send_single_i() - "
                 "message too large at index %d size %d\n", i, iov[i].iov_len));
      return -1;
    }
    std::memcpy(iter, iov[i].iov_base, iov[i].iov_len);
    iter += iov[i].iov_len;
  }
  const ssize_t result = USE_SOCKET.send(buffer, iter - buffer, addr);
#else
  const ssize_t result = USE_SOCKET.send(iov, n, addr);
#endif
  if (result < 0) {
    ACE_TCHAR addr_buff[256] = {};
    addr.addr_to_string(addr_buff, 256, 0);
    ACE_ERROR((LM_ERROR, "(%P|%t) RtpsUdpSendStrategy::send_single_i() - "
      "destination %s failed %p\n", addr_buff, ACE_TEXT("send")));
  }
  return result;
}
コード例 #5
0
ファイル: FlReactor_Test.cpp プロジェクト: azraelly/knetwork
int run_main (int argc, ACE_TCHAR *argv[])
{
  ACE_START_TEST (ACE_TEXT ("FlReactor_Test"));

  Fl_Window window (300, 370);

  Test_Window tw (10, 75, window.w () - 20, window.h ()-90);
  window.resizable (&tw);

  Fl_Hor_Slider slider (60, 5, window.w () - 70, 30, "Sides:");
  slider.align (FL_ALIGN_LEFT);
  slider.callback (sides_cb, &tw);
  slider.value (tw.sides ());
  slider.step (1);
  slider.bounds (3, 10);

  ACE_FlReactor reactor;
  ACE_Reactor r (&reactor);

  Fl_Box *box = new Fl_Box (FL_UP_BOX, 10, 40,
                            window.w () - 20, 30,
                            "Setting up");
  box->labelfont (FL_BOLD);

  Acceptor acceptor (&tw, box);

  ACE_INET_Addr address;

  if (acceptor.open (address, &r) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open acceptor"),
                      -1);

  acceptor.acceptor ().get_local_addr (address);

  const int bufsiz = 128;
  char buf[bufsiz];

  address.addr_to_string (buf, bufsiz, 0);

  char msg[2 * bufsiz];
  ACE_OS::sprintf (msg, "Listening on <%s>\n", buf);

  box->label (msg);
  box->redraw ();

  window.end ();
  window.show (argc, argv);
  tw.show ();

  return Fl::run ();

  ACE_END_TEST;
}
コード例 #6
0
ファイル: PSession.cpp プロジェクト: binghuo365/BaseLab
void
PSession::addresses (const ACE_INET_Addr& peer, const ACE_INET_Addr& local)
{
  ACE_TCHAR str_peer [256];
  ACE_TCHAR str_local[256];

  if (0 != peer.addr_to_string (str_peer, sizeof (str_peer)/sizeof (ACE_TCHAR)))
    ACE_OS::strcpy (str_peer, ACE_TEXT("????"));

  if (0 != local.addr_to_string (str_local, sizeof (str_local)/sizeof (ACE_TCHAR)))
    ACE_OS::strcpy (str_peer, ACE_TEXT("????"));

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%t) %s=%d Remote=%s Local=%s\n"),
              this->get_name(),
              this->index(),
              str_peer,
              str_local));
  return;
}
コード例 #7
0
ファイル: ClientService.cpp プロジェクト: xy365/ACE-Samples
// Registers the new ClientService instance with the reactor
//
// We try to print a log message stating which host connected, then call ACE_Reactor::register_handler() 
// to register for input events with the reactor. We return the return value of register_handler(), 
// which is 0 for success and �for failure. This value is passed back to the ClientAcceptor::handle_input() method, 
// and handle_input() also will return �for error and 0 for success. 
// If ClientAcceptor::handle_input() returns �we will have to close the handle.
//
int ClientService::open()
{
	ACE_TCHAR		peerName[MAXHOSTNAMELEN];
	ACE_INET_Addr	peerAddress;

	if (this->m_socket.get_remote_addr(peerAddress) == 0 && peerAddress.addr_to_string(peerName, MAXHOSTNAMELEN) == 0)
	{
		ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) Connection from %s\n"), peerName));
	}

	return this->reactor()->register_handler(this, ACE_Event_Handler::READ_MASK);
}
コード例 #8
0
ファイル: TCP.cpp プロジェクト: asdlei00/ACE
int
TAO_AV_TCP_Flow_Handler::open (void * /*arg*/)
{

#if defined (TCP_NODELAY)
  int nodelay = 1;

  if (this->peer ().set_option (IPPROTO_TCP,
                                TCP_NODELAY,
                                (void *) &nodelay,
                                sizeof (nodelay)) == -1)
    ORBSVCS_ERROR_RETURN ((LM_ERROR,
                       "NODELAY failed\n"),
                      -1);
#endif /* TCP_NODELAY */

  int buf_size = BUFSIZ;
  int s = sizeof (buf_size);
  if (this->peer ().get_option (SOL_SOCKET, /*IPPROTO_TCP,*/
                                SO_RCVBUF,
                                (void *) &buf_size, &s) == -1)
       buf_size = BUFSIZ;

    ((TAO_AV_TCP_Object*)(this->protocol_object_))->frame_.size (buf_size);

  // Called by the <Strategy_Acceptor> when the handler is completely
  // connected.
  ACE_INET_Addr addr;

  if (this->peer ().get_remote_addr (addr) == -1)
    return -1;

  ACE_TCHAR server[MAXHOSTNAMELEN + 16];

  (void) addr.addr_to_string (server, sizeof (server));

  if (TAO_debug_level > 0)
    if (TAO_debug_level > 0) ORBSVCS_DEBUG ((LM_DEBUG,
                "(%P|%t) connection to server <%s> on %d\n",
                server, this->peer ().get_handle ()));

  this->peer ().enable (ACE_NONBLOCK);
  // Register the handler with the reactor.
  if (this->reactor ()
      && this->reactor ()->register_handler
      (this,
       ACE_Event_Handler::READ_MASK) == -1)
    ORBSVCS_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("unable to register client handler")),
                      -1);
  return 0;
}
コード例 #9
0
string chromatic_handler::client_addr()
{
	ACE_INET_Addr addr;
	char addrstr[MAXHOSTNAMELEN + 1];

	peer().get_remote_addr( addr );
	addr.addr_to_string( addrstr , sizeof( addrstr ) );	

	string strTemp( addrstr );

	return ( strTemp );
}
コード例 #10
0
 static void addr_to_string (const ACE_INET_Addr &ip_addr,
                             ACE_TCHAR *ret_string,  // results here.
                             size_t len,
                             int clip_portnum)       // clip port# info?
   {
     if (ip_addr.addr_to_string (ret_string, len, 1) == -1)
       ACE_OS::strcpy (ret_string, ACE_TEXT ("<?>"));
     else
       {
         ACE_TCHAR *pc = ACE_OS::strrchr (ret_string, ACE_TEXT (':'));
         if (clip_portnum && pc)
           *pc = ACE_TEXT ('\0'); // clip port# info.
       }
   }
コード例 #11
0
ファイル: SCTP_SEQ.cpp プロジェクト: binary42/OCI
int
TAO_AV_SCTP_SEQ_Acceptor::open_default (TAO_Base_StreamEndPoint *endpoint,
                                        TAO_AV_Core *av_core,
                                        TAO_FlowSpec_Entry *entry,
                                        TAO_AV_Flow_Protocol_Factory *factory,
                                        TAO_AV_Core::Flow_Component flow_comp)
{
    this->flow_protocol_factory_ = factory;
    this->av_core_ = av_core;
    this->endpoint_ = endpoint;
    this->entry_ = entry;
    if (flow_comp == TAO_AV_Core::TAO_AV_CONTROL)
        this->flowname_ = TAO_AV_Core::get_control_flowname (entry->flowname());
    else
        this->flowname_ = entry->flowname ();

    ACE_INET_Addr *address;
    ACE_NEW_RETURN (address,
                    ACE_INET_Addr ("0"),
                    -1);

    int result = this->acceptor_.acceptor_open (this,
                 av_core->reactor (),
                 *address,
                 entry);


    if (result < 0)
        ORBSVCS_ERROR_RETURN ((LM_ERROR,
                               "TAO_AV_SCTP_SEQ_Acceptor::open failed"),
                              -1);

    this->acceptor_.acceptor ().get_local_addr (*address);

    address->set (address->get_port_number (),
                  address->get_host_name ());

    char buf[BUFSIZ];
    address->addr_to_string (buf,BUFSIZ);

    if (TAO_debug_level > 0)
        ORBSVCS_DEBUG ((LM_DEBUG,
                        "TAO_AV_SCTP_SEQ_Acceptor::open_default: %s\n",
                        buf));

    entry->set_local_addr (address);

    return 0;
}
コード例 #12
0
ファイル: INET_Addr_Test.cpp プロジェクト: INMarkus/ATCD
static bool test_multiple (void)
{

  bool success = true;

  // Check the behavior when there are multiple addresses assigned to a name.
  // The NTP pool should always return multiples, though always different.
  ACE_INET_Addr ntp;
  if (ntp.set (123, ACE_TEXT ("pool.ntp.org")) == -1)
    {
      // This is just a warning to prevent fails on lookups on hosts with no
      // DNS service. The real value of this test is to accurately get
      // multiples from the result.
      ACE_ERROR ((LM_WARNING, ACE_TEXT ("%p\n"), ACE_TEXT ("pool.ntp.org")));
      return true;
    }
  size_t count = 0;
  ACE_TCHAR addr_string[256];
  do
    {
      ++count;      // If lookup succeeded, there's at least one
      ntp.addr_to_string (addr_string, sizeof (addr_string));
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv4 %B: %s\n"), count, addr_string));
    }
  while (ntp.next ());
  success = count > 1;

#if defined (ACE_HAS_IPV6)
  ACE_INET_Addr ntp6;
  if (ntp6.set (123, ACE_TEXT ("2.pool.ntp.org"), 1, AF_INET6) == -1)
    {
      ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("2.pool.ntp.org")));
      return false;
    }
  count = 0;
  do
    {
      ++count;      // If lookup succeeded, there's at least one
      ntp6.addr_to_string (addr_string, sizeof (addr_string));
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv6 %B: %s\n"), count, addr_string));
    }
  while (ntp6.next ());
  if (count <= 1)
    success = false;
#endif /* ACE_HAS_IPV6 */

  return success;
}
コード例 #13
0
ファイル: server.cpp プロジェクト: DOCGroup/ACE_TAO
int
ACE_TMAIN (int argc, ACE_TCHAR * argv[])
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Server: ")
              ACE_TEXT ("At start of main\n")));
  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);
  local.addr_to_string (host, MAXHOSTNAMELEN);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Server: ")
              ACE_TEXT ("listening at %s\n"),
              host));

  ACE_SOCK_Acceptor acc (local, 1);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Server: ")
              ACE_TEXT ("opened listener\n")));

  Accept_Handler handler (acc);
  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);
    }

  ACE_Reactor::instance()->run_reactor_event_loop();
  return 0;
}
コード例 #14
0
void RealmList::UpdateRealm(uint32 ID, const std::string& name, ACE_INET_Addr const& address, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float popu, uint32 build)
{
    // Create new if not exist or update existed
    Realm& realm = m_realms[name];

    realm.m_ID = ID;
    realm.name = name;
    realm.icon = icon;
    realm.flag = flag;
    realm.timezone = timezone;
    realm.allowedSecurityLevel = allowedSecurityLevel;
    realm.populationLevel = popu;

    // Append port to IP address.
    address.addr_to_string(realm.address, ACE_MAX_FULLY_QUALIFIED_NAME_LEN + 16);
    realm.gamebuild = build;
}
コード例 #15
0
void
RPG_Net_Client_AsynchConnector::connect(const ACE_INET_Addr& peer_address)
{
    RPG_TRACE(ACE_TEXT("RPG_Net_Client_AsynchConnector::connect"));

    int result =
        inherited::connect(peer_address,                           // remote SAP
                           ACE_sap_any_cast(const ACE_INET_Addr&), // local SAP
                           1,                                      // re-use address (SO_REUSEADDR) ?
                           NULL);                                  // ACT
    if (result == -1)
    {
        ACE_TCHAR buffer[RPG_COMMON_BUFSIZE];
        ACE_OS::memset(buffer, 0, sizeof(buffer));
        if (peer_address.addr_to_string(buffer,
                                        sizeof(buffer)) == -1)
            ACE_DEBUG((LM_ERROR,
                       ACE_TEXT("failed to ACE_INET_Addr::addr_to_string(): \"%m\", continuing\n")));
        ACE_DEBUG((LM_ERROR,
                   ACE_TEXT("failed to ACE_Asynch_Connector::connect(\"%s\"): \"%m\", aborting\n"),
                   buffer));
    } // end IF
}
コード例 #16
0
int
RPG_Net_Client_AsynchConnector::validate_connection(const ACE_Asynch_Connect::Result& result_in,
        const ACE_INET_Addr& remoteSAP_in,
        const ACE_INET_Addr& localSAP_in)
{
    RPG_TRACE(ACE_TEXT("RPG_Net_Client_AsynchConnector::validate_connection"));

    // success ?
    if (result_in.success() == 0)
    {
        // debug info
        ACE_TCHAR buffer[RPG_COMMON_BUFSIZE];
        ACE_OS::memset(buffer, 0, sizeof(buffer));
        if (remoteSAP_in.addr_to_string(buffer, sizeof(buffer)) == -1)
            ACE_DEBUG((LM_ERROR,
                       ACE_TEXT("failed to ACE_INET_Addr::addr_to_string(): \"%m\", continuing\n")));
        ACE_DEBUG((LM_ERROR,
                   ACE_TEXT("failed to RPG_Net_Client_AsynchConnector::connect(\"%s\"): \"%s\", aborting\n"),
                   buffer,
                   ACE_OS::strerror(result_in.error())));
    } // end IF

    return ((result_in.success() == 1) ? 0 : -1);
}
コード例 #17
0
ファイル: client.cpp プロジェクト: asdlei00/ACE
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;
}
コード例 #18
0
ファイル: Util.cpp プロジェクト: Chuck5ta/server-1
std::string GetAddressString(ACE_INET_Addr const& addr)
{
    char buf[ACE_MAX_FULLY_QUALIFIED_NAME_LEN + 16];
    addr.addr_to_string(buf, ACE_MAX_FULLY_QUALIFIED_NAME_LEN + 16);
    return buf;
}
コード例 #19
0
ファイル: ethManager.cpp プロジェクト: elen4/icub-main
bool TheEthManager::createSocket(ACE_INET_Addr local_addr)
{
    yTrace();
    managerMutex.wait();

//     if(NULL == handle)
//     {
//         yError() << "Called createSocket while EthManager is not instantiated";
//         handle = TheEthManager::instance();
//     }

    if(!UDP_initted)
    {
        UDP_socket = new ACE_SOCK_Dgram();
        char tmp[64];
        if(-1 == UDP_socket->open(local_addr))
        {
            local_addr.addr_to_string(tmp, 64);
            yError() <<   "\n/---------------------------------------------------\\"
                     <<   "\n| Unable to bind to local IP address " << tmp
                     <<   "\n\\---------------------------------------------------/";
            delete UDP_socket;
            UDP_socket = NULL;
            UDP_initted = false;
        }
        else
        {
            UDP_initted = true;

            sender = new EthSender();
            receiver = new EthReceiver();

            //managerMutex.post();

            sender->config(UDP_socket, this);
            receiver->config(UDP_socket, this);
            /* Start the threads sending to and receiving messages from the boards.
             * It will execute the threadInit and pass its return value to the following calls
             * afterStart to check if they started correctly.
             */
            bool ret1, ret2;
            ret1 = sender->start();
            ret2 = receiver->start();

            //managerMutex.wait();
            if(!ret1 || !ret2)
            {
                yError() << "EthManager: issue while starting threads for UDP communication with EMSs";

                // stop threads
                stopThreads();

                delete UDP_socket;
                UDP_initted = false;
                return false;
            }
            else
            {
                yTrace() << "Both sending and Receiving thread started correctly!";

            }
        }
    }

    managerMutex.post();
    return UDP_initted;
}
コード例 #20
0
template <class HANDLER> void
ACE_Asynch_Connector<HANDLER>::parse_address (const ACE_Asynch_Connect::Result &result,
                                              ACE_INET_Addr &remote_address,
                                              ACE_INET_Addr &local_address)
{
#if defined (ACE_HAS_IPV6)
  // Getting the addresses.
  sockaddr_in6 local_addr;
  sockaddr_in6 remote_addr;
#else
  // Getting the addresses.
  sockaddr_in local_addr;
  sockaddr_in remote_addr;
#endif /* ACE_HAS_IPV6 */

  // Get the length.
  int local_size = sizeof (local_addr);
  int remote_size = sizeof (remote_addr);

  // Get the local address.
  if (ACE_OS::getsockname (result.connect_handle (),
                           reinterpret_cast<sockaddr *> (&local_addr),
                           &local_size) < 0)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT("%p\n"),
                ACE_TEXT("ACE_Asynch_Connector::<getsockname> failed")));

  // Get the remote address.
  if (ACE_OS::getpeername (result.connect_handle (),
                           reinterpret_cast<sockaddr *> (&remote_addr),
                           &remote_size) < 0)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT("%p\n"),
                ACE_TEXT("ACE_Asynch_Connector::<getpeername> failed")));

  // Set the addresses.
  local_address.set  (reinterpret_cast<sockaddr_in *> (&local_addr),
                      local_size);
  remote_address.set (reinterpret_cast<sockaddr_in *> (&remote_addr),
                      remote_size);

#if 0
  // @@ Just debugging.
  char local_address_buf  [BUFSIZ];
  char remote_address_buf [BUFSIZ];

  if (local_address.addr_to_string (local_address_buf,
                                    sizeof local_address_buf) == -1)
    ACE_ERROR ((LM_ERROR,
                "Error:%m:can't obtain local_address's address string"));

  ACE_DEBUG ((LM_DEBUG,
              "ACE_Asynch_Connector<HANDLER>::parse_address : "
              "Local address %s\n",
              local_address_buf));

  if (remote_address.addr_to_string (remote_address_buf,
                                     sizeof remote_address_buf) == -1)
    ACE_ERROR ((LM_ERROR,
                "Error:%m:can't obtain remote_address's address string"));

  ACE_DEBUG ((LM_DEBUG,
              "ACE_Asynch_Connector<HANDLER>::parse_address : "
              "Remote address %s\n",
              remote_address_buf));
#endif /* 0 */

  return;
}
コード例 #21
0
int
TAO_SHMIOP_Connection_Handler::open (void*)
{
  if (this->shared_open() == -1)
    return -1;

  TAO_SHMIOP_Protocol_Properties protocol_properties;

  // Initialize values from ORB params.
  protocol_properties.send_buffer_size_ =
    this->orb_core ()->orb_params ()->sock_sndbuf_size ();
  protocol_properties.recv_buffer_size_ =
    this->orb_core ()->orb_params ()->sock_rcvbuf_size ();
  protocol_properties.no_delay_ =
    this->orb_core ()->orb_params ()->nodelay ();

  TAO_Protocols_Hooks *tph = this->orb_core ()->get_protocols_hooks ();

  if (tph != 0)
    {
      try
        {
          if (this->transport ()->opened_as () == TAO::TAO_CLIENT_ROLE)
            {
              tph->client_protocol_properties_at_orb_level (protocol_properties);
            }
          else
            {
              tph->server_protocol_properties_at_orb_level (protocol_properties);
            }
        }
      catch (const ::CORBA::Exception&)
        {
          return -1;
        }
    }

  if (this->set_socket_option (this->peer (),
                               protocol_properties.send_buffer_size_,
                               protocol_properties.recv_buffer_size_) == -1)
    return -1;

#if !defined (ACE_LACKS_TCP_NODELAY)
  if (this->peer ().set_option (ACE_IPPROTO_TCP,
                                TCP_NODELAY,
                                (void *) &protocol_properties.no_delay_,
                                sizeof (protocol_properties.no_delay_)) == -1)
    return -1;

#endif /* ! ACE_LACKS_TCP_NODELAY */

  if (this->transport ()->wait_strategy ()->non_blocking ())
    {
      if (this->peer ().enable (ACE_NONBLOCK) == -1)
        return -1;
    }

  // Called by the <Strategy_Acceptor> when the handler is
  // completely connected.
  ACE_INET_Addr addr;

  ACE_TCHAR local_as_string[MAXHOSTNAMELEN + 16];

  // Get the peername.
  if (this->peer ().get_remote_addr (addr) == -1)
    return -1;

  // Verify that we can resolve the peer hostname.
  else if (addr.addr_to_string (local_as_string, sizeof (local_as_string)) == -1)
    return -1;

  if (TAO_debug_level > 0)
    {
      TAOLIB_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("TAO (%P|%t) - SHMIOP connection from client")
                  ACE_TEXT ("<%s> on %d\n"),
                  local_as_string, this->peer ().get_handle ()));
    }

  // Set that the transport is now connected, if fails we return -1
  // Use C-style cast b/c otherwise we get warnings on lots of
  // compilers
  if (!this->transport ()->post_open ((size_t) this->get_handle ()))
    return -1;

  // Not needed, anyway
  this->state_changed (TAO_LF_Event::LFS_SUCCESS,
                       this->orb_core ()->leader_follower ());

  return 0;
}
コード例 #22
0
ファイル: server.cpp プロジェクト: akostrikov/ATCD
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;
}
コード例 #23
0
ファイル: RW_Process_Mutex_Test.cpp プロジェクト: jiaoyk/ATCD
/*
 * The set of readers and the writer will operate in a staggered sequence
 * of acquiring and releasing the lock. The sequence is designed to exercise
 * waiting behavior of both readers and writer, as well as allowing multiple
 * readers in, without getting tripped up by any differences in ordering
 * on different platforms which may favor writers, or vice-versa.
 * In this timeline, time on seconds is on the left, time holding the lock
 * is solid, time waiting is dots, acquire/release point is a dash, and
 * time without the lock is blank.
 *
 *   TIME        WRITER    READER1    READER2    READER3
 *     0            |
 *                  |
 *     1            |         .
 *                  |         .
 *     2            -         -          -
 *                            |          |
 *     3                      |          |          -
 *                            |          |          |
 *     4                      -          |          |
 *                                       |          |
 *     5                                 -          |
 *                                                  |
 *     6            -                               -
 *                  |
 *     7            |         .          .          .
 *                  |         .          .          .
 *     8            -         -          -          -
 *                            |          |          |
 *     9                      |          |          |
 *
 * A file is used to test the sequencing. When the writer first gets the
 * lock, it will ensure the file is not present. At the end of its time
 * holding the lock the first time, it will write a "writer 1" string to
 * the file. When it gets the lock the second time, it will write a
 * different string to the file, and just before releasing the second time
 * write a "writer 2" string to the file. The readers all check to be sure
 * that the file is present and says "writer 1" at the start and end of
 * their periods of holding the reader lock and, similarly, check for
 * "writer 2" the second time they hold the lock.
 */
static void
reader (int num)
{
    // Let the writer get there first.
    ACE_OS::sleep (1);

    ACE_SOCK_Dgram sock;
    ACE_INET_Addr parent;
    parent.set (reporting_port, ACE_LOCALHOST, 1, AF_INET);
    ACE_TCHAR me_str[80];
    parent.addr_to_string (me_str, 80);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Sending reports to %s\n"), me_str));

    if (sock.open (ACE_Addr::sap_any, PF_INET) == -1)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("UDP open")));

    Range_Report report;
    report.child_ = num;
    ACE_Time_Value start (ACE_Time_Value::zero), stop (ACE_Time_Value::zero);

    ACE_RW_Process_Mutex mutex (mutex_name.c_str ());

    // Make sure the constructor succeeded
    if (ACE_LOG_MSG->op_status () != 0)
    {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d, mutex %s %p\n"),
                    num,
                    mutex_name.c_str (),
                    ACE_TEXT ("ctor")));
        return;
    }

    ACE_OS::sleep (num);
    // Grab the lock
    if (-1 == mutex.acquire_read ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d %p\n"),
                    num,
                    ACE_TEXT ("first acquire_read")));
    else
    {
        start = ACE_OS::gettimeofday ();
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Reader %d acquired first time\n"),
                    num));
    }

    // Wait a bit, then release and report the range held.
    ACE_OS::sleep (num);

    // Release the lock then wait; in the interim, the writer should change
    // the file.
    stop = ACE_OS::gettimeofday ();
    if (-1 == mutex.release ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d %p\n"),
                    num,
                    ACE_TEXT ("first release")));
    else
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reader %d released first time\n"), num));
    report.range_.set (start, stop);
    ssize_t bytes = sock.send (&report, sizeof (report), parent);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("Reader %d sent %b byte report\n"),
                num,
                bytes));

    ACE_OS::sleep (4 - num);
    start = stop = ACE_Time_Value::zero;
    if (-1 == mutex.acquire_read ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d %p\n"),
                    num,
                    ACE_TEXT ("second acquire_read")));
    else
    {
        start = ACE_OS::gettimeofday ();
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Reader %d acquired second time\n"),
                    num));
    }

    // Done; small delay, release, report, and return.
    ACE_OS::sleep (1);
    stop = ACE_OS::gettimeofday ();
    if (-1 == mutex.release ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Reader %d %p\n"),
                    num,
                    ACE_TEXT ("second release")));
    else
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Reader %d released second time; done\n"),
                    num));
    report.range_.set (start, stop);
    bytes = sock.send (&report, sizeof (report), parent);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("Reader %d sent %b byte report\n"),
                num,
                bytes));
    sock.close ();
    return;
}
コード例 #24
0
ファイル: RW_Process_Mutex_Test.cpp プロジェクト: jiaoyk/ATCD
static void
writer (void)
{
    ACE_RW_Process_Mutex mutex (mutex_name.c_str ());

    // Make sure the constructor succeeded
    if (ACE_LOG_MSG->op_status () != 0)
    {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer, mutex %s %p\n"),
                    mutex_name.c_str (),
                    ACE_TEXT ("ctor")));
        return;
    }

    ACE_SOCK_Dgram sock;
    ACE_INET_Addr parent;
    parent.set (reporting_port, ACE_LOCALHOST, 1, AF_INET);
    ACE_TCHAR me_str[80];
    parent.addr_to_string (me_str, 80);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Sending reports to %s\n"), me_str));
    if (sock.open (ACE_Addr::sap_any, PF_INET) == -1)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("UDP open")));

    Range_Report report;
    report.child_ = 0;   // We're the writer
    ACE_Time_Value start (ACE_Time_Value::zero), stop (ACE_Time_Value::zero);

    // Grab the lock
    if (-1 == mutex.acquire_write ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer first %p\n"),
                    ACE_TEXT ("acquire_write")));
    else
    {
        start = ACE_OS::gettimeofday ();
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer acquired first time\n")));
    }

    // Now sleep, making the readers wait for the lock. Then release the lock,
    // sleep, and reacquire the lock.
    ACE_OS::sleep (2);
    stop = ACE_OS::gettimeofday ();
    if (-1 == mutex.release ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer %p\n"),
                    ACE_TEXT ("first release")));
    else
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Writer released first time\n")));

    report.range_.set (start, stop);
    ssize_t bytes = sock.send (&report, sizeof (report), parent);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer sent %b byte report\n"), bytes));

    ACE_OS::sleep (1);   // Ensure we don't immediately grab the lock back

    start = stop = ACE_Time_Value::zero;

    if (-1 == mutex.acquire_write ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer second %p\n"),
                    ACE_TEXT ("acquire_write")));
    else
    {
        start = ACE_OS::gettimeofday ();
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer acquired second time\n")));
    }

    ACE_OS::sleep (2);
    stop = ACE_OS::gettimeofday ();
    if (-1 == mutex.release ())
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Writer %p\n"),
                    ACE_TEXT ("second release")));
    else
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("Writer released second time\n")));
    report.range_.set (start, stop);
    bytes = sock.send (&report, sizeof (report), parent);
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Writer sent %b byte report\n"), bytes));
    sock.close ();
    return;
}
コード例 #25
0
ファイル: RW_Process_Mutex_Test.cpp プロジェクト: jiaoyk/ATCD
int
run_main (int argc, ACE_TCHAR *argv[])
{
    parse_args (argc, argv);

    // Child process code.
    if (child_nr >= 0)
    {
        ACE_TCHAR lognm[MAXPATHLEN];
        int mypid (ACE_OS::getpid ());
        ACE_OS::sprintf(lognm,
                        ACE_TEXT ("RW_Process_Mutex_Test-child-%d"),
                        (int)mypid);
        ACE_START_TEST (lognm);
        if (child_nr == 0)
            writer ();
        else
            reader (child_nr);
        ACE_END_LOG;
    }
    else
    {
        ACE_START_TEST (ACE_TEXT ("RW_Process_Mutex_Test"));
        // Although it should be safe for each process to construct and
        // destruct the rw lock, this can disturb other process still
        // using the lock. This is not really correct, and should be
        // looked at, but it gets things moving.
        // Also see Process_Mutex_Test.cpp for similar issue.
        ACE_RW_Process_Mutex mutex (mutex_name.c_str ());
        // Make sure the constructor succeeded
        if (ACE_LOG_MSG->op_status () != 0)
        {
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("Parent, mutex %s %p\n"),
                        mutex_name.c_str (),
                        ACE_TEXT ("ctor")));
        }
#if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
        static const ACE_TCHAR* format = ACE_TEXT ("%ls -c %d -p %u -n %ls");
#else
        static const ACE_TCHAR* format = ACE_TEXT ("%s -c %d -p %u -n %s");
#endif /* !ACE_WIN32 && ACE_USES_WCHAR */

        // The parent process reads time ranges sent from the children via
        // UDP. Grab an unused UDP port to tell the children to send to.
        ACE_INET_Addr me;
        ACE_SOCK_Dgram sock;
        if (sock.open (ACE_Addr::sap_any, PF_INET) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                               ACE_TEXT ("Socket %p\n"),
                               ACE_TEXT ("open")),
                              -1);
        sock.get_local_addr (me);
        ACE_TCHAR me_str[80];
        me.addr_to_string (me_str, 80);
        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Receiving on %s\n"), me_str));

        // Spawn 1 writer and 3 reader processes that will contend for the
        // lock.
        Child writer;
        Child readers[Nr_Processes - 1];
        int i;

        for (i = 0; i < Nr_Processes; i++)
        {
            Child *child = (i == 0 ? &writer : &readers[i-1]);
            ACE_Process_Options options;
            options.command_line (format,
                                  argc > 0 ? argv[0] : ACE_TEXT ("RW_Process_Mutex_Test"),
                                  i,
                                  (unsigned int)me.get_port_number (),
                                  mutex_name.c_str ());
            if (child->spawn (options) == -1)
            {
                ACE_ERROR_RETURN ((LM_ERROR,
                                   ACE_TEXT ("spawn of child %d %p\n"),
                                   i,
                                   ACE_TEXT ("failed")),
                                  -1);
            }
            else
            {
                ACE_DEBUG ((LM_DEBUG,
                            ACE_TEXT ("Child process %d has pid = %d.\n"),
                            i,
                            (int)(child->getpid ())));
            }
        }

        // Keep reading time ranges reported from the children until all the
        // children have exited. Alternate between checking for a range and
        // checking for exits.
        int processes = Nr_Processes;
        Child *children[Nr_Processes];
        for (i = 0; i < Nr_Processes; i++)
            children[i] = (i == 0 ? &writer : &readers[i-1]);

        Range_Report report;
        ACE_Time_Value poll (0);
        ACE_INET_Addr from;
        ssize_t bytes;
        while (processes > 0)
        {
            ACE_Time_Value limit (10);
            bytes = sock.recv (&report, sizeof (report), from, 0, &limit);
            if (bytes > 0)
            {
                ACE_DEBUG ((LM_DEBUG,
                            ACE_TEXT ("Report from child %d; %b bytes\n"),
                            report.child_, bytes));
                if (report.child_ == 0)
                    writer.add_range (report.range_);
                else
                {
                    if (report.child_ >= 1 && report.child_ < Nr_Processes)
                        readers[report.child_ - 1].add_range (report.range_);
                    else
                        ACE_ERROR ((LM_ERROR,
                                    ACE_TEXT ("Report from out-of-range child #%d\n"),
                                    report.child_));
                }
            }
            else
            {
                if (errno == ETIME)
                    ACE_DEBUG ((LM_DEBUG,
                                ACE_TEXT ("UDP time out; check child exits\n")));
                else
                    ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("UDP recv")));
            }

            for (i = 0; i < Nr_Processes; i++)
            {
                if (children[i] == 0)
                    continue;
                ACE_exitcode child_status;
                // See if the child has exited.
                int wait_result = children[i]->wait (poll, &child_status);
                if (wait_result == -1)
                    ACE_ERROR ((LM_ERROR, ACE_TEXT ("Wait for child %d, %p\n"),
                                i, ACE_TEXT ("error")));
                else if (wait_result != 0)
                {
                    if (child_status == 0)
                        ACE_DEBUG ((LM_DEBUG,
                                    ACE_TEXT ("Child %d finished ok\n"),
                                    (int)(children[i]->getpid ())));
                    else
                        ACE_ERROR ((LM_ERROR,
                                    ACE_TEXT ("Child %d finished with status %d\n"),
                                    (int)(children[i]->getpid ()), child_status));
                    children[i] = 0;
                    --processes;
                }
            }
        }

        sock.close ();

        if (0 != mutex.remove ())
            ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("mutex remove")));

        ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Comparing time ranges...\n")));
        // The writer should never overlap any readers
        bool writer_overlap = false;
        for (i = 0; i < Nr_Processes - 1; ++i)
        {
            if (writer.any_overlaps (readers[i]))
            {
                ACE_ERROR ((LM_ERROR,
                            ACE_TEXT ("Writer overlaps reader %d\n"),
                            i+1));
                writer_overlap = true;
            }
        }
        if (!writer_overlap)
            ACE_DEBUG ((LM_DEBUG,
                        ACE_TEXT ("Writer does not overlap with readers; Ok\n")));

        // And there should be some overlap between readers.
        bool reader_overlap = false;
        for (i = 0; i < Nr_Processes - 1; ++i)
        {
            // Just compare to those higher, else it compares the same ones,
            // only in reverse.
            for (int j = i + 1; j < Nr_Processes - 1; ++j)
            {
                if (readers[i].any_overlaps (readers[j]))
                {
                    ACE_DEBUG ((LM_DEBUG,
                                ACE_TEXT ("Reader %d overlaps reader %d; Ok\n"),
                                i + 1, j + 1));
                    reader_overlap = true;
                }
            }
        }
        if (!reader_overlap)
            ACE_ERROR ((LM_ERROR, ACE_TEXT ("No readers overlapped!\n")));

        ACE_END_TEST;
    }

    return 0;
}
コード例 #26
0
int
Echo_Handler::handle_input (ACE_HANDLE)
{
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Echo_Handler::handle_input - ")
              ACE_TEXT ("activity occurred on handle %d!\n"),
              this->ping_socket ().get_handle ()));

  ACE_TCHAR buf[BUFSIZ];
  ACE_OS::memset (buf, 0, sizeof buf);

  ACE_INET_Addr addr;
  int rval_recv = -1;

  // Receive an <n> byte <buf> from the datagram socket
  // (uses<recvfrom(3)>).
  rval_recv =
    this->ping_socket ().recv (this->ping_socket ().icmp_recv_buff (),
                               ACE_Ping_Socket::PING_BUFFER_SIZE,
                               addr);
  switch (rval_recv)
    {
    case -1:
      // Complain and leave, but keep registered, returning 0.
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("(%P|%t) Echo_Handler::handle_input - ")
                         ACE_TEXT ("%p: bad read\n"),
                         ACE_TEXT ("client")),
                        0);
      // NOTREACHED

    case 0:
      // Complain and leave
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("(%P|%t) Echo_Handler::handle_input - ")
                         ACE_TEXT ("closing daemon (fd = %d)\n"),
                         this->get_handle ()),
                        0);
      // NOTREACHED

    default:
      ACE_DEBUG ((LM_INFO,
                  ACE_TEXT ("(%P|%t) Echo_Handler::handle_input - ")
                  ACE_TEXT ("message from %d bytes received.\n"),
                  rval_recv));

      if (! this->ping_socket ().process_incoming_dgram (
             this->ping_socket ().icmp_recv_buff (),
             rval_recv))
        {
          for (size_t k = 0; k <this->number_remotes_; ++k)
            {
              if (addr.is_ip_equal (this->remote_addrs_[k]))
                {
                  if (addr.addr_to_string (buf, sizeof buf) == -1)
                    {
                      ACE_ERROR ((LM_ERROR,
                                  ACE_TEXT ("%p\n"),
                                  ACE_TEXT ("can't obtain peer's address")));
                    }
                  else
                    {
                      ACE_DEBUG
                        ((LM_INFO,
                          ACE_TEXT ("(%P|%t) Echo_Handler::handle_input - ")
                          ACE_TEXT ("ECHO_REPLY received ")
                          ACE_TEXT ("from %s; marking this peer alive\n"),
                          buf));
                    }
                  // mark as successful
                  this->success_status_[k] = 0;
                  break;
                }
            }
        }
      break;
    }

  return 0;
}
コード例 #27
0
ファイル: Basic_Robust.cpp プロジェクト: azraelly/knetwork
int ACE_TMAIN (int, ACE_TCHAR *[])
{
  /*
   * Here we will use the default ctor and the set()
   * method to configure it. After each set() we will
   * display the address as a string and then connect
   * to each respective server. We can reuse the addr
   * instance once connection has been established.
   *
   // Listing 1 code/ch06
  ACE_INET_Addr addr;
  ...
  addr.set ("HAStatus", ACE_LOCALHOST);
  ...
  addr.set ("HALog", ACE_LOCALHOST);
   // Listing 1
   *
   */

  ACE_INET_Addr addr;
  ACE_TCHAR peerAddress[64];

  // Listing 2 code/ch06
  addr.set (ACE_TEXT("HAStatus"), ACE_LOCALHOST);
  if (addr.addr_to_string (peerAddress,
                           sizeof(peerAddress), 0) == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) Connecting to %s\n"),
                  peerAddress));
    }
  // Listing 2

  // Listing 3 code/ch06
  ACE_SOCK_Stream status;
  ACE_OS::last_error(0);
  ACE_SOCK_Connector statusConnector (status, addr);
  if (ACE_OS::last_error())
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("status")), 100);
  // Listing 3

  addr.set (ACE_TEXT("HALog"), ACE_LOCALHOST);
  if (addr.addr_to_string (peerAddress,
                           sizeof(peerAddress), 0) == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%P|%t) Connecting to %s\n"),
                  peerAddress ));
    }

  // Listing 4 code/ch06
  ACE_SOCK_Connector logConnector;
  ACE_Time_Value timeout (10);
  ACE_SOCK_Stream log;
  if (logConnector.connect (log, addr, &timeout) == -1)
    {
      if (ACE_OS::last_error() == ETIME)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) Timeout while ")
                      ACE_TEXT ("connecting to log server\n")));
        }
      else
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n"),
                      ACE_TEXT ("log")));
        }
      return (101);
    }
  // Listing 4

  /*
   * We generally let the OS pick our local port number but
   * if you want, you can choose that also:
   // Listing 5 code/ch06
  ACE_SOCK_Connector logConnector;
  ACE_INET_Addr local (4200, ACE_LOCALHOST);
  if (logConnector.connect (log, addr, 0, local) == -1)
    {
      ...
   // Listing 5
    }
   */

  char buf[64];

  // Listing 6 code/ch06
  ACE_Time_Value sendTimeout (0, 5);
  if (status.send_n ("uptime\n", 7, &sendTimeout) == -1)
    {
      if (ACE_OS::last_error() == ETIME)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) Timeout while sending ")
                      ACE_TEXT ("query to status server\n")));
        }
      // Listing 6
      else
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n"),
                      ACE_TEXT ("send_n")));
        }
      return (102);
    }

  // Listing 7 code/ch06
  ssize_t bc ;
  ACE_Time_Value recvTimeout (0, 1);
  if ((bc = status.recv (buf, sizeof(buf), &recvTimeout)) == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("%p\n"),
                  ACE_TEXT ("recv")));
      return (103);
    }

  log.send_n (buf, bc);
  // Listing 7

  status.close ();
  log.close ();

  return (0);
}
コード例 #28
0
ファイル: Commands.cpp プロジェクト: BearWare/TeamTalk5
 ACE_TString InetAddrToString(const ACE_INET_Addr& addr)
 {
     ACE_TCHAR buf[MAX_STRING_LENGTH+1] = {};
     addr.addr_to_string(buf, MAX_STRING_LENGTH);
     return buf;
 }