mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-05-05 23:17:01 +02:00
parent
b8fd9ba83f
commit
626ed720a6
12 changed files with 57 additions and 208 deletions
|
@ -14,10 +14,10 @@ public class DaemonSingleton {
|
|||
public static DaemonSingleton getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
public synchronized void addStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.add(listener); }
|
||||
public synchronized void removeStateChangeListener(StateUpdateListener listener) { stateUpdateListeners.remove(listener); }
|
||||
|
||||
|
||||
public synchronized void stopAcceptingTunnels() {
|
||||
if(isStartedOkay()){
|
||||
state=State.gracefulShutdownInProgress;
|
||||
|
@ -25,21 +25,20 @@ public class DaemonSingleton {
|
|||
I2PD_JNI.stopAcceptingTunnels();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void onNetworkStateChange(boolean isConnected) {
|
||||
I2PD_JNI.onNetworkStateChanged(isConnected);
|
||||
}
|
||||
|
||||
|
||||
private boolean startedOkay;
|
||||
|
||||
public static enum State {uninitialized,starting,jniLibraryLoaded,startedOkay,startFailed,gracefulShutdownInProgress};
|
||||
|
||||
private State state = State.uninitialized;
|
||||
|
||||
public State getState() { return state; }
|
||||
|
||||
public synchronized void start(final String confDir, final String dataDir) {
|
||||
|
||||
private State state = State.uninitialized;
|
||||
|
||||
public State getState() { return state; }
|
||||
|
||||
public synchronized void start() {
|
||||
if(state != State.uninitialized)return;
|
||||
state = State.starting;
|
||||
fireStateUpdate();
|
||||
|
@ -63,15 +62,7 @@ public class DaemonSingleton {
|
|||
}
|
||||
try {
|
||||
synchronized (DaemonSingleton.this) {
|
||||
|
||||
String args[] = new String[] {
|
||||
"i2pd", "--service", "--daemon",
|
||||
"--datadir=" + dataDir,
|
||||
"--conf=" + confDir + "/i2pd.conf",
|
||||
"--tunconf=" + confDir + "/tunnels.conf"
|
||||
};
|
||||
|
||||
daemonStartResult = I2PD_JNI.startDaemon(args);
|
||||
daemonStartResult = I2PD_JNI.startDaemon();
|
||||
if("ok".equals(daemonStartResult)){
|
||||
state=State.startedOkay;
|
||||
setStartedOkay(true);
|
||||
|
@ -85,9 +76,9 @@ public class DaemonSingleton {
|
|||
fireStateUpdate();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}, "i2pdDaemonStart").start();
|
||||
}
|
||||
private Throwable lastThrowable;
|
||||
|
@ -96,10 +87,10 @@ public class DaemonSingleton {
|
|||
private synchronized void fireStateUpdate() {
|
||||
Log.i(TAG, "daemon state change: "+state);
|
||||
for(StateUpdateListener listener : stateUpdateListeners) {
|
||||
try {
|
||||
listener.daemonStateUpdate();
|
||||
} catch (Throwable tr) {
|
||||
Log.e(TAG, "exception in listener ignored", tr);
|
||||
try {
|
||||
listener.daemonStateUpdate();
|
||||
} catch (Throwable tr) {
|
||||
Log.e(TAG, "exception in listener ignored", tr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -111,7 +102,7 @@ public class DaemonSingleton {
|
|||
public String getDaemonStartResult() {
|
||||
return daemonStartResult;
|
||||
}
|
||||
|
||||
|
||||
private final Object startedOkayLock = new Object();
|
||||
|
||||
public boolean isStartedOkay() {
|
||||
|
|
|
@ -1,83 +0,0 @@
|
|||
package org.purplei2p.i2pd;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
public class Decompress {
|
||||
private static final int BUFFER_SIZE = 1024 * 10;
|
||||
private static final String TAG = "Decompress";
|
||||
|
||||
public static void unzipFromAssets(Context context, String zipFile, String destination) {
|
||||
try {
|
||||
if (destination == null || destination.length() == 0)
|
||||
destination = context.getFilesDir().getAbsolutePath();
|
||||
InputStream stream = context.getAssets().open(zipFile);
|
||||
unzip(stream, destination);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void unzip(String zipFile, String location) {
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(zipFile);
|
||||
unzip(fin, location);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void unzip(InputStream stream, String destination) {
|
||||
dirChecker(destination, "");
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
try {
|
||||
ZipInputStream zin = new ZipInputStream(stream);
|
||||
ZipEntry ze = null;
|
||||
|
||||
while ((ze = zin.getNextEntry()) != null) {
|
||||
Log.v(TAG, "Unzipping " + ze.getName());
|
||||
|
||||
if (ze.isDirectory()) {
|
||||
dirChecker(destination, ze.getName());
|
||||
} else {
|
||||
File f = new File(destination + ze.getName());
|
||||
if (!f.exists()) {
|
||||
FileOutputStream fout = new FileOutputStream(destination + ze.getName());
|
||||
int count;
|
||||
while ((count = zin.read(buffer)) != -1) {
|
||||
fout.write(buffer, 0, count);
|
||||
}
|
||||
zin.closeEntry();
|
||||
fout.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
zin.close();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "unzip", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void dirChecker(String destination, String dir) {
|
||||
File f = new File(destination + dir);
|
||||
|
||||
if (!f.isDirectory()) {
|
||||
boolean success = f.mkdirs();
|
||||
if (!success) {
|
||||
Log.w(TAG, "Failed to create folder " + f.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,8 +5,6 @@ import android.app.NotificationManager;
|
|||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import android.os.Binder;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
@ -30,20 +28,13 @@ public class ForegroundService extends Service {
|
|||
}
|
||||
}
|
||||
|
||||
private String dataDir;
|
||||
private String confDir;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||
dataDir = this.getDir("data", Context.MODE_PRIVATE).toString();
|
||||
confDir = Environment.getExternalStoragePublicDirectory("i2pd").toString();
|
||||
|
||||
// Display a notification about us starting. We put an icon in the status bar.
|
||||
showNotification();
|
||||
|
||||
Log.i("ForegroundService", "About to start daemon with dataDir: " + dataDir + ", confDir: " + confDir);
|
||||
daemon.start(confDir, dataDir);
|
||||
daemon.start();
|
||||
// Tell the user we started.
|
||||
Toast.makeText(this, R.string.i2pd_service_started, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
@ -51,7 +42,7 @@ public class ForegroundService extends Service {
|
|||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.i("ForegroundService", "Received start id " + startId + ": " + intent);
|
||||
daemon.start(confDir, dataDir);
|
||||
daemon.start();
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
|
@ -59,7 +50,7 @@ public class ForegroundService extends Service {
|
|||
public void onDestroy() {
|
||||
// Cancel the persistent notification.
|
||||
notificationManager.cancel(NOTIFICATION);
|
||||
|
||||
|
||||
stopForeground(true);
|
||||
|
||||
// Tell the user we stopped.
|
||||
|
@ -100,7 +91,7 @@ public class ForegroundService extends Service {
|
|||
//mNM.notify(NOTIFICATION, notification);
|
||||
startForeground(NOTIFICATION, notification);
|
||||
}
|
||||
|
||||
|
||||
private final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
||||
}
|
||||
|
||||
|
|
|
@ -2,13 +2,6 @@ package org.purplei2p.i2pd;
|
|||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
|
@ -31,12 +24,12 @@ public class I2PD extends Activity {
|
|||
private static final String TAG = "i2pd";
|
||||
|
||||
private TextView textView;
|
||||
|
||||
|
||||
private final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
||||
|
||||
private DaemonSingleton.StateUpdateListener daemonStateUpdatedListener =
|
||||
new DaemonSingleton.StateUpdateListener() {
|
||||
|
||||
|
||||
@Override
|
||||
public void daemonStateUpdate() {
|
||||
runOnUiThread(new Runnable(){
|
||||
|
@ -60,14 +53,11 @@ public class I2PD extends Activity {
|
|||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
//install certs every time
|
||||
Decompress.unzipFromAssets(this, "certificates.zip", this.getDir("data", Context.MODE_PRIVATE).toString() + "/" );
|
||||
|
||||
textView = new TextView(this);
|
||||
setContentView(textView);
|
||||
DaemonSingleton.getInstance().addStateChangeListener(daemonStateUpdatedListener);
|
||||
|
@ -133,7 +123,7 @@ public class I2PD extends Activity {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
private boolean mIsBound;
|
||||
|
||||
private void doBindService() {
|
||||
|
@ -157,7 +147,7 @@ public class I2PD extends Activity {
|
|||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
getMenuInflater().inflate(R.menu.options_main, menu);
|
||||
getMenuInflater().inflate(R.menu.options_main, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -226,9 +216,9 @@ public class I2PD extends Activity {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
quit();
|
||||
quit();
|
||||
}
|
||||
|
||||
|
||||
}, 10*60*1000/*milliseconds*/);
|
||||
}else{
|
||||
quit();
|
||||
|
@ -237,7 +227,7 @@ public class I2PD extends Activity {
|
|||
Log.e(TAG,"",tr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
},"gracQuitInit").start();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,12 +6,12 @@ public class I2PD_JNI {
|
|||
* returns error info if failed
|
||||
* returns "ok" if daemon initialized and started okay
|
||||
*/
|
||||
public static native String startDaemon(String args[]);
|
||||
public static native String startDaemon();
|
||||
//should only be called after startDaemon() success
|
||||
public static native void stopDaemon();
|
||||
|
||||
|
||||
public static native void stopAcceptingTunnels();
|
||||
|
||||
|
||||
public static native void onNetworkStateChanged(boolean isConnected);
|
||||
|
||||
public static void loadLibraries() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue