/interpreter/tags/at2-build270707/src/edu/vub/at/actors/net/ConnectionListenerManager.java

http://ambienttalk.googlecode.com/ · Java · 176 lines · 89 code · 12 blank · 75 comment · 29 complexity · 577655c1f79e51f3ab9415fac53520e2 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.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, listener);
  67. connectionListeners_.put(virtualMachine, new WeakReference(listener));
  68. }
  69. /**
  70. * Unregisters <code>listener</code> such that it will no longer be notified whenever a
  71. * particular virtual machine becomes (un)reachable.
  72. */
  73. public synchronized void removeConnectionListener(VirtualMachineID virtualMachine, ConnectionListener listener) {
  74. Set listeners = (Set)connectionListeners_.get(virtualMachine);
  75. if(listeners != null) {
  76. for (Iterator i = listeners.iterator(); i.hasNext();) {
  77. WeakReference pooled = (WeakReference) i.next();
  78. if (pooled != null) {
  79. ConnectionListener list = (ConnectionListener) pooled.get();
  80. if( list != null){
  81. if (list.equals(listener)) {
  82. Logging.VirtualMachine_LOG.info("Removing ELFarReference from CLM " + this);
  83. i.remove();
  84. }
  85. }else{
  86. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  87. i.remove();
  88. }
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * Notify all connection listeners for the given VM id that that VM has come online
  95. */
  96. public synchronized void notifyConnected(VirtualMachineID vmId) {
  97. //notify all connectionlisteners for this member
  98. Set listeners = (Set)connectionListeners_.get(vmId);
  99. if (listeners != null) {
  100. for (Iterator i = listeners.iterator(); i.hasNext();) {
  101. WeakReference pooled = (WeakReference) i.next();
  102. if (pooled != null) {
  103. ConnectionListener listener = (ConnectionListener) pooled.get();
  104. if (listener != null){
  105. listener.connected();
  106. }else{
  107. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  108. i.remove();
  109. }
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * Notify all connection listeners for the given VM id that that VM has gone offline
  116. */
  117. public synchronized void notifyDisconnected(VirtualMachineID vmId){
  118. //notify all connectionlisteners for this member
  119. Set listeners = (Set)connectionListeners_.get(vmId);
  120. if (listeners != null) {
  121. for (Iterator i = listeners.iterator(); i.hasNext();) {
  122. WeakReference pooled = (WeakReference) i.next();
  123. if (pooled != null) {
  124. ConnectionListener listener = (ConnectionListener) pooled.get();
  125. if (listener != null){
  126. listener.disconnected();
  127. }else{
  128. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  129. i.remove();
  130. }
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * Notify all connection listeners registered on the given remote object
  137. */
  138. public synchronized void notifyObjectTakenOffline(ATObjectID objId){
  139. //notify only the connectionlisteners for this objId
  140. Set listeners = (Set)connectionListeners_.get(objId.getVirtualMachineId());
  141. if (listeners != null) {
  142. for (Iterator i = listeners.iterator(); i.hasNext();) {
  143. WeakReference pooled = (WeakReference) i.next();
  144. if (pooled != null) {
  145. ConnectionListener listener = (ConnectionListener) pooled.get();
  146. if (listener instanceof ELFarReference) {
  147. ATObjectID destination = ((ELFarReference)listener).getDestination();
  148. if (destination.equals(objId)){
  149. listener.takenOffline();
  150. //The entry on the table is removed so that the remote far reference is never
  151. //notified when the vmid hosting the offline object becomes (un)reachable.
  152. //In fact, the reference doesn't care about the such notifications because
  153. //an offline object will never become online.
  154. i.remove();
  155. }
  156. }else{
  157. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  158. i.remove();
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }