Java: Added a session handler with authentication capability.
[doldaconnect.git] / lib / java / dolda / dolcon / protocol / Connection.java
index 31bb172..54aea90 100644 (file)
@@ -14,13 +14,14 @@ public class Connection {
     private String aspec;
     private String state;
     private Set<ConnectListener> connls = new HashSet<ConnectListener>();
+    private Set<NotifyListener> notls = new HashSet<NotifyListener>();
     private Exception error;
     
     public interface ConnectListener {
        public void connected() throws Exception;
        public void error(Exception cause);
     }
-
+    
     public Connection(String aspec) {
        this.aspec = aspec;
        state = "idle";
@@ -162,6 +163,18 @@ public class Connection {
        return(error);
     }
 
+    public void addNotifyListener(NotifyListener l) {
+       synchronized(notls) {
+           notls.add(l);
+       }
+    }
+
+    public void removeNotifyListener(NotifyListener l) {
+       synchronized(notls) {
+           notls.remove(l);
+       }
+    }
+
     public synchronized void addConnectListener(ConnectListener l) {
        if((state != "idle") && (state != "connecting"))
            throw(new IllegalStateException("Already connected"));
@@ -199,6 +212,7 @@ public class Connection {
                    notifyAll();
                }
            };
+       qcmd(cmd);
        synchronized(l) {
            while(!donep[0]) {
                l.wait();
@@ -271,7 +285,9 @@ public class Connection {
                            out.append(' ');
                        out.append(quote(s));
                    }
+                   out.append("\r\n");
                    w.write(out.toString());
+                   w.flush();
                }
            } catch(IOException e) {
                throw(new StopCondition(e, false));
@@ -299,6 +315,12 @@ public class Connection {
                    queue.notifyAll();
                }
                resp.cmd.done(resp);
+           } else {
+               synchronized(notls) {
+                   for(NotifyListener l : notls) {
+                       l.notified(resp);
+                   }
+               }
            }
        }
 
@@ -359,7 +381,6 @@ public class Connection {
                                code = Integer.parseInt(ct.toString());
                                ct.setLength(0);
                                state = "start";
-                               continue eat;
                            } else {
                                ct.append(c);
                            }