]> Pileus Git - ~andy/spades/commitdiff
Improve client modeing
authorAndy Spencer <andy753421@gmail.com>
Tue, 23 Apr 2013 07:20:06 +0000 (07:20 +0000)
committerAndy Spencer <andy753421@gmail.com>
Tue, 23 Apr 2013 07:20:06 +0000 (07:20 +0000)
src/org/pileus/spades/Client.java
src/org/pileus/spades/Main.java
src/org/pileus/spades/Task.java

index f1657840c02924a06864a4b85e9ba885f4b69e72..e34dcc858a7dbc95dfd7b9646e0ccc11074ee9de 100644 (file)
@@ -5,6 +5,14 @@ import java.net.*;
 
 public class Client
 {
+       /* Constnats */
+       enum State {
+               INIT,
+               SETUP,
+               READY,
+               ABORT,
+       };
+
        /* Preference data */
        public  String         server   = "irc.freenode.net";
        public  int            port     = 6667;
@@ -17,7 +25,7 @@ public class Client
        public  String         hostname = "localhost";
 
        /* Public data */
-       public  boolean        ready    = false;
+       public  State          state    = State.INIT;
        public  String         name     = "";
 
                /* Connection data */
@@ -59,7 +67,9 @@ public class Client
                Os.debug("Client: connect");
 
                try {
-                       this.socket = new Socket(this.server, this.port);
+                       this.state  = State.SETUP;
+                       this.socket = new Socket();
+                       this.socket.connect(new InetSocketAddress(this.server, this.port));
                        this.input  = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
                        this.output = new PrintWriter(this.socket.getOutputStream());
                } catch (Exception e) {
@@ -76,22 +86,25 @@ public class Client
                return true;
        }
 
-       public boolean abort()
+       public void abort()
        {
                Os.debug("Client: abort");
-               try {
-                       this.socket.close();
-                       this.ready = false;
-                       return true;
-               } catch (Exception e) {
-                       Os.debug("Client: error closing socket", e);
-                       return false;
-               }
+               this.state = State.ABORT;
+               this.validate();
+       }
+
+       public void reset()
+       {
+               Os.debug("Client: reset");
+               this.state = State.INIT;
        }
 
        public void raw(String line)
        {
                try {
+                       if (this.validate() != State.SETUP &&
+                           this.validate() != State.READY)
+                               return;
                        Os.debug("< " + line);
                        this.output.println(line);
                        this.output.flush();
@@ -102,7 +115,7 @@ public class Client
 
        public Message send(String txt)
        {
-               if (!this.ready)
+               if (this.validate() != State.READY)
                        return null;
                Message msg = new Message(this.channel, this.name, txt);
                if (msg.type == Message.Type.JOIN)
@@ -114,7 +127,10 @@ public class Client
        public Message recv()
        {
                try {
-                       String line = input.readLine();
+                       if (this.validate() != State.SETUP &&
+                           this.validate() != State.READY)
+                               return null;
+                       String line = this.input.readLine();
                        if (line == null)
                                return null;
                        Os.debug("> " + line);
@@ -124,22 +140,37 @@ public class Client
                                this.dosasl(msg);
                        return msg;
                } catch (SocketException e) {
-                       this.ready = false;
+                       this.state = State.INIT;
                        return null;
                } catch (Exception e) {
-                       this.ready = false;
+                       this.state = State.INIT;
                        Os.debug("Client: error in recv", e);
                        return null;
                }
        }
 
        /* Private methods */
+       private State validate()
+       {
+               try {
+                       if (this.state == State.ABORT) {
+                               if (this.socket != null) {
+                                       this.socket.close();
+                                       this.state = State.INIT;
+                               }
+                       }
+               } catch (Exception e) {
+                       Os.debug("Client: error closing socket", e);
+               }
+               return this.state;
+       }
+
        private void process(Message msg)
        {
                if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
                        this.raw("JOIN "  + this.channel);
                        this.raw("TOPIC " + this.channel);
-                       this.ready = true;
+                       this.state = State.READY;
                }
                if (msg.cmd.equals("433")) {
                        this.name   = this.nickname + this.mangle;
index 568d617afe0f1121d3699447dfb464a205d8d142..e186078efe810bdefe3ca8353a95db127db326f6 100644 (file)
@@ -191,6 +191,7 @@ public class Main extends Activity
                Os.debug("Main: disconnect");
                startService(new Intent(this, Task.class)
                                .putExtra("Command", Task.DISCONNECT));
+               this.running = false;
        }
 
        private void quit()
index a606d90c1add51b75f019bd77ae988a27dcc1bf3..ac1a243bf3073fd2988f949ce2a703195dcbd798 100644 (file)
@@ -149,7 +149,7 @@ public class Task extends Service implements Runnable
                }
 
                // Wait for login
-               while (!this.client.ready) {
+               while (this.client.state == Client.State.SETUP) {
                        Message msg = this.client.recv();
                        if (msg == null)
                                break;
@@ -157,7 +157,7 @@ public class Task extends Service implements Runnable
                }
 
                // Notify connection status
-               if (this.client.ready) {
+               if (this.client.state == Client.State.READY) {
                        this.command(CONNECT, null);
                        this.notify("Connected", android.R.drawable.presence_online);
                } else {
@@ -166,7 +166,7 @@ public class Task extends Service implements Runnable
                }
 
                // Process messages
-               while (this.client.ready) {
+               while (this.client.state == Client.State.READY) {
                        Message msg = this.client.recv();
                        if (msg == null)
                                break;