protected void start(String host, int port, String cluster_name, boolean nio) { try { stub=new RouterStub(null, 0, InetAddress.getByName(host), port, nio, null); stub.connect(); stub.getMembers(cluster_name, this); promise.getResult(5000); } catch(Exception ex) { ex.printStackTrace(); stub.destroy(); } }
public void disconnectStubs() { stopReconnector(); for(RouterStub stub : stubs) { try { stub.disconnect(cluster_name, local_addr); } catch (Throwable e) { } } }
public void connectStubs() { for(RouterStub stub : stubs) { try { if(!stub.isConnected()) stub.connect(cluster_name, local_addr, logical_name, phys_addr); } catch (Throwable e) { moveStubToReconnects(stub); } } }
protected boolean reconnect(Target target) { RouterStub stub=new RouterStub(target.bind_addr, target.router_addr, this.use_nio, this).receiver(target.receiver); if(!add(stub)) return false; try { stub.connect(this.cluster_name, this.local_addr, this.logical_name, this.phys_addr); log.debug("re-established connection to %s successfully for group=%s and address=%s", stub.remote(), this.cluster_name, this.local_addr); return true; } catch(Throwable t) { remove(stub); return false; } }
/** * Fetches a list of {@link PingData} from the GossipRouter, one for each member in the given group. This call * returns immediately and when the results are available, the * {@link org.jgroups.stack.RouterStub.MembersNotification#members(List)} callback will be invoked. * @param group The group for which we need members information * @param callback The callback to be invoked. */ public void getMembers(final String group, MembersNotification callback) throws Exception { if(callback == null) return; // if(!isConnected()) throw new Exception ("not connected"); synchronized(get_members_map) { List<MembersNotification> set=get_members_map.get(group); if(set == null) get_members_map.put(group, set=new ArrayList<>()); set.add(callback); } try { writeRequest(new GossipData(GossipType.GET_MBRS, group, null)); } catch(Exception ex) { removeResponse(group, callback); throw new Exception(String.format("connection to %s broken. Could not send %s request: %s", gossipRouterAddress(), GossipType.GET_MBRS, ex)); } }
public RouterStub createAndRegisterStub(IpAddress local, IpAddress router_addr) { RouterStub stub=new RouterStub(local, router_addr, use_nio, this); RouterStub old_stub=unregisterStub(router_addr); if(old_stub != null) old_stub.destroy(); add(stub); return stub; }
public void sendToAllMembers(final String group, Address sender, final byte[] data, final int offset, final int length) throws Exception { stubManager.forAny( stub -> { try { if(log.isTraceEnabled()) log.trace("sent a message to all members, GR used %s", stub.gossipRouterAddress()); stub.sendToAllMembers(group, sender, data, offset, length); } catch (Exception ex) { log.warn("failed sending a message to all members, router used %s", stub.gossipRouterAddress()); } }); }
public void start() throws Exception { //loopback turned on is mandatory loopback = true; stub = new RouterStub(router_host,router_port,bind_addr); stub.setConnectionListener(new StubConnectionListener()); local_addr=stub.getLocalAddress(); if(additional_data != null && local_addr instanceof IpAddress) ((IpAddress)local_addr).setAdditionalData(additional_data); super.start(); }
/** * Applies action to a randomly picked RouterStub that's connected * @param action */ public void forAny(Consumer<RouterStub> action) { while(!stubs.isEmpty()) { RouterStub stub=Util.pickRandomElement(stubs); if(stub != null && stub.isConnected()) { action.accept(stub); return; } } }
throw new Exception("groupname is null"); if(!isConnected()){ this.groupname = groupname; try{ sock.setSoLinger(true, 500); output = new DataOutputStream(sock.getOutputStream()); GossipData req = new GossipData(GossipRouter.CONNECT, groupname, getLocalAddress(),null); req.writeTo(output); output.flush(); input = new DataInputStream(sock.getInputStream()); connectionStateChanged(STATUS_CONNECTED); }catch(Exception e){ if(log.isWarnEnabled()) Util.close(input); Util.close(output); connectionStateChanged(STATUS_CONNECTION_LOST); throw e;
public void sendToMember(String group, Address dest, Address sender, byte[] data, int offset, int length) throws Exception { try { writeRequest(new GossipData(GossipType.MESSAGE, group, dest, data, offset, length).setSender(sender)); } catch(Exception ex) { throw new Exception(String.format("connection to %s broken. Could not send message to %s: %s", gossipRouterAddress(), dest, ex)); } }
public void sendToSingleMember(final String group, final Address dest, Address sender, final byte[] data, final int offset, final int length) throws Exception { stubManager.forAny( stub -> { try { if(log.isTraceEnabled()) log.trace("sent a message to %s (router used %s)", dest, stub.gossipRouterAddress()); stub.sendToMember(group, dest, sender, data, offset, length); } catch (Exception ex) { log.warn("failed sending a message to %s (router used %s):", dest, stub.gossipRouterAddress(), ex); } }); }
@Override public void findMembers(List<Address> members, boolean initial_discovery, Responses responses) { if(this.cluster_name == null) { log.error(Util.getMessage("ClusternameIsNullCannotGetMembership")); return; } log.trace("fetching members from GossipRouter(s)"); stubManager.forEach( stub -> { try { stub.getMembers(TCPGOSSIP.this.cluster_name, TCPGOSSIP.this); } catch(Throwable t) { log.warn("failed fetching members from %s: %s, cause: %s", stub.gossipRouterAddress(), t, t.getCause()); } }); }
/** * Registers mbr with the GossipRouter under the given group, with the given logical name and physical address. * Establishes a connection to the GossipRouter and sends a CONNECT message. * @param group The group cluster) name under which to register the member * @param addr The address of the member * @param logical_name The logical name of the member * @param phys_addr The physical address of the member * @throws Exception Thrown when the registration failed */ public void connect(String group, Address addr, String logical_name, PhysicalAddress phys_addr) throws Exception { synchronized(this) { _doConnect(); } try { writeRequest(new GossipData(GossipType.REGISTER, group, addr, logical_name, phys_addr)); } catch(Exception ex) { throw new Exception(String.format("connection to %s failed: %s", group, ex)); } }
public synchronized void sendToSingleMember(Address dest, byte[] data, int offset, int length) throws Exception { if(isConnected()){ try{ // 1. Group name output.writeUTF(groupname); // 2. Destination address (null in case of mcast) Util.writeAddress(dest, output); // 3. Length of byte buffer output.writeInt(data.length); // 4. Byte buffer output.write(data, 0, data.length); output.flush(); }catch(SocketException se){ if(log.isWarnEnabled()) log.warn("Router stub " + this + " did not send message to " + (dest == null ? "mcast" : dest + " since underlying socket is closed")); connectionStateChanged(STATUS_CONNECTION_LOST); }catch(Exception e){ if(log.isErrorEnabled()) log.error("Router stub " + this + " failed sending message to router"); connectionStateChanged(STATUS_CONNECTION_LOST); throw new Exception("dest=" + dest + " (" + length + " bytes)", e); } } }
public void run() { while(stub.isConnected()){ Address dest = null; Address src = null; int len; byte[] data = null; DataInputStream input = null; try{ input = stub.getInputStream(); dest = Util.readAddress(input); len = input.readInt(); if(len > 0){ data = new byte[len]; input.readFully(data, 0, len); receive(dest, src, data, 0, len); } }catch(SocketException se){ //if(log.isWarnEnabled()) log.warn("failure in TUNNEL receiver thread", se); }catch(IOException ioe){ //if(log.isWarnEnabled()) log.warn("failure in TUNNEL receiver thread", ioe); }catch(Exception e){ if(log.isWarnEnabled()) log.warn("failure in TUNNEL receiver thread", e); } } } }
public void run() { try{ stub.connect(channel_name); }catch(Exception ex){ if(log.isTraceEnabled()) log.trace("failed reconnecting", ex); } } };
public void sendToAllMembers(byte[] data, int offset, int length) throws Exception { stub.sendToAllMembers(data, offset, length); }
protected boolean remove(RouterStub stub) { if(stub == null) return false; stub.destroy(); List<RouterStub> new_stubs=new ArrayList<>(stubs); boolean retval=new_stubs.remove(stub); this.stubs=new_stubs; return retval; }
protected RouterStub find(IpAddress router_addr) { for(RouterStub stub: stubs) { IpAddress addr=stub.gossipRouterAddress(); if(Objects.equals(addr, router_addr)) return stub; } return null; }