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

http://ambienttalk.googlecode.com/ · Java · 235 lines · 134 code · 15 blank · 86 comment · 53 complexity · cb0f1de2c94efe0d67ac403a1b458781 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 java.lang.ref.WeakReference;
  30. import java.util.Iterator;
  31. import java.util.Set;
  32. import edu.vub.at.actors.id.ATObjectID;
  33. import edu.vub.at.actors.id.VirtualMachineID;
  34. import edu.vub.at.actors.natives.NATFarReference;
  35. import edu.vub.at.actors.natives.NATLocalFarRef;
  36. import edu.vub.at.util.logging.Logging;
  37. import edu.vub.util.MultiMap;
  38. /**
  39. * An instance of this class manages disconnection and reconnection subscriptions and
  40. * notifications for far references. Whenever virtual machines connect or
  41. * disconnect from the multicast group, this object is notified. Its role is to propagate
  42. * these notifications to all registered ConnectionListeners, which will usually be remote
  43. * references pointing to objects hosted by the connecting/disconnecting VM.
  44. *
  45. * @author tvcutsem
  46. */
  47. public class ConnectionListenerManager {
  48. /**
  49. * A collection of ConnectionListeners which are interested in the (dis)appearance of a single
  50. * node in the overlay network.
  51. */
  52. private final MultiMap connectionListeners_;
  53. /**
  54. * Creates a new manager on which ConnectionListeners monitoring the (dis)appearance
  55. * of a single address can register to.
  56. */
  57. public ConnectionListenerManager() {
  58. connectionListeners_ = new MultiMap();
  59. }
  60. /**
  61. * Registers <code>listener</code> to be notified whenever a virtual machine becomes (un)reachable.
  62. *
  63. * @param virtualMachine - an address of the virtual machine hosting the object the listener is interested in
  64. * @param listener - a listener which will be notified whenever the said address connects or disconnects
  65. */
  66. public synchronized void addConnectionListener(VirtualMachineID virtualMachine, ConnectionListener 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 && !(listener instanceof NATLocalFarRef)){
  105. //do not notify hardware connections on local far references
  106. listener.connected();
  107. }else{
  108. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  109. i.remove();
  110. }
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Notify all connection listeners for the given VM id that that VM has gone offline
  117. */
  118. public synchronized void notifyDisconnected(VirtualMachineID vmId){
  119. //notify all connectionlisteners for this member
  120. Set listeners = (Set)connectionListeners_.get(vmId);
  121. if (listeners != null) {
  122. for (Iterator i = listeners.iterator(); i.hasNext();) {
  123. WeakReference pooled = (WeakReference) i.next();
  124. if (pooled != null) {
  125. ConnectionListener listener = (ConnectionListener) pooled.get();
  126. if (listener != null && !(listener instanceof NATLocalFarRef)){
  127. //do not notify hardware disconnections on local far references
  128. listener.disconnected();
  129. }else{
  130. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  131. i.remove();
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Notify all connection listeners registered on the given remote object
  139. */
  140. public synchronized void notifyObjectTakenOffline(ATObjectID objId){
  141. //notify only the connectionlisteners for this objId
  142. Set listeners = (Set)connectionListeners_.get(objId.getVirtualMachineId());
  143. if (listeners != null) {
  144. for (Iterator i = listeners.iterator(); i.hasNext();) {
  145. WeakReference pooled = (WeakReference) i.next();
  146. if (pooled != null) {
  147. ConnectionListener listener = (ConnectionListener) pooled.get();
  148. if (listener != null){
  149. if (listener instanceof NATFarReference) {
  150. ATObjectID destination = ((NATFarReference)listener).impl_getObjectId();
  151. if (destination.equals(objId)){
  152. listener.takenOffline();
  153. //The entry on the table is removed so that the remote far reference is never
  154. //notified when the vmid hosting the offline object becomes (un)reachable.
  155. //In fact, the reference doesn't care about the such notifications because
  156. //an offline object will never become online.
  157. i.remove();
  158. }
  159. }
  160. }else{
  161. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  162. i.remove();
  163. }
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * Notify all connection listeners registered on the given remote object
  170. */
  171. public synchronized void notifyObjectDisconnected(ATObjectID objId){
  172. //notify only the connectionlisteners for this objId
  173. Set listeners = (Set)connectionListeners_.get(objId.getVirtualMachineId());
  174. if (listeners != null) {
  175. for (Iterator i = listeners.iterator(); i.hasNext();) {
  176. WeakReference pooled = (WeakReference) i.next();
  177. if (pooled != null) {
  178. ConnectionListener listener = (ConnectionListener) pooled.get();
  179. if (listener != null){
  180. if (listener instanceof NATFarReference) {
  181. ATObjectID destination = ((NATFarReference)listener).impl_getObjectId();
  182. if (destination.equals(objId)){
  183. listener.disconnected();
  184. }
  185. }
  186. } else {
  187. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  188. i.remove();
  189. }
  190. }
  191. }
  192. }
  193. }
  194. /**
  195. * Notify all connection listeners registered on the given remote object
  196. */
  197. public synchronized void notifyObjectReconnected(ATObjectID objId){
  198. //notify only the connectionlisteners for this objId
  199. Set listeners = (Set)connectionListeners_.get(objId.getVirtualMachineId());
  200. if (listeners != null) {
  201. for (Iterator i = listeners.iterator(); i.hasNext();) {
  202. WeakReference pooled = (WeakReference) i.next();
  203. if (pooled != null) {
  204. ConnectionListener listener = (ConnectionListener) pooled.get();
  205. if (listener != null){
  206. if (listener instanceof NATFarReference) {
  207. ATObjectID destination = ((NATFarReference)listener).impl_getObjectId();
  208. if (destination.equals(objId)){
  209. listener.connected();
  210. }
  211. }
  212. } else {
  213. // the listener referenced by the WeakReference was already gced => remove the pointer to WeakReference.
  214. i.remove();
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }