From 114840debf57f7c880cf5846a16c39aa3e326be7 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sat, 13 Apr 2013 01:13:42 +0000 Subject: [PATCH] Improve connection handling --- src/org/pileus/spades/Client.java | 33 ++++++---- src/org/pileus/spades/Main.java | 1 - src/org/pileus/spades/Task.java | 101 ++++++++++++++++++++++++------ 3 files changed, 102 insertions(+), 33 deletions(-) diff --git a/src/org/pileus/spades/Client.java b/src/org/pileus/spades/Client.java index 3ec0b8d..693ec55 100644 --- a/src/org/pileus/spades/Client.java +++ b/src/org/pileus/spades/Client.java @@ -17,6 +17,9 @@ public class Client private BufferedReader input = null; private PrintWriter output = null; + /* Public data */ + public boolean ready = false; + /* Public Methods */ public Client(String username, String hostname) { @@ -39,17 +42,17 @@ public class Client this.channel = channel; try { - socket = new Socket(this.server, this.port); - input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output = new PrintWriter(socket.getOutputStream()); + this.socket = new Socket(this.server, this.port); + this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); + this.output = new PrintWriter(this.socket.getOutputStream()); } catch (Exception e) { Os.debug("Client: failed to create connection: " + e); return false; } Os.debug("Client: connected"); - raw("USER "+username+" "+hostname+" "+server+" :"+nickname); - raw("NICK "+nickname); + this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.nickname); + this.raw("NICK "+this.nickname); return true; } @@ -59,6 +62,7 @@ public class Client Os.debug("Client: abort"); try { this.socket.close(); + this.ready = false; return true; } catch (Exception e) { Os.debug("Client: error closing socket", e); @@ -70,8 +74,8 @@ public class Client { try { Os.debug("< " + line); - output.println(line); - output.flush(); + this.output.println(line); + this.output.flush(); } catch (Exception e) { Os.debug("Client: error writing line", e); } @@ -79,8 +83,8 @@ public class Client public Message send(String txt) { - Message msg = new Message(channel, nickname, txt); - raw(msg.line); + Message msg = new Message(this.channel, this.nickname, txt); + this.raw(msg.line); return msg; } @@ -92,11 +96,13 @@ public class Client return null; Os.debug("> " + line); Message msg = new Message(line); - process(msg); + this.process(msg); return msg; } catch (SocketException e) { + this.ready = false; return null; } catch (Exception e) { + this.ready = false; Os.debug("Client: error in recv", e); return null; } @@ -106,11 +112,12 @@ public class Client private void process(Message msg) { if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) { - raw("JOIN " + channel); - raw("TOPIC " + channel); + this.raw("JOIN " + this.channel); + this.raw("TOPIC " + this.channel); + this.ready = true; } if (msg.cmd.equals("PING")) { - raw("PING " + msg.msg); + this.raw("PING " + msg.msg); } } } diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index 3855eb9..29bfff9 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -83,7 +83,6 @@ public class Main extends Activity if (msg == null) return; this.input.setText(""); - this.log.append(msg.from + ": " + msg.msg + "\n"); } /* Activity Methods */ diff --git a/src/org/pileus/spades/Task.java b/src/org/pileus/spades/Task.java index 5aad096..2ed5961 100644 --- a/src/org/pileus/spades/Task.java +++ b/src/org/pileus/spades/Task.java @@ -3,15 +3,20 @@ package org.pileus.spades; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.os.IBinder; +import android.os.Looper; import android.os.Messenger; +import android.widget.Toast; public class Task extends Service implements Runnable { /* Commands */ - public static final int REGISTER = 0; - public static final int MESSAGE = 1; + public static final int REGISTER = 0; + public static final int MESSAGE = 1; + public static final int CONNECT = 2; + public static final int DISCONNECT = 3; /* Configuration */ private String server = "irc.freenode.net"; @@ -22,6 +27,7 @@ public class Task extends Service implements Runnable private Messenger messenger = null; private Thread thread = null; private Client client = null; + private Toast toast = null; /* Private methods */ private void command(int cmd, Object value) @@ -36,12 +42,33 @@ public class Task extends Service implements Runnable } } + private void notify(String text, int icon) + { + // Log + Os.debug("Task: notify - " + text); + + // Toast + this.toast.setText(text); + this.toast.show(); + + // Notify + Notification note = new Notification(icon, null, 0); + Intent intent = new Intent(this, Main.class); + PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0); + + note.setLatestEventInfo(this, "Spades!", text, pend); + this.startForeground(1, note); + } + /* Public methods */ public Message send(String txt) { if (this.client == null) return null; - return this.client.send(txt); + Message msg = this.client.send(txt); + if (msg != null) + this.command(MESSAGE, msg); + return msg; } /* Runnable methods */ @@ -50,18 +77,52 @@ public class Task extends Service implements Runnable { Os.debug("Task: thread run"); - if (!client.connect(server, nickname, channel)) + // Android Toast setup + Looper.prepare(); + + // Setup notification bar + this.notify("Connecting..", android.R.drawable.presence_invisible); + + // Start connecting + if (!this.client.connect(server, nickname, channel)) { + this.command(DISCONNECT, null); + this.notify("Unable to connect", android.R.drawable.presence_offline); + this.thread = null; return; + } - while (true) { - Message msg = client.recv(); + // Wait for login + while (!this.client.ready) { + Message msg = this.client.recv(); if (msg == null) break; this.command(MESSAGE, msg); } - if (!client.abort()) - return; + // Notify connection status + if (this.client.ready) { + this.command(CONNECT, null); + this.notify("Connected", android.R.drawable.presence_online); + } else { + this.command(DISCONNECT, null); + this.notify("Connetion aborted", android.R.drawable.presence_offline); + } + + // Process messages + while (this.client.ready) { + Message msg = this.client.recv(); + if (msg == null) + break; + this.command(MESSAGE, msg); + } + + // Notify disconnect disconnected + this.notify("Disconnected", android.R.drawable.presence_offline); + this.command(DISCONNECT, null); + + // Shutdown the client + this.client.abort(); + this.thread = null; Os.debug("Task: thread exit"); } @@ -73,18 +134,12 @@ public class Task extends Service implements Runnable Os.debug("Task: onCreate"); super.onCreate(); - /* Setup notification bar */ - Notification note = new Notification(android.R.drawable.presence_online, null, 0); - Intent intent = new Intent(this, Main.class); - PendingIntent pend = PendingIntent.getActivity(this, 0, intent, 0); - - note.setLatestEventInfo(this, "Spades Title", "Spades Message", pend); - startForeground(1, note); + // Setup toast + Context context = this.getApplicationContext(); + this.toast = Toast.makeText(context, "", Toast.LENGTH_SHORT); - /* Start client thread */ - client = new Client(); - thread = new Thread(this); - thread.start(); + // Create the client + this.client = new Client(); } @Override @@ -104,8 +159,16 @@ public class Task extends Service implements Runnable { Os.debug("Task: onStart"); super.onStart(intent, startId); + + // Setup communication with Main this.messenger = (Messenger)intent.getExtras().get("Messenger"); this.command(REGISTER, this); + + // Create client thread + if (this.thread == null) { + this.thread = new Thread(this); + this.thread.start(); + } } @Override -- 2.43.2