PageRenderTime 15ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2-build060407/src/edu/vub/at/actors/net/ConnectionListenerManager.java

http://ambienttalk.googlecode.com/
Java | 131 lines | 51 code | 11 blank | 69 comment | 11 complexity | 80c9aa289daefbb1c3beb627faa474fe MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * MembershipNotifier.java created on Feb 16, 2007 at 1:14:08 PM
  4 * (c) Programming Technology Lab, 2006 - 2007
  5 * Authors: Tom Van Cutsem & Stijn Mostinckx
  6 * 
  7 * Permission is hereby granted, free of charge, to any person
  8 * obtaining a copy of this software and associated documentation
  9 * files (the "Software"), to deal in the Software without
 10 * restriction, including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense, and/or
 12 * sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following
 14 * conditions:
 15 *
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26 * OTHER DEALINGS IN THE SOFTWARE.
 27 */
 28package edu.vub.at.actors.net;
 29
 30import edu.vub.at.actors.id.ATObjectID;
 31import edu.vub.at.actors.id.VirtualMachineID;
 32import edu.vub.at.actors.natives.ELFarReference;
 33import edu.vub.util.MultiMap;
 34
 35import java.util.Iterator;
 36import java.util.Set;
 37
 38/**
 39 * An instance of this class manages disconnection and reconnection subscriptions and
 40 * notifications for far references. Whenever virtual machines connect or
 41 * disconnect from the multicast group, this object is notified. Its role is to propagate
 42 * these notifications to all registered ConnectionListeners, which will usually be remote
 43 * references pointing to objects hosted by the connecting/disconnecting VM.
 44 *
 45 * @author tvcutsem
 46 */
 47public class ConnectionListenerManager {
 48	
 49	/**
 50	 * A collection of ConnectionListeners which are interested in the (dis)appearance of a single
 51	 * node in the overlay network.
 52	 */
 53	private final MultiMap connectionListeners_;
 54	
 55	/**
 56	 * Creates a new manager on which ConnectionListeners monitoring the (dis)appearance
 57	 * of a single address can register to.
 58	 */
 59	public ConnectionListenerManager() {
 60		connectionListeners_ = new MultiMap();
 61	}
 62	
 63	/**
 64	 * Registers <code>listener</code> to be notified whenever a virtual machine becomes (un)reachable.
 65	 * 
 66	 * TODO: store only WEAK references to the remote references
 67	 * 
 68	 * @param virtualMachine - an address of the virtual machine hosting the object the listener is interested in
 69	 * @param listener - a listener which will be notified whenever the said address connects or disconnects
 70	 */
 71	public synchronized void addConnectionListener(VirtualMachineID virtualMachine, ConnectionListener listener) {
 72		connectionListeners_.put(virtualMachine, listener);
 73	}
 74	
 75	/**
 76	 * Unregisters <code>listener</code> such that it will no longer be notified whenever a 
 77	 * particular virtual machine becomes (un)reachable.
 78	 */
 79	public synchronized void removeConnectionListener(VirtualMachineID virtualMachine, ConnectionListener listener) {
 80		connectionListeners_.removeValue(virtualMachine, listener);
 81	}
 82
 83	/**
 84	 * Notify all connection listeners for the given VM id that that VM has come online
 85	 */
 86	public synchronized void notifyConnected(VirtualMachineID vmId) {
 87		//notify all connectionlisteners for this member
 88		Set listeners = (Set)connectionListeners_.get(vmId);
 89		if (listeners != null) {
 90			for (Iterator i = listeners.iterator(); i.hasNext();) {
 91				ConnectionListener listener = (ConnectionListener) i.next();
 92				listener.connected();
 93			}
 94		}
 95	}
 96	
 97	/**
 98	 * Notify all connection listeners for the given VM id that that VM has gone offline
 99	 */
100	public synchronized void notifyDisconnected(VirtualMachineID vmId){
101		//notify all connectionlisteners for this member
102		Set listeners = (Set)connectionListeners_.get(vmId);
103		if (listeners != null) {
104			for (Iterator i = listeners.iterator(); i.hasNext();) {
105				ConnectionListener listener = (ConnectionListener) i.next();
106				listener.disconnected();
107			}
108		}
109	}
110	
111	/**
112	 * Notify all connection listeners registered on the given remote object
113	 * TODO: should refactor this: add expired(ATObjectID) method to ConnectionListener interface?
114	 */
115	public synchronized void notifyObjectExpired(ATObjectID objId){
116		//notify only the connectionlisteners for this objId
117		Set listeners = (Set)connectionListeners_.get(objId.getVirtualMachineId());
118		if (listeners != null) {
119			for (Iterator i = listeners.iterator(); i.hasNext();) {
120				ConnectionListener listener = (ConnectionListener) i.next();
121				if (listener instanceof ELFarReference) {
122					ATObjectID destination = ((ELFarReference)listener).getDestination();
123					if (destination.equals(objId)){
124						((ELFarReference)listener).expired();
125					}
126				}
127			}
128		}
129	}
130	
131}