Commit 3436bd97 authored by akwizgran's avatar akwizgran

Allow client to specify keepalive interval.

parent 5986b329
......@@ -21,4 +21,6 @@ interface Constants {
int CONNECT_TIMEOUT_MS = 30 * 1000;
int RECONNECT_DELAY_MS = 10 * 1000;
int KEEPALIVE_INTERVAL_MS = 60 * 1000;
}
......@@ -3,8 +3,10 @@ package org.briarproject.snooze;
import android.content.Context;
import android.os.PowerManager;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
......@@ -14,21 +16,22 @@ import static org.briarproject.snooze.Constants.CONNECT_TIMEOUT_MS;
import static org.briarproject.snooze.Constants.RECONNECT_DELAY_MS;
import static org.briarproject.snooze.Constants.SOCKET_TIMEOUT_MS;
class NetworkThread extends Thread {
class NetworkClient extends Thread {
private static final String TAG = NetworkThread.class.getName();
private static final String LOCK_TAG = NetworkClient.class.getName();
private final String host;
private final int port;
private final int port, keepaliveIntervalSecs;
private final Logger log;
private final PowerManager.WakeLock wakeLock;
NetworkThread(String host, int port, Context appContext) {
NetworkClient(String host, int port, int keepaliveIntervalMs, Context appContext) {
this.host = host;
this.port = port;
keepaliveIntervalSecs = (keepaliveIntervalMs / 1000) & 0xffff;
log = ((SnoozeApplication) appContext).getLogger();
PowerManager pm = (PowerManager) appContext.getSystemService(POWER_SERVICE);
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, TAG);
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, LOCK_TAG);
wakeLock.setReferenceCounted(false);
setDaemon(true);
}
......@@ -42,8 +45,11 @@ class NetworkThread extends Thread {
s.setSoTimeout(SOCKET_TIMEOUT_MS);
log.log("Connecting to " + host + ":" + port);
s.connect(new InetSocketAddress(host, port), CONNECT_TIMEOUT_MS);
DataOutputStream out = new DataOutputStream(s.getOutputStream());
InputStream in = s.getInputStream();
log.log("Connected");
out.writeShort(keepaliveIntervalSecs);
out.flush();
for (int read = 0; read != -1 && !isInterrupted(); read = in.read(buf)) {
wakeLock.acquire();
log.log("Read " + read + " bytes");
......@@ -58,7 +64,7 @@ class NetworkThread extends Thread {
tryToClose(s);
}
} catch (IOException e) {
log.log("Network error: " + e.toString());
log.log("Network error: " + e);
tryToClose(s);
}
try {
......
......@@ -22,10 +22,11 @@ import static org.briarproject.snooze.Constants.EXTRA_ALARM_TYPE;
import static org.briarproject.snooze.Constants.EXTRA_FOREGROUND_SERVICE;
import static org.briarproject.snooze.Constants.EXTRA_WAKE_LOCK;
import static org.briarproject.snooze.Constants.EXTRA_WIFI_LOCK;
import static org.briarproject.snooze.Constants.KEEPALIVE_INTERVAL_MS;
public class SnoozeService extends Service {
private static final String TAG = SnoozeService.class.getName();
private static final String LOCK_TAG = SnoozeService.class.getName();
private final Binder binder = new Binder();
private final PowerManagementEventReceiver receiver = new PowerManagementEventReceiver();
......@@ -33,7 +34,7 @@ public class SnoozeService extends Service {
private boolean foreground = false;
private PowerManager.WakeLock wakeLock = null;
private WifiManager.WifiLock wifiLock = null;
private NetworkThread networkThread = null;
private NetworkClient networkClient = null;
private Alarm alarm = null;
private volatile Logger log = null;
......@@ -123,7 +124,7 @@ public class SnoozeService extends Service {
if (wakeLock != null) throw new IllegalStateException();
log.log("Acquiring wake lock");
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, TAG);
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, LOCK_TAG);
wakeLock.setReferenceCounted(false);
wakeLock.acquire();
}
......@@ -139,7 +140,7 @@ public class SnoozeService extends Service {
if (wifiLock != null) throw new IllegalStateException();
log.log("Acquiring wifi lock");
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
wifiLock = wm.createWifiLock(WIFI_MODE_FULL, TAG);
wifiLock = wm.createWifiLock(WIFI_MODE_FULL, LOCK_TAG);
wifiLock.setReferenceCounted(false);
wifiLock.acquire();
}
......@@ -152,11 +153,12 @@ public class SnoozeService extends Service {
}
private void startNetworkThread() {
if (networkThread != null) networkThread.interrupt();
if (networkClient != null) networkClient.interrupt();
String host = getString(R.string.host);
int port = getResources().getInteger(R.integer.port);
networkThread = new NetworkThread(host, port, getApplicationContext());
networkThread.start();
networkClient = new NetworkClient(host, port, KEEPALIVE_INTERVAL_MS,
getApplicationContext());
networkClient.start();
}
private void setAlarm(AlarmType alarmType) {
......
package org.briarproject.snooze;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
......@@ -7,30 +8,27 @@ import java.net.Socket;
public class SnoozeServer implements Runnable {
private static final int SOCKET_TIMEOUT_MS = 5 * 60 * 1000;
private static final int SOCKET_TIMEOUT_MS = 2 * 60 * 1000;
private final int port, writeIntervalMs;
private final int port;
public SnoozeServer(int port, int writeIntervalMs) {
public SnoozeServer(int port) {
this.port = port;
this.writeIntervalMs = writeIntervalMs;
}
public static void main(String[] args) {
if (args.length != 2) printUsageAndExit();
if (args.length != 1) printUsageAndExit();
try {
int port = Integer.parseInt(args[0]);
if (port < 1 || port > 65535) printUsageAndExit();
int writeIntervalMs = Integer.parseInt(args[1]);
if (writeIntervalMs < 1) printUsageAndExit();
new SnoozeServer(port, writeIntervalMs).run();
new SnoozeServer(port).run();
} catch (NumberFormatException e) {
printUsageAndExit();
}
}
private static void printUsageAndExit() {
System.err.println("Usage: SnoozeServer <port> <write_interval_ms>");
System.err.println("Usage: SnoozeServer <port>");
System.exit(1);
}
......@@ -39,7 +37,7 @@ public class SnoozeServer implements Runnable {
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Listening: " + ss.getLocalSocketAddress());
while (true) new WriterThread(ss.accept(), writeIntervalMs).start();
while (true) new WriterThread(ss.accept()).start();
} catch (IOException e) {
System.err.println(e.toString());
}
......@@ -48,11 +46,9 @@ public class SnoozeServer implements Runnable {
private static class WriterThread extends Thread {
private final Socket socket;
private final int writeIntervalMs;
private WriterThread(Socket socket, int writeIntervalMs) {
private WriterThread(Socket socket) {
this.socket = socket;
this.writeIntervalMs = writeIntervalMs;
setDaemon(true);
}
......@@ -60,11 +56,13 @@ public class SnoozeServer implements Runnable {
public void run() {
try {
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
DataInputStream in = new DataInputStream(socket.getInputStream());
OutputStream out = socket.getOutputStream();
int keepaliveIntervalSecs = in.readUnsignedShort();
while (!socket.isClosed()) {
System.out.println("Writing: " + socket.getRemoteSocketAddress());
out.write(0);
sleep(writeIntervalMs);
sleep(keepaliveIntervalSecs * 1000);
}
System.out.println("Socket closed: " + socket.getRemoteSocketAddress());
} catch (IOException | InterruptedException e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment