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

http://ambienttalk.googlecode.com/ · 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. */
  28. package edu.vub.at.actors.net;
  29. import edu.vub.at.actors.id.VirtualMachineID;
  30. import edu.vub.at.actors.net.comm.Address;
  31. import edu.vub.at.util.logging.Logging;
  32. import java.util.Hashtable;
  33. /**
  34. *
  35. * The VMAddressBook encapsulates the bi-directional mapping:
  36. * AmbientTalk VM VirtualMachineID --> network Address
  37. * network Address --> AmbientTalk VM VirtualMachineID
  38. *
  39. * It is necessary to maintain such a mapping to correctly identify and restore
  40. * the connection between AmbientTalk VMs. A network address cannot be used as
  41. * a good identification key because an AmbientTalk VM may be known under different
  42. * addresses as it goes offline and online.
  43. *
  44. * When a new VM joins, after handshaking, an entry is added to the address book.
  45. * When a VM disjoins, the entry corresponding to the address that went offline is removed.
  46. *
  47. * @author elgonzal
  48. * @author tvcutsem
  49. */
  50. public class VMAddressBook {
  51. private final Hashtable guidToAddress_;
  52. private final Hashtable addressToGuid_;
  53. public VMAddressBook(){
  54. guidToAddress_ = new Hashtable();
  55. addressToGuid_ = new Hashtable();
  56. }
  57. public synchronized void addEntry( VirtualMachineID vmId, Address vmAddress ){
  58. guidToAddress_.put(vmId, vmAddress);
  59. addressToGuid_.put(vmAddress, vmId);
  60. }
  61. /**
  62. * Remove all entries that map to this VM address from the VM Address book.
  63. */
  64. public synchronized void removeEntry(Address vmAddress ){
  65. VirtualMachineID vmId = (VirtualMachineID) addressToGuid_.get(vmAddress);
  66. Logging.VirtualMachine_LOG.debug("Removed VM binding " + vmAddress + " -> " + vmId);
  67. guidToAddress_.remove(vmId);
  68. addressToGuid_.remove(vmAddress);
  69. }
  70. /**
  71. * Resolve a remote VM's unique identifier to a concrete network address.
  72. * @return the VM's address, or <tt>null</tt> if the VM is no longer connected.
  73. */
  74. public synchronized Address getAddressOf(VirtualMachineID vmId) {
  75. return (Address) guidToAddress_.get(vmId);
  76. }
  77. /**
  78. * Resolve a concrete network address to a remote VM's unique identifier.
  79. * @return the VM Id of the given address, or <tt>null</tt> if the address is no longer connected.
  80. */
  81. public synchronized VirtualMachineID getGUIDOf(Address vmAddress) {
  82. return (VirtualMachineID) addressToGuid_.get(vmAddress);
  83. }
  84. }