/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. */
  28. package edu.vub.at.actors.net;
  29. import edu.vub.at.actors.id.ATObjectID;
  30. import edu.vub.at.actors.id.VirtualMachineID;
  31. import edu.vub.at.actors.natives.ELFarReference;
  32. import edu.vub.util.MultiMap;
  33. import java.util.Iterator;
  34. import java.util.Set;
  35. /**
  36. * An instance of this class manages disconnection and reconnection subscriptions and
  37. * notifications for far references. Whenever virtual machines connect or
  38. * disconnect from the multicast group, this object is notified. Its role is to propagate
  39. * these notifications to all registered ConnectionListeners, which will usually be remote
  40. * references pointing to objects hosted by the connecting/disconnecting VM.
  41. *
  42. * @author tvcutsem
  43. */
  44. public class ConnectionListenerManager {
  45. /**
  46. * A collection of ConnectionListeners which are interested in the (dis)appearance of a single
  47. * node in the overlay network.
  48. */
  49. private final MultiMap connectionListeners_;
  50. /**
  51. * Creates a new manager on which ConnectionListeners monitoring the (dis)appearance
  52. * of a single address can register to.
  53. */
  54. public ConnectionListenerManager() {
  55. connectionListeners_ = new MultiMap();
  56. }
  57. /**
  58. * Registers <code>listener</code> to be notified whenever a virtual machine becomes (un)reachable.
  59. *
  60. * TODO: store only WEAK references to the remote references
  61. *
  62. * @param virtualMachine - an address of the virtual machine hosting the object the listener is interested in
  63. * @param listener - a listener which will be notified whenever the said address connects or disconnects
  64. */
  65. public synchronized void addConnectionListener(VirtualMachineID virtualMachine, ConnectionListener listener) {
  66. connectionListeners_.put(virtualMachine, listener);
  67. }
  68. /**
  69. * Unregisters <code>listener</code> such that it will no longer be notified whenever a
  70. * particular virtual machine becomes (un)reachable.
  71. */
  72. public synchronized void removeConnectionListener(VirtualMachineID virtualMachine, ConnectionListener listener) {
  73. connectionListeners_.removeValue(virtualMachine, listener);
  74. }
  75. /**
  76. * Notify all connection listeners for the given VM id that that VM has come online
  77. */
  78. public synchronized void notifyConnected(VirtualMachineID vmId) {
  79. //notify all connectionlisteners for this member
  80. Set listeners = (Set)connectionListeners_.get(vmId);
  81. if (listeners != null) {
  82. for (Iterator i = listeners.iterator(); i.hasNext();) {
  83. ConnectionListener listener = (ConnectionListener) i.next();
  84. listener.connected();
  85. }
  86. }
  87. }
  88. /**
  89. * Notify all connection listeners for the given VM id that that VM has gone offline
  90. */
  91. public synchronized void notifyDisconnected(VirtualMachineID vmId){
  92. //notify all connectionlisteners for this member
  93. Set listeners = (Set)connectionListeners_.get(vmId);
  94. if (listeners != null) {
  95. for (Iterator i = listeners.iterator(); i.hasNext();) {
  96. ConnectionListener listener = (ConnectionListener) i.next();
  97. listener.disconnected();
  98. }
  99. }
  100. }
  101. /**
  102. * Notify all connection listeners registered on the given remote object
  103. * TODO: should refactor this: add expired(ATObjectID) method to ConnectionListener interface?
  104. */
  105. public synchronized void notifyObjectExpired(ATObjectID objId){
  106. //notify only the connectionlisteners for this objId
  107. Set listeners = (Set)connectionListeners_.get(objId.getVirtualMachineId());
  108. if (listeners != null) {
  109. for (Iterator i = listeners.iterator(); i.hasNext();) {
  110. ConnectionListener listener = (ConnectionListener) i.next();
  111. if (listener instanceof ELFarReference) {
  112. ATObjectID destination = ((ELFarReference)listener).getDestination();
  113. if (destination.equals(objId)){
  114. ((ELFarReference)listener).expired();
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }