From 3abd457e7c58f8d39f27ce4868b652b5d58572ff Mon Sep 17 00:00:00 2001 From: Dor Bivas Date: Sat, 8 Feb 2014 18:38:05 +0200 Subject: client: parse commands upon reading. respond to PING * Parse commands with ParsedLine upon reading * Print a PRIVMSG nicer * Respond to PING and don't print it --- src/Client/ChatClient.java | 2 +- src/Client/ChatClientReader.java | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Client/ChatClient.java b/src/Client/ChatClient.java index df2c553..b29c756 100644 --- a/src/Client/ChatClient.java +++ b/src/Client/ChatClient.java @@ -94,7 +94,7 @@ public class ChatClient try { soc = new Socket(host, port); - ChatClientReader reader = new ChatClientReader(soc); + ChatClientReader reader = new ChatClientReader(this, soc); reader.setDisplay(ui); diff --git a/src/Client/ChatClientReader.java b/src/Client/ChatClientReader.java index 976bf84..46b5c46 100644 --- a/src/Client/ChatClientReader.java +++ b/src/Client/ChatClientReader.java @@ -8,13 +8,15 @@ import javax.swing.JTextArea; public class ChatClientReader extends Thread { public static Scanner reader = new Scanner(System.in); + private ChatClient client; private Socket soc; UserInterface ui; /** constructor * @param the socket */ - public ChatClientReader (Socket soc) + public ChatClientReader (ChatClient client, Socket soc) { + this.client = client; this.soc = soc; } @@ -40,16 +42,30 @@ public class ChatClientReader extends Thread { soc.close(); System.err.println("The line is empty"); + break; } - line = "[" + line + "]"; + System.out.println(""); System.out.println(line); - ui.outputWriter(line); + ParsedLine parsed = new ParsedLine(line); - } while (line != null); - + if (parsed.get("command").compareTo("PRIVMSG") == 0) + { + ui.outputWriter(parsed.get("nick") + ": " + parsed.get("message")); + } + else if (parsed.get("command").compareTo("PING") == 0) + { + this.client.runSendline("PONG :me"); + } + + else + { + ui.outputWriter("[" + line + "]"); + } + } while (line != null); + // FIXME: close socket here? } catch (IOException e) { System.err.println(e); return; -- cgit v1.2.3