]> Pileus Git - ~andy/spades/blobdiff - src/org/pileus/spades/Client.java
Add IRC Service and update UI
[~andy/spades] / src / org / pileus / spades / Client.java
index a8275f2e4ecc3c60a1a9ccb74894aa87ca12916c..4b1b4f9f09a939512fd76064fca9fba0dc2cdf87 100644 (file)
@@ -3,8 +3,6 @@ package org.pileus.spades;
 import java.io.BufferedReader;
 import java.io.PrintWriter;
 
-import android.util.Log;
-
 public class Client
 {
        /* Private data */
@@ -20,43 +18,6 @@ public class Client
        /* Public data */
        public  boolean        running  = true;
 
-       /* Private methods */
-       private void process(Message msg)
-       {
-               if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
-                       putline("JOIN "  + channel);
-                       putline("TOPIC " + channel);
-               }
-               if (msg.cmd.equals("PING")) {
-                       putline("PING " + msg.msg);
-               }
-       }
-
-       private String getline()
-       {
-               try {
-                       String line = input.readLine();
-                       Log.d("Spades", "> " + line);
-                       return line;
-               } catch (Exception e) {
-                       Log.d("Spades", "Error reading line", e);
-                       this.running = false;
-                       return "";
-               }
-       }
-
-       private void putline(String line)
-       {
-               try {
-                       Log.d("Spades", "< " + line);
-                       output.println(line);
-                       output.flush();
-               } catch (Exception e) {
-                       Log.d("Spades", "Error writing line", e);
-                       this.running = false;
-               }
-       }
-
        /* Public Methods */
        public Client(String server, String nickname, String channel,
                        String username, String hostname)
@@ -66,39 +27,84 @@ public class Client
                this.channel  = channel;
                this.username = username;
                this.hostname = hostname;
-               Log.d("Spades", "Client create");
+               Os.debug("Client: create");
        }
 
        public Client(String server, String nickname, String channel)
        {
                this(server, nickname, channel, "user", "localhost");
-               Log.d("Spades", "Client create");
        }
 
        public void connect(BufferedReader input, PrintWriter output)
        {
                this.input  = input;
                this.output = output;
-               Log.d("Spades", "Client connect");
+               Os.debug("Client: connect");
                putline("USER "+username+" "+hostname+" "+server+" :"+nickname);
                putline("NICK "+nickname);
        }
 
-       public void send(String txt)
+       public Message send(String txt)
        {
+               Message msg = new Message(channel, nickname, txt);
+               putline(msg.line);
+               return msg;
        }
 
        public Message recv()
        {
                try {
                        String line = getline();
-                       Message msg = new Message(line);
-                       process(msg);
-                       return msg;
+                       if (line == null) {
+                               this.running = false;
+                               return null;
+                       } else {
+                               Message msg = new Message(line);
+                               process(msg);
+                               return msg;
+                       }
                } catch (Exception e) {
-                       Log.d("Spades", "Error in recv", e);
+                       Os.debug("Client: error in recv", e);
                        this.running = false;
                        return null;
                }
        }
+
+       /* Private methods */
+       private void process(Message msg)
+       {
+               if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
+                       putline("JOIN "  + channel);
+                       putline("TOPIC " + channel);
+               }
+               if (msg.cmd.equals("PING")) {
+                       putline("PING " + msg.msg);
+               }
+       }
+
+       private String getline()
+       {
+               try {
+                       String line = input.readLine();
+                       if (line != null)
+                               Os.debug("> " + line);
+                       return line;
+               } catch (Exception e) {
+                       Os.debug("Client: error reading line", e);
+                       this.running = false;
+                       return "";
+               }
+       }
+
+       private void putline(String line)
+       {
+               try {
+                       Os.debug("< " + line);
+                       output.println(line);
+                       output.flush();
+               } catch (Exception e) {
+                       Os.debug("Client: error writing line", e);
+                       this.running = false;
+               }
+       }
 }