4 import dolda.dolcon.protocol.*;
6 public class Session implements NotifyListener {
7 private Connection conn;
9 private Set<HubListener> hubls = new HashSet<HubListener>();
10 private boolean listening = false;
11 private String[] hubstate = {"none"};
12 private String[][] states = {hubstate};
13 private Map<Integer, Hub> hubs = new TreeMap<Integer, Hub>();
15 public Session(String aspec, String username, List<Authenticator> auth) throws AuthException, ProtocolException, InterruptedException {
17 conn = new Connection(aspec);
18 conn.expectVersion(2);
21 } catch(ConnectException e) {
22 throw(new ProtocolException(e));
25 authenticate(username, auth);
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 System.out.println(a);
45 use = a.handles(mechs);
52 throw(new NoMechException());
53 resp = conn.ecmd("login", use, username);
55 if(resp.code == 200) {
57 } else if((resp.code / 100) == 3) {
58 resp = conn.ecmd(au.step(resp));
59 } else if((resp.code / 100) == 5) {
60 throw(new AuthException(resp.token(0, 0)));
62 throw(new ResponseException(resp, 0));
65 } catch(ClosedException e) {
66 throw(new ProtocolException(e));
70 private void checkstates() {
71 boolean active = false;
72 for(String[] sp : states) {
78 if(listening && !active)
79 conn.removeNotifyListener(this);
80 else if(!listening && active)
81 conn.addNotifyListener(this);
84 private int atoi(String a) {
85 return(Integer.parseInt(a));
88 private void fetchhubs() {
89 synchronized(hubstate) {
90 if(hubstate[0] != "none")
92 hubstate[0] = "fetch";
94 Command cmd = new Command("lsnodes");
96 public void done(Response r) {
99 for(List<String> line : r.lines) {
100 Hub h = new Hub(atoi(line.get(0)));
101 h.fnet = line.get(1).intern();
102 h.name = line.get(2);
103 h.numpeers = atoi(line.get(3));
104 h.state = new String[] {"syn", "hs", "est", "dead"}[atoi(line.get(4))];
110 public void error(Exception e) {
113 conn.qcmd(new Command("notify fn:act on"), cmd);
116 public void addHubListener(HubListener hl, boolean addexisting) {
118 synchronized(hubls) {
123 public void removeHubListener(HubListener hl) {
124 synchronized(hubls) {
126 if(hubls.isEmpty()) {
128 hubstate[0] = "none";
134 public void notified(Response resp) {
137 public void close() {
142 protected void finalize() {
143 if(state != "closed")