Skip to content

Commit

Permalink
Use new timer API.
Browse files Browse the repository at this point in the history
  • Loading branch information
js-labs committed Mar 18, 2019
1 parent 5ea9326 commit 6c8102a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/jsl/wfwt/AudioRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private AudioRecorder(
m_thread = new Thread( this, LOG_TAG + " [" + audioFormat + "]" );
m_byteBufferCache = new RetainableByteBufferCache(
/* Take a buffer large enough for 4 audio frame messages */
true, 4*Protocol.AudioFrame.getMessageSize(frameSize), 16 );
true, 4*Protocol.AudioFrame.getMessageSize(frameSize), Protocol.BYTE_ORDER, 16);
m_lock = new ReentrantLock();
m_cond = m_lock.newCondition();
m_state = IDLE;
Expand Down Expand Up @@ -136,7 +136,8 @@ else if (m_state == STOP)
Log.i( LOG_TAG, "Replayed " + replayedFrames + " frames." );
}

Log.i( LOG_TAG, "Sent " + frames + " frames." );
Log.i( LOG_TAG, "Sent " + frames + " frames, " + (frames*m_frameSize) + " bytes" );
frames = 0;
continue;
}
else if (m_state == SHTDN)
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/org/jsl/wfwt/ChannelSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,19 @@ private String getLogPrefix()
return m_channel.getName() + " " + m_session.getRemoteAddress() + ": ";
}

private class TimerHandler implements Runnable
private class TimerHandler implements TimerQueue.Task
{
public void run()
private long m_interval;

TimerHandler(long interval)
{
m_interval = interval;
}

public long run()
{
handlePingTimeout();
return m_interval;
}
}

Expand Down Expand Up @@ -153,15 +161,15 @@ protected int validateHeader( ByteBuffer header )
};
}

public ChannelSession(
ChannelSession(
Channel channel,
String serviceName,
Session session,
StreamDefragger streamDefragger,
SessionManager sessionManager,
AudioPlayer audioPlayer,
TimerQueue timerQueue,
int pingInterval )
int pingInterval)
{
m_channel = channel;
m_serviceName = serviceName;
Expand All @@ -173,9 +181,8 @@ public ChannelSession(

if (pingInterval > 0)
{
m_timerHandler = new TimerHandler();
m_timerQueue.scheduleAtFixedRate(
m_timerHandler, pingInterval, pingInterval, TimeUnit.SECONDS );
m_timerHandler = new TimerHandler(TimeUnit.SECONDS.toMillis(pingInterval));
m_timerQueue.schedule(m_timerHandler, pingInterval, TimeUnit.SECONDS);
}

m_sessionManager.addSession( this );
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/jsl/wfwt/HandshakeClientSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ public class HandshakeClientSession implements Session.Listener
private final StreamDefragger m_streamDefragger;
private final TimerQueue m_timerQueue;
private final int m_pingInterval;
private Runnable m_timerHandler;
private TimerHandler m_timerHandler;

private class TimerHandler implements Runnable
private class TimerHandler implements TimerQueue.Task
{
public void run()
public long run()
{
Log.i( LOG_TAG, getLogPrefix() + "session timeout, close connection." );
Log.i(LOG_TAG, getLogPrefix() + "session timeout, close connection.");
m_session.closeConnection();
return 0;
}
}

Expand Down Expand Up @@ -77,7 +78,7 @@ public HandshakeClientSession(
if (pingInterval > 0)
{
m_timerHandler = new TimerHandler();
timerQueue.schedule( m_timerHandler, pingInterval, TimeUnit.SECONDS );
timerQueue.schedule(m_timerHandler, pingInterval, TimeUnit.SECONDS);
}

try
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/jsl/wfwt/HandshakeServerSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ public class HandshakeServerSession implements Session.Listener
private final int m_pingInterval;
private TimerHandler m_timerHandler;

private class TimerHandler implements Runnable
private class TimerHandler implements TimerQueue.Task
{
public void run()
public long run()
{
Log.i( LOG_TAG, getLogPrefix() + "session timeout, close connection." );
Log.i(LOG_TAG, getLogPrefix() + "session timeout, close connection.");
m_session.closeConnection();
return 0;
}
}

Expand All @@ -53,14 +54,14 @@ private String getLogPrefix()
return m_channel.getName() + " " + m_session.getRemoteAddress() + ": ";
}

public HandshakeServerSession(
HandshakeServerSession(
String audioFormat,
String stationName,
Channel channel,
Session session,
SessionManager sessionManager,
TimerQueue timerQueue,
int pingInterval )
int pingInterval)
{
m_audioFormat = audioFormat;
m_stationName = stationName;
Expand All @@ -70,14 +71,13 @@ public HandshakeServerSession(
m_sessionManager = sessionManager;
m_timerQueue = timerQueue;
m_pingInterval = pingInterval;

if (pingInterval > 0)
{
m_timerHandler = new TimerHandler();
m_timerQueue.schedule( m_timerHandler, pingInterval, TimeUnit.SECONDS );
m_timerQueue.schedule(m_timerHandler, pingInterval, TimeUnit.SECONDS);
}

Log.i( LOG_TAG, getLogPrefix() + "connection accepted" );
Log.i(LOG_TAG, getLogPrefix() + "connection accepted");
}

public void onDataReceived( RetainableByteBuffer data )
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jsl/wfwt/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.jsl.collider.RetainableByteBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
Expand All @@ -38,6 +39,7 @@ public class Protocol
private static final short MSG_STATION_NAME = 0x0007;

public static final byte VERSION = 1;
public static final ByteOrder BYTE_ORDER = ByteOrder.BIG_ENDIAN;

public static class Message
{
Expand Down Expand Up @@ -369,6 +371,5 @@ public static String getStationName( RetainableByteBuffer msg ) throws Character
}
return ret;
}

}
}

0 comments on commit 6c8102a

Please sign in to comment.