public void disconnect(String group, Address addr) throws Exception { writeRequest(new GossipData(GossipType.UNREGISTER, group, addr)); }
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)); } }
/** * 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)); } }
/** * 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)); } }
protected void removeFromAddressMappings(Address client_addr) { if(client_addr == null) return; Set<Tuple<String,Address>> suspects=null; // group/address pairs for(Map.Entry<String,ConcurrentMap<Address,Entry>> entry: address_mappings.entrySet()) { ConcurrentMap<Address,Entry> map=entry.getValue(); for(Map.Entry<Address,Entry> entry2: map.entrySet()) { Entry e=entry2.getValue(); if(client_addr.equals(e.client_addr)) { map.remove(entry2.getKey()); log.debug("connection to %s closed", client_addr); if(map.isEmpty()) address_mappings.remove(entry.getKey()); if(suspects == null) suspects=new HashSet<>(); suspects.add(new Tuple<>(entry.getKey(), entry2.getKey())); break; } } } if(emit_suspect_events && suspects != null && !suspects.isEmpty()) { for(Tuple<String,Address> suspect: suspects) { String group=suspect.getVal1(); Address addr=suspect.getVal2(); ConcurrentMap<Address,Entry> map=address_mappings.get(group); if(map == null) continue; GossipData data=new GossipData(GossipType.SUSPECT, group, addr); sendToAllMembersInGroup(map.entrySet(), data); } } }
protected GossipData readRequest(DataInput in) { GossipData data=new GossipData(); try { data.readFrom(in); return data; } catch(Exception ex) { log.error(Util.getMessage("FailedReadingRequest"), ex); return null; } }
protected GossipData readRequest(DataInput in, GossipType type) { GossipData data=new GossipData(type); try { data.readFrom(in, false); return data; } catch(Exception ex) { log.error(Util.getMessage("FailedReadingRequest"), ex); return null; } }
protected void handleGetMembersRequest(Address sender, DataInput in) { GossipData req=readRequest(in, GossipType.GET_MBRS); if(req == null) return; GossipData rsp=new GossipData(GossipType.GET_MBRS_RSP, req.getGroup(), null); Map<Address,Entry> members=address_mappings.get(req.getGroup()); if(members != null) { for(Map.Entry<Address,Entry> entry : members.entrySet()) { Address logical_addr=entry.getKey(); PhysicalAddress phys_addr=entry.getValue().phys_addr; String logical_name=entry.getValue().logical_name; PingData data=new PingData(logical_addr, true, logical_name, phys_addr); rsp.addPingData(data); } } ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(rsp.serializedSize()); try { rsp.writeTo(out); server.send(sender, out.buffer(), 0, out.position()); } catch(Exception ex) { log.error("failed sending %d to %s: %s", GossipType.GET_MBRS_RSP, sender, ex); } }
public void receive(Address sender, DataInput in) throws Exception { GossipData data=new GossipData(); data.readFrom(in); switch(data.getType()) { case MESSAGE: case SUSPECT: if(receiver != null) receiver.receive(data); break; case GET_MBRS_RSP: notifyResponse(data.getGroup(), data.getPingData()); break; } }
@Override public void receive(Address sender, byte[] buf, int offset, int length) { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); GossipData data=new GossipData(); try { data.readFrom(in); switch(data.getType()) { case MESSAGE: case SUSPECT: if(receiver != null) receiver.receive(data); break; case GET_MBRS_RSP: notifyResponse(data.getGroup(), data.getPingData()); break; } } catch(Exception ex) { log.error(Util.getMessage("FailedReadingData"), ex); } }
GossipData data=new GossipData(); data.readFrom(input); dump(data);
public void disconnect(String group, Address addr) throws Exception { writeRequest(new GossipData(GossipType.UNREGISTER, group, addr)); }
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)); } }
/** * 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)); } }
protected GossipData readRequest(DataInput in) { GossipData data=new GossipData(); try { data.readFrom(in); return data; } catch(Exception ex) { log.error(Util.getMessage("FailedReadingRequest"), ex); return null; } }
protected GossipData readRequest(DataInput in, GossipType type) { GossipData data=new GossipData(type); try { data.readFrom(in, false); return data; } catch(Exception ex) { log.error(Util.getMessage("FailedReadingRequest"), ex); return null; } }
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); } } }
protected void handleGetMembersRequest(Address sender, DataInput in) { GossipData req=readRequest(in, GossipType.GET_MBRS); if(req == null) return; GossipData rsp=new GossipData(GossipType.GET_MBRS_RSP, req.getGroup(), null); Map<Address,Entry> members=address_mappings.get(req.getGroup()); if(members != null) { for(Map.Entry<Address,Entry> entry : members.entrySet()) { Address logical_addr=entry.getKey(); PhysicalAddress phys_addr=entry.getValue().phys_addr; String logical_name=entry.getValue().logical_name; PingData data=new PingData(logical_addr, true, logical_name, phys_addr); rsp.addPingData(data); } } ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(rsp.serializedSize()); try { rsp.writeTo(out); server.send(sender, out.buffer(), 0, out.position()); } catch(Exception ex) { log.error("failed sending %d to %s: %s", GossipType.GET_MBRS_RSP, sender, ex); } }
public void receive(Address sender, DataInput in) throws Exception { GossipData data=new GossipData(); data.readFrom(in); switch(data.getType()) { case MESSAGE: case SUSPECT: if(receiver != null) receiver.receive(data); break; case GET_MBRS_RSP: notifyResponse(data.getGroup(), data.getPingData()); break; } }
@Override public void receive(Address sender, byte[] buf, int offset, int length) { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); GossipData data=new GossipData(); try { data.readFrom(in); switch(data.getType()) { case MESSAGE: case SUSPECT: if(receiver != null) receiver.receive(data); break; case GET_MBRS_RSP: notifyResponse(data.getGroup(), data.getPingData()); break; } } catch(Exception ex) { log.error(Util.getMessage("FailedReadingData"), ex); } }