Skip to content

Instantly share code, notes, and snippets.

@colin-haber
Created April 23, 2012 02:59
Show Gist options
  • Save colin-haber/2468542 to your computer and use it in GitHub Desktop.
Save colin-haber/2468542 to your computer and use it in GitHub Desktop.
package com.n1nja.networking;
import java.io.*;
import java.net.*;
import java.util.Arrays;
public abstract class Client {
private final Socket socket;
private BufferedReader netReader;
private BufferedWriter netWriter;
public Client() {
socket = new Socket();
}
public Client(InetAddress hostAddress, int port) {
this();
this.connect(hostAddress, port);
}
public void connect(InetAddress hostAddress, int port) {
try {
this.socket.connect(new InetSocketAddress(hostAddress, port));
this.netReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.netWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isConnected() {
return this.socket.isConnected();
}
public void start() {
new Thread(
new Runnable() {
@Override
public void run() {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
if (consoleReader.ready()) {
String[] args = consoleReader.readLine().split(" ");
command(args[0].toUpperCase(), Arrays.copyOfRange(args, 0, args.length));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
).start();
}
public void stop() {
if (!this.socket.isClosed()) {
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.exit(0);
}
public void write(char c) {
this.write((int) c);
}
public void write(int i) {
try {
this.netWriter.write(i);
this.netWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeLine(String s) {
try {
this.netWriter.write(s + System.getProperty("line.separator"));
this.netWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public int read() {
int read = -1;
try {
read = this.netReader.read();
} catch (IOException e) {
e.printStackTrace();
}
return read;
}
public String readLine() {
String readLine = null;
try {
readLine = this.netReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return readLine;
}
public abstract void command(String command, String[] params);
}
package com.limyc.limychat.client;
import java.net.*;
import com.limyc.chat.CHAT;
import com.limyc.limychat.User;
public class Client extends com.n1nja.networking.Client {
private static enum Command {
CONNECT(new String[] {
"CONNECT"
}),
STOP(new String[] {
"STOP",
"EXIT",
"QUIT"
});
private String[] aliases;
private Command(String[] aliases) {
this.aliases = aliases;
}
public String toString() {
return aliases[0];
}
public boolean matches(String s) {
boolean matches = false;
matcher:for (String a : aliases) {
if (a.equalsIgnoreCase(s)) {
matches = true;
break matcher;
}
}
return matches;
}
}
public static void main(String[] args) {
new Client().start();
}
private User user;
private boolean isRegistered;
public Client() {
super();
this.user = null;
this.isRegistered = false;
}
@Override
public void start() {
super.start();
System.out.println("Type \"connect <IP:port>\" to connect to a server.");
}
@Override
public void stop() {
this.disconnect();
super.stop();
}
@Override
public void command(String command, String[] params) {
if (Command.CONNECT.matches(command) && params.length >= 1) {
String[] address = params[0].split(":");
try {
this.connect(InetAddress.getByName(address[0]), Integer.parseInt(address[1]));
} catch (NumberFormatException | UnknownHostException e) {
e.printStackTrace();
}
} else if (Command.STOP.matches(command) && params.length >= 0) {
this.stop();
}
}
public void register(String name) {
User register = null;
if (this.isConnected()) {
this.writeLine(CHAT.getCommand(CHAT.Command.REGISTER) + ":" + name);
String response = null;
do {
response = this.readLine();
} while (response == null);
if (response.split(":")[2].equals(CHAT.Code.REGISTER_SUCCESS.toString())) {
register = new User(name, response.split(":")[3]);
this.isRegistered = true;
}
} else {
System.out.println("Not connected. Type \"connect <IP:port>\".");
}
this.user = register;
}
public void message(String message) {
if (this.isRegistered) {
this.writeLine(CHAT.getCommand(CHAT.Command.MESSAGE) + ":" + this.user.getID() + ":" + message);
} else {
System.out.println("Not registered. Type \"register <name>\".");
}
}
public void disconnect() {
if (this.isConnected()) {
this.writeLine(CHAT.getCommand(CHAT.Command.DISCONNECT));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment