Commit f6987c44 authored by akwizgran's avatar akwizgran

Allow client to specify keepalive size.

parent 88a50641
......@@ -18,6 +18,8 @@ public interface Constants {
String EXTRA_KEEPALIVE_INTERVAL_MS = "org.briarproject.snooze.EXTRA_KEEPALIVE_INTERVAL_MS";
String EXTRA_KEEPALIVE_BYTES = "org.briarproject.snooze.EXTRA_KEEPALIVE_BYTES";
String EXTRA_ALARM_TYPE = "org.briarproject.snooze.EXTRA_ALARM_TYPE";
int SOCKET_TIMEOUT_MS = 60 * 1000;
......
......@@ -20,7 +20,7 @@ class NetworkClient extends Thread {
private static final String LOCK_TAG = NetworkClient.class.getName();
private final String host;
private final int port, keepaliveIntervalMs;
private final int port, keepaliveIntervalMs, keepaliveBytes;
private final Logger log;
private final PowerManager.WakeLock wakeLock;
......@@ -29,10 +29,15 @@ class NetworkClient extends Thread {
private volatile long lastActive;
NetworkClient(String host, int port, int keepaliveIntervalMs, Context appContext) {
NetworkClient(String host, int port, int keepaliveIntervalMs, int keepaliveBytes,
Context appContext) {
if (keepaliveIntervalMs / 1000 > Short.MAX_VALUE)
throw new IllegalArgumentException();
if (keepaliveBytes > Short.MAX_VALUE) throw new IllegalArgumentException();
this.host = host;
this.port = port;
this.keepaliveIntervalMs = keepaliveIntervalMs;
this.keepaliveBytes = keepaliveBytes;
log = ((SnoozeApplication) appContext).getLogger();
PowerManager pm = (PowerManager) appContext.getSystemService(POWER_SERVICE);
wakeLock = pm.newWakeLock(PARTIAL_WAKE_LOCK, LOCK_TAG);
......@@ -78,23 +83,31 @@ class NetworkClient extends Thread {
log.log("Connecting to " + host + ":" + port);
lastActive = System.currentTimeMillis();
s.connect(new InetSocketAddress(host, port), CONNECT_TIMEOUT_MS);
log.log("Requesting keepalives every "
log.log("Requesting " + keepaliveBytes + " bytes every "
+ (keepaliveIntervalMs / 1000) + " seconds");
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeShort(keepaliveIntervalMs / 1000);
out.writeShort(keepaliveBytes);
out.flush();
lastActive = System.currentTimeMillis();
InputStream in = s.getInputStream();
int read = 0;
while (read != -1) {
byte[] buf = new byte[keepaliveBytes];
while (true) {
wakeLock.release();
try {
read = in.read();
int offset = 0;
while (offset < buf.length) {
int read = in.read(buf, offset, buf.length - offset);
if (read == -1) break;
log.log("Read " + read + " bytes");
offset += read;
}
if (offset < buf.length) break;
log.log("Read keepalive");
} finally {
wakeLock.acquire();
}
lastActive = System.currentTimeMillis();
if (read != -1) log.log("Read keepalive");
}
log.log("End of stream");
} catch (IOException e) {
......
......@@ -23,6 +23,7 @@ import static org.briarproject.snooze.Constants.ACTION_STOP;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_INTERVAL_MS;
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_KEEPALIVE_BYTES;
import static org.briarproject.snooze.Constants.EXTRA_KEEPALIVE_INTERVAL_MS;
import static org.briarproject.snooze.Constants.EXTRA_WAKE_LOCK;
import static org.briarproject.snooze.Constants.EXTRA_WIFI_LOCK;
......@@ -31,7 +32,7 @@ import static org.briarproject.snooze.alarm.AlarmType.TIMER;
public class SettingsFragment extends Fragment {
private AlarmType alarmType = TIMER;
private TextView alarmIntervalTextView, keepaliveIntervalTextView;
private TextView alarmIntervalTextView, keepaliveIntervalTextView, keepaliveBytesTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
......@@ -83,6 +84,23 @@ public class SettingsFragment extends Fragment {
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
keepaliveBytesTextView = (TextView) layout.findViewById(R.id.keepaliveBytesTextView);
keepaliveBytesTextView.setText("1");
((SeekBar) layout.findViewById(R.id.keepaliveBytesSeekBar)).setOnSeekBarChangeListener(
new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
keepaliveBytesTextView.setText(String.valueOf(progress + 1));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
......@@ -112,6 +130,8 @@ public class SettingsFragment extends Fragment {
((SeekBar) layout.findViewById(R.id.alarmIntervalSeekBar)).getProgress() + 1;
int keepaliveIntervalMins =
((SeekBar) layout.findViewById(R.id.keepaliveIntervalSeekBar)).getProgress() + 1;
int keepaliveBytes =
((SeekBar) layout.findViewById(R.id.keepaliveBytesSeekBar)).getProgress() + 1;
Intent i = new Intent(getContext(), SnoozeService.class);
i.setAction(ACTION_START);
i.putExtra(EXTRA_FOREGROUND_SERVICE, foreground);
......@@ -119,6 +139,7 @@ public class SettingsFragment extends Fragment {
i.putExtra(EXTRA_WIFI_LOCK, wifiLock);
i.putExtra(EXTRA_ALARM_INTERVAL_MS, alarmIntervalMins * 60 * 1000);
i.putExtra(EXTRA_KEEPALIVE_INTERVAL_MS, keepaliveIntervalMins * 60 * 1000);
i.putExtra(EXTRA_KEEPALIVE_BYTES, keepaliveBytes);
i.putExtra(EXTRA_ALARM_TYPE, alarmType.name());
getContext().startService(i);
}
......
......@@ -26,6 +26,7 @@ import static org.briarproject.snooze.Constants.ACTION_STOP;
import static org.briarproject.snooze.Constants.EXTRA_ALARM_INTERVAL_MS;
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_KEEPALIVE_BYTES;
import static org.briarproject.snooze.Constants.EXTRA_KEEPALIVE_INTERVAL_MS;
import static org.briarproject.snooze.Constants.EXTRA_WAKE_LOCK;
import static org.briarproject.snooze.Constants.EXTRA_WIFI_LOCK;
......@@ -43,7 +44,7 @@ public class SnoozeService extends Service {
private WifiManager.WifiLock wifiLock = null;
private NetworkClient networkClient = null;
private Alarm alarm = null;
private int alarmIntervalMs, keepaliveIntervalMs;
private int alarmIntervalMs, keepaliveIntervalMs, keepaliveBytes;
private volatile Logger log = null;
......@@ -68,6 +69,7 @@ public class SnoozeService extends Service {
boolean newWifiLock = i.getBooleanExtra(EXTRA_WIFI_LOCK, false);
alarmIntervalMs = i.getIntExtra(EXTRA_ALARM_INTERVAL_MS, -1);
keepaliveIntervalMs = i.getIntExtra(EXTRA_KEEPALIVE_INTERVAL_MS, -1);
keepaliveBytes = i.getIntExtra(EXTRA_KEEPALIVE_BYTES, -1);
AlarmType newAlarmType = AlarmType.valueOf(i.getStringExtra(EXTRA_ALARM_TYPE));
log.log("Setting alarm " + newAlarmType);
if (newForeground && !foreground) startForegroundService();
......@@ -171,7 +173,8 @@ public class SnoozeService extends Service {
if (networkClient != null) networkClient.closeConnection();
String host = getString(R.string.host);
int port = getResources().getInteger(R.integer.port);
networkClient = new NetworkClient(host, port, keepaliveIntervalMs, getApplicationContext());
networkClient = new NetworkClient(host, port, keepaliveIntervalMs, keepaliveBytes,
getApplicationContext());
networkClient.start();
}
......
......@@ -98,6 +98,35 @@
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_marginBottom="8dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:text="@string/keepalive_bytes"/>
<SeekBar
android:id="@+id/keepaliveBytesSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:max="99"/>
<TextView
android:id="@+id/keepaliveBytesTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
tools:text="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
......
......@@ -9,6 +9,7 @@
<string name="use_wifi_lock">Wi-Fi lock</string>
<string name="alarm_interval">Alarm interval</string>
<string name="keepalive_interval">Keepalive interval</string>
<string name="keepalive_bytes">Keepalive bytes</string>
<string name="interval">%d min</string>
<string name="alarm_type">Alarm type</string>
<string-array name="alarm_types">
......
......@@ -65,11 +65,13 @@ public class SnoozeServer implements Runnable {
DataInputStream in = new DataInputStream(socket.getInputStream());
OutputStream out = socket.getOutputStream();
int keepaliveIntervalSecs = Math.max(1, in.readUnsignedShort());
LOG.info("Keepalive interval " + keepaliveIntervalSecs + " seconds: " +
socket.getRemoteSocketAddress());
int keepaliveBytes = Math.max(1, in.readUnsignedShort());
LOG.info("Keepalive interval " + keepaliveIntervalSecs + " seconds, size "
+ keepaliveBytes + " bytes: " + socket.getRemoteSocketAddress());
byte[] buf = new byte[keepaliveBytes];
while (!socket.isClosed()) {
LOG.info("Writing: " + socket.getRemoteSocketAddress());
out.write(0);
out.write(buf);
sleep(keepaliveIntervalSecs * 1000);
}
LOG.info("Socket closed: " + socket.getRemoteSocketAddress());
......
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