Stubs for I2CP connections over Unix domain sockets

This commit is contained in:
str4d
2014-07-15 12:54:22 +00:00
parent f364a83f4f
commit 0c7a3a3a39
6 changed files with 125 additions and 15 deletions

View File

@ -64,7 +64,9 @@ class ClientListenerRunner implements Runnable {
return new ServerSocket(_port, 0, InetAddress.getByName(listenInterface));
}
}
public void run() { runServer(); }
/**
* Start up the socket listener, listens for connections, and
* fires those connections off via {@link #runConnection runConnection}.
@ -72,7 +74,7 @@ class ClientListenerRunner implements Runnable {
* failure.
*
*/
public void runServer() {
protected void runServer() {
_running = true;
int curDelay = 1000;
while (_running) {
@ -173,5 +175,4 @@ class ClientListenerRunner implements Runnable {
_socket = null;
} catch (IOException ioe) {}
}
public void run() { runServer(); }
}

View File

@ -10,8 +10,10 @@ package net.i2p.router.client;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
@ -37,6 +39,7 @@ import net.i2p.router.JobImpl;
import net.i2p.router.RouterContext;
import net.i2p.util.I2PThread;
import net.i2p.util.Log;
import net.i2p.util.SystemVersion;
/**
* Coordinate connections and various tasks
@ -45,7 +48,7 @@ import net.i2p.util.Log;
*/
class ClientManager {
private final Log _log;
protected ClientListenerRunner _listener;
protected List<ClientListenerRunner> _listeners;
// Destination --> ClientConnectionRunner
// Locked for adds/removes but not lookups
private final Map<Destination, ClientConnectionRunner> _runners;
@ -87,6 +90,7 @@ class ClientManager {
// "How large are messages received by the client?",
// "ClientMessages",
// new long[] { 60*1000l, 60*60*1000l, 24*60*60*1000l });
_listeners = new ArrayList<ClientListenerRunner>();
_runners = new ConcurrentHashMap<Destination, ClientConnectionRunner>();
_runnersByHash = new ConcurrentHashMap<Hash, ClientConnectionRunner>();
_pendingRunners = new HashSet<ClientConnectionRunner>();
@ -105,14 +109,22 @@ class ClientManager {
/** Todo: Start a 3rd listener for IPV6? */
protected void startListeners() {
ClientListenerRunner listener;
if (SystemVersion.isAndroid()) {
listener = new DomainClientListenerRunner(_ctx, this);
Thread t = new I2PThread(listener, "DomainClientListener", true);
t.start();
_listeners.add(listener);
}
if (!_ctx.getBooleanProperty(PROP_DISABLE_EXTERNAL)) {
// there's no option to start both an SSL and non-SSL listener
if (_ctx.getBooleanProperty(PROP_ENABLE_SSL))
_listener = new SSLClientListenerRunner(_ctx, this, _port);
listener = new SSLClientListenerRunner(_ctx, this, _port);
else
_listener = new ClientListenerRunner(_ctx, this, _port);
Thread t = new I2PThread(_listener, "ClientListener:" + _port, true);
listener = new ClientListenerRunner(_ctx, this, _port);
Thread t = new I2PThread(listener, "ClientListener:" + _port, true);
t.start();
_listeners.add(listener);
}
_isStarted = true;
}
@ -132,8 +144,9 @@ class ClientManager {
public synchronized void shutdown(String msg) {
_isStarted = false;
_log.info("Shutting down the ClientManager");
if (_listener != null)
_listener.stopListening();
for (ClientListenerRunner listener : _listeners)
listener.stopListening();
_listeners.clear();
Set<ClientConnectionRunner> runners = new HashSet<ClientConnectionRunner>();
synchronized (_runners) {
for (ClientConnectionRunner runner : _runners.values()) {
@ -169,8 +182,13 @@ class ClientManager {
return hisQueue;
}
public boolean isAlive() {
return _isStarted && (_listener == null || _listener.isListening());
public synchronized boolean isAlive() {
boolean listening = true;
if (!_listeners.isEmpty()) {
for (ClientListenerRunner listener : _listeners)
listening = listening && listener.isListening();
}
return _isStarted && (_listeners.isEmpty() || listening);
}
public void registerConnection(ClientConnectionRunner runner) {

View File

@ -0,0 +1,29 @@
package net.i2p.router.client;
import java.io.IOException;
import java.net.ServerSocket;
import net.i2p.client.DomainSocketFactory;
import net.i2p.router.RouterContext;
/**
* Unix domain socket version of ClientListenerRunner.
* This is a stub that does nothing.
* This class is replaced in the Android build.
*
* @since 0.9.15
*/
public class DomainClientListenerRunner extends ClientListenerRunner {
public DomainClientListenerRunner(RouterContext context, ClientManager manager) {
super(context, manager, -1);
}
/**
* @throws IOException
*/
@Override
protected ServerSocket getServerSocket() throws IOException {
final DomainSocketFactory fact = new DomainSocketFactory(_context);
return fact.createServerSocket(DomainSocketFactory.I2CP_SOCKET_ADDRESS);
}
}

View File

@ -181,7 +181,7 @@ class SSLClientListenerRunner extends ClientListenerRunner {
* Create (if necessary) and load the key store, then run.
*/
@Override
public void runServer() {
protected void runServer() {
File keyStore = new File(_context.getConfigDir(), "keystore/i2cp.ks");
if (verifyKeyStore(keyStore) && initializeFactory(keyStore)) {
super.runServer();