com.vodafone360.people.service.transport.tcp
Class HeartbeatSenderThread

java.lang.Object
  extended by com.vodafone360.people.service.transport.tcp.HeartbeatSenderThread
All Implemented Interfaces:
IWakeupListener, java.lang.Runnable

public class HeartbeatSenderThread
extends java.lang.Object
implements java.lang.Runnable, IWakeupListener

Sends heartbeats to the RPG in order to keep the connection alive.

Author:
Rudy Norff (rudy.norff@vodafone.com)

Field Summary
private  TcpConnectionThread mConnThread
          The managing thread that needs to be called back if an IOException occurs sending a heartbeat.
protected static HeartbeatSenderThread mCurrentThread
          The HeartbeatSenderThread that was created by the TcpConnectionThread.
protected  long mHeartbeatInterval
          This is the interval at which the heartbeat gets sent.
private static long MILLIS_PER_SEC
          The milliseconds in a second.
private  boolean mIsConnectionRunning
          Indicates that the connection is running if true.
protected  java.io.OutputStream mOs
          The output stream to write the heartbeat to.
private  RemoteService mService
          The service under which context the hb sender runs in.
private  java.net.Socket mSocket
          The socket to write to.
protected  java.lang.Thread mThread
          The thread continuously sending the heartbeats.
private  PowerManager.WakeLock mWakeLock
          Keeps a partial wake lock on the CPU that will prevent it from sleeping and allow the Sender to send off heartbeats.
 
Fields inherited from interface com.vodafone360.people.service.transport.IWakeupListener
ALARM_HB_THREAD
 
Constructor Summary
HeartbeatSenderThread(TcpConnectionThread connThread, RemoteService service, java.net.Socket socket)
          Constructs a heartbeat-sender and passes the connection thread to call back to in case of errors.
 
Method Summary
private  byte[] getHeartbeatHessianPayload()
          Returns a byte-array containing the data needed for sending a heartbeat to the RPG.
 boolean getIsActive()
           Returns true if the heartbeat thread is currently running/active.
 void notifyOfWakeupAlarm()
          Called whenever the Service receives a Wakeup Alarm intent.
 void run()
          The run-method overriding Thread.run().
 void sendHeartbeat()
          Prepares the necessary Hessian payload and writes it directly to the open output-stream of the socket.
 void setOutputStream(java.io.OutputStream outputStream)
          Sets the output-stream so that the heartbeat-sender can send its heartbeats to the RPG.
 void startConnection()
          Sets the state of the connection to run and spawns a thread to run it in.
 void stopConnection()
          Stops the heartbeat-senders connection and closes the output-stream to the socket-connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mCurrentThread

protected static HeartbeatSenderThread mCurrentThread
The HeartbeatSenderThread that was created by the TcpConnectionThread. It will be compared to this thread. If they differ this thread must be a lock thread that got stuck when the user changed APNs or switched from WiFi to another data connection type.


MILLIS_PER_SEC

private static final long MILLIS_PER_SEC
The milliseconds in a second.

See Also:
Constant Field Values

mService

private RemoteService mService
The service under which context the hb sender runs in. Used for setting alarms that wake up the CPU for sending out heartbeats.


mConnThread

private TcpConnectionThread mConnThread
The managing thread that needs to be called back if an IOException occurs sending a heartbeat.


mThread

protected java.lang.Thread mThread
The thread continuously sending the heartbeats.


mOs

protected java.io.OutputStream mOs
The output stream to write the heartbeat to.


mHeartbeatInterval

protected long mHeartbeatInterval
This is the interval at which the heartbeat gets sent. The problem with this is that this timeout interval is only proven in the Vodafone network. In other networks we will do an autodetect for the correct interval settings.


mIsConnectionRunning

private boolean mIsConnectionRunning
Indicates that the connection is running if true.


mWakeLock

private PowerManager.WakeLock mWakeLock
Keeps a partial wake lock on the CPU that will prevent it from sleeping and allow the Sender to send off heartbeats.


mSocket

private java.net.Socket mSocket
The socket to write to.

Constructor Detail

HeartbeatSenderThread

public HeartbeatSenderThread(TcpConnectionThread connThread,
                             RemoteService service,
                             java.net.Socket socket)
Constructs a heartbeat-sender and passes the connection thread to call back to in case of errors.

Parameters:
connThread - The connection thread to call back to in case of networking issues.
service - The remote service that we register with once we have set the heartbeat alarm.
Method Detail

startConnection

public void startConnection()
Sets the state of the connection to run and spawns a thread to run it in.


stopConnection

public void stopConnection()
Stops the heartbeat-senders connection and closes the output-stream to the socket-connection.


setOutputStream

public void setOutputStream(java.io.OutputStream outputStream)
Sets the output-stream so that the heartbeat-sender can send its heartbeats to the RPG.

Parameters:
outputStream - The open output-stream that the heartbeats shall be sent to. Avoid passing null as this will result in an error-callback to RpgTcpConnectionThread which will completely reestablish the socket connection.

run

public void run()
The run-method overriding Thread.run(). The method continuously sends out heartbeats by calling sendHeartbeat() and then waits for a Thread.interrupt(), a notify or for the wait timer to time out after a timer-value defined in Settings.TCP_VF_HEARTBEAT_INTERVAL.

Specified by:
run in interface java.lang.Runnable

sendHeartbeat

public void sendHeartbeat()
                   throws java.io.IOException,
                          java.lang.Exception
Prepares the necessary Hessian payload and writes it directly to the open output-stream of the socket.

Throws:
java.lang.Exception - Thrown if there was an unknown problem writing to the output-stream.
java.io.IOException - Thrown if there was a problem regarding IO while writing to the output-stream.

getHeartbeatHessianPayload

private byte[] getHeartbeatHessianPayload()
                                   throws java.io.IOException
Returns a byte-array containing the data needed for sending a heartbeat to the RPG.

Returns:
A byte array representing the heartbeat.
Throws:
java.io.IOException - If there was an exception serializing the hash map to a hessian byte array.

getIsActive

public boolean getIsActive()

Returns true if the heartbeat thread is currently running/active.

Calling startConnection() will make this method return as the heartbeat thread gets started.

stopConnection() will stop the connection again and make this method return false.

Returns:
True if the connection is running, false otherwise.

notifyOfWakeupAlarm

public void notifyOfWakeupAlarm()
Description copied from interface: IWakeupListener
Called whenever the Service receives a Wakeup Alarm intent. The listener should react on this call by carrying out network activity, e.g. to keep the connection alive.

Specified by:
notifyOfWakeupAlarm in interface IWakeupListener


JavaDoc