]> Pileus Git - ~andy/spades/commitdiff
Improve connection handling
authorAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 01:13:42 +0000 (01:13 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 13 Apr 2013 01:13:42 +0000 (01:13 +0000)
src/org/pileus/spades/Client.java
src/org/pileus/spades/Main.java
src/org/pileus/spades/Task.java

index 3ec0b8d516d8c6e155747c480babc994ccd2d31f..693ec551b19fc0f11e30800aecf37d745e190724 100644 (file)
@@ -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);
                }
        }
 }
index 3855eb92cbc94b5a6c3a8fc91513677058c2aa04..29bfff9cd624c518b970c5934ccd8f1fbd700080 100644 (file)
@@ -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 */
index 5aad096554237ec13cc761ccf385cdb2ec994b08..2ed59618003f35fce1b7b165c9912980b141be0c 100644 (file)
@@ -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