Added description comments to methods.
This commit is contained in:
parent
9d7612cd55
commit
4ab912125e
@ -23,10 +23,11 @@ public class Client implements Runnable {
|
|||||||
if(args.length != 2) {
|
if(args.length != 2) {
|
||||||
System.out.println("Usage: java Client [server] [port]");
|
System.out.println("Usage: java Client [server] [port]");
|
||||||
} else {
|
} 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) {
|
public Client(String server, int port) {
|
||||||
System.out.println("ConsoleChat client " + version + " Copyright (C) 2014 Nicolás A. Ortega\n" +
|
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" +
|
"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() {
|
public void run() {
|
||||||
String uinput;
|
String uinput;
|
||||||
while(thread != null) {
|
while(thread != null) {
|
||||||
@ -64,6 +66,7 @@ public class Client implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle messages
|
||||||
public void handle(String msg) {
|
public void handle(String msg) {
|
||||||
if(msg.equals("/quit")) {
|
if(msg.equals("/quit")) {
|
||||||
System.out.println("Goodbye bye. Press RETURN to exit...");
|
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 {
|
private void start() throws IOException {
|
||||||
console = new BufferedReader(new InputStreamReader(System.in));
|
console = new BufferedReader(new InputStreamReader(System.in));
|
||||||
streamOut = new DataOutputStream(socket.getOutputStream());
|
streamOut = new DataOutputStream(socket.getOutputStream());
|
||||||
@ -87,6 +91,7 @@ public class Client implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop and close all necessary threads
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if(thread != null) {
|
if(thread != null) {
|
||||||
thread.interrupt();
|
thread.interrupt();
|
||||||
|
@ -16,6 +16,7 @@ public class ClientThread extends Thread {
|
|||||||
private DataInputStream streamIn = null;
|
private DataInputStream streamIn = null;
|
||||||
private boolean run = false;
|
private boolean run = false;
|
||||||
|
|
||||||
|
// Constructor
|
||||||
public ClientThread(Client _client, Socket _socket) {
|
public ClientThread(Client _client, Socket _socket) {
|
||||||
client = _client;
|
client = _client;
|
||||||
socket = _socket;
|
socket = _socket;
|
||||||
@ -23,6 +24,7 @@ public class ClientThread extends Thread {
|
|||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Open all necessary streams/threads
|
||||||
public void open() {
|
public void open() {
|
||||||
try {
|
try {
|
||||||
streamIn = new DataInputStream(socket.getInputStream());
|
streamIn = new DataInputStream(socket.getInputStream());
|
||||||
@ -33,11 +35,13 @@ public class ClientThread extends Thread {
|
|||||||
run = true;
|
run = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the streams
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if(streamIn != null) { streamIn.close(); }
|
if(streamIn != null) { streamIn.close(); }
|
||||||
run = false;
|
run = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The run method which will be called every frame
|
||||||
public void run() {
|
public void run() {
|
||||||
while(run) {
|
while(run) {
|
||||||
try {
|
try {
|
||||||
|
@ -23,10 +23,11 @@ public class Server implements Runnable {
|
|||||||
if(args.length != 1) {
|
if(args.length != 1) {
|
||||||
System.out.println("You are using the program incorrectly.");
|
System.out.println("You are using the program incorrectly.");
|
||||||
} else {
|
} else {
|
||||||
Server server = new Server(Integer.parseInt(args[0]));
|
new Server(Integer.parseInt(args[0]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The constructor method
|
||||||
public Server(int port) {
|
public Server(int port) {
|
||||||
System.out.println("ConsoleChat server " + version + " Copyright (C) 2014 Nicolás A. Ortega\n" +
|
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" +
|
"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() {
|
public void run() {
|
||||||
while(thread != null) {
|
while(thread != null) {
|
||||||
try {
|
try {
|
||||||
@ -55,6 +57,7 @@ public class Server implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start the server thread
|
||||||
public void start() {
|
public void start() {
|
||||||
if(thread == null) {
|
if(thread == null) {
|
||||||
thread = new Thread(this);
|
thread = new Thread(this);
|
||||||
@ -62,6 +65,7 @@ public class Server implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop the server thread and all other threads
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if(thread != null) {
|
if(thread != null) {
|
||||||
thread.interrupt();
|
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) {
|
public int findClient(int id) {
|
||||||
for(int i = 0; i < clientCount; i++) {
|
for(int i = 0; i < clientCount; i++) {
|
||||||
if(clients.get(i).getID() == id) {
|
if(clients.get(i).getID() == id) {
|
||||||
@ -87,6 +92,7 @@ public class Server implements Runnable {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle any messages the server recieves
|
||||||
public synchronized void handle(int id, String username, String input) {
|
public synchronized void handle(int id, String username, String input) {
|
||||||
if(input.startsWith("/")) {
|
if(input.startsWith("/")) {
|
||||||
if(input.equals("/quit")) {
|
if(input.equals("/quit")) {
|
||||||
@ -135,6 +141,7 @@ public class Server implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove a client
|
||||||
public synchronized void remove(int id) {
|
public synchronized void remove(int id) {
|
||||||
int pos = findClient(id);
|
int pos = findClient(id);
|
||||||
if(pos >= 0) {
|
if(pos >= 0) {
|
||||||
@ -156,6 +163,7 @@ public class Server implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add a new client
|
||||||
public void addThread(Socket socket) {
|
public void addThread(Socket socket) {
|
||||||
clients.add(new ServerThread(this, socket));
|
clients.add(new ServerThread(this, socket));
|
||||||
try {
|
try {
|
||||||
@ -167,7 +175,9 @@ public class Server implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Getter methods
|
||||||
public String getPasswd() { return passwd; }
|
public String getPasswd() { return passwd; }
|
||||||
|
|
||||||
|
// Setter methods
|
||||||
public void setPasswd(String npasswd) { this.passwd = npasswd; }
|
public void setPasswd(String npasswd) { this.passwd = npasswd; }
|
||||||
}
|
}
|
@ -20,6 +20,7 @@ public class ServerThread extends Thread {
|
|||||||
private boolean run = false;
|
private boolean run = false;
|
||||||
private boolean admin = false;
|
private boolean admin = false;
|
||||||
|
|
||||||
|
// Constructor method
|
||||||
public ServerThread(Server _server, Socket _socket) {
|
public ServerThread(Server _server, Socket _socket) {
|
||||||
super();
|
super();
|
||||||
this.socket = _socket;
|
this.socket = _socket;
|
||||||
@ -27,6 +28,7 @@ public class ServerThread extends Thread {
|
|||||||
id = socket.getPort();
|
id = socket.getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method used to send a message to this client
|
||||||
public void send(String msg) {
|
public void send(String msg) {
|
||||||
try {
|
try {
|
||||||
streamOut.writeUTF(msg);
|
streamOut.writeUTF(msg);
|
||||||
@ -38,6 +40,7 @@ public class ServerThread extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The run method which will run in a loop
|
||||||
public void run() {
|
public void run() {
|
||||||
System.out.println("Server thread " + id + " running.");
|
System.out.println("Server thread " + id + " running.");
|
||||||
while(run) {
|
while(run) {
|
||||||
@ -51,6 +54,7 @@ public class ServerThread extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run a command
|
||||||
public void runCommand(String command) {
|
public void runCommand(String command) {
|
||||||
if(command.equals("/help")) {
|
if(command.equals("/help")) {
|
||||||
send(" - /admin [passwd] -- Gain admin privileges.\n" +
|
send(" - /admin [passwd] -- Gain admin privileges.\n" +
|
||||||
@ -98,6 +102,7 @@ public class ServerThread extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Open the streams
|
||||||
public void open() throws IOException {
|
public void open() throws IOException {
|
||||||
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||||
streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
|
||||||
@ -105,6 +110,7 @@ public class ServerThread extends Thread {
|
|||||||
run = true;
|
run = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the streams
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if(socket != null) { socket.close(); }
|
if(socket != null) { socket.close(); }
|
||||||
if(streamIn != null) { streamIn.close(); }
|
if(streamIn != null) { streamIn.close(); }
|
||||||
@ -112,9 +118,11 @@ public class ServerThread extends Thread {
|
|||||||
run = false;
|
run = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Getter methods
|
||||||
public int getID() { return id; }
|
public int getID() { return id; }
|
||||||
public String getUsername() { return username; }
|
public String getUsername() { return username; }
|
||||||
public boolean isAdmin() { return admin; }
|
public boolean isAdmin() { return admin; }
|
||||||
|
|
||||||
|
// Setter methods
|
||||||
public void setAdmin(boolean a) { this.admin = a; }
|
public void setAdmin(boolean a) { this.admin = a; }
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user