Added description comments to methods.

This commit is contained in:
Deathsbreed 2014-05-07 10:34:45 -05:00
parent 9d7612cd55
commit 4ab912125e
4 changed files with 29 additions and 2 deletions

View File

@ -23,10 +23,11 @@ public class Client implements Runnable {
if(args.length != 2) {
System.out.println("Usage: java Client [server] [port]");
} else {
Client client = new Client(args[0], Integer.parseInt(args[1]));
new Client(args[0], Integer.parseInt(args[1]));
}
}
// Constructor method
public Client(String server, int port) {
System.out.println("ConsoleChat client " + version + " Copyright (C) 2014 Nicolás A. Ortega\n" +
"This program comes with ABSOLUTELY NO WARRANTY; details in WARRANTY file.\n" +
@ -46,6 +47,7 @@ public class Client implements Runnable {
}
}
// The run method containing the main loop
public void run() {
String uinput;
while(thread != null) {
@ -64,6 +66,7 @@ public class Client implements Runnable {
}
}
// Handle messages
public void handle(String msg) {
if(msg.equals("/quit")) {
System.out.println("Goodbye bye. Press RETURN to exit...");
@ -76,6 +79,7 @@ public class Client implements Runnable {
}
}
// Open and start all necessary threads
private void start() throws IOException {
console = new BufferedReader(new InputStreamReader(System.in));
streamOut = new DataOutputStream(socket.getOutputStream());
@ -87,6 +91,7 @@ public class Client implements Runnable {
}
}
// Stop and close all necessary threads
public void stop() {
if(thread != null) {
thread.interrupt();

View File

@ -16,6 +16,7 @@ public class ClientThread extends Thread {
private DataInputStream streamIn = null;
private boolean run = false;
// Constructor
public ClientThread(Client _client, Socket _socket) {
client = _client;
socket = _socket;
@ -23,6 +24,7 @@ public class ClientThread extends Thread {
start();
}
// Open all necessary streams/threads
public void open() {
try {
streamIn = new DataInputStream(socket.getInputStream());
@ -33,11 +35,13 @@ public class ClientThread extends Thread {
run = true;
}
// Close the streams
public void close() throws IOException {
if(streamIn != null) { streamIn.close(); }
run = false;
}
// The run method which will be called every frame
public void run() {
while(run) {
try {

View File

@ -23,10 +23,11 @@ public class Server implements Runnable {
if(args.length != 1) {
System.out.println("You are using the program incorrectly.");
} else {
Server server = new Server(Integer.parseInt(args[0]));
new Server(Integer.parseInt(args[0]));
}
}
// The constructor method
public Server(int port) {
System.out.println("ConsoleChat server " + version + " Copyright (C) 2014 Nicolás A. Ortega\n" +
"This program comes with ABSOLUTELY NO WARRANTY; details in WARRANTY file.\n" +
@ -43,6 +44,7 @@ public class Server implements Runnable {
}
}
// The run method that will be called every frame
public void run() {
while(thread != null) {
try {
@ -55,6 +57,7 @@ public class Server implements Runnable {
}
}
// Start the server thread
public void start() {
if(thread == null) {
thread = new Thread(this);
@ -62,6 +65,7 @@ public class Server implements Runnable {
}
}
// Stop the server thread and all other threads
public void stop() {
if(thread != null) {
thread.interrupt();
@ -77,6 +81,7 @@ public class Server implements Runnable {
}
}
// This function loops through all the clients and returns the one with the ID entered
public int findClient(int id) {
for(int i = 0; i < clientCount; i++) {
if(clients.get(i).getID() == id) {
@ -87,6 +92,7 @@ public class Server implements Runnable {
return -1;
}
// Handle any messages the server recieves
public synchronized void handle(int id, String username, String input) {
if(input.startsWith("/")) {
if(input.equals("/quit")) {
@ -135,6 +141,7 @@ public class Server implements Runnable {
}
}
// Remove a client
public synchronized void remove(int id) {
int pos = findClient(id);
if(pos >= 0) {
@ -156,6 +163,7 @@ public class Server implements Runnable {
}
}
// Add a new client
public void addThread(Socket socket) {
clients.add(new ServerThread(this, socket));
try {
@ -167,7 +175,9 @@ public class Server implements Runnable {
}
}
// Getter methods
public String getPasswd() { return passwd; }
// Setter methods
public void setPasswd(String npasswd) { this.passwd = npasswd; }
}

View File

@ -20,6 +20,7 @@ public class ServerThread extends Thread {
private boolean run = false;
private boolean admin = false;
// Constructor method
public ServerThread(Server _server, Socket _socket) {
super();
this.socket = _socket;
@ -27,6 +28,7 @@ public class ServerThread extends Thread {
id = socket.getPort();
}
// Method used to send a message to this client
public void send(String msg) {
try {
streamOut.writeUTF(msg);
@ -38,6 +40,7 @@ public class ServerThread extends Thread {
}
}
// The run method which will run in a loop
public void run() {
System.out.println("Server thread " + id + " running.");
while(run) {
@ -51,6 +54,7 @@ public class ServerThread extends Thread {
}
}
// Run a command
public void runCommand(String command) {
if(command.equals("/help")) {
send(" - /admin [passwd] -- Gain admin privileges.\n" +
@ -98,6 +102,7 @@ public class ServerThread extends Thread {
}
}
// Open the streams
public void open() throws IOException {
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
@ -105,6 +110,7 @@ public class ServerThread extends Thread {
run = true;
}
// Close the streams
public void close() throws IOException {
if(socket != null) { socket.close(); }
if(streamIn != null) { streamIn.close(); }
@ -112,9 +118,11 @@ public class ServerThread extends Thread {
run = false;
}
// Getter methods
public int getID() { return id; }
public String getUsername() { return username; }
public boolean isAdmin() { return admin; }
// Setter methods
public void setAdmin(boolean a) { this.admin = a; }
}