1
0
mirror of http://galexander.org/git/simplesshd.git synced 2024-12-28 17:08:08 +00:00

add locking to resolve some thread safety issues

This commit is contained in:
Greg Alexander 2014-12-17 20:54:40 -05:00
parent 5c7d92415c
commit 637de21aca
3 changed files with 48 additions and 23 deletions

View File

@ -19,6 +19,7 @@ import java.io.BufferedReader;
public class SimpleSSHD extends Activity
{
private static final Object lock = new Object();
private EditText log_view;
private Button startstop_view;
public static SimpleSSHD curr = null;
@ -34,14 +35,18 @@ public class SimpleSSHD extends Activity
public void onResume() {
super.onResume();
curr = this;
update_startstop();
synchronized (lock) {
curr = this;
}
update_startstop_prime();
updater = new UpdaterThread();
updater.start();
}
public void onPause() {
curr = null;
synchronized (lock) {
curr = null;
}
updater.interrupt();
super.onPause();
}
@ -66,7 +71,7 @@ public class SimpleSSHD extends Activity
}
}
public void update_startstop() {
private void update_startstop_prime() {
if (SimpleSSHDService.is_started()) {
startstop_view.setText("STOP");
startstop_view.setTextColor(0xFF881111);
@ -76,6 +81,23 @@ public class SimpleSSHD extends Activity
}
}
public static void update_startstop() {
Thread t = new Thread() {
public void run() {
synchronized (lock) {
if (curr != null) {
curr.update_startstop_prime();
}
}
}
};
synchronized (lock) {
if (curr != null) {
curr.runOnUiThread(t);
}
}
}
public void startstop_clicked(View v) {
Intent i = new Intent(this, SimpleSSHDService.class);
if (SimpleSSHDService.is_started()) {
@ -84,7 +106,7 @@ public class SimpleSSHD extends Activity
startService(i);
}
public void update_log() {
private void update_log_prime() {
String[] lines = new String[50];
int curr_line = 0;
boolean wrapped = false;
@ -118,4 +140,21 @@ public class SimpleSSHD extends Activity
log_view.setText(output);
log_view.setSelection(output.length());
}
public static void update_log() {
Thread t = new Thread() {
public void run() {
synchronized (lock) {
if (curr != null) {
curr.update_log_prime();
}
}
}
};
synchronized (lock) {
if (curr != null) {
curr.runOnUiThread(t);
}
}
}
}

View File

@ -34,7 +34,7 @@ public class SimpleSSHDService extends Service {
return START_STICKY;
} else {
stop_sshd();
update_activity();
SimpleSSHD.update_startstop();
stopSelf();
/* XXX - need stopForeground() too ? */
return START_NOT_STICKY;
@ -75,21 +75,11 @@ public class SimpleSSHDService extends Service {
if (sshd_pid == pid) {
sshd_pid = 0;
}
update_activity();
SimpleSSHD.update_startstop();
}
}).start();
}
update_activity();
}
private static void update_activity() {
if (SimpleSSHD.curr != null) {
SimpleSSHD.curr.runOnUiThread(new Runnable() {
public void run() {
SimpleSSHD.curr.update_startstop();
}
});
}
SimpleSSHD.update_startstop();
}
private static void read_pidfile() {

View File

@ -18,11 +18,7 @@ public class UpdaterThread extends Thread {
long mod = f.lastModified();
long len = f.length();
if ((mod != lastmod) || (len != lastlen)) {
SimpleSSHD.curr.runOnUiThread(new Thread() {
public void run() {
SimpleSSHD.curr.update_log();
}
});
SimpleSSHD.update_log();
lastmod = mod;
lastlen = len;
}