Пример #1
0
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
#if !defined (ACE_LACKS_ACE_IOSTREAM)
  const ACE_TCHAR *server_host = argc > 1 ? argv[1] : ACE_DEFAULT_SERVER_HOST;
  u_short server_port = argc > 2 ? ACE_OS::atoi (argv[2]) : ACE_DEFAULT_SERVER_PORT;

  ACE_IOStream<ACE_SOCK_Stream> server;
  ACE_SOCK_Connector connector;
  ACE_INET_Addr addr (server_port,
                      server_host);

  if (connector.connect (server, addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);

  // Buffer up some things to send to the server.
  server << "1 2.3 testing" << endl;

  int i;
  float f;

#if defined (ACE_HAS_STRING_CLASS)
  ACE_IOStream_String s1;
  ACE_IOStream_String s2;
  server >> s1 >> i >> f >> s2;

  cerr << "Server said:\n\t";
  cerr << s1 << " ";
  cerr << i << " ";
  cerr << f << " ";
  cerr << s2 << endl;
#else
  server >> i >> f;

  cerr << "(" << ACE_OS::getpid () << ") Server sent:\n\t";
  cerr << "(" << i << ") ";
  cerr << "(" << f << ")" << endl;
#endif /* ACE_HAS_STRING_CLASS */

  if (server.close () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "close"),
                      -1);
#else
  ACE_UNUSED_ARG (argc);
  ACE_UNUSED_ARG (argv);
  ACE_ERROR ((LM_ERROR, "ACE_IOSTREAM not supported on this platform\n"));
#endif /* !ACE_LACKS_ACE_IOSTREAM */
  return 0;
}
Пример #2
0
int Client::open(const char *server, u_short port)
{
    ACE_SOCK_Connector connector;
    ACE_INET_Addr addr(port, server);
    if(connector.connect(*this, addr) == -1)
    {
        ACE_ERROR_RETURN((LM_ERROR, "%p\n", "open"), -1);
    }
    initialized_ = 1;
    return 0;
}
Пример #3
0
int
Sender::open (const ACE_TCHAR *host,
               u_short port)
{
  // Initialize stuff

  // Open input file (in OVERLAPPED mode)
  this->input_file_ =
    ACE_OS::open (file, GENERIC_READ | FILE_FLAG_OVERLAPPED);
  if (this->input_file_ == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "ACE_OS::open"), -1);

  // Find file size
  this->file_size_ =
    ACE_OS::filesize (this->input_file_);

  // Connect to remote host
  ACE_INET_Addr address (port, host);
  ACE_SOCK_Connector connector;
  if (connector.connect (this->stream_,
                         address) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "ACE_SOCK_Connector::connect"),
                      -1);

  // Open ACE_Asynch_Write_Stream
  if (this->ws_.open (*this) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "ACE_Asynch_Write_Stream::open"),
                      -1);

  // Open ACE_Asynch_Read_File
  if (this->rf_.open (*this, this->input_file_) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "ACE_Asynch_Read_File::open"),
                      -1);

  // Start an asynchronous transmit file
  if (this->transmit_file () == -1)
    return -1;

  // Start an asynchronous read file
  if (this->initiate_read_file () == -1)
    return -1;

  return 0;
}
Пример #4
0
int
Scavenger_Task::svc(void)
{
  this->the_barrier_->wait ();
  ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting scavenger thread\n"));

  ACE_SOCK_Stream stream;
  {
    ACE_INET_Addr remote_sap (this->endpoint_);
    ACE_SOCK_Connector connector;

    if (connector.connect(stream, remote_sap) == -1)
      {
        ACE_ERROR((LM_ERROR, "Cannot connect to <%s>\n", endpoint_));
        return -1;
      }
  }

  for (;;)
    {
      ACE_Time_Value period (0, this->period_in_usecs_);
      ACE_OS::sleep (period);

      {
        ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->mutex_, -1);
        if (this->stopped_)
          break;
      }
      ACE_hrtime_t start = ACE_OS::gethrtime ();
      ssize_t n = stream.send_n(&start, sizeof(start));
      if (n == 0 || n == -1)
        break;

      ACE_hrtime_t end;
      n = stream.recv(&end, sizeof(end));
      if (n == 0 || n == -1)
        break;

      if (start != end)
      {
        ACE_ERROR((LM_ERROR,
                   "Mismatched response from <%s>\n", endpoint_));
        break;
      }

    }
  stream.close();

  ACE_DEBUG ((LM_DEBUG, "(%P|%t) Finishing scavenger thread\n"));
  return 0;
}
Пример #5
0
 bool open()
  {
      ACE_SOCK_Connector connector;
      ACE_INET_Addr addr(80,"127.0.0.1");
      ACE_Time_Value timeout(5,0);
     if(connector.connect(peer,addr,&timeout) != 0)
      {
         cout<<"connecetd fail"<<endl;
         return false;
      }
      ACE_Reactor::instance()->register_handler(this,ACE_Event_Handler::READ_MASK);
      cout<<"connecetd "<<endl;
     return true;
  }
static ACE_THR_FUNC_RETURN
worker (void *)
{
  ACE_OS::sleep (3);
  const ACE_TCHAR *msg = ACE_TEXT ("Message from Connection worker");
  ACE_TCHAR buf [BUFSIZ];
  buf[0] = static_cast<ACE_TCHAR> ((ACE_OS::strlen (msg) + 1));
  ACE_OS::strcpy (&buf[1], msg);

  ACE_INET_Addr addr (rendezvous);

  ACE_DEBUG((LM_DEBUG,
             "(%t) Spawning %d client threads...\n",
             cli_thrno));
  int grp = ACE_Thread_Manager::instance ()->spawn_n (cli_thrno,
                                                      &cli_worker,
                                                      buf);
  ACE_TEST_ASSERT (grp != -1);

  ACE_Thread_Manager::instance ()->wait_grp (grp);

  ACE_DEBUG ((LM_DEBUG,
              "(%t) Client threads done; shutting down...\n"));
  ACE_SOCK_Stream stream;
  ACE_SOCK_Connector connect;

  if (connect.connect (stream, addr) == -1)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%t) %p Error while connecting\n"),
                ACE_TEXT ("connect")));

  const ACE_TCHAR *sbuf = ACE_TEXT ("\011shutdown");

  ACE_DEBUG ((LM_DEBUG,
              "shutdown stream handle = %x\n",
              stream.get_handle ()));

  if (stream.send_n (sbuf, (ACE_OS::strlen (sbuf) + 1) * sizeof (ACE_TCHAR)) == -1)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%t) %p\n"),
                ACE_TEXT ("send_n")));

  ACE_DEBUG ((LM_DEBUG,
              "Sent message of length  = %d\n",
              ACE_OS::strlen (sbuf)));
  stream.close ();

  return 0;
}
Пример #7
0
int ShmemHybridStream::connect(const ACE_INET_Addr& ace_address)
{
    if (m_bLinked) return -1;

    ACE_SOCK_Connector connector;
    YARP_SSIZE_T result=connector.connect(m_SockStream,ace_address);
    if (result<0)
    {
        YARP_ERROR(Logger::get(),ConstString("ShmemHybridStream client returned ")+NetType::toString((int)result));
        close();
        return -1;
    }

    ACE_INET_Addr local,remote;
    m_SockStream.get_local_addr(local);
    m_SockStream.get_remote_addr(remote);
    m_LocalAddress=Contact(local.get_host_addr(),local.get_port_number());
    m_RemoteAddress=Contact(remote.get_host_addr(),remote.get_port_number());

    out.open(m_LocalAddress.getPort());

    ShmemPacket_t send_conn_data;
    send_conn_data.command=CONNECT;
    send_conn_data.size=SHMEM_DEFAULT_SIZE;
    result=m_SockStream.send_n(&send_conn_data,sizeof send_conn_data);
    if (result<=0)
    {
        YARP_ERROR(Logger::get(),ConstString("Socket returned ")+NetType::toString((int)result));
        close();
        return -1;
    }

    ShmemPacket_t recv_conn_data;
    result=m_SockStream.recv_n(&recv_conn_data,sizeof recv_conn_data);
    if (result<=0)
    {
        YARP_ERROR(Logger::get(),ConstString("Socket returned ")+NetType::toString((int)result));
        close();
        return -1;
    }

    in.open(m_RemoteAddress.getPort(),&m_SockStream);

    m_bLinked=true;

    m_SockStream.enable(ACE_NONBLOCK);

    return 0;
}
Пример #8
0
int SocketTwoWayStream::open(const Contact& address) {
    if (address.getPort()==-1) {
        return -1;
    }
    String host = address.getHost();
#ifdef YARP_HAS_ACE
    ACE_SOCK_Connector connector;
    if (address.getHost() == "localhost") {
        // ACE does not like localhost.  At all.
        NameConfig config;
        host = config.getHostName(true);
    }
    ACE_INET_Addr addr(address.getPort(),host.c_str());
    ACE_Time_Value openTimeout;
    ACE_Time_Value *timeout = NULL;
    if (address.hasTimeout()) {
        openTimeout.set(address.getTimeout());
        timeout = &openTimeout;
    }
    int result = connector.connect(stream,addr,timeout,ACE_Addr::sap_any,1);
#else
    TcpConnector connector;
    int result = connector.connect(stream, address);
#endif
    if (result>=0) {
        happy = true;
    } else {
        YARP_SPRINTF2(Logger::get(),
                      debug,
                      "TCP connection to tcp://%s:%d failed to open",
                      host.c_str(),
                      address.getPort());
    }
    updateAddresses();
    return result;
}
Пример #9
0
int SOYALDevice::make_handler(KSGDeviceNode* node,ACE_HANDLE* handler)
{
	if(!node)
		return -1;
	std::string ip = node->GetDevAddr().GetConnect();
	int port = node->GetDevAddr().GetPort();
	ACE_INET_Addr addr(port,ip.c_str());
	ACE_SOCK_Connector conn;
	ACE_SOCK_Stream stream;
	ACE_Time_Value tv = KSGGetTaskTimeoutIntval();
	int err_code;
	ACE_DEBUG((LM_TRACE,"开始连接soyal控制器,[%s][%s]",node->get_name().c_str(),ip.c_str()));
	if(conn.connect(stream,addr,&tv))
	{
		err_code = ACE_OS::last_error();
		// TODO: 返回连接的错误码
		if(EWOULDBLOCK == err_code)
		{
			ACE_DEBUG((LM_ERROR,"连接控制器失败"));
		}
		else if(EHOSTUNREACH == err_code || ENETUNREACH == err_code)
		{
			ACE_DEBUG((LM_ERROR,"无法连接设备主机"));
			node->SetState(KSGDeviceNode::dsError);
		}
		else
		{
			ACE_DEBUG((LM_ERROR,"连接主机未知错误![%d][%s]ip[%s]"
				,err_code,ACE_OS::strerror(err_code),ip.c_str()));
		}
		// add by cash 释放 SOCKET 
		// 2007-01-29
		stream.close();
		return -1;
	}
	// 设置 handler 为 BLOCK 的
	// stream.disable(ACE_NONBLOCK);
	// 设置 linger 属性
	struct linger lg;
	ACE_OS::memset(&lg,0,sizeof lg);
	lg.l_onoff = 1;
	// 3s 
	lg.l_linger = 3;
	stream.set_option(SOL_SOCKET,SO_LINGER,&lg,sizeof lg);
	node->SetState(KSGDeviceNode::dsOnline);
	*handler = stream.get_handle();
	return 0;
}
Пример #10
0
int GDCT_Transfer::do_connect_svr()
{
	ACE_SOCK_Connector conn;
	ACE_INET_Addr addr(svr_port_,svr_ip_.c_str());
	ACE_Time_Value to = default_timeout_;
	if(conn.connect(connector_,addr,&default_timeout_)!=0)
	{
		int r = ACE_OS::last_error();
		ACE_DEBUG((LM_ERROR,"连接讯源服务器失败[%s:%d]ret[%d:%s]",svr_ip_.c_str(),svr_port_,
			r,ACE_OS::strerror(r)));
		return -1;
	}
	ACE_DEBUG((LM_INFO,"连接讯源服务器成功[%s:%d]",svr_ip_.c_str(),svr_port_));
	is_connected_ = 1;
	return 0;
}
Пример #11
0
/* Open the connection to the server.  This is traditional ACE.  We
   simply construct an endpoint and use a connector to establish the
   link.
*/
int Client::open( void )
{
    ACE_INET_Addr addr(port_,server_);
    ACE_SOCK_Connector con;

    if( con.connect(peer(),addr) == -1 )
    {
        ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ACE_SOCK_Connector::connect()"), -1);
    }

        // Something new here...  We have to use the protocol stream
        // to ensure that our data is in the correct format when
        // received by the server.  Thus, we open the stream and
        // transfer ownership of the peer.
    return stream().open( peer() );
}
Пример #12
0
		bool open()  
		{  
			ACE_SOCK_Connector connector;  
			ACE_INET_Addr addr(7777,"127.0.0.1");  
			ACE_Time_Value timeout(5,0);  
			if(connector.connect(peer,addr,&timeout) != 0)  
			{  
				cout<<endl<<"connect fail.";  
				return false;  
			}  
			ACE_Reactor::instance()->register_handler(this,ACE_Event_Handler::READ_MASK);  
			cout<<endl<<"connected.";  
			strcpy(buf, "hello,Server\n");  
			peer.send(buf,strlen(buf));  
			return true;  
		}  
Пример #13
0
int main(int argc, char** argv)
{
    const char* pathname = argc > 1 ? argv[1] : "/index.html";
    const char* server_hostname = argc > 2 ? argv[2] : "ace.ece.uci.edu";

    // Add funcation call for demo.
    dummy();

    ACE_SOCK_Connector connector;
    ACE_SOCK_Stream peer;
    ACE_INET_Addr peer_addr;

    if (-1 ==  peer_addr.set(80, server_hostname))
    {
        log("peer_addr.set failed\n");
        return 1;
    }
    else if (-1 == connector.connect(peer, peer_addr))
    {
        log("connector.connect failed\n");
        return 1;
    }
    
    
    char buf[BUFSIZ];

    iovec iov[3];
    iov[0].iov_base = (void*)"GET ";
    iov[0].iov_len = 4;
    iov[1].iov_base = (void*)pathname;
    iov[1].iov_len = strlen(pathname);
    iov[2].iov_base = (void*)" HTTP/1.0\r\n\r\n";
    iov[2].iov_len = 13;

    if (-1 == peer.sendv_n(iov, 3))
    {
        log("peer.sendv_v failed\n");
        return 1;
    }

    for (ssize_t n = 0; (n = peer.recv(buf, sizeof(buf))) > 0; )
    {
        ACE::write_n(ACE_STDOUT, buf, n);
    }

    return peer.close();
}
int
Pipe::open (void)
{
  ACE_INET_Addr my_addr;
  ACE_SOCK_Acceptor acceptor;
  ACE_SOCK_Connector connector;
  ACE_SOCK_Stream reader;
  ACE_SOCK_Stream writer;
  int result = 0;

  // Bind listener to any port and then find out what the port was.
  if (acceptor.open (ACE_Addr::sap_any) == -1
      || acceptor.get_local_addr (my_addr) == -1)
    result = -1;
  else
    {
      int af = my_addr.get_type ();
      const ACE_TCHAR *local = ACE_LOCALHOST;
#if defined (ACE_HAS_IPV6)
      if (af == AF_INET6)
        local = ACE_IPV6_LOCALHOST;
#endif /* ACE_HAS_IPV6 */
      ACE_INET_Addr sv_addr (my_addr.get_port_number (),
                             local,
                             af);

      // Establish a connection within the same process.
      if (connector.connect (writer, sv_addr) == -1)
        result = -1;
      else if (acceptor.accept (reader) == -1)
        {
          writer.close ();
          result = -1;
        }
    }

  // Close down the acceptor endpoint since we don't need it anymore.
  acceptor.close ();
  if (result == -1)
    return -1;

  this->handles_[0] = reader.get_handle ();
  this->handles_[1] = writer.get_handle ();

  return 0;
}
Пример #15
0
/* Open a connection to the server.  This hides the use of
  ACE_SOCK_Connector from our caller.  Since our caller probably
  doesn't care *how* we connect, this is a good thing.  */
int
Client::open (const char *server,
              u_short port)
{
  /* This is right out of Tutorial 3.  The only thing we've added is
    to set the initialized_ member variable on success.  */

  ACE_SOCK_Connector connector;
  ACE_INET_Addr addr (port, server);

  if (connector.connect (*this, addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);
  initialized_ = 1;
  return 0;
}
Пример #16
0
int
ACE::HTBP::ID_Requestor::connect_to_server (ACE_SOCK_Stream *cli_stream)
{
  if (port_ == 0 || host_.length() == 0)
    {
      int host_start = url_.find (ACE_TEXT("http://")) + 7;
      int port_sep = 0;
      int sep = 0;
      if (host_start == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT("(%P|%t) ACE::HTBP::ID_Requestor::")
                           ACE_TEXT("connect_to_server: ")
                           ACE_TEXT("invalid URL: \"%s\"\n"),
                           url_.c_str()),
                          -1);
      port_sep = url_.find (ACE_TEXT(":"),(size_t)host_start);
      sep = url_.find (ACE_TEXT("/"),(size_t)host_start);
      if (sep == -1 || sep == host_start +1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT("(%P|%t) ACE::HTBP::ID_Requestor::")
                           ACE_TEXT("connect_to_server: ")
                           ACE_TEXT("invalid URL: \"%s\"\n"),
                           url_.c_str()),
                          -1);
      if (port_sep == -1)
        {
          port_sep = sep;
          port_ = 80; // should be a default?
        }
      host_ = url_.substr(host_start,port_sep - host_start);
    }

  ACE_INET_Addr remote_addr (static_cast<u_short> (port_), host_.c_str());
  ACE_SOCK_Connector con;
  if (con.connect (*cli_stream,
                   remote_addr) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT("(%P|%t) ACE::HTBP::ID_Requestor::")
                       ACE_TEXT("connect_to_server: ")
                       ACE_TEXT("%p\n"),
                       ACE_TEXT("socket connect")),
                      -1);
  return 0;
}
Пример #17
0
static u_int
host_is_up (ACE_TCHAR hostname[])
{
  ACE_SOCK_Connector con;
  ACE_SOCK_Stream sock;

  // The ACE_INET_Addr construction causes gethostbyname_r to be
  // called, so we need to copy the hostname.
  ACE_TCHAR test_host[MAXHOSTNAMELEN];
  ACE_OS::strcpy (test_host, hostname);

  ACE_INET_Addr another_host ((u_short) 7, test_host);
  ACE_Time_Value timeout_value (5);
  int const status = con.connect (sock,
                                  another_host,
                                  &timeout_value);
  sock.close ();
  return status == 0  ?  1  :  0;
}
int Sender::open (const ACE_TCHAR *host, u_short port)
{
  // Initialize stuff
  // Connect to remote host
  ACE_INET_Addr address (port, host);
  ACE_SOCK_Connector connector;

  if (connector.connect (this->stream_,
                         address) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "%p\n",
                         "ACE_SOCK_Connector::connect"),
                        -1);
    }

  // Open ACE_Asynch_Write_Stream
  if (this->ws_.open (*this) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "ACE_Asynch_Write_Stream::open"),
                      -1);

  // Open ACE_Asynch_Read_Stream
  if (this->rs_.open (*this) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "ACE_Asynch_Read_File::open"),
                      -1);

  // Start an asynchronous transmit file
  if ( this->initiate_write_stream () == -1)
    return -1;

  if ( duplex != 0 )
    {
      // Start an asynchronous read file
      if (this->initiate_read_stream () == -1)
        return -1;
    }

  return 0;
}
Пример #19
0
static void *
client (void *)
{
  char buf[100];
  size_t mes_len;
  ACE_OS::sleep (1);

  ACE_DEBUG ((LM_DEBUG,
              " (%P) Client: Starting...\n"));

  ACE_SOCK_Stream stream;
  ACE_SOCK_Connector connector;
  ACE_OS::sprintf (buf, "Client: the life was good!");

  mes_len = (int) htonl (ACE_OS::strlen (buf) + 1);

  if (connector.connect (stream,
                         ACE_INET_Addr (SERV_TCP_PORT,
                                        ACE_DEFAULT_SERVER_HOST)) == -1)
    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket open"));

  if (stream.send (4,
                   (void *) &mes_len,
                   sizeof (size_t),
                   (void *)buf,
                   ACE_OS::strlen (buf) + 1) == -1)

    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket send"));

  if (stream.close () == -1)
    ACE_ERROR ((LM_ERROR,
                "(%P) %p\n",
                "Socket close"));

  ACE_DEBUG ((LM_DEBUG,
              "(%P) Client: Message has been sent, about to exit...\n"));
  return 0;
}
Пример #20
0
static ACE_THR_FUNC_RETURN
cli_worker (void *arg)
{
  // Client thread function.
  ACE_INET_Addr addr (rendezvous);
  ACE_SOCK_Stream stream;
  ACE_SOCK_Connector connect;
  ACE_Time_Value delay (0, req_delay);
  size_t len = * reinterpret_cast<ACE_TCHAR *> (arg);

  for (size_t i = 0 ; i < cli_conn_no; i++)
    {
      if (connect.connect (stream, addr) < 0)
        {
          ACE_ERROR ((LM_ERROR,
                      "(%t) %p\n",
                      "connect"));
          continue;
        }

      for (size_t j = 0; j < cli_req_no; j++)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%t) conn_worker handle 0x%x, req %d\n",
                      stream.get_handle (),
                      j+1));
          if (stream.send_n (arg,
                             (len + 1) * sizeof (ACE_TCHAR)) == -1)
            {
              ACE_ERROR ((LM_ERROR,
                          "(%t) %p\n",
                          "send_n"));
              continue;
            }
          ACE_OS::sleep (delay);
        }

      stream.close ();
    }

  return 0;
}
Пример #21
0
  int CloudBus::svc(void)
  {
    
    while (true) {
      if (connected_) {
	//
      } else {
	std::string connect_addr(relay_inet_addr_);
	if (connect_addr == "localhost") {
	  connect_addr = node_info_.address;
	}
	ACE_INET_Addr server(relay_port_,connect_addr.c_str());
	ACE_SOCK_Connector connector;

	if (connector.connect(this->peer(),server) == 0) {
	  ACE_TCHAR peer_name[MAXHOSTNAMELENGTH];
	  ACE_INET_Addr peer_addr;
	  if ((this->peer().get_remote_addr (peer_addr) == 0) && 
	      (peer_addr.addr_to_string (peer_name, MAXHOSTNAMELENGTH) == 0)) {

	    GDEBUG("CloudBus connected to relay at  %s\n", peer_name);
	    if (this->reactor ()->register_handler(this, ACE_Event_Handler::READ_MASK) != 0) {
	      GERROR("Failed to register read handler\n");
	      return -1;
	    }

	    mtx_.acquire();
	    connected_ = true;
	    mtx_.release();
	    if (!query_mode_) {
	      send_node_info();
	    }
	  } 
	}
      }
      //Sleep for 5 seconds
      ACE_Time_Value tv (5);
      ACE_OS::sleep (tv);	  	
    }
    return 0;
  }
Пример #22
0
int RemoteLogger::connectRemoteServer()
{
	ACE_SOCK_Connector connector;
	ACE_INET_Addr addr(m_remote_addr.c_str());
	m_logger_session = new LoggerSession();
	ACE_Time_Value conn_time_out(1, 0);
	if (connector.connect(m_logger_session->peer(), addr, &conn_time_out) == -1)
	{
		m_remote_logger_enable = false;
		return -1;
	}
	else
	{
		m_logger_session->reactor(NULL);
		m_logger_session->open(NULL);

		m_manage_logger_input.setLoggerSession(m_logger_session, this);
		m_manage_logger_output.setLoggerSession(m_logger_session, this);

		if (m_manage_logger_input.init() == -1)
		{
			return -1;
		}

		if (m_manage_logger_output.init() == -1)
		{
			return -1;
		}

		m_remote_session_reference_no = 2;

		char * first_log = ManageLogger::instance()->getBuffer();
		strcpy(first_log, m_log_identify.c_str());
		memcpy(first_log + m_log_identify.length(), "\n", 1);
		m_manage_logger_output.handleLogBuffer(first_log);

		m_remote_logger_enable = true;

		return 0;
	}
}
Пример #23
0
int main(int argc, char *argv[])
{
	ACE_INET_Addr addr(1500, "127.0.0.1"); //remote address
	ACE_SOCK_Connector con; // connector for socket client
	ACE_SOCK_Stream stream; // stream is for socket read/write

	if (con.connect(stream, addr) == -1) //connect to remote address
	{
		ACE_DEBUG((LM_DEBUG,
			ACE_TEXT("(%P|%t) %p\n"),
			ACE_TEXT("connection failed")));
		return 1;
	}

	const char msg[] = "Hello,ACE!";

	stream.send_n(msg, sizeof(msg)); // send_n function send exactly n bytes

	char buffer[1024] = { 0 };

	if (stream.recv(buffer, sizeof(buffer) - 1) == -1) // just call socket recv
	{
		ACE_DEBUG((LM_DEBUG,
			ACE_TEXT("(%P|%t) %p\n"),
			ACE_TEXT("recv failed")));
		return 1;
	}
	ACE_DEBUG((LM_DEBUG,
		ACE_TEXT("(%P|%t) recv:%s\n"),
		buffer));

	if (stream.close() == -1) //close the connection
	{
		ACE_ERROR((LM_ERROR,
			ACE_TEXT("(%P|%t) %p\n"),
			ACE_TEXT("close")));
		return 1;
	}

	return 0;
}
static void *
client (void *arg)
{
  ACE_INET_Addr *remote_addr = reinterpret_cast<ACE_INET_Addr *> (arg);

  ACE_INET_Addr server_addr (remote_addr->get_port_number (),
                             "::1");

  ACE_SOCK_Stream cli_stream;

  ACE_SOCK_Connector con;
  ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) Connecting to port %d\n"),
              server_addr.get_port_number()));

  // Initiate connection with server; don't wait forever
  if (con.connect (cli_stream,
                   server_addr,
                   &timeout) == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("(%P|%t) %p\n"),
                  ACE_TEXT ("connection failed")));

      return 0;
    }

  // if connect succesful, what is the max port number we connected
  // up to now.
  int connected_port = server_addr.get_port_number ();

  if (connected_port > max_connected_port)
    max_connected_port = connected_port;

  cli_stream.close ();

  return 0;
}
Пример #25
0
ACE_SOCK_Stream *
ACE_TSS_Connection::make_TSS_TYPE (void) const
{
  ACE_TRACE ("ACE_TSS_Connection::make_TSS_TYPE");

  ACE_SOCK_Connector connector;
  ACE_SOCK_Stream *stream = 0;

  ACE_NEW_RETURN (stream,
                  ACE_SOCK_Stream,
                  0);

  if (connector.connect (*stream, server_address_) == -1)
    {
      delete stream;
      errno = ECONNREFUSED;
      return 0;
    }

  ACE_DEBUG ((LM_DEBUG,  ACE_TEXT ("ACE_TSS_Connection new connection\n")));
  return stream;
}
Пример #26
0
Session_t SessionPoolImp::ISessionPool_connect(const string & connect_addr)
{
	ACE_SOCK_Connector connector;
	ACE_INET_Addr addr;
	
	addr.set(connect_addr.c_str());
	CellSession * cell_session = new CellSession(true);

	if (connector.connect(cell_session->peer(), addr) == -1)
	{
		DEF_LOG_ERROR("failed to connect the addr : <%s>, last error is <%d>\n", connect_addr.c_str(), ACE_OS::last_error());
		return 0;
	}

	if (cell_session->setReadEvent() == -1)
	{
		DEF_LOG_ERROR("failed to call open of cell session, last error is <%d>\n", ACE_OS::last_error());
		return 0;
	}

	if (m_socket_intput_buffer_size > 0)
	{
		cell_session->setBufSize(m_socket_intput_buffer_size, m_socket_output_buffer_size);
	}

	cell_session->setHandleInput(this);
	m_input_session_pool.handleSession(cell_session);
	m_output_session_pool.handleSession(cell_session);


	DEF_LOG_INFO("success to connect the addr <%s>\n", connect_addr.c_str());
	if (NULL != m_handle_session_event)
	{
		m_handle_session_event->ISessionPoolEvent_newConnection(cell_session, cell_session->isClientSide());
	}

	return cell_session;
}
Пример #27
0
int
Peer_Handler::open (void *)
{
  if (host_ != 0) // Connector
    {
      ACE_INET_Addr addr (port_, host_);
      ACE_SOCK_Connector connector;

      // Establish connection with server.
      if (connector.connect (stream_, addr) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "connect"), -1);

      ACE_DEBUG ((LM_DEBUG, "(%t) connected.\n"));
    }
  else // Acceptor
    {
      ACE_SOCK_Acceptor acceptor;
      ACE_INET_Addr local_addr (port_);

      if ((acceptor.open (local_addr) == -1) ||
          (acceptor.accept (this->stream_) == -1))
        ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "accept failed"), -1);

      ACE_DEBUG ((LM_DEBUG, "(%t) accepted.\n"));
    }

  int result = this->rd_stream_.open (*this);
  if (result != 0)
    return result;

  result = this->wr_stream_.open (*this);
  if (result != 0)
    return result;

  result = this->rd_stream_.read (this->mb_,
                                  this->mb_.size ());
  return result;
}
Пример #28
0
    void send_work_to_server(ACE_TCHAR* arg)
    {
      ACE_SOCK_Stream stream;
      ACE_SOCK_Connector connect;
      ACE_Time_Value delay (0, req_delay);
      size_t len = * reinterpret_cast<ACE_TCHAR *> (arg);

      for (size_t i = 0 ; i < cli_conn_no; i++)
        {
          if (connect.connect (stream, addr_) < 0)
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("(%t) %p\n"),
                          ACE_TEXT ("connect")));
              continue;
            }

          for (size_t j = 0; j < cli_req_no; j++)
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("Sending work to server on handle 0x%x, req %d\n"),
                          stream.get_handle (),
                          j+1));
              if (stream.send_n (arg,
                                 (len + 1) * sizeof (ACE_TCHAR)) == -1)
                {
                  ACE_ERROR ((LM_ERROR,
                              ACE_TEXT ("(%t) %p\n"),
                              ACE_TEXT ("send_n")));
                  continue;
                }
              ACE_OS::sleep (delay);
            }

          stream.close ();
        }

    }
Пример #29
0
    void shut_down()
    {
      ACE_SOCK_Stream stream;
      ACE_SOCK_Connector connect;

      if (connect.connect (stream, addr_) == -1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("(%t) %p Error while connecting\n"),
                    ACE_TEXT ("connect")));

      const ACE_TCHAR *sbuf = ACE_TEXT ("\011shutdown");

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("shutdown stream handle = %x\n"),
                  stream.get_handle ()));

      if (stream.send_n (sbuf, (ACE_OS::strlen (sbuf) + 1) * sizeof (ACE_TCHAR)) == -1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("(%t) %p\n"),
                    ACE_TEXT ("send_n")));

      stream.close ();
    }
Пример #30
0
int ACEEngine::AddConnector(int connectorIndex, char* szIP, unsigned short port)
{
	ACE_SOCK_Stream* stream = new ACE_SOCK_Stream();
	ACE_INET_Addr connectAddr(port, szIP);
	ACE_SOCK_Connector connector;
	int result = connector.connect(*stream, connectAddr);
	if (-1 == result)
		return -1;

	_SessionDesc sessionDesc;
	sessionDesc.identifier = connectorIndex;
	sessionDesc.sessionType = SESSION_TYPE::SESSION_CONNECTOR;

	ProactorService* pService = new ProactorService();
	pService->SetOwner(this);
	pService->SetSessionDesc(sessionDesc);

	ACE_Message_Block mb;
	pService->open(stream->get_handle(), mb);
	delete stream;
	stream = NULL;

	return pService->GetSerial();
}