stella.io
Class PollingRS485Server.Poller

java.lang.Object
  extended by java.util.TimerTask
      extended by stella.io.PollingRS485Server.Poller
All Implemented Interfaces:
Runnable
Enclosing class:
PollingRS485Server

protected class PollingRS485Server.Poller
extends TimerTask

The polling timer task. Once started this thread writes a command to the serial server and block-reads the return value. This return value is then stored in the embedding class's chache. After one value has been retrieved this thread is rescheduled after a certain time, specified by PollingRS485Server.KEY_POLLBASE. To allow different read-out times in active and passive states, the poller drops some read cycles, depending on PollingRS485Server.KEY_ACTIVEDROP and PollingRS485Server.KEY_PASSIVEDROP.


Field Summary
private  Trigger cycle
          True if polling is retrieving data.
private  int dropcycle
          The dropping counter.
private  int listindex
          The actual index in the list.
private  Object monitor
          An object to notify on the start of each run.
private  int polldrop
          The actual polldrop of the poller.
private  List pollist
          The actual poller's list of keys to poll.
private  boolean running
          True if poller is running.
 
Constructor Summary
protected PollingRS485Server.Poller()
          Constructs a new polling thread.
 
Method Summary
 boolean cancel()
          Cancels this polling thread.
 boolean isRunning()
          Returns the status of the running flag.
private  void notifyCacheList(String key, String newval)
          Notifies all listeners subscribed to the specified key.
 void run()
          The run method of the poller.
private  void setMonitor(Object mon)
          Sets an object that should be notified at the start of each run cycle.
protected  void setPollingDrop(int newdrop)
          Sets the list of command this poller should query.
protected  void setPollingList(List newlist)
          Sets the list of command this poller should query.
 
Methods inherited from class java.util.TimerTask
scheduledExecutionTime
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pollist

private List pollist
The actual poller's list of keys to poll.


listindex

private int listindex
The actual index in the list.


polldrop

private int polldrop
The actual polldrop of the poller.


dropcycle

private int dropcycle
The dropping counter. If polldrop is reached, a read is performed.


cycle

private Trigger cycle
True if polling is retrieving data. Object to synchronize on it.


running

private boolean running
True if poller is running.


monitor

private Object monitor
An object to notify on the start of each run.

Constructor Detail

PollingRS485Server.Poller

protected PollingRS485Server.Poller()
Constructs a new polling thread. Does not start automatically.

Method Detail

setMonitor

private void setMonitor(Object mon)
Sets an object that should be notified at the start of each run cycle. If called with a null argument, notification is reset.


run

public void run()
The run method of the poller. Per cycle one command string is read from the pollers actual command list. It is writtn as a command to the serial port and the answer block-reads into the cache. This thread then waits for the actual polling time before starting the next cycle with the next command.
Changes in the cached value are reported to registered CacheChangedListeners. The null element is allowed in the cache and may even spark the listener's notification.

A call to cancel() allows this thread to die.

Specified by:
run in interface Runnable
Specified by:
run in class TimerTask

cancel

public boolean cancel()
Cancels this polling thread. The thread should die within a time determined by PollingRS485Server.KEY_POLLBASE multiplied by the current drop rate.

Overrides:
cancel in class TimerTask

isRunning

public boolean isRunning()
Returns the status of the running flag. The poller is considered running if it was scheduled for at least one time and has not been canceled since.


setPollingList

protected void setPollingList(List newlist)
Sets the list of command this poller should query. Warning: this method is not synchronized. If called during the live-time of the poller, external synchronization on the poller's cache is necessary.


setPollingDrop

protected void setPollingDrop(int newdrop)
Sets the list of command this poller should query. Warning: this method is not synchronized. If called during the live-time of the poller, external synchronization on the poller's cache is necessary because setting a primitive long must not be atomic.


notifyCacheList

private void notifyCacheList(String key,
                             String newval)
Notifies all listeners subscribed to the specified key. The calling method ensures that the cache value has changed, so no checks are performed here.