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(); }
@Override public void start() throws Exception { if(running.compareAndSet(false, true)) { super.start(); selector=Selector.open(); conn=createConnection(remote_addr); conn.connect(remote_addr, false); local_addr=conn.localAddress(); if(use_peer_connections) conn.sendLocalAddress(local_addr); conn.start(); acceptor=factory.newThread(new Acceptor(), "NioClient.Acceptor [srv=" + remote_addr + "]"); acceptor.setDaemon(true); acceptor.start(); } }
public String toString() { return conn == null? String.format("%s -> %s [not connected]", localAddress(), remoteAddress()) : String.format("%s", conn); }
@Override public void send(Address dest, ByteBuffer data) throws Exception { send(data); }
/** * 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); }
@Override public void send(Address dest, byte[] data, int offset, int length) throws Exception { send(data, offset, length); }
/** * 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); }
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 String toString() { return conn == null? String.format("%s -> %s [not connected]", localAddress(), remoteAddress()) : String.format("%s", conn); }
@Override public void send(Address dest, byte[] data, int offset, int length) throws Exception { send(data, offset, length); }
@Override public void start() throws Exception { if(running.compareAndSet(false, true)) { super.start(); selector=Selector.open(); conn=createConnection(remote_addr); conn.connect(remote_addr, false); local_addr=conn.localAddress(); if(use_peer_connections) conn.sendLocalAddress(local_addr); conn.start(); acceptor=factory.newThread(new Acceptor(), "NioClient.Acceptor [srv=" + remote_addr + "]"); acceptor.setDaemon(true); acceptor.start(); } }
/** * 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); }
@Override public void send(Address dest, ByteBuffer data) throws Exception { send(data); }
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(); } }
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(); }
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); }
/** * 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); }
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(); } }