- Cleanups, concurrent

- Basic b32 verification
This commit is contained in:
zzz
2011-07-19 21:17:52 +00:00
parent 91f2206a4f
commit d397eaaa08
5 changed files with 36 additions and 76 deletions

View File

@@ -49,6 +49,7 @@ import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import net.i2p.I2PAppContext;
@@ -70,6 +71,9 @@ import net.i2p.util.EventDispatcherImpl;
import net.i2p.util.Log;
/**
* An I2PTunnel tracks one or more I2PTunnelTasks and one or more I2PSessions.
* Usually one of each.
*
* Todo: Most events are not listened to elsewhere, so error propagation is poor
*/
public class I2PTunnel extends EventDispatcherImpl implements Logging {
@@ -95,7 +99,7 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
private static final String nocli_args[] = { "-nocli", "-die"};
private final List tasks = new ArrayList();
private final List<I2PTunnelTask> tasks = new ArrayList();
private int next_task_id = 1;
private final Set listeners = new CopyOnWriteArraySet();
@@ -212,9 +216,7 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
if (tsk.isOpen()) {
tsk.setId(next_task_id);
next_task_id++;
synchronized (tasks) {
tasks.add(tsk);
}
tasks.add(tsk);
}
}
@@ -1255,10 +1257,8 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
*/
public void runQuit(Logging l) {
purgetasks(l);
synchronized (tasks) {
if (tasks.isEmpty()) {
System.exit(0);
}
if (tasks.isEmpty()) {
System.exit(0);
}
l.log("There are running tasks. Try 'list'.");
notifyEvent("quitResult", "error");
@@ -1274,11 +1274,8 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
*/
public void runList(Logging l) {
purgetasks(l);
synchronized (tasks) {
for (int i = 0; i < tasks.size(); i++) {
I2PTunnelTask t = (I2PTunnelTask) tasks.get(i);
l.log("[" + t.getId() + "] " + t.toString());
}
for (I2PTunnelTask t : tasks) {
l.log("[" + t.getId() + "] " + t.toString());
}
notifyEvent("listDone", "done");
}
@@ -1307,14 +1304,8 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
argindex++;
}
if (args[argindex].equalsIgnoreCase("all")) {
List curTasks = null;
synchronized (tasks) {
curTasks = new LinkedList(tasks);
}
boolean error = false;
for (int i = 0; i < curTasks.size(); i++) {
I2PTunnelTask t = (I2PTunnelTask) curTasks.get(i);
for (I2PTunnelTask t : tasks) {
if (!closetask(t, forced, l)) {
notifyEvent("closeResult", "error");
error = true;
@@ -1436,9 +1427,7 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
boolean closed = false;
_log.debug(getPrefix() + "closetask(): looking for task " + num);
synchronized (tasks) {
for (Iterator it = tasks.iterator(); it.hasNext();) {
I2PTunnelTask t = (I2PTunnelTask) it.next();
for (I2PTunnelTask t : tasks) {
int id = t.getId();
if (_log.shouldLog(Log.DEBUG))
_log.debug(getPrefix() + "closetask(): parsing task " + id + " (" + t.toString() + ")");
@@ -1448,7 +1437,6 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
} else if (id > num) {
break;
}
}
}
return closed;
}
@@ -1476,15 +1464,14 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
*
*/
private void purgetasks(Logging l) {
synchronized (tasks) {
for (Iterator it = tasks.iterator(); it.hasNext();) {
I2PTunnelTask t = (I2PTunnelTask) it.next();
List<I2PTunnelTask> removed = new ArrayList();
for (I2PTunnelTask t : tasks) {
if (!t.isOpen()) {
_log.debug(getPrefix() + "Purging inactive tunnel: [" + t.getId() + "] " + t.toString());
it.remove();
removed.add(t);
}
}
}
tasks.removeAll(removed);
}
/**

View File

@@ -686,11 +686,11 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna
synchronized (sockLock) {
if (sockMgr != null) {
mySockets.retainAll(sockMgr.listSockets());
if (!forced && mySockets.size() != 0) {
l.log("There are still active connections!");
if ((!forced) && (!mySockets.isEmpty())) {
l.log("Not closing, there are still active connections!");
_log.debug("can't close: there are still active connections!");
for (Iterator it = mySockets.iterator(); it.hasNext();) {
l.log("->" + it.next());
for (I2PSocket s : mySockets) {
l.log(" -> " + s.toString());
}
return false;
}
@@ -706,7 +706,8 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna
try {
if (ss != null) ss.close();
} catch (IOException ex) {
ex.printStackTrace();
if (_log.shouldLog(Log.WARN))
_log.warn("error closing", ex);
return false;
}
//l.log("Client closed.");

View File

@@ -13,9 +13,7 @@ import net.i2p.util.EventDispatcherImpl;
* Either a Server or a Client.
*/
public abstract class I2PTunnelTask implements EventDispatcher {
private final EventDispatcherImpl _event = new EventDispatcherImpl();
public abstract class I2PTunnelTask extends EventDispatcherImpl {
private int id;
private String name;
@@ -77,41 +75,4 @@ public abstract class I2PTunnelTask implements EventDispatcher {
public String toString() {
return name;
}
/* Required by the EventDispatcher interface */
public EventDispatcher getEventDispatcher() {
return _event;
}
public void attachEventDispatcher(EventDispatcher e) {
_event.attachEventDispatcher(e.getEventDispatcher());
}
public void detachEventDispatcher(EventDispatcher e) {
_event.detachEventDispatcher(e.getEventDispatcher());
}
public void notifyEvent(String e, Object a) {
_event.notifyEvent(e, a);
}
public Object getEventValue(String n) {
return _event.getEventValue(n);
}
public Set getEvents() {
return _event.getEvents();
}
public void ignoreEvents() {
_event.ignoreEvents();
}
public void unIgnoreEvents() {
_event.unIgnoreEvents();
}
public Object waitEventValue(String n) {
return _event.waitEventValue(n);
}
}
}

View File

@@ -22,7 +22,10 @@ import net.i2p.util.Log;
import net.i2p.util.SecureFileOutputStream;
/**
* Coordinate the runtime operation and configuration of a tunnel.
* Coordinate the runtime operation and configuration of a single I2PTunnel.
* An I2PTunnel tracks one or more I2PTunnelTasks and one or more I2PSessions.
* Usually one of each.
*
* These objects are bundled together under a TunnelControllerGroup where the
* entire group is stored / loaded from a single config file.
*

View File

@@ -5,6 +5,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.i2p.client.streaming.I2PSocketManager;
import net.i2p.data.Base32;
import net.i2p.i2ptunnel.I2PTunnel;
import net.i2p.i2ptunnel.Logging;
import net.i2p.util.EventDispatcher;
@@ -90,6 +91,13 @@ public class DCCClientManager extends EventReceiver {
* @param localPort bind to port or 0; if nonzero it will be the rv
*/
private int newIncoming(String b32, int port, String type, int localPort) {
b32 = b32.toLowerCase();
// do some basic verification before starting the client
if (b32.length() != 60 || !b32.endsWith(".b32.i2p"))
return -1;
byte[] dec = Base32.decode(b32.substring(0, 52));
if (dec == null || dec.length != 32)
return -1;
expireInbound();
if (_incoming.size() >= MAX_INCOMING_PENDING ||
_active.size() >= MAX_INCOMING_PENDING) {