PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at_build150307/src/edu/vub/at/actors/net/VMAddressBook.java

http://ambienttalk.googlecode.com/
Java | 107 lines | 33 code | 16 blank | 58 comment | 2 complexity | d473435ae65a94d5851015936564d558 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * Actorscript.java created on 2-mrt-2007 at 11:14:27
  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 java.util.Hashtable;
 31
 32import org.jgroups.Address;
 33
 34import edu.vub.at.actors.id.GUID;
 35
 36/**
 37 * 
 38 * The VMAddressBook encapsulates the bi-directional mapping:
 39 *  AmbientTalk VM GUID  -->  JGroups Address
 40 *  JGroups Address      -->  AmbientTalk VM GUID
 41 *
 42 * It is necessary to maintain such a mapping to correctly identify and restore
 43 * the connection between AmbientTalk VMs. A JGroups address cannot be used as
 44 * a good identification key because an AmbientTalk VM may be known under different
 45 * addresses as it goes offline and online.
 46 * 
 47 * When a new VM joins, after handshaking, an entry is added to the address book.
 48 * When a VM disjoins, the entry corresponding to the address that went offline is removed.
 49 * 
 50 * @author elgonzal
 51 * @author tvcutsem
 52 */
 53public class VMAddressBook {
 54	
 55	private final Hashtable guidToAddress_;
 56	private final Hashtable addressToGuid_;
 57	
 58	public VMAddressBook(){
 59		guidToAddress_ = new Hashtable();
 60		addressToGuid_ = new Hashtable();
 61	}
 62	
 63	public synchronized void addEntry( GUID vmId, Address vmAddress ){
 64		guidToAddress_.put(vmId, vmAddress);
 65		addressToGuid_.put(vmAddress, vmId);
 66	}
 67
 68	/**
 69	 * Remove all entries that map to this VM address from the VM Address book.
 70	 */
 71	public synchronized void removeEntry(Address vmAddress ){
 72		
 73		GUID vmId = (GUID) addressToGuid_.get(vmAddress);
 74		Logging.VirtualMachine_LOG.debug("Removed VM binding " + vmAddress + " -> " + vmId);
 75
 76		guidToAddress_.remove(vmId);
 77		addressToGuid_.remove(vmAddress);
 78	}
 79	
 80	
 81	/**
 82	 * Resolve a remote VM's unique identifier to a concrete network address.
 83	 */
 84	public synchronized Address getAddressOf(GUID vmId) {
 85			Address a = (Address) guidToAddress_.get(vmId);
 86			if (a == null) {
 87				Logging.VirtualMachine_LOG.error("Asked for the address of an unknown vmId: " + vmId);
 88				throw new RuntimeException("Asked for the address of an unknown vmId: " + vmId);
 89			}
 90			return a;
 91	}
 92
 93	/**
 94	 * Resolve a concrete network address to a remote VM's unique identifier.
 95	 */
 96	public synchronized GUID getGUIDOf(Address vmAddress) {
 97			return (GUID) addressToGuid_.get(vmAddress);
 98//			if (g == null) {
 99//				Logging.VirtualMachine_LOG.error("Asked for the GUID of an unknown vmAddress: " + vmAddress);
100//				throw new RuntimeException("Asked for the GUID of an unknown vmAddress: " + vmAddress);
101//			}
102//			return g;
103	}
104	
105	
106
107}