4 import dolda.dolcon.protocol.*;
9 private boolean listening = false;
10 private Dispatcher dispatcher;
13 public Session(String aspec, String username, List<Authenticator> auth) throws AuthException, ProtocolException, InterruptedException {
15 conn = new Connection(aspec);
16 conn.expectVersion(2);
19 } catch(ConnectException e) {
20 throw(new ProtocolException(e));
23 authenticate(username, auth);
25 dispatcher = new Dispatcher();
29 public Session(String aspec, String username, Authenticator... auth) throws AuthException, ProtocolException, InterruptedException {
30 this(aspec, username, Arrays.asList(auth));
33 private void authenticate(String username, List<Authenticator> auth) throws AuthException, ProtocolException, InterruptedException {
37 resp = ResponseException.check(conn.ecmd("lsauth"), 200);
38 List<String> mechs = new LinkedList<String>();
39 for(List<String> mech : resp.lines)
40 mechs.add(mech.get(0).intern());
42 Authenticator au = null;
43 for(Authenticator a : auth) {
44 use = a.handles(mechs);
51 throw(new NoMechException());
52 resp = conn.ecmd("login", use, username);
54 if(resp.code == 200) {
56 } else if((resp.code / 100) == 3) {
57 resp = conn.ecmd(au.step(resp));
58 } else if((resp.code / 100) == 5) {
59 throw(new AuthException(resp.token(0, 0)));
61 throw(new ResponseException(resp, 0));
64 } catch(ClosedException e) {
65 throw(new ProtocolException(e));
69 private HubManager gethm() {
71 hm = new HubManager(this);
76 public synchronized void addHubListener(HubListener hl, boolean addexisting) {
77 gethm().addls(hl, addexisting);
80 public synchronized void removeHubListener(HubListener hl) {
84 public synchronized Collection<Hub> getHubs() throws InterruptedException {
85 return(gethm().gethubs());
93 protected void finalize() {
96 dispatcher.interrupt();
99 void dispatch(Runnable ev) {
100 dispatcher.dispatch(ev);
103 private static class Dispatcher extends Thread {
104 private Queue<Runnable> q = new LinkedList<Runnable>();
106 private Dispatcher() {
110 public void dispatch(Runnable ev) {
122 while((r = q.poll()) == null)
126 } catch(Throwable t) {