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