Refine search
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; } }
public void init(String local_addr, String remote_addr, int local_port, int remote_port) throws Exception { local=new InetSocketAddress(local_addr, local_port); remote=new InetSocketAddress(remote_addr, remote_port); destination=new IpAddress(remote.getAddress(), remote.getPort()); srv_sock=Util.createServerSocket(new DefaultSocketFactory(), "server", local.getAddress(), local.getPort()); System.out.println("Listening on " + srv_sock.getLocalSocketAddress()); acceptor=new Acceptor(); acceptor.start(); sock=new Socket(); //sock.bind(local); sock.setSendBufferSize(SOCK_SEND_BUF_SIZE); sock.setReceiveBufferSize(SOCK_RECV_BUF_SIZE); try { sock.connect(remote); output=new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); System.out.println("Connected to " + sock.getRemoteSocketAddress()); } catch(Throwable t) { System.out.println("Failed connecting to " + remote + ": will only act as server"); } }
@Override public int compare(Target o1, Target o2) { return o1.router_addr.compareTo(o2.router_addr); }
/** * 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); }
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()); }
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); }
if(entry.getIpAddress() == null || entry.getPort() == 0) { if(log.isErrorEnabled()) log.error("entry.host or entry.port is null"); continue; sock=new Socket(); destAddr=new InetSocketAddress(entry.getIpAddress(), entry.getPort()); sock.connect(destAddr, SOCKET_TIMEOUT); out=new DataOutputStream(sock.getOutputStream()); Util.close(out); Util.close(in); if(sock != null) { try {sock.close();} catch(IOException e) {}
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); } }
@ManagedOperation public boolean removeInitialHost(String hostname, int port) { InetSocketAddress isa = new InetSocketAddress(hostname, port); stubManager.unregisterStub(new IpAddress(isa.getAddress(), isa.getPort())); return initial_hosts.remove(isa); }
/** * 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(); } }
@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 TcpConnection(Socket s, TcpServer server) throws Exception { this.sock=s; this.server=server; if(s == null) throw new IllegalArgumentException("Invalid parameter s=" + s); setSocketParameters(s); this.out=new DataOutputStream(createBufferedOutputStream(s.getOutputStream())); this.in=new DataInputStream(createBufferedInputStream(s.getInputStream())); this.connected=sock.isConnected(); this.peer_addr=server.usePeerConnections()? readPeerAddress(s) : new IpAddress((InetSocketAddress)s.getRemoteSocketAddress()); last_access=getTimestamp(); // last time a message was sent or received (ns) }
public Connection(Socket sock) throws Exception { this.sock=sock; peer_addr=new IpAddress((InetSocketAddress)sock.getRemoteSocketAddress()); in=new DataInputStream(createBufferedInputStream(sock.getInputStream())); out=new DataOutputStream(createBufferedOutputStream(sock.getOutputStream())); runner=new Runner(new DefaultThreadFactory("tcp", false, true), "conn-" + sock.getLocalPort(), this, null); }
@Override public ClusterNode getClusterNode(Address a) { IpAddress result = addressMap.get(a); if (result == null) { result = (IpAddress) channel.down(new Event(Event.GET_PHYSICAL_ADDRESS, a)); if (result == null) { throw MESSAGES.addressNotRegistered(a); } addressMap.put(a, result); } InetSocketAddress socketAddress = new InetSocketAddress(result.getIpAddress(), result.getPort()); String id = channel.getName(a); if (id == null) { id = socketAddress.getAddress().getHostAddress() + ":" + socketAddress.getPort(); } return new ClusterNodeImpl(id, a, socketAddress); } }
if(entry.getIpAddress() == null || entry.getPort() == 0) { if(log.isErrorEnabled()) log.error("entry.host or entry.port is null"); continue; log.trace("UNREGISTER(" + group + ", " + mbr + ") with GossipRouter at " + entry.getIpAddress() + ':' + entry.getPort()); sock=new Socket(entry.getIpAddress(), entry.getPort()); out=new DataOutputStream(sock.getOutputStream()); gossip_req=new GossipData(GossipRouter.UNREGISTER, group, mbr, null); Util.close(out); if(sock != null) { try {sock.close();} catch(IOException e) {}
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; }
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; } }
/** * Creates a socket to <code>dest</code>, and assigns it to ping_sock. Also assigns ping_input */ boolean setupPingSocket(IpAddress dest) { synchronized(sock_mutex) { if(dest == null) { if(log.isErrorEnabled()) log.error("destination address is null"); return false; } try { ping_sock=new Socket(dest.getIpAddress(), dest.getPort()); ping_sock.setSoLinger(true, 1); ping_sock.setKeepAlive(keep_alive); ping_input=ping_sock.getInputStream(); return true; } catch(Throwable ex) { return false; } } }
public void handleConnect() { if (cluster_name == null || local_addr == null) log.error(Util.getMessage("GroupaddrOrLocaladdrIsNullCannotRegisterWithGossipRouterS")); else { InetAddress bind_addr=getTransport().getBindAddress(); log.trace("registering " + local_addr + " under " + cluster_name + " with GossipRouter"); stubManager.destroyStubs(); PhysicalAddress physical_addr = (PhysicalAddress) down_prot.down(new Event(Event.GET_PHYSICAL_ADDRESS, local_addr)); stubManager = new RouterStubManager(this, cluster_name, local_addr, NameCache.get(local_addr), physical_addr, reconnect_interval).useNio(this.use_nio); for (InetSocketAddress host : initial_hosts) { RouterStub stub=stubManager.createAndRegisterStub(new IpAddress(bind_addr, 0), new IpAddress(host.getAddress(), host.getPort())); stub.socketConnectionTimeout(sock_conn_timeout); } stubManager.connectStubs(); } }
public Object handleDownEvent(Event evt) { Object retEvent = super.handleDownEvent(evt); switch (evt.getType()) { case Event.CONNECT: case Event.CONNECT_WITH_STATE_TRANSFER: case Event.CONNECT_USE_FLUSH: case Event.CONNECT_WITH_STATE_TRANSFER_USE_FLUSH: String group=evt.getArg(); Address local=local_addr; if(stubManager != null) stubManager.destroyStubs(); PhysicalAddress physical_addr=getPhysicalAddressFromCache(local); String logical_name=org.jgroups.util.NameCache.get(local); stubManager = new RouterStubManager(this,group,local, logical_name, physical_addr, getReconnectInterval()).useNio(this.use_nio); for (InetSocketAddress gr : gossip_router_hosts) { stubManager.createAndRegisterStub(new IpAddress(bind_addr, bind_port), new IpAddress(gr.getAddress(), gr.getPort())) .receiver(this).set("tcp_nodelay", tcp_nodelay); } stubManager.connectStubs(); break; case Event.DISCONNECT: disconnectStub(); break; } return retEvent; }