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 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; }
}