@Override protected TcpConnection createConnection(Address dest) throws Exception { return new TcpConnection(dest, this); }
public void send(ByteBuffer data) throws Exception { if(conn == null) throw new IllegalStateException("connection to server " + remote_addr + " doesn't exist (has start() been called?)"); conn.send(data); }
@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(sock == null) return "n/a"; if(isConnected()) return "connected"; if(isOpen()) return "open"; return "closed"; }
public TcpConnection(Socket s, TcpServer server) throws Exception { this.sock=s; this.server=server; if(s == null) throw new IllegalArgumentException("Invalid parameter s=" + s); setSocketParameters(s); this.out=new DataOutputStream(createBufferedOutputStream(s.getOutputStream())); this.in=new DataInputStream(createBufferedInputStream(s.getInputStream())); this.connected=sock.isConnected(); this.peer_addr=server.usePeerConnections()? readPeerAddress(s) : new IpAddress((InetSocketAddress)s.getRemoteSocketAddress()); last_access=getTimestamp(); // last time a message was sent or received (ns) }
protected void handleAccept(final Socket client_sock) throws Exception { TcpConnection conn=null; try { conn=new TcpConnection(client_sock, TcpServer.this); Address peer_addr=conn.peerAddress(); synchronized(this) { boolean conn_exists=hasConnection(peer_addr), replace=conn_exists && use_peer_connections && local_addr.compareTo(peer_addr) < 0; // bigger conn wins if(!conn_exists || replace) { replaceConnection(peer_addr, conn); // closes old conn conn.start(); log.trace("%s: accepted connection from %s", local_addr, peer_addr); } else { log.trace("%s: rejected connection from %s %s", local_addr, peer_addr, explanation(conn_exists, replace)); Util.close(conn); // keep our existing conn, reject accept() and close client_sock } } } catch(Exception ex) { Util.close(conn); throw ex; } } }
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)); Util.connect(this.sock, destAddr, server.sock_conn_timeout); if(this.sock.getLocalSocketAddress() != null && this.sock.getLocalSocketAddress().equals(destAddr)) throw new IllegalStateException("socket's bind and connect address are the same: " + destAddr); this.out=new DataOutputStream(createBufferedOutputStream(sock.getOutputStream())); this.in=new DataInputStream(createBufferedInputStream(sock.getInputStream())); connected=sock.isConnected(); if(send_local_addr) sendLocalAddress(server.localAddress()); } catch(Exception t) { Util.close(this.sock); connected=false; throw t; } }
/** * * @param data Guaranteed to be non null * @param offset * @param length */ public void send(byte[] data, int offset, int length) throws Exception { if(out == null) return; writers.incrementAndGet(); send_lock.lock(); try { doSend(data, offset, length); updateLastAccessed(); } catch(InterruptedException iex) { Thread.currentThread().interrupt(); // set interrupt flag again } finally { if(writers.decrementAndGet() == 0) // only the last active writer thread calls flush() flush(); // won't throw an exception send_lock.unlock(); } }
/** 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) }
public String toString() { Socket tmp_sock=sock; if(tmp_sock == null) return "<null socket>"; InetAddress local=tmp_sock.getLocalAddress(), remote=tmp_sock.getInetAddress(); String local_str=local != null? Util.shortName(local) : "<null>"; String remote_str=remote != null? Util.shortName(remote) : "<null>"; return String.format("%s:%s --> %s:%s (%d secs old) [%s] [recv_buf=%d]", local_str, tmp_sock.getLocalPort(), remote_str, tmp_sock.getPort(), TimeUnit.SECONDS.convert(getTimestamp() - last_access, TimeUnit.NANOSECONDS), status(), receiver != null? receiver.bufferSize() : 0); }
ProgressBar p = new ProgressDialog(LoginPageActivity.this); Private Handler handler = new Handler(); p.setVisibality(0); //makes visible new Thread(new Runnable() { public void run() { TcpConnection loginConnect = new TcpConnection(); loginConnect.run(); handler.post(new Runnable() { public void run() { p.setVisibility(8);//Makes Invisible } }); } }).start();
@Override public boolean isConnected() {return conn != null && conn.isConnected();}
protected void updateLastAccessed() { if(server.connExpireTime() > 0) last_access=getTimestamp(); }
public void connect(Address dest) throws Exception { connect(dest, server.usePeerConnections()); }
public Receiver(ThreadFactory f) { recv=f.newThread(this,"Connection.Receiver [" + getSockAddress() + "]"); }
public TcpConnection(Socket s, TcpServer server) throws Exception { this.sock=s; this.server=server; if(s == null) throw new IllegalArgumentException("Invalid parameter s=" + s); setSocketParameters(s); this.out=new DataOutputStream(createBufferedOutputStream(s.getOutputStream())); this.in=new DataInputStream(createBufferedInputStream(s.getInputStream())); this.connected=sock.isConnected(); this.peer_addr=server.usePeerConnections()? readPeerAddress(s) : new IpAddress((InetSocketAddress)s.getRemoteSocketAddress()); last_access=getTimestamp(); // last time a message was sent or received (ns) }
protected void handleAccept(final Socket client_sock) throws Exception { TcpConnection conn=null; try { conn=new TcpConnection(client_sock, TcpServer.this); Address peer_addr=conn.peerAddress(); synchronized(this) { boolean conn_exists=hasConnection(peer_addr), replace=conn_exists && use_peer_connections && local_addr.compareTo(peer_addr) < 0; // bigger conn wins if(!conn_exists || replace) { replaceConnection(peer_addr, conn); // closes old conn conn.start(); log.trace("%s: accepted connection from %s", local_addr, peer_addr); } else { log.trace("%s: rejected connection from %s %s", local_addr, peer_addr, explanation(conn_exists, replace)); Util.close(conn); // keep our existing conn, reject accept() and close client_sock } } } catch(Exception ex) { Util.close(conn); throw ex; } } }
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)); Util.connect(this.sock, destAddr, server.sock_conn_timeout); if(this.sock.getLocalSocketAddress() != null && this.sock.getLocalSocketAddress().equals(destAddr)) throw new IllegalStateException("socket's bind and connect address are the same: " + destAddr); this.out=new DataOutputStream(createBufferedOutputStream(sock.getOutputStream())); this.in=new DataInputStream(createBufferedInputStream(sock.getInputStream())); connected=sock.isConnected(); if(send_local_addr) sendLocalAddress(server.localAddress()); } catch(Exception t) { Util.close(this.sock); connected=false; throw t; } }
/** * * @param data Guaranteed to be non null * @param offset * @param length */ public void send(byte[] data, int offset, int length) throws Exception { if(out == null) return; writers.incrementAndGet(); send_lock.lock(); try { doSend(data, offset, length); updateLastAccessed(); } catch(InterruptedException iex) { Thread.currentThread().interrupt(); // set interrupt flag again } finally { if(writers.decrementAndGet() == 0) // only the last active writer thread calls flush() flush(); // won't throw an exception send_lock.unlock(); } }