X-Git-Url: http://pileus.org/git/?p=~andy%2Fspades;a=blobdiff_plain;f=src%2Forg%2Fpileus%2Fspades%2FClient.java;h=3ec0b8d516d8c6e155747c480babc994ccd2d31f;hp=4b1b4f9f09a939512fd76064fca9fba0dc2cdf87;hb=444f43d4b3f1859dd940eb141dd7d0ee7f705de2;hpb=05aa01c6414c011546b2839573267ae3e1923d09 diff --git a/src/org/pileus/spades/Client.java b/src/org/pileus/spades/Client.java index 4b1b4f9..3ec0b8d 100644 --- a/src/org/pileus/spades/Client.java +++ b/src/org/pileus/spades/Client.java @@ -1,71 +1,103 @@ package org.pileus.spades; -import java.io.BufferedReader; -import java.io.PrintWriter; +import java.io.*; +import java.net.*; public class Client { /* Private data */ private String server = null; + private int port = 6667; private String nickname = null; private String channel = null; private String username = null; private String hostname = null; + private Socket socket = null; private BufferedReader input = null; private PrintWriter output = null; - /* Public data */ - public boolean running = true; - /* Public Methods */ - public Client(String server, String nickname, String channel, - String username, String hostname) + public Client(String username, String hostname) { - this.server = server; - this.nickname = nickname; - this.channel = channel; this.username = username; this.hostname = hostname; Os.debug("Client: create"); } - public Client(String server, String nickname, String channel) + public Client() { - this(server, nickname, channel, "user", "localhost"); + this("user", "localhost"); } - public void connect(BufferedReader input, PrintWriter output) + public boolean connect(String server, String nickname, String channel) { - this.input = input; - this.output = output; Os.debug("Client: connect"); - putline("USER "+username+" "+hostname+" "+server+" :"+nickname); - putline("NICK "+nickname); + + this.server = server; + this.nickname = nickname; + this.channel = channel; + + try { + socket = new Socket(this.server, this.port); + input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + output = new PrintWriter(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); + + return true; + } + + public boolean abort() + { + Os.debug("Client: abort"); + try { + this.socket.close(); + return true; + } catch (Exception e) { + Os.debug("Client: error closing socket", e); + return false; + } + } + + public void raw(String line) + { + try { + Os.debug("< " + line); + output.println(line); + output.flush(); + } catch (Exception e) { + Os.debug("Client: error writing line", e); + } } public Message send(String txt) { - Message msg = new Message(channel, nickname, txt); - putline(msg.line); + Message msg = new Message(channel, nickname, txt); + raw(msg.line); return msg; } public Message recv() { try { - String line = getline(); - if (line == null) { - this.running = false; + String line = input.readLine(); + if (line == null) return null; - } else { - Message msg = new Message(line); - process(msg); - return msg; - } + Os.debug("> " + line); + Message msg = new Message(line); + process(msg); + return msg; + } catch (SocketException e) { + return null; } catch (Exception e) { Os.debug("Client: error in recv", e); - this.running = false; return null; } } @@ -74,37 +106,11 @@ public class Client private void process(Message msg) { if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) { - putline("JOIN " + channel); - putline("TOPIC " + channel); + raw("JOIN " + channel); + raw("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; + raw("PING " + msg.msg); } } }