Пример #1
0
 /** Creates a connection stub and binds it, use {@link #connect(Address)} to connect */
 public TcpConnection(Address peer_addr, TcpBaseServer server) throws Exception {
   this.server = server;
   if (peer_addr == null)
     throw new IllegalArgumentException("Invalid parameter peer_addr=" + peer_addr);
   this.peer_addr = peer_addr;
   this.sock = server.socketFactory().createSocket("jgroups.tcp.sock");
   setSocketParameters(sock);
   last_access = getTimestamp(); // last time a message was sent or received (ns)
 }
Пример #2
0
  public void start() {
    if (receiver != null) receiver.stop();
    receiver = new Receiver(server.factory).start();

    if (isSenderUsed()) {
      if (sender != null) sender.stop();
      sender = new Sender(server.factory, server.sendQueueSize()).start();
    }
  }
Пример #3
0
  /**
   * Reads the peer's address. First a cookie has to be sent which has to match my own cookie,
   * otherwise the connection will be refused
   */
  protected Address readPeerAddress(Socket client_sock) throws Exception {
    int timeout = client_sock.getSoTimeout();
    client_sock.setSoTimeout(server.peerAddressReadTimeout());

    try {
      // read the cookie first
      byte[] input_cookie = new byte[cookie.length];
      in.readFully(input_cookie, 0, input_cookie.length);
      if (!Arrays.equals(cookie, input_cookie))
        throw new SocketException(
            "BaseServer.TcpConnection.readPeerAddress(): cookie read by "
                + server.localAddress()
                + " does not match own cookie; terminating connection");
      // then read the version
      short version = in.readShort();
      if (!Version.isBinaryCompatible(version))
        throw new IOException(
            "packet from "
                + client_sock.getInetAddress()
                + ":"
                + client_sock.getPort()
                + " has different version ("
                + Version.print(version)
                + ") from ours ("
                + Version.printVersion()
                + "); discarding it");
      short addr_len = in.readShort(); // only needed by NioConnection

      Address client_peer_addr = new IpAddress();
      client_peer_addr.readFrom(in);
      updateLastAccessed();
      return client_peer_addr;
    } finally {
      client_sock.setSoTimeout(timeout);
    }
  }
Пример #4
0
 protected void connect(Address dest, boolean send_local_addr) throws Exception {
   SocketAddress destAddr =
       new InetSocketAddress(((IpAddress) dest).getIpAddress(), ((IpAddress) dest).getPort());
   try {
     if (!server.defer_client_binding)
       this.sock.bind(new InetSocketAddress(server.client_bind_addr, server.client_bind_port));
     if (this.sock.getLocalSocketAddress() != null
         && this.sock.getLocalSocketAddress().equals(destAddr))
       throw new IllegalStateException(
           "socket's bind and connect address are the same: " + destAddr);
     Util.connect(this.sock, destAddr, server.sock_conn_timeout);
     this.out = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
     this.in = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
     if (send_local_addr) sendLocalAddress(server.localAddress());
   } catch (Exception t) {
     Util.close(this.sock);
     throw t;
   }
 }
Пример #5
0
 public void connect(Address dest) throws Exception {
   connect(dest, server.usePeerConnections());
 }
Пример #6
0
 protected void updateLastAccessed() {
   if (server.connExpireTime() > 0) last_access = getTimestamp();
 }
Пример #7
0
 protected boolean isSenderUsed() {
   return server.sendQueueSize() > 0 && server.use_send_queues;
 }
Пример #8
0
 protected long getTimestamp() {
   return server.timeService() != null ? server.timeService().timestamp() : System.nanoTime();
 }