/** Distribute peer information */ private void sendHello() { HeartBeatMsg.Hello msg = new HeartBeatMsg.Hello(); for (Map.Entry<String, JiniPeer> entry : agent.getPeers().entrySet()) { if (!entry.getValue().getReady()) continue; msg.appendPeers(entry.getKey()); } msg.setUnversionedHeaders(agent.getHeaders()); for (JiniPeer peer : agent.getPeers().values()) { if (!peer.getReady()) continue; msg.setSequence(peer.incSequence()); peer.send(msg); } }
@Override public long processTimer(long interval) { ArrayList<String> expired = new ArrayList<String>(); for (JiniPeer peer : agent.getPeers().values()) { if (peer.expiredAt() > 0 && System.currentTimeMillis() >= peer.expiredAt()) { expired.add(peer.getEndpoint()); agent.onEvent(JiniEvent.PEER_EXIT, peer.getEndpoint()); } else if (System.currentTimeMillis() >= peer.evasiveAt()) { peer.send(new HeartBeatMsg.Ping(peer.incSequence())); } } if (!expired.isEmpty()) agent.removePeer(expired.toArray(new String[0])); return interval; }
@Override public void processCallback(JiniMsg msg) { String endpoint = msg.getIdentityString(); JiniPeer peer = agent.findPeer(endpoint); if (peer == null && msg.getId() == HeartBeatMsg.PING) peer = agent.connectPeer(endpoint); if (peer == null) return; switch (msg.getId()) { case HeartBeatMsg.PING: peer.send(new HeartBeatMsg.PingOk(peer.incSequence())); break; case HeartBeatMsg.PING_OK: if (!peer.getReady()) { peer.setReady(true); sendHello(); agent.onEvent(JiniEvent.PEER_ENTER, peer.getEndpoint()); } break; case HeartBeatMsg.HELLO: HeartBeatMsg.Hello hello = (HeartBeatMsg.Hello) msg; List<String> peers = hello.getPeers(); for (String peerAddr : peers) { if (peerAddr.equals(agent.getAddress())) continue; JiniPeer peer_ = agent.findPeer(peerAddr); if (peer_ == null) { // Other node knows new peer information agent.connectPeer(peerAddr); } } break; } peer.refresh(evasive, expired); }
@Override public void initialize(JiniAgent agent, Object... args) { this.agent = agent; for (String addr : addrs) agent.connectPeer(addr); }