/** * Creates an instance of NioServer. * @param bind_addr The local bind address and port. If null, a bind address and port will be picked by the OS. */ public NioServer(IpAddress bind_addr) throws Exception { this(bind_addr != null? bind_addr.getIpAddress() : null, bind_addr != null? bind_addr.getPort() : 0); }
public static boolean match(IpAddress sender, InetSocketAddress addr) { return !(sender == null || addr == null) && addr.getAddress().equals(sender.getIpAddress()) && (addr.getPort() == 0 || addr.getPort() == sender.getPort()); }
/** * Creates an instance of TcpServer. * @param bind_addr The local bind address and port. If null, a bind address and port will be picked by the OS. */ public TcpServer(IpAddress bind_addr) throws Exception { this(bind_addr != null? bind_addr.getIpAddress() : null, bind_addr != null? bind_addr.getPort() : 0); }
/** * 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 server_addr The address of the server to connect to * @throws Exception If the creation failed */ public NioClient(IpAddress bind_addr, IpAddress server_addr) { this(bind_addr != null? bind_addr.getIpAddress() : null, bind_addr != null? bind_addr.getPort() : 0, server_addr != null? server_addr.getIpAddress() : null, server_addr != null? server_addr.getPort() : 0); }
/** * 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 server_addr The address of the server to connect to * @throws Exception If the creation failed */ public TcpClient(IpAddress bind_addr, IpAddress server_addr) { this(bind_addr != null? bind_addr.getIpAddress() : null, bind_addr != null? bind_addr.getPort() : 0, server_addr != null? server_addr.getIpAddress() : null, server_addr != null? server_addr.getPort() : 0); }
public String toString(Object value) { if(value instanceof Collection) { StringBuilder sb=new StringBuilder(); Collection<IpAddress> list=(Collection<IpAddress>)value; boolean first=true; for(IpAddress addr: list) { if(first) first=false; else sb.append(","); sb.append(addr.getIpAddress().getHostAddress()).append("[").append(addr.getPort()).append("]"); } return sb.toString(); } else return value.getClass().getName(); }
protected boolean addPhysicalAddressToCache(Address logical_addr, PhysicalAddress physical_addr) { IpAddress tmp=(IpAddress)physical_addr; addr_table.put(logical_addr, new InetSocketAddress(tmp.getIpAddress(), tmp.getPort())); return super.addPhysicalAddressToCache(logical_addr, physical_addr); }
protected void sendTo(Address dest, byte[] buffer, int offset, int length) throws Exception { SocketAddress physical_dest=null; if(dest instanceof IpAddress) { IpAddress ip_addr=(IpAddress)dest; physical_dest=new InetSocketAddress(ip_addr.getIpAddress(), ip_addr.getPort()); } else physical_dest=addr_table.get(dest); if(physical_dest == null) throw new Exception(String.format("physical address for %s not found", dest)); Connection conn=getConnection(physical_dest); conn.send(buffer, offset, length); }
@Override public Node createNode(Address address) { return this.members.computeIfAbsent(address, key -> { IpAddress ipAddress = (IpAddress) this.dispatcher.getChannel().down(new Event(Event.GET_PHYSICAL_ADDRESS, address)); // Physical address might be null if node is no longer a member of the cluster InetSocketAddress socketAddress = (ipAddress != null) ? new InetSocketAddress(ipAddress.getIpAddress(), ipAddress.getPort()) : new InetSocketAddress(0); // If no logical name exists, create one using physical address String name = Optional.ofNullable(NameCache.get(address)).orElseGet(() -> String.format("%s:%s", socketAddress.getHostString(), socketAddress.getPort())); return new AddressableNode(address, name, socketAddress); }); }
public void sendUnicast(PhysicalAddress dest, byte[] data, int offset, int length) throws Exception { _send(((IpAddress)dest).getIpAddress(), ((IpAddress)dest).getPort(), data, offset, length); }
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; } }
protected Tuple<InputStream,Object> createStreamToProvider(Address provider, StateHeader hdr) throws Exception { IpAddress address=hdr.bind_addr; Socket socket=null; try { socket=getSocketFactory().createSocket("jgroups.state_sock.sock"); socket.bind(new InetSocketAddress(bind_addr, 0)); socket.setReceiveBufferSize(buffer_size); Util.connect(socket, new InetSocketAddress(address.getIpAddress(), address.getPort()), 0); log.debug("%s: connected to state provider %s:%d", local_addr, address.getIpAddress(), address.getPort()); DataOutputStream out=new DataOutputStream(socket.getOutputStream()); Util.writeAddress(local_addr, out); return new Tuple<>(new BufferedInputStream(socket.getInputStream(), buffer_size), socket); } catch(Throwable t) { Util.close(socket); if(t instanceof Exception) throw (Exception)t; throw new Exception("failed creating socket", t); } }
public void sendMulticast(byte[] data, int offset, int length) throws Exception { if(ip_mcast && mcast_addr != null) _send(mcast_addr.getIpAddress(), mcast_addr.getPort(), data, offset, length); else sendToMembers(members, data, offset, length); }
public Object down(Event evt) { Object retval=super.down(evt); switch(evt.type()) { case Event.ADD_PHYSICAL_ADDRESS: Tuple<Address,PhysicalAddress> tuple=evt.arg(); IpAddress val=(IpAddress)tuple.getVal2(); addr_table.put(tuple.getVal1(), new InetSocketAddress(val.getIpAddress(), val.getPort())); break; case Event.VIEW_CHANGE: for(Iterator<Map.Entry<Address,SocketAddress>> it=addr_table.entrySet().iterator(); it.hasNext();) { Map.Entry<Address,SocketAddress> entry=it.next(); if(!view.containsMember(entry.getKey())) { SocketAddress sock_addr=entry.getValue(); it.remove(); Connection conn=connections.remove(sock_addr); Util.close(conn); } } break; } return retval; }
public JGAddress(UUID uuid, IpAddress ipaddr) { super(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); ip_addr = ipaddr.getIpAddress(); port = ipaddr.getPort(); vmViewId = -1; }
/** * Creates a socket to {@code dest}, and assigns it to ping_sock. Also assigns ping_input */ protected boolean setupPingSocket(IpAddress dest) { lock.lock(); try { SocketAddress destAddr=new InetSocketAddress(dest.getIpAddress(), dest.getPort()); ping_sock=getSocketFactory().createSocket("jgroups.fd.ping_sock"); Util.bind(ping_sock, bind_addr, client_bind_port, client_bind_port+port_range); ping_sock.setSoLinger(true, 1); ping_sock.setKeepAlive(keep_alive); Util.connect(ping_sock, destAddr, sock_conn_timeout); ping_input=ping_sock.getInputStream(); return true; } catch(Throwable ex) { if(!shuttin_down) log.debug("%s: failed connecting to %s: %s", local_addr, ping_dest != null? ping_dest : dest, ex.getMessage()); return false; } finally { lock.unlock(); } }
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 connect(Address dest, boolean send_local_addr) throws Exception { SocketAddress destAddr=new InetSocketAddress(((IpAddress)dest).getIpAddress(), ((IpAddress)dest).getPort()); try { if(!server.deferClientBinding()) this.channel.bind(new InetSocketAddress(server.clientBindAddress(), server.clientBindPort())); this.key=server.register(channel, SelectionKey.OP_CONNECT | SelectionKey.OP_READ, this); if(Util.connect(channel, destAddr) && channel.finishConnect()) { clearSelectionKey(SelectionKey.OP_CONNECT); this.connected=channel.isConnected(); } if(this.channel.getLocalAddress() != null && this.channel.getLocalAddress().equals(destAddr)) throw new IllegalStateException("socket's bind and connect address are the same: " + destAddr); if(send_local_addr) sendLocalAddress(server.localAddress()); } catch(Exception t) { close(); throw t; } }
if(address instanceof IpAddress) { IpAddress ip = ((IpAddress) address); if(record_type == DNSResolver.DNSRecordType.SRV && ip.getPort() > 0) { ports_found=true; cluster_members.add(ip);
/** * Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads */ public void start() throws Exception { if(use_ip_addrs) { PhysicalAddress tmp=getPhysicalAddress(); if(tmp instanceof IpAddress) { local_addr=new IpAddressUUID(((IpAddress)tmp).getIpAddress(), ((IpAddress)tmp).getPort()); stack.getTopProtocol().down(new Event(Event.SET_LOCAL_ADDRESS, local_addr)); stack.getTopProtocol().up(new Event(Event.SET_LOCAL_ADDRESS, local_addr)); } } fetchLocalAddresses(); if(timer == null) throw new Exception("timer is null"); startDiagnostics(); if(bundler == null) { bundler=createBundler(bundler_type); bundler.init(this); bundler.start(); } // local_addr is null when shared transport setInAllThreadFactories(cluster_name != null? cluster_name.toString() : null, local_addr, thread_naming_pattern); }