/** * 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; } } }
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); } } }
public String toString() { return "RouterStub[local_address=" + local_addr + ",router_host=" + router_host + ",router_port=" + router_port + ",connected=" + isConnected() + "]"; }
public DataInputStream getInputStream() throws IOException { if(!isConnected()){ throw new IOException("InputStream is closed"); } return input; }
/** * 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; } } }
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); } } }
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); } } } }
throw new Exception("groupname is null"); if(!isConnected()){ this.groupname = groupname; try{
public synchronized void disconnect() { if(isConnected()){ try{ GossipData req = new GossipData(GossipRouter.DISCONNECT, groupname, local_addr,null); req.writeTo(output); output.flush(); }catch(Exception e){ }finally{ Util.close(output); Util.close(input); Util.close(sock); Util.close(my_sock); sock = null; connectionStateChanged(STATUS_DISCONNECTED); } } }