/interpreter/tags/at2-build190607/src/edu/vub/at/actors/net/VMAddressBook.java
Java | 95 lines | 29 code | 11 blank | 55 comment | 0 complexity | 0dc682b283bb0130981b58a067f996d4 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 edu.vub.at.actors.id.VirtualMachineID; 31import edu.vub.at.actors.net.comm.Address; 32import edu.vub.at.util.logging.Logging; 33 34import java.util.Hashtable; 35 36/** 37 * 38 * The VMAddressBook encapsulates the bi-directional mapping: 39 * AmbientTalk VM VirtualMachineID --> network Address 40 * network Address --> AmbientTalk VM VirtualMachineID 41 * 42 * It is necessary to maintain such a mapping to correctly identify and restore 43 * the connection between AmbientTalk VMs. A network 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( VirtualMachineID 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 VirtualMachineID vmId = (VirtualMachineID) addressToGuid_.get(vmAddress); 73 Logging.VirtualMachine_LOG.debug("Removed VM binding " + vmAddress + " -> " + vmId); 74 75 guidToAddress_.remove(vmId); 76 addressToGuid_.remove(vmAddress); 77 } 78 79 /** 80 * Resolve a remote VM's unique identifier to a concrete network address. 81 * @return the VM's address, or <tt>null</tt> if the VM is no longer connected. 82 */ 83 public synchronized Address getAddressOf(VirtualMachineID vmId) { 84 return (Address) guidToAddress_.get(vmId); 85 } 86 87 /** 88 * Resolve a concrete network address to a remote VM's unique identifier. 89 * @return the VM Id of the given address, or <tt>null</tt> if the address is no longer connected. 90 */ 91 public synchronized VirtualMachineID getGUIDOf(Address vmAddress) { 92 return (VirtualMachineID) addressToGuid_.get(vmAddress); 93 } 94 95}