From 81f9b1d078d1ba2fac5f53c3ed3d35680f25922d Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Tue, 23 Apr 2013 07:20:06 +0000 Subject: [PATCH] Improve client modeing --- src/org/pileus/spades/Client.java | 63 +++++++++++++++++++++++-------- src/org/pileus/spades/Main.java | 1 + src/org/pileus/spades/Task.java | 6 +-- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/org/pileus/spades/Client.java b/src/org/pileus/spades/Client.java index f165784..e34dcc8 100644 --- a/src/org/pileus/spades/Client.java +++ b/src/org/pileus/spades/Client.java @@ -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; diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index 568d617..e186078 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -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() diff --git a/src/org/pileus/spades/Task.java b/src/org/pileus/spades/Task.java index a606d90..ac1a243 100644 --- a/src/org/pileus/spades/Task.java +++ b/src/org/pileus/spades/Task.java @@ -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; -- 2.43.2