de.unikassel.android.sdcframework.transmission
Class TransferManagerImpl

Package class diagram package TransferManagerImpl
java.lang.Object
  extended by java.lang.Thread
      extended by de.unikassel.android.sdcframework.util.AbstractWorkerThread
          extended by de.unikassel.android.sdcframework.transmission.TransferManagerImpl
All Implemented Interfaces:
SampleRateChangeResponder, TransferManager, UpdatableTransmissionComponent<TransmissionConfiguration>, EventObserver<NetworkStateChangeEvent>, LifeCycleObject, ObservableEventSource<TransferEvent>, ObserverRegistration<TransferEvent>, WorkerThread, Runnable

public class TransferManagerImpl
extends AbstractWorkerThread
implements TransferManager, EventObserver<NetworkStateChangeEvent>

Implementation of the transfer manager which is the main access component for the transmission module.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
private  ObservableAlarm alarm
          The observable wake up alarm
private  EventObserver<AlarmEvent> alarmObserver
          The alarm observer to delegate to
static int COLLECTING
          The internal COLLECTING state of the transfer service
private static long CONNECTION_WAKE_UP_TIME
          The internal wake up time in case of lost connection ( milli seconds )
private  NetworkConnectionObserver connectionObserver
          The network connection observer
private  Object connectionWaitLock
          The wait lock for unavailable connection
private  SampleCollection currentSamples
          The current sample collection to transfer
private  AtomicInteger currentState
          The current state
private  DatabaseManager dbManager
          The database adapter used for DB access
private  FileManager fileManager
          The manager for the file access and archive creation
private  ObservableAlarm forcedActivationAlarm
          The observable wake up alarm for forced transmission
private  Object frequencyWaitLock
          The frequency wait lock
private  SampleGatheringController gatheringController
          The controller for the sample gather task
static int INIT
          The internal COLLECTING state of the transfer service
static long INITIAL_DELAY
          The start delay after transfer manager is activated.
private  AtomicLong lastExecutionTimeStamp
          The time stamp of last execution cycle
static long MIN_FREQUENCY
          The internal lower limit of wait time in seconds
private  AtomicLong minFrequency
          The minimal transfer frequency in milliseconds
static int PREPARATION
          The internal PREPARATION state of the transfer service
private  Object protocolWaitLock
          The wait lock for invalid url
private  Object sampleWaitLock
          The sample rate wait lock
private  ObservableEventSource<TransferEvent> transferEventSource
          The event source for the observable transfer events.
static int TRANSMISSION
          The internal transmission state of the transfer service
private  UploadManager uploadManager
          The upload manager
private static int WAIT_TIME_FOR_CONFIG_CHANGES
          The time to wait at maximum for configuration changes if transfer fails due to invalid settings
private  WakeLockHolder wakeLockHolder
          The wake lock maintainer
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TransferManagerImpl(Context applicationContext, TransmissionConfiguration config, DatabaseManager dbManager, UUID uuid, Class<?> controlActivityClass)
          Constructor
 
Method Summary
private  void addSamples(Collection<DatabaseSample> samples)
          Does add a collection of database samples to the current sample collection for transmission
protected  void doCleanUp()
          The clean up method executed on termination
private  void doPickSamplesFromDatabase()
          Does remove available samples from the database.
private  boolean doPrepareArchive()
          Does create the archive for transmission
 void doReactOnUploadError()
           
protected  void doSignalConnectionStateChange()
          Method to signal connection state changes
protected  void doSignalFrequencyChange()
          Method to signal frequency changes
protected  void doSignalProtocolChange()
          Method to signal protocol changes
protected  void doSignalSampleRateChanged()
          Method to signal sample rate changes
private  boolean doTransferArchive()
          Does upload the archive to the configured host
 void doWaitForSamples(long waitTime)
          Method to wait for samples available for transmission
protected  void doWork()
          The working method executed in the running loop if started
 boolean forcedActivation()
          Does force an activation of the transfer service.
 String getCurrentArchiveFileName()
          Method to access the filename of the current archive
private  long getCurrentRecordCount()
          Getter for the record count in the database
 int getCurrentState()
          Getter for the current state
 int getMaxSampleCount()
          Getter for the maximum sample count to transfer
 long getMinFrequency()
          Getter for the minimum frequency to set
 int getMinSampleCount()
          Getter for the minimum sample count to transfer
 long getTimeStamp()
          Getter for the time stamp of last execution
 boolean hasObservers()
          Test method for observers.
 void notify(TransferEvent data)
          Does set the changed flag and notifies all observers
 void onCreate(Context applicationContext)
          Creation method
 void onDestroy(Context applicationContext)
          Destroy method
 void onEvent(ObservableEventSource<? extends NetworkStateChangeEvent> eventSource, NetworkStateChangeEvent observedEvent)
          This method is called whenever the observed object does create an observed event
 void onPause(Context applicationContext)
          Pause method
 void onResume(Context applicationContext)
          Resume method
 void onSampleRateChanged()
          Method to react on signaled sample rate changes
 void registerEventObserver(EventObserver<? extends TransferEvent> observer)
          Does register an observer
 void removeAllObservers()
          Does remove all registered observers
 void unregisterEventObserver(EventObserver<? extends TransferEvent> observer)
          Does delete a registered observer
 void updateConfiguration(Context context, TransmissionConfiguration config)
          Method to update the component by a configuration
 
Methods inherited from class de.unikassel.android.sdcframework.util.AbstractWorkerThread
doTerminate, hasTerminated, isLogging, isWorking, logMessage, run, setLogging, start, startWork, stopWork
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface de.unikassel.android.sdcframework.util.facade.WorkerThread
doTerminate, hasTerminated, isLogging, isWorking, setLogging, startWork, stopWork
 

Field Detail

WAIT_TIME_FOR_CONFIG_CHANGES

private static final int WAIT_TIME_FOR_CONFIG_CHANGES
The time to wait at maximum for configuration changes if transfer fails due to invalid settings

See Also:
Constant Field Values

INIT

public static final int INIT
The internal COLLECTING state of the transfer service

See Also:
Constant Field Values

COLLECTING

public static final int COLLECTING
The internal COLLECTING state of the transfer service

See Also:
Constant Field Values

PREPARATION

public static final int PREPARATION
The internal PREPARATION state of the transfer service

See Also:
Constant Field Values

TRANSMISSION

public static final int TRANSMISSION
The internal transmission state of the transfer service

See Also:
Constant Field Values

MIN_FREQUENCY

public static final long MIN_FREQUENCY
The internal lower limit of wait time in seconds

See Also:
Constant Field Values

INITIAL_DELAY

public static final long INITIAL_DELAY
The start delay after transfer manager is activated.

See Also:
Constant Field Values

CONNECTION_WAKE_UP_TIME

private static final long CONNECTION_WAKE_UP_TIME
The internal wake up time in case of lost connection ( milli seconds )

See Also:
Constant Field Values

dbManager

private final DatabaseManager dbManager
The database adapter used for DB access


fileManager

private final FileManager fileManager
The manager for the file access and archive creation


uploadManager

private final UploadManager uploadManager
The upload manager


currentState

private final AtomicInteger currentState
The current state


connectionObserver

private final NetworkConnectionObserver connectionObserver
The network connection observer


connectionWaitLock

private final Object connectionWaitLock
The wait lock for unavailable connection


protocolWaitLock

private final Object protocolWaitLock
The wait lock for invalid url


frequencyWaitLock

private final Object frequencyWaitLock
The frequency wait lock


sampleWaitLock

private final Object sampleWaitLock
The sample rate wait lock


minFrequency

private final AtomicLong minFrequency
The minimal transfer frequency in milliseconds


lastExecutionTimeStamp

private final AtomicLong lastExecutionTimeStamp
The time stamp of last execution cycle


currentSamples

private final SampleCollection currentSamples
The current sample collection to transfer


gatheringController

private final SampleGatheringController gatheringController
The controller for the sample gather task


alarm

private final ObservableAlarm alarm
The observable wake up alarm


forcedActivationAlarm

private final ObservableAlarm forcedActivationAlarm
The observable wake up alarm for forced transmission


alarmObserver

private final EventObserver<AlarmEvent> alarmObserver
The alarm observer to delegate to


wakeLockHolder

private final WakeLockHolder wakeLockHolder
The wake lock maintainer


transferEventSource

private final ObservableEventSource<TransferEvent> transferEventSource
The event source for the observable transfer events.

Constructor Detail

TransferManagerImpl

public TransferManagerImpl(Context applicationContext,
                           TransmissionConfiguration config,
                           DatabaseManager dbManager,
                           UUID uuid,
                           Class<?> controlActivityClass)
Constructor

Parameters:
applicationContext - the application context
config - the current transmission configuration
dbManager - the database manager to use
uuid - the unique device identifier created by the service
controlActivityClass - the control activity class or null
Method Detail

forcedActivation

public boolean forcedActivation()
Description copied from interface: TransferManager
Does force an activation of the transfer service.

Specified by:
forcedActivation in interface TransferManager
Returns:
true if activated, false if database is empty.

onCreate

public final void onCreate(Context applicationContext)
Description copied from interface: LifeCycleObject
Creation method

Specified by:
onCreate in interface LifeCycleObject
Parameters:
applicationContext - the application context

onResume

public final void onResume(Context applicationContext)
Description copied from interface: LifeCycleObject
Resume method

Specified by:
onResume in interface LifeCycleObject
Parameters:
applicationContext - the application context

onPause

public final void onPause(Context applicationContext)
Description copied from interface: LifeCycleObject
Pause method

Specified by:
onPause in interface LifeCycleObject
Parameters:
applicationContext - the application context

onDestroy

public void onDestroy(Context applicationContext)
Description copied from interface: LifeCycleObject
Destroy method

Specified by:
onDestroy in interface LifeCycleObject
Parameters:
applicationContext - the application context

doCleanUp

protected final void doCleanUp()
Description copied from class: AbstractWorkerThread
The clean up method executed on termination

Specified by:
doCleanUp in class AbstractWorkerThread

updateConfiguration

public final void updateConfiguration(Context context,
                                      TransmissionConfiguration config)
Description copied from interface: UpdatableTransmissionComponent
Method to update the component by a configuration

Specified by:
updateConfiguration in interface UpdatableTransmissionComponent<TransmissionConfiguration>
Parameters:
context - the application context
config - the configuration to update from

doWork

protected final void doWork()
Description copied from class: AbstractWorkerThread
The working method executed in the running loop if started

Specified by:
doWork in class AbstractWorkerThread

doWaitForSamples

public void doWaitForSamples(long waitTime)
                      throws InterruptedException
Method to wait for samples available for transmission

Parameters:
waitTime - the time to wait
Throws:
InterruptedException - in case of thread interruption

doReactOnUploadError

public void doReactOnUploadError()
                          throws InterruptedException
Throws:
InterruptedException

doTransferArchive

private final boolean doTransferArchive()
Does upload the archive to the configured host

Returns:
true if successful, false otherwise

doPrepareArchive

private final boolean doPrepareArchive()
Does create the archive for transmission

Returns:
true if successful, false otherwise

doPickSamplesFromDatabase

private final void doPickSamplesFromDatabase()
Does remove available samples from the database.


addSamples

private final void addSamples(Collection<DatabaseSample> samples)
Does add a collection of database samples to the current sample collection for transmission

Parameters:
samples - the database sample collection to add to current sample collection

getCurrentRecordCount

private long getCurrentRecordCount()
Getter for the record count in the database

Returns:
the current available record count

getMinFrequency

public final long getMinFrequency()
Getter for the minimum frequency to set

Returns:
the minimum frequency to set

getMinSampleCount

public final int getMinSampleCount()
Getter for the minimum sample count to transfer

Returns:
the minimum sample count to transfer

getMaxSampleCount

public final int getMaxSampleCount()
Getter for the maximum sample count to transfer

Returns:
the maximum sample count to transfer

getCurrentState

public final int getCurrentState()
Getter for the current state

Returns:
the current state

getTimeStamp

public final long getTimeStamp()
Getter for the time stamp of last execution

Returns:
the time stamp of last execution

getCurrentArchiveFileName

public final String getCurrentArchiveFileName()
Method to access the filename of the current archive

Returns:
the name of the current archive file

onEvent

public void onEvent(ObservableEventSource<? extends NetworkStateChangeEvent> eventSource,
                    NetworkStateChangeEvent observedEvent)
Description copied from interface: EventObserver
This method is called whenever the observed object does create an observed event

Specified by:
onEvent in interface EventObserver<NetworkStateChangeEvent>
Parameters:
eventSource - the event generator
observedEvent - the event created by the observed object

onSampleRateChanged

public void onSampleRateChanged()
Description copied from interface: SampleRateChangeResponder
Method to react on signaled sample rate changes

Specified by:
onSampleRateChanged in interface SampleRateChangeResponder

doSignalProtocolChange

protected void doSignalProtocolChange()
Method to signal protocol changes


doSignalConnectionStateChange

protected void doSignalConnectionStateChange()
Method to signal connection state changes


doSignalSampleRateChanged

protected void doSignalSampleRateChanged()
Method to signal sample rate changes


doSignalFrequencyChange

protected void doSignalFrequencyChange()
Method to signal frequency changes


notify

public void notify(TransferEvent data)
Description copied from interface: ObservableEventSource
Does set the changed flag and notifies all observers

Specified by:
notify in interface ObservableEventSource<TransferEvent>
Parameters:
data - the observable event data to create notifications for

hasObservers

public boolean hasObservers()
Description copied from interface: ObservableEventSource
Test method for observers.

Specified by:
hasObservers in interface ObservableEventSource<TransferEvent>
Returns:
true if observers are registered, false otherwise

registerEventObserver

public void registerEventObserver(EventObserver<? extends TransferEvent> observer)
Description copied from interface: ObserverRegistration
Does register an observer

Specified by:
registerEventObserver in interface ObserverRegistration<TransferEvent>
Parameters:
observer - the observer to add

unregisterEventObserver

public void unregisterEventObserver(EventObserver<? extends TransferEvent> observer)
Description copied from interface: ObserverRegistration
Does delete a registered observer

Specified by:
unregisterEventObserver in interface ObserverRegistration<TransferEvent>
Parameters:
observer - the observer to delete

removeAllObservers

public void removeAllObservers()
Description copied from interface: ObserverRegistration
Does remove all registered observers

Specified by:
removeAllObservers in interface ObserverRegistration<TransferEvent>