hu.mta.sztaki.lpds.cloud.simulator.notifications
Class StateDependentEventHandler<T,P>

java.lang.Object
  extended by hu.mta.sztaki.lpds.cloud.simulator.notifications.StateDependentEventHandler<T,P>
Type Parameters:
T - the kind of state change for which this handler is prepared to notify about.
P - the kind of data to be passed on to the notified party

public class StateDependentEventHandler<T,P>
extends Object

The main non-time dependent event handling mechanism in DISSECT-CF. This class is kept generic, and every user could either derive from it or use this as an embedded object. It is expected that the user of this class will want to deliver notifications about its internal state changes. The kind of state changes possible are not known by this generic mechanism, but the user of this class is expected to express this via the generic parameter of the class. The individual notifications can be customized via the class's constructor.

Author:
"Gabor Kecskemeti, Laboratory of Parallel and Distributed Systems, MTA SZTAKI (c) 2015"

Field Summary
private  ArrayList<T> cancelledListeners
          if the notificaiton process is underway, then to be removed listeners are registered here (they will still receive notifications in the current notification round as their de-registration is actually a result of the current notification round)
private  ArrayList<T> listeners
          The listeners that will receive notifications if the notify listeners function is called
private  SingleNotificationHandler<T,P> myHandler
          the entity that is actually used to perform the notifications
private  ArrayList<T> newListeners
          if the notificaiton process is underway, then new listeners are registered here (they will not receive notifications in the current notification round as their registration is actually a result of the current notification round)
private  boolean noEventDispatchingInProcess
          a marker to show if there is a notification process underway
 
Constructor Summary
StateDependentEventHandler(SingleNotificationHandler<T,P> handler)
          Initialization of the event handling mechanism.
 
Method Summary
private  void mainNotificationLoop(P payload)
          The main event dispatching loop.
 void notifyListeners(P payload)
          Sends out the notifications via the user defined event handler for all currently listed listeners.
 void subscribeToEvents(T listener)
          To get state dependent events one must subscribe to them via this function.
 void unsubscribeFromEvents(T listener)
          If there are no events needed for a particular listener then they can be cancelled here.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listeners

private ArrayList<T> listeners
The listeners that will receive notifications if the notify listeners function is called


newListeners

private ArrayList<T> newListeners
if the notificaiton process is underway, then new listeners are registered here (they will not receive notifications in the current notification round as their registration is actually a result of the current notification round)


cancelledListeners

private ArrayList<T> cancelledListeners
if the notificaiton process is underway, then to be removed listeners are registered here (they will still receive notifications in the current notification round as their de-registration is actually a result of the current notification round)


noEventDispatchingInProcess

private boolean noEventDispatchingInProcess
a marker to show if there is a notification process underway


myHandler

private final SingleNotificationHandler<T,P> myHandler
the entity that is actually used to perform the notifications

Constructor Detail

StateDependentEventHandler

public StateDependentEventHandler(SingleNotificationHandler<T,P> handler)
Initialization of the event handling mechanism.

Parameters:
handler - via this interface's implementation will the actual observed object be capable of dispatching custom events abouts its state change
Method Detail

subscribeToEvents

public void subscribeToEvents(T listener)
To get state dependent events one must subscribe to them via this function. The listener will be called every time when the observed object changes its particular state and uses the notifylisteners function below.

Parameters:
listener - who should receive notifications upon state changes in the observed object

unsubscribeFromEvents

public void unsubscribeFromEvents(T listener)
If there are no events needed for a particular listener then they can be cancelled here.

Parameters:
listener - the listener which does not need the state related events anymore

mainNotificationLoop

private void mainNotificationLoop(P payload)
The main event dispatching loop. It is not intended for external use as it is not prepared to handle cases when the number of subscribers change while the dispatching is in process.


notifyListeners

public void notifyListeners(P payload)
Sends out the notifications via the user defined event handler for all currently listed listeners. If there are new/cancelled subscriptions then they are added/removed to/from the list of listeners at the end of the event dispatching loop.

Parameters:
payload - to be sent out for all interested parties upon receiving this notification


Copyright © 2012–2015 University of Innsbruck & MTA SZTAKI. All rights reserved.