opencard.core.event
Class EventGenerator

java.lang.Object
  |
  +--opencard.core.event.EventGenerator

public final class EventGenerator
extends java.lang.Object
implements Observer, java.lang.Runnable

The EventGenerator singleton acts as a generator and multicaster for card terminal events. The singleton instance can be obtained by calling the static method getGenerator. It periodically polls the terminals and generates CARD_INSERTED or CARD_REMOVED events if it detects a card insertion or removal, respectively. The generated events are sent to all CTListeners which have been registered using the method addCTListener. If not interested in card terminal events any longer, CTListeners can remove themselves from the notification list of the registry by calling the method removeCTListener. For the case that cards were inserted before an application was started, the method createEventsForPresentCards creates events for inserted cards a posteriori.

See Also:
CardTerminalEvent, CTListener, CardTerminal, CardTerminalRegistry

Method Summary
 void addCTListener(CTListener listener)
          Adds a CTListener.
 void createEventsForPresentCards(CTListener ctListener)
          Generates events for cards which are already inserted.
static EventGenerator getGenerator()
          Gets the unique instance of CardTerminalRegistry.
 int getPollInterval()
          Gets the duration of the poll interval in ms.
 void removeCTListener(CTListener ctListener)
          Removes a CTListener.
 void run()
          Periodically checks all Pollable terminals.
 void setPollInterval(int duration)
          Sets the poll interval in ms
 void updateCards(CardTerminal terminal, int slotID, boolean cardInserted)
          Notify listeners that a card was inserted into or removed from a slot of a terminal.
 boolean updateTerminals(Pollable p, boolean terminalAdded)
          .
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getGenerator

public static EventGenerator getGenerator()
Gets the unique instance of CardTerminalRegistry. (Singleton Pattern)
Returns:
The card terminal registry singleton.

addCTListener

public void addCTListener(CTListener listener)
Adds a CTListener.
Parameters:
listener - The CTListener to be added.
See Also:
removeCTListener(CTListener)

createEventsForPresentCards

public void createEventsForPresentCards(CTListener ctListener)
                                 throws CardTerminalException
Generates events for cards which are already inserted. This method should be called when using the event-driven paradigm (in contrast to the procedural waitForCard () approach) and the application needs to handle cards that are already inserted. This method iterates over all registered terminals.
Parameters:
ctListener - the Card Terminal Listener to which the events shall be sent.
Throws:
CardTerminalException - thrown in case of errors in a registered CardTerminal.

removeCTListener

public void removeCTListener(CTListener ctListener)
Removes a CTListener.
Parameters:
listener - The CTListener to be removed.
See Also:
addCTListener(CTListener)

setPollInterval

public void setPollInterval(int duration)
Sets the poll interval in ms
Returns:
duration in ms

getPollInterval

public int getPollInterval()
Gets the duration of the poll interval in ms.
Returns:
the poll interval duration in ms

run

public void run()
Periodically checks all Pollable terminals. The terminals implementation must then make sure to generate the proper events whenever they detect that a card was inserted or removed from one of their slots.
Specified by:
run in interface java.lang.Runnable

updateCards

public void updateCards(CardTerminal terminal,
                        int slotID,
                        boolean cardInserted)
Notify listeners that a card was inserted into or removed from a slot of a terminal.
Specified by:
updateCards in interface Observer
Parameters:
terminal - terminal where a card was inserted/removed
slot - slot where a card was inserted/removed
cardInserted - true if a card was inserted, false, if a card was removed

updateTerminals

public boolean updateTerminals(Pollable p,
                               boolean terminalAdded)
. Keep track of pollable terminals, used by CardTerminalRegistry
Specified by:
updateTerminals in interface Observer
Parameters:
p - terminal that was added/removed to the registry
terminalAdded - true if a terminal was added. False, if a terminal was removed.