private static InetAddress getInetAddress(Object obj) throws IllegalArgumentException { if (obj == null) throw new IllegalArgumentException("Input argument must represent a non-null IP address") ; if (obj instanceof InetAddress) return (InetAddress) obj ; else if (obj instanceof IpAddress) return ((IpAddress) obj).getIpAddress() ; else if (obj instanceof InetSocketAddress) return ((InetSocketAddress) obj).getAddress() ; else throw new IllegalArgumentException("Input argument does not represent one of InetAddress. IpAddress not InetSocketAddress") ; }
/** Checks whether 2 Addresses are on the same host */ public static boolean sameHost(Address one,Address two) { InetAddress a, b; String host_a, host_b; if(one == null || two == null) return false; if(!(one instanceof IpAddress) || !(two instanceof IpAddress)) { return false; } a=((IpAddress)one).getIpAddress(); b=((IpAddress)two).getIpAddress(); if(a == null || b == null) return false; host_a=a.getHostAddress(); host_b=b.getHostAddress(); // System.out.println("host_a=" + host_a + ", host_b=" + host_b); return host_a.equals(host_b); }
/** * 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); }
protected InetAddress getHostFor(Address mbr) { PhysicalAddress phys_addr=getPhysicalAddress(mbr); return phys_addr instanceof IpAddress? ((IpAddress)phys_addr).getIpAddress() : null; }
/** * 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 JGAddress(UUID uuid, IpAddress ipaddr) { super(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); ip_addr = ipaddr.getIpAddress(); port = ipaddr.getPort(); vmViewId = -1; }
void closeMulticastSocket() { if(mcast_sock != null) { try { if(mcast_addr != null) { mcast_sock.leaveGroup(mcast_addr.getIpAddress()); } getSocketFactory().close(mcast_sock); // this will cause the mcast receiver thread to break out of its loop mcast_sock=null; if(log.isDebugEnabled()) log.debug("multicast socket closed"); } catch(IOException ex) { } mcast_addr=null; } }
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; } }
protected void verifySuspectWithICMP(Address suspected_mbr) { InetAddress host=suspected_mbr instanceof IpAddress? ((IpAddress)suspected_mbr).getIpAddress() : null; if(host == null) throw new IllegalArgumentException("suspected_mbr is not of type IpAddress - FD_ICMP only works with these"); try { if(log.isTraceEnabled()) log.trace("pinging host " + suspected_mbr + " using interface " + intf); long start=getCurrentTimeMillis(), stop; boolean rc=host.isReachable(intf, 0, (int)timeout); stop=getCurrentTimeMillis(); if(rc) // success log.trace("successfully received response from " + host + " (after " + (stop-start) + "ms)"); else { // failure log.debug("failed pinging " + suspected_mbr + " after " + (stop-start) + "ms; passing up SUSPECT event"); removeSuspect(suspected_mbr); up_prot.up(new Event(Event.SUSPECT, Collections.singletonList(suspected_mbr))); } } catch(Exception ex) { log.error(Util.getMessage("FailedPinging"),suspected_mbr, ex); } }