X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Fjava%2Fdolda%2Fdolcon%2FSession.java;h=b2ff07b81b6036284b45a9e9010bf801104180b0;hb=refs%2Fheads%2Fjava;hp=7f3cece028fe5bfba97dafbbd327403a34caa81c;hpb=92719bd69eccc35cf4aec15d0fc98049cfd950cc;p=doldaconnect.git diff --git a/lib/java/dolda/dolcon/Session.java b/lib/java/dolda/dolcon/Session.java index 7f3cece..b2ff07b 100644 --- a/lib/java/dolda/dolcon/Session.java +++ b/lib/java/dolda/dolcon/Session.java @@ -4,9 +4,14 @@ import java.util.*; import dolda.dolcon.protocol.*; public class Session { - private Connection conn; + Connection conn; + private String state; + private boolean listening = false; + private Dispatcher dispatcher; + HubManager hm = null; public Session(String aspec, String username, List auth) throws AuthException, ProtocolException, InterruptedException { + state = "connecting"; conn = new Connection(aspec); conn.expectVersion(2); try { @@ -14,7 +19,11 @@ public class Session { } catch(ConnectException e) { throw(new ProtocolException(e)); } + state = "auth"; authenticate(username, auth); + state = ""; + dispatcher = new Dispatcher(); + dispatcher.start(); } public Session(String aspec, String username, Authenticator... auth) throws AuthException, ProtocolException, InterruptedException { @@ -32,7 +41,6 @@ public class Session { String use = null; Authenticator au = null; for(Authenticator a : auth) { - System.out.println(a); use = a.handles(mechs); if(use != null) { au = a; @@ -58,7 +66,67 @@ public class Session { } } + private HubManager gethm() { + if(hm == null) { + hm = new HubManager(this); + } + return(hm); + } + + public synchronized void addHubListener(HubListener hl, boolean addexisting) { + gethm().addls(hl, addexisting); + } + + public synchronized void removeHubListener(HubListener hl) { + gethm().rmls(hl); + } + + public synchronized Collection getHubs() throws InterruptedException { + return(gethm().gethubs()); + } + public void close() { conn.close(); + state = "closed"; + } + + protected void finalize() { + if(state != "closed") + close(); + dispatcher.interrupt(); + } + + void dispatch(Runnable ev) { + dispatcher.dispatch(ev); + } + + private static class Dispatcher extends Thread { + private Queue q = new LinkedList(); + + private Dispatcher() { + setDaemon(true); + } + + public void dispatch(Runnable ev) { + synchronized(q) { + q.offer(ev); + q.notifyAll(); + } + } + + public void run() { + while(true) { + try { + Runnable r; + synchronized(q) { + while((r = q.poll()) == null) + q.wait(); + } + r.run(); + } catch(Throwable t) { + t.printStackTrace(); + } + } + } } }