All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sapia.ubik.mcast.View Maven / Gradle / Ivy

The newest version!
package org.sapia.ubik.mcast;

import org.sapia.ubik.net.ServerAddress;

import java.util.*;


/**
 * Encapsulates the addresses of the nodes that compose an event channel. An
 * instance of this class is encapsulated by an EventChannel. Its
 * provides a "view" of the domain.
 * 

* An instance of this class encapsulates the address of each of the sibling of * an EventChannel node. * * @author Yanick Duchesne *

*
Copyright:
Copyright © 2002-2003 Sapia Open Source Software. All Rights Reserved.
*
License:
Read the license.txt file of the jar or visit the * license page at the Sapia OSS web site
*
*/ public class View { private Map _addresses = new HashMap(); private Map _nodeToAddr = new HashMap(); private long _timeout; /** * Constructor for View. */ public View(long timeout) { _timeout = timeout; } /** * Returns this instance's List of ServerAddresses. * * @return a List of ServerAddresses. */ public synchronized List getHosts() { return new AddressList(_addresses.keySet()); } /** * Returns the ServerAddress corresponding to the given * node. * * @return a ServerAddress. */ public ServerAddress getAddressFor(String node) { NodeInfo info = (NodeInfo) _nodeToAddr.get(node); return info.addr; } /** * @param timeout the timeout after which nodes that haven't sent a heartbeat * are removed from this instance. */ public void setTimeout(long timeout){ _timeout = timeout; } /** * Adds the given address to this instance. * * @param addr the ServerAddress corresponding to a remote * EventChannel. * @param node node identifier. */ void addHost(ServerAddress addr, String node) { NodeInfo info = new NodeInfo(addr, node); _nodeToAddr.put(node, info); _addresses.put(info, new Long(System.currentTimeMillis())); } /** * Updates the "last access" flag corresponding to the passed in * ServerAddress. * * @param ServerAddress. * @param node node identifier. */ void heartbeat(ServerAddress addr, String node) { _addresses.put(new NodeInfo(addr, node), new Long(System.currentTimeMillis())); } /** * Removes the "dead" (timed-out) hosts from this instance. */ void removeDeadHosts() { Map.Entry entry; Map.Entry[] hosts; synchronized (_addresses) { hosts = (Map.Entry[]) _addresses.entrySet().toArray(new Map.Entry[_addresses.size()]); NodeInfo info; for (int i = 0; i < hosts.length; i++) { if ((System.currentTimeMillis() - ((Long) hosts[i].getValue()).longValue()) > _timeout) { info = (NodeInfo) hosts[i].getKey(); _addresses.remove(info); _nodeToAddr.remove(info.node); } } } } /*////////////////////////////////////////////////// INNER CLASSES //////////////////////////////////////////////////*/ static class NodeInfo { final ServerAddress addr; final String node; NodeInfo(ServerAddress addr, String node) { this.addr = addr; this.node = node; } public boolean equals(Object obj) { NodeInfo inf = (NodeInfo) obj; return inf.addr.equals(addr) && inf.node.equals(node); } public int hashCode() { return addr.hashCode(); } } public static class AddressList extends ArrayList { AddressList(Collection infos) { super(infos); } public Object get(int idx) { try { return ((NodeInfo) super.get(idx)).addr; } catch (ClassCastException e) { throw e; } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy