public class Port extends Loader {
- private final InetAddress bindTo;
- private final int port;
- private final Thread listener;
+ final InetAddress bindTo;
+ final int port;
- public Port(String path, InetAddress bindTo, int port) {
+ private static Hash cache = new Hash();
+ public static Port newPort(String path, InetAddress bindTo, int port) throws IOException {
+ String canonical = new File(path).getCanonicalPath();
+ Port p = (Port)cache.get(canonical);
+ if (p == null) cache.put(canonical, p = new Port(path, bindTo, port));
+ else Log.warn(Port.class, " sharing " + bindTo+":"+port+" -> "+ (p.bindTo+":"+p.port));
+ p.spawn(bindTo, port);
+ return p;
+ }
+
+ void spawn(InetAddress bindTo, int port) { new PortThread(bindTo, port).start(); }
+ private Port(String path, InetAddress bindTo, int port) {
super(path);
this.bindTo = bindTo;
this.port = port;
- this.listener = new PortThread();
- listener.start();
}
public void changed(Watched w) {
if (c == null) return false;
if (!(Listener.class.isAssignableFrom(c) && c != Listener.class)) return false;
Log.info(this, "dispatching connection on port " + port + " to " + c.getName());
- new Thread() { public void run() {
+ new Thread(tg, new Runnable() { public void run() {
Log.clearnotes();
Thread.currentThread().setContextClassLoader(cl);
try {
} finally {
conn.close();
}
- } }.start();
+ } }).start();
return true;
} catch (Exception e) { Log.error(this, e); }
return false;
}
private class PortThread extends Thread {
+ InetAddress bindTo;
+ int port;
+ public PortThread(InetAddress bindTo, int port) { this.bindTo = bindTo; this.port = port; }
public void run() {
try {
Log.warn(this, "Now listening on address " + (bindTo == null ? "all interfaces" : bindTo.toString()) +