public RouterStub(IpAddress local, IpAddress remote, boolean use_nio, CloseListener l) { this.local=local; this.remote=remote; this.use_nio=use_nio; this.close_listener=l; client=use_nio? new NioClient(local, remote) : new TcpClient(local, remote); client.receiver(this); client.addConnectionListener(this); client.socketConnectionTimeout(sock_conn_timeout).tcpNodelay(tcp_nodelay); }
protected void updateLastAccessed() { if(server.connExpireTime() > 0) last_access=getTimestamp(); }
protected void updateLastAccessed() { if(server.connExpireTime() > 0) last_access=getTimestamp(); }
@Override public void start() throws Exception { if(running.compareAndSet(false, true)) { super.start(); conn=createConnection(remote_addr); addConnectionListener(this); conn.connect(remote_addr, false); local_addr=conn.localAddress(); if(use_peer_connections) conn.sendLocalAddress(local_addr); conn.start(); // starts the receiver thread } }
@Override public String status() { if(channel == null) return "n/a"; if(isConnected()) return "connected"; if(isConnectionPending()) return "connection pending"; if(isOpen()) return "open"; return "closed"; }
public String toString() { if(conn == null || !conn.isConnected()) return String.format("%s -> %s [not connected yet]", localAddress(), remoteAddress()); return String.format("%s", conn); } }
@Override public void connect(Address dest) throws Exception { connect(dest, server.usePeerConnections()); }
@Override public void connectionClosed(Connection conn, String reason) { removeConnectionIfPresent(conn.peerAddress(), conn); }
public void connect(Address dest) throws Exception { connect(dest, server.usePeerConnections()); }
public boolean canRun() {return isRunning() && isConnected();} public int bufferSize() {return buffer != null? buffer.length : 0;}
@Override public String status() { if(sock == null) return "n/a"; if(isConnected()) return "connected"; if(isOpen()) return "open"; return "closed"; }
/** * Called by a {@link Connection} implementation when a message has been received. Note that data might be a * reused buffer, so unless used to de-serialize an object from it, it should be copied (e.g. if we store a ref * to it beyone the scope of this receive() method) */ public void receive(Address sender, byte[] data, int offset, int length) { if(this.receiver != null) this.receiver.receive(sender, data, offset, length); }
/** * Sends a message. If the previous write didn't complete, tries to complete it. If this still doesn't complete, * the message is dropped (needs to be retransmitted, e.g. by UNICAST3 or NAKACK2). * @param buf * @throws Exception */ @Override public void send(ByteBuffer buf) throws Exception { send(buf, true); }
protected void clear(int op) { try { clearSelectionKey(op); } catch(Throwable t) { } }
@Override public void send(Address dest, byte[] data, int offset, int length) throws Exception { send(data, offset, length); }
protected PhysicalAddress getPhysicalAddress() { return server != null? (PhysicalAddress)server.localAddress() : null; } }
/** * Called by a {@link Connection} implementation when a message has been received */ public void receive(Address sender, ByteBuffer buf) { if(this.receiver != null) this.receiver.receive(sender, buf); }