|
|
@ -8,20 +8,20 @@ import android.util.Log; |
|
|
|
public class DaemonSingleton { |
|
|
|
public class DaemonSingleton { |
|
|
|
private static final String TAG="i2pd"; |
|
|
|
private static final String TAG="i2pd"; |
|
|
|
private static final DaemonSingleton instance = new DaemonSingleton(); |
|
|
|
private static final DaemonSingleton instance = new DaemonSingleton(); |
|
|
|
public static interface StateChangeListener { void daemonStateChanged(); } |
|
|
|
public static interface StateUpdateListener { void daemonStateUpdate(); } |
|
|
|
private final Set<StateChangeListener> stateChangeListeners = new HashSet<StateChangeListener>(); |
|
|
|
private final Set<StateUpdateListener> stateUpdateListeners = new HashSet<StateUpdateListener>(); |
|
|
|
|
|
|
|
|
|
|
|
public static DaemonSingleton getInstance() { |
|
|
|
public static DaemonSingleton getInstance() { |
|
|
|
return instance; |
|
|
|
return instance; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public synchronized void addStateChangeListener(StateChangeListener listener) { stateChangeListeners.add(listener); } |
|
|
|
public synchronized void addStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.add(listener); } |
|
|
|
public synchronized void removeStateChangeListener(StateChangeListener listener) { stateChangeListeners.remove(listener); } |
|
|
|
public synchronized void removeStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.remove(listener); } |
|
|
|
|
|
|
|
|
|
|
|
public synchronized void stopAcceptingTunnels() { |
|
|
|
public synchronized void stopAcceptingTunnels() { |
|
|
|
if(isStartedOkay()){ |
|
|
|
if(isStartedOkay()){ |
|
|
|
state=State.gracefulShutdownInProgress; |
|
|
|
state=State.gracefulShutdownInProgress; |
|
|
|
fireStateChange(); |
|
|
|
fireStateUpdate(); |
|
|
|
I2PD_JNI.stopAcceptingTunnels(); |
|
|
|
I2PD_JNI.stopAcceptingTunnels(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -32,61 +32,63 @@ public class DaemonSingleton { |
|
|
|
|
|
|
|
|
|
|
|
private boolean startedOkay; |
|
|
|
private boolean startedOkay; |
|
|
|
|
|
|
|
|
|
|
|
public static enum State {starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress}; |
|
|
|
public static enum State {uninitialized,starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress}; |
|
|
|
|
|
|
|
|
|
|
|
private State state = State.starting; |
|
|
|
private State state = State.uninitialized; |
|
|
|
|
|
|
|
|
|
|
|
public State getState() { return state; } |
|
|
|
public State getState() { return state; } |
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
public synchronized void start() { |
|
|
|
synchronized(this){ |
|
|
|
if(state != State.uninitialized)return; |
|
|
|
fireStateChange(); |
|
|
|
state = State.starting; |
|
|
|
new Thread(new Runnable(){ |
|
|
|
fireStateUpdate(); |
|
|
|
|
|
|
|
new Thread(new Runnable(){ |
|
|
|
@Override |
|
|
|
|
|
|
|
public void run() { |
|
|
|
@Override |
|
|
|
try { |
|
|
|
public void run() { |
|
|
|
I2PD_JNI.loadLibraries(); |
|
|
|
try { |
|
|
|
synchronized (DaemonSingleton.this) { |
|
|
|
I2PD_JNI.loadLibraries(); |
|
|
|
state = State.jniLibraryLoaded; |
|
|
|
synchronized (DaemonSingleton.this) { |
|
|
|
fireStateChange(); |
|
|
|
state = State.jniLibraryLoaded; |
|
|
|
} |
|
|
|
fireStateUpdate(); |
|
|
|
} catch (Throwable tr) { |
|
|
|
|
|
|
|
lastThrowable=tr; |
|
|
|
|
|
|
|
synchronized (DaemonSingleton.this) { |
|
|
|
|
|
|
|
state = State.startFailed; |
|
|
|
|
|
|
|
fireStateChange(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
} catch (Throwable tr) { |
|
|
|
synchronized (DaemonSingleton.this) { |
|
|
|
lastThrowable=tr; |
|
|
|
daemonStartResult = I2PD_JNI.startDaemon(); |
|
|
|
synchronized (DaemonSingleton.this) { |
|
|
|
if("ok".equals(daemonStartResult)){state=State.startedOkay;setStartedOkay(true);} |
|
|
|
state = State.startFailed; |
|
|
|
else state=State.startFailed; |
|
|
|
fireStateUpdate(); |
|
|
|
fireStateChange(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (Throwable tr) { |
|
|
|
|
|
|
|
lastThrowable=tr; |
|
|
|
|
|
|
|
synchronized (DaemonSingleton.this) { |
|
|
|
|
|
|
|
state = State.startFailed; |
|
|
|
|
|
|
|
fireStateChange(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
synchronized (DaemonSingleton.this) { |
|
|
|
|
|
|
|
daemonStartResult = I2PD_JNI.startDaemon(); |
|
|
|
|
|
|
|
if("ok".equals(daemonStartResult)){ |
|
|
|
|
|
|
|
state=State.startedOkay; |
|
|
|
|
|
|
|
setStartedOkay(true); |
|
|
|
|
|
|
|
}else state=State.startFailed; |
|
|
|
|
|
|
|
fireStateUpdate(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (Throwable tr) { |
|
|
|
|
|
|
|
lastThrowable=tr; |
|
|
|
|
|
|
|
synchronized (DaemonSingleton.this) { |
|
|
|
|
|
|
|
state = State.startFailed; |
|
|
|
|
|
|
|
fireStateUpdate(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}, "i2pdDaemonStart").start(); |
|
|
|
}, "i2pdDaemonStart").start(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
private Throwable lastThrowable; |
|
|
|
private Throwable lastThrowable; |
|
|
|
private String daemonStartResult="N/A"; |
|
|
|
private String daemonStartResult="N/A"; |
|
|
|
|
|
|
|
|
|
|
|
private synchronized void fireStateChange() { |
|
|
|
private synchronized void fireStateUpdate() { |
|
|
|
Log.i(TAG, "daemon state change: "+state); |
|
|
|
Log.i(TAG, "daemon state change: "+state); |
|
|
|
for(StateChangeListener listener : stateChangeListeners) { |
|
|
|
for(StateUpdateListener listener : stateUpdateListeners) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
listener.daemonStateChanged(); |
|
|
|
listener.daemonStateUpdate(); |
|
|
|
} catch (Throwable tr) { |
|
|
|
} catch (Throwable tr) { |
|
|
|
Log.e(TAG, "exception in listener ignored", tr); |
|
|
|
Log.e(TAG, "exception in listener ignored", tr); |
|
|
|
} |
|
|
|
} |
|
|
|