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

http://ambienttalk.googlecode.com/ · Java · 175 lines · 89 code · 12 blank · 74 comment · 29 complexity · a63a093438d10d8958dc9bcd0553f668 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.NATFarReference;
  32. import edu.vub.at.util.logging.Logging;
  33. import edu.vub.util.MultiMap;
  34. import java.lang.ref.WeakReference;
  35. import java.util.Iterator;
  36. import java.util.Set;
  37. /**
  38. * An instance of this class manages disconnection and reconnection subscriptions and
  39. * notifications for far references. Whenever virtual machines connect or
  40. * disconnect from the multicast group, this object is notified. Its role is to propagate
  41. * these notifications to all registered ConnectionListeners, which will usually be remote
  42. * references pointing to objects hosted by the connecting/disconnecting VM.
  43. *
  44. * @author tvcutsem
  45. */
  46. public class ConnectionListenerManager {
  47. /**
  48. * A collection of ConnectionListeners which are interested in the (dis)appearance of a single
  49. * node in the overlay network.
  50. */
  51. private final MultiMap connectionListeners_;
  52. /**
  53. * Creates a new manager on which ConnectionListeners monitoring the (dis)appearance
  54. * of a single address can register to.
  55. */
  56. public ConnectionListenerManager() {
  57. connectionListeners_ = new MultiMap();
  58. }
  59. /**
  60. * Registers <code>listener</code> to be notified whenever a virtual machine becomes (un)reachable.
  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, new WeakReference(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. Set listeners = (Set)connectionListeners_.get(virtualMachine);
  74. if(listeners != null) {
  75. for (Iterator i = listeners.iterator(); i.hasNext();) {
  76. WeakReference pooled = (WeakReference) i.next();
  77. if (pooled != null) {
  78. ConnectionListener list = (ConnectionListener) pooled.get();
  79. if( list != null){
  80. if (list.equals(listener)) {
  81. Logging.VirtualMachine_LOG.info("Removing ELFarReference from CLM " + this);
  82. i.remove();
  83. }
  84. }else{
  85. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  86. i.remove();
  87. }
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * Notify all connection listeners for the given VM id that that VM has come online
  94. */
  95. public synchronized void notifyConnected(VirtualMachineID vmId) {
  96. //notify all connectionlisteners for this member
  97. Set listeners = (Set)connectionListeners_.get(vmId);
  98. if (listeners != null) {
  99. for (Iterator i = listeners.iterator(); i.hasNext();) {
  100. WeakReference pooled = (WeakReference) i.next();
  101. if (pooled != null) {
  102. ConnectionListener listener = (ConnectionListener) pooled.get();
  103. if (listener != null){
  104. listener.connected();
  105. }else{
  106. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  107. i.remove();
  108. }
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * Notify all connection listeners for the given VM id that that VM has gone offline
  115. */
  116. public synchronized void notifyDisconnected(VirtualMachineID vmId){
  117. //notify all connectionlisteners for this member
  118. Set listeners = (Set)connectionListeners_.get(vmId);
  119. if (listeners != null) {
  120. for (Iterator i = listeners.iterator(); i.hasNext();) {
  121. WeakReference pooled = (WeakReference) i.next();
  122. if (pooled != null) {
  123. ConnectionListener listener = (ConnectionListener) pooled.get();
  124. if (listener != null){
  125. listener.disconnected();
  126. }else{
  127. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  128. i.remove();
  129. }
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * Notify all connection listeners registered on the given remote object
  136. */
  137. public synchronized void notifyObjectTakenOffline(ATObjectID objId){
  138. //notify only the connectionlisteners for this objId
  139. Set listeners = (Set)connectionListeners_.get(objId.getVirtualMachineId());
  140. if (listeners != null) {
  141. for (Iterator i = listeners.iterator(); i.hasNext();) {
  142. WeakReference pooled = (WeakReference) i.next();
  143. if (pooled != null) {
  144. ConnectionListener listener = (ConnectionListener) pooled.get();
  145. if (listener instanceof NATFarReference) {
  146. ATObjectID destination = ((NATFarReference)listener).getObjectId();
  147. if (destination.equals(objId)){
  148. listener.takenOffline();
  149. //The entry on the table is removed so that the remote far reference is never
  150. //notified when the vmid hosting the offline object becomes (un)reachable.
  151. //In fact, the reference doesn't care about the such notifications because
  152. //an offline object will never become online.
  153. i.remove();
  154. }
  155. }else{
  156. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  157. i.remove();
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }