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); }
public void start() throws Exception { server=new NioServer(getThreadFactory(), getSocketFactory(), bind_addr, bind_port, bind_port+port_range, external_addr, external_port); server.receiver(this) .timeService(time_service) .receiveBufferSize(recv_buf_size) .sendBufferSize(send_buf_size) .socketConnectionTimeout(sock_conn_timeout) .tcpNodelay(tcp_nodelay).linger(linger) .clientBindAddress(client_bind_addr).clientBindPort(client_bind_port).deferClientBinding(defer_client_bind_addr) .log(this.log); server.maxSendBuffers(max_send_buffers).usePeerConnections(true); server.copyOnPartialWrite(this.copy_on_partial_write).readerIdleTime(this.reader_idle_time); if(reaper_interval > 0 || conn_expire_time > 0) { if(reaper_interval == 0) { reaper_interval=5000; log.warn("reaper_interval was 0, set it to %d", reaper_interval); } if(conn_expire_time == 0) { conn_expire_time=(long) 1000 * 60 * 5; log.warn("conn_expire_time was 0, set it to %d", conn_expire_time); } server.connExpireTimeout(conn_expire_time).reaperInterval(reaper_interval); } super.start(); }
public void start(String ... options) throws Exception { options(options); if(server) { srv=nio? new NioServer(host, port) : new TcpServer(host, port); srv.connExpireTimeout(0); srv.tcpNodelay(false); srv.receiver(this); srv.start(); System.out.printf("server started on %s (ctrl-c to terminate)\n", srv.localAddress()); } else { srv=nio? new NioClient(null, 0, host, port) : new TcpClient(null, 0, host, port); srv.tcpNodelay(false); srv.receiver(this); srv.start(); } }
public void send(Address dest, ByteBuffer data) throws Exception { if(!validateArgs(dest, data)) return; if(dest == null) { sendToAll(data); return; } if(dest.equals(local_addr)) { receive(dest, data); return; } // Get a connection (or create one if not yet existent) and send the data Connection conn=null; try { conn=getConnection(dest); conn.send(data); } catch(Exception ex) { removeConnectionIfPresent(dest, conn); throw ex; } }
protected void start(InetAddress srv_addr, int srv_port, boolean nio) throws Exception { client=nio? new NioClient(InetAddress.getLocalHost(), 0, srv_addr, srv_port) : new TcpClient(InetAddress.getLocalHost(), 0, srv_addr, srv_port); client.receiver(this); client.addConnectionListener(this); client.start(); send(String.format("%s joined", name)); eventLoop(); client.stop(); }
conn=createConnection(dest); replaceConnection(dest, conn); log.trace("%s: connecting to %s", local_addr, dest); conn.connect(dest); notifyConnectionEstablished(conn); conn.start(); removeConnectionIfPresent(dest, conn); // removes and closes the conn throw connect_exception;
public synchronized void addConnection(Address peer_addr, Connection conn) throws Exception { boolean conn_exists=hasConnection(peer_addr), replace=conn_exists && local_addr.compareTo(peer_addr) < 0; // bigger conn wins if(!conn_exists || replace) { replaceConnection(peer_addr, conn); // closes old conn conn.start(); } 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 } }
/** * Lifecycle operation. Called after create(). When this method is called, the managed attributes * have already been set.<br> * Brings the Router into a fully functional state. */ @ManagedOperation(description="Lifecycle operation. Called after create(). When this method is called, " + "the managed attributes have already been set. Brings the Router into a fully functional state.") public void start() throws Exception { if(!running.compareAndSet(false, true)) return; if(jmx) JmxConfigurator.register(this, Util.getMBeanServer(), "jgroups:name=GossipRouter"); InetAddress tmp=bind_addr != null? InetAddress.getByName(bind_addr) : null; server=use_nio? new NioServer(thread_factory, socket_factory, tmp, port, port, null, 0) : new TcpServer(thread_factory, socket_factory, tmp, port, port, null, 0); server.receiver(this); server.start(); server.addConnectionListener(this); Runtime.getRuntime().addShutdownHook(new Thread(GossipRouter.this::stop)); }
/** * Creates an instance of an {@link NioClient} that acts as a <em>client</em>: no server channel is created and * no acceptor is started to listen for incoming connections. Instead, a channel is created * (bound to bind_addr/bind_port) and connected to server_addr/server_port. This is used to send messages to the * remote server and receive messages from it. Note that there is only a single TCP connection established between * the client and server. * @param bind_addr The address to which the local channel should bind to. Can be null, then the OS picks the address * @param bind_port The local port. Can be 0, then the OS picks the port. * @param server_addr The address of the server to connect to * @param server_port The port of the server to connect to. * @throws Exception If the creation failed */ public NioClient(InetAddress bind_addr, int bind_port, InetAddress server_addr, int server_port) { this(new DefaultThreadFactory("nio", false), new DefaultSocketFactory()); clientBindAddress(bind_addr).clientBindPort(bind_port); this.remote_addr=new IpAddress(server_addr, server_port); }
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; } } }
public void send(Address dest, byte[] data, int offset, int length) throws Exception { if(!validateArgs(dest, data)) return; if(dest == null) { sendToAll(data, offset, length); return; } if(dest.equals(local_addr)) { receive(dest, data, offset, length); return; } // Get a connection (or create one if not yet existent) and send the data Connection conn=null; try { conn=getConnection(dest); conn.send(data, offset, length); } catch(Exception ex) { removeConnectionIfPresent(dest, conn); throw ex; } }
protected void start(InetAddress srv_addr, int srv_port, boolean nio) throws Exception { client=nio? new NioClient(InetAddress.getLocalHost(), 0, srv_addr, srv_port) : new TcpClient(InetAddress.getLocalHost(), 0, srv_addr, srv_port); client.receiver(this); client.addConnectionListener(this); client.start(); send(String.format("%s joined", name)); eventLoop(); client.stop(); }
conn=createConnection(dest); replaceConnection(dest, conn); log.trace("%s: connecting to %s", local_addr, dest); conn.connect(dest); notifyConnectionEstablished(conn); conn.start(); removeConnectionIfPresent(dest, conn); // removes and closes the conn throw connect_exception;
public synchronized void addConnection(Address peer_addr, Connection conn) throws Exception { boolean conn_exists=hasConnection(peer_addr), replace=conn_exists && local_addr.compareTo(peer_addr) < 0; // bigger conn wins if(!conn_exists || replace) { replaceConnection(peer_addr, conn); // closes old conn conn.start(); } 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 } }
/** * Lifecycle operation. Called after create(). When this method is called, the managed attributes * have already been set.<br> * Brings the Router into a fully functional state. */ @ManagedOperation(description="Lifecycle operation. Called after create(). When this method is called, " + "the managed attributes have already been set. Brings the Router into a fully functional state.") public void start() throws Exception { if(!running.compareAndSet(false, true)) return; if(jmx) JmxConfigurator.register(this, Util.getMBeanServer(), "jgroups:name=GossipRouter"); InetAddress tmp=bind_addr != null? InetAddress.getByName(bind_addr) : null; server=use_nio? new NioServer(thread_factory, socket_factory, tmp, port, port, null, 0) : new TcpServer(thread_factory, socket_factory, tmp, port, port, null, 0); server.receiver(this); server.start(); server.addConnectionListener(this); Runtime.getRuntime().addShutdownHook(new Thread(GossipRouter.this::stop)); }
/** * Creates an instance of an {@link TcpClient} that acts as a <em>client</em>: no server socket is created and * no acceptor is started to listen for incoming connections. Instead, a client socket is created * (bound to bind_addr/bind_port) and connected to server_addr/server_port. This is used to send messages to the * remote server and receive messages from it. Note that there is only a single TCP connection established between * the client and server. * @param bind_addr The address to which the local socket should bind to. Can be null, then the OS picks the address * @param bind_port The local port. Can be 0, then the OS picks the port. * @param server_addr The address of the server to connect to * @param server_port The port of the server to connect to. * @throws Exception If the creation failed */ public TcpClient(InetAddress bind_addr, int bind_port, InetAddress server_addr, int server_port) { this(new DefaultThreadFactory("tcp", false), new DefaultSocketFactory()); clientBindAddress(bind_addr).clientBindPort(bind_port); this.remote_addr=new IpAddress(server_addr, server_port); }
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; } } }
public void start() throws Exception { server=new TcpServer(getThreadFactory(), getSocketFactory(), bind_addr, bind_port, bind_port+port_range, external_addr, external_port); server.receiver(this) .timeService(time_service) .receiveBufferSize(recv_buf_size) .sendBufferSize(send_buf_size) .socketConnectionTimeout(sock_conn_timeout) .tcpNodelay(tcp_nodelay).linger(linger) .clientBindAddress(client_bind_addr).clientBindPort(client_bind_port).deferClientBinding(defer_client_bind_addr) .log(this.log); server.setBufferedInputStreamSize(buffered_input_stream_size).setBufferedOutputStreamSize(buffered_output_stream_size) .peerAddressReadTimeout(peer_addr_read_timeout) .usePeerConnections(true) .socketFactory(getSocketFactory()); if(reaper_interval > 0 || conn_expire_time > 0) { if(reaper_interval == 0) { reaper_interval=5000; log.warn("reaper_interval was 0, set it to %d", reaper_interval); } if(conn_expire_time == 0) { conn_expire_time=(long) 1000 * 60 * 5; log.warn("conn_expire_time was 0, set it to %d", conn_expire_time); } server.connExpireTimeout(conn_expire_time).reaperInterval(reaper_interval); } // we first start threads in TP (http://jira.jboss.com/jira/browse/JGRP-626) super.start(); }
public void send(Address dest, byte[] data, int offset, int length) throws Exception { if(!validateArgs(dest, data)) return; if(dest == null) { sendToAll(data, offset, length); return; } if(dest.equals(local_addr)) { receive(dest, data, offset, length); return; } // Get a connection (or create one if not yet existent) and send the data Connection conn=null; try { conn=getConnection(dest); conn.send(data, offset, length); } catch(Exception ex) { removeConnectionIfPresent(dest, conn); throw ex; } }
/** * Creates a stub to a remote GossipRouter * @param bind_addr The local address to bind to. If null, one will be picked * @param bind_port The local port. If 0, a random port will be used * @param router_host The address of the remote {@link GossipRouter} * @param router_port The port on which the remote GossipRouter is listening * @param use_nio Whether to use blocking or non-blocking IO * @param l The {@link org.jgroups.stack.RouterStub.CloseListener} */ public RouterStub(InetAddress bind_addr, int bind_port, InetAddress router_host, int router_port, boolean use_nio, CloseListener l) { local=new IpAddress(bind_addr, bind_port); this.remote=new IpAddress(router_host, router_port); this.use_nio=use_nio; this.close_listener=l; client=use_nio? new NioClient(bind_addr, bind_port, router_host, router_port) : new TcpClient(bind_addr, bind_port, router_host, router_port); client.addConnectionListener(this); client.receiver(this); client.socketConnectionTimeout(sock_conn_timeout).tcpNodelay(tcp_nodelay); }