]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Client.java
a8275f2e4ecc3c60a1a9ccb74894aa87ca12916c
[~andy/spades] / src / org / pileus / spades / Client.java
1 package org.pileus.spades;
2
3 import java.io.BufferedReader;
4 import java.io.PrintWriter;
5
6 import android.util.Log;
7
8 public class Client
9 {
10         /* Private data */
11         private String         server   = null;
12         private String         nickname = null;
13         private String         channel  = null;
14         private String         username = null;
15         private String         hostname = null;
16
17         private BufferedReader input    = null;
18         private PrintWriter    output   = null;
19
20         /* Public data */
21         public  boolean        running  = true;
22
23         /* Private methods */
24         private void process(Message msg)
25         {
26                 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
27                         putline("JOIN "  + channel);
28                         putline("TOPIC " + channel);
29                 }
30                 if (msg.cmd.equals("PING")) {
31                         putline("PING " + msg.msg);
32                 }
33         }
34
35         private String getline()
36         {
37                 try {
38                         String line = input.readLine();
39                         Log.d("Spades", "> " + line);
40                         return line;
41                 } catch (Exception e) {
42                         Log.d("Spades", "Error reading line", e);
43                         this.running = false;
44                         return "";
45                 }
46         }
47
48         private void putline(String line)
49         {
50                 try {
51                         Log.d("Spades", "< " + line);
52                         output.println(line);
53                         output.flush();
54                 } catch (Exception e) {
55                         Log.d("Spades", "Error writing line", e);
56                         this.running = false;
57                 }
58         }
59
60         /* Public Methods */
61         public Client(String server, String nickname, String channel,
62                         String username, String hostname)
63         {
64                 this.server   = server;
65                 this.nickname = nickname;
66                 this.channel  = channel;
67                 this.username = username;
68                 this.hostname = hostname;
69                 Log.d("Spades", "Client create");
70         }
71
72         public Client(String server, String nickname, String channel)
73         {
74                 this(server, nickname, channel, "user", "localhost");
75                 Log.d("Spades", "Client create");
76         }
77
78         public void connect(BufferedReader input, PrintWriter output)
79         {
80                 this.input  = input;
81                 this.output = output;
82                 Log.d("Spades", "Client connect");
83                 putline("USER "+username+" "+hostname+" "+server+" :"+nickname);
84                 putline("NICK "+nickname);
85         }
86
87         public void send(String txt)
88         {
89         }
90
91         public Message recv()
92         {
93                 try {
94                         String line = getline();
95                         Message msg = new Message(line);
96                         process(msg);
97                         return msg;
98                 } catch (Exception e) {
99                         Log.d("Spades", "Error in recv", e);
100                         this.running = false;
101                         return null;
102                 }
103         }
104 }