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(); }
/** * Creates an instance of {@link TcpServer} that creates a server socket and listens for connections * Needs to be started next. * @param thread_factory The thread factory used to create new threads * @param socket_factory The socket factory used to create sockets * @param bind_addr The local address to bind to. If null, the address will be picked by the OS * @param srv_port The local port to bind to. If 0, the port will be picked by the OS. * @param end_port If srv_port is taken, the next port is tried, until end_port has been reached, in which case an * exception will be thrown. If srv_port == end_port, only 1 port will be tried. * @param external_addr The external address in case of NAT. Ignored if null. * @param external_port The external port on the NA. If 0, srv_port is used. * @throws Exception Thrown if the creation failed */ public TcpServer(ThreadFactory thread_factory, SocketFactory socket_factory, InetAddress bind_addr, int srv_port, int end_port, InetAddress external_addr, int external_port) throws Exception { this(thread_factory, socket_factory); // this.srv_sock=this.socket_factory.createServerSocket("jgroups.tcp.server"); // this.srv_sock.setReuseAddress(reuse_addr); // Util.bind(this.srv_sock, bind_addr, srv_port, end_port); this.srv_sock=Util.createServerSocket(this.socket_factory, "jgroups.tcp.server", bind_addr, srv_port, end_port); acceptor=factory.newThread(new Acceptor(),"TcpServer.Acceptor[" + srv_sock.getLocalPort() + "]"); local_addr=localAddress(bind_addr, srv_sock.getLocalPort(), external_addr, external_port); addConnectionListener(this); }
@ManagedOperation(description="Clears all connections (they will get re-established). For testing only, don't use !") public void clearConnections() { server.clearConnections(); }
/** * 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)); }
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 BufferedInputStream createBufferedInputStream(InputStream in) { int size=(server instanceof TcpServer)? ((TcpServer)server).getBufferedInputStreamSize() : 0; return size == 0? new BufferedInputStream(in) : new BufferedInputStream(in, size); }
@ManagedAttribute public int getOpenConnections() { return server.getNumConnections(); }
protected BufferedOutputStream createBufferedOutputStream(OutputStream out) { int size=(server instanceof TcpServer)? ((TcpServer)server).getBufferedOutputStreamSize() : 0; return size == 0? new BufferedOutputStream(out) : new BufferedOutputStream(out, size); }
protected PhysicalAddress getPhysicalAddress() { return server != null? (PhysicalAddress)server.localAddress() : null; } }
protected void start(InetAddress bind_addr, int port, boolean nio) throws Exception { server=nio? new NioServer(bind_addr, port) : new TcpServer(bind_addr, port); server.receiver(this); server.start(); JmxConfigurator.register(server, Util.getMBeanServer(), "pub:name=pub-server"); int local_port=server.localAddress() instanceof IpAddress? ((IpAddress)server.localAddress()).getPort(): 0; System.out.printf("\nPubServer listening at %s:%s\n", bind_addr != null? bind_addr : "0.0.0.0", local_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; } } }
protected BufferedInputStream createBufferedInputStream(InputStream in) { int size=(server instanceof TcpServer)? ((TcpServer)server).getBufferedInputStreamSize() : 0; return size == 0? new BufferedInputStream(in) : new BufferedInputStream(in, size); }
@ManagedAttribute public int getOpenConnections() { return server.getNumConnections(); }
protected BufferedOutputStream createBufferedOutputStream(OutputStream out) { int size=(server instanceof TcpServer)? ((TcpServer)server).getBufferedOutputStreamSize() : 0; return size == 0? new BufferedOutputStream(out) : new BufferedOutputStream(out, size); }
protected PhysicalAddress getPhysicalAddress() { return server != null? (PhysicalAddress)server.localAddress() : null; } }
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 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(); } }
/** * Creates an instance of {@link TcpServer} that creates a server socket and listens for connections * Needs to be started next. * @param thread_factory The thread factory used to create new threads * @param socket_factory The socket factory used to create sockets * @param bind_addr The local address to bind to. If null, the address will be picked by the OS * @param srv_port The local port to bind to. If 0, the port will be picked by the OS. * @param end_port If srv_port is taken, the next port is tried, until end_port has been reached, in which case an * exception will be thrown. If srv_port == end_port, only 1 port will be tried. * @param external_addr The external address in case of NAT. Ignored if null. * @param external_port The external port on the NA. If 0, srv_port is used. * @throws Exception Thrown if the creation failed */ public TcpServer(ThreadFactory thread_factory, SocketFactory socket_factory, InetAddress bind_addr, int srv_port, int end_port, InetAddress external_addr, int external_port) throws Exception { this(thread_factory, socket_factory); // this.srv_sock=this.socket_factory.createServerSocket("jgroups.tcp.server"); // this.srv_sock.setReuseAddress(reuse_addr); // Util.bind(this.srv_sock, bind_addr, srv_port, end_port); this.srv_sock=Util.createServerSocket(this.socket_factory, "jgroups.tcp.server", bind_addr, srv_port, end_port); acceptor=factory.newThread(new Acceptor(),"TcpServer.Acceptor[" + srv_sock.getLocalPort() + "]"); local_addr=localAddress(bind_addr, srv_sock.getLocalPort(), external_addr, external_port); addConnectionListener(this); }
@ManagedOperation(description="Clears all connections (they will get re-established). For testing only, don't use !") public void clearConnections() { server.clearConnections(); }
public static class TcpServerStarter extends Thread { @Override public void run() { new TcpServer(8500).run(); } } public static class UdpServerStarter extends Thread { @Override public void run() { new UdpServer(1000).run(); } }