/** Sends a GET_MBR_REQ to *all* GossipRouters, merges responses. */ private List _getMembers(String group) { List ret = new LinkedList(); Socket sock = null; SocketAddress destAddr; DataOutputStream out = null; DataInputStream in = null; IpAddress entry; GossipData gossip_req, gossip_rsp; Address mbr; for (int i = 0; i < gossip_servers.size(); i++) { entry = (IpAddress) gossip_servers.elementAt(i); if (entry.getIpAddress() == null || entry.getPort() == 0) { if (log.isErrorEnabled()) log.error("entry.host or entry.port is null"); continue; } try { // sock=new Socket(entry.getIpAddress(), entry.getPort()); sock = new Socket(); destAddr = new InetSocketAddress(entry.getIpAddress(), entry.getPort()); sock.connect(destAddr, SOCKET_TIMEOUT); out = new DataOutputStream(sock.getOutputStream()); gossip_req = new GossipData(GossipRouter.GOSSIP_GET, group, null, null); // must send GossipData as fast as possible, otherwise the // request might be rejected gossip_req.writeTo(out); out.flush(); in = new DataInputStream(sock.getInputStream()); gossip_rsp = new GossipData(); gossip_rsp.readFrom(in); if (gossip_rsp.mbrs != null) { // merge with ret for (Iterator it = gossip_rsp.mbrs.iterator(); it.hasNext(); ) { mbr = (Address) it.next(); if (!ret.contains(mbr)) ret.add(mbr); } } } catch (Exception ex) { if (log.isErrorEnabled()) log.error("exception connecting to host " + entry); } finally { Util.close(out); Util.close(in); if (sock != null) { try { sock.close(); } catch (IOException e) { } } } } return ret; }
void _unregister(String group, Address mbr) { Socket sock = null; DataOutputStream out = null; IpAddress entry; GossipData gossip_req; for (int i = 0; i < gossip_servers.size(); i++) { entry = (IpAddress) gossip_servers.elementAt(i); if (entry.getIpAddress() == null || entry.getPort() == 0) { if (log.isErrorEnabled()) log.error("entry.host or entry.port is null"); continue; } try { if (log.isTraceEnabled()) 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); // must send GossipData as fast as possible, otherwise the // request might be rejected gossip_req.writeTo(out); out.flush(); } catch (Exception ex) { if (log.isErrorEnabled()) log.error("exception connecting to host " + entry); } finally { Util.close(out); if (sock != null) { try { sock.close(); } catch (IOException e) { } } } } }