consolechat-server/src-server/ServerThread.java

130 lines
3.9 KiB
Java
Raw Normal View History

2014-05-04 18:55:37 +00:00
import java.net.*;
import java.io.*;
/**
* @author Nicolás A. Ortega
2014-09-18 02:07:36 +00:00
* @copyright Nicolás A. Ortega
* @license MIT
2014-05-04 18:55:37 +00:00
* @year 2014
*
* For details on the copyright, look at the COPYRIGHT file that came with
* this program.
*
*/
public class ServerThread extends Thread {
private Server server = null;
private Socket socket = null;
private int id = -1;
private String username = "User";
private DataInputStream streamIn = null;
private DataOutputStream streamOut = null;
private boolean run = false;
private boolean admin = false;
2014-05-07 15:34:45 +00:00
// Constructor method
2014-05-04 18:55:37 +00:00
public ServerThread(Server _server, Socket _socket) {
super();
this.socket = _socket;
this.server = _server;
id = socket.getPort();
}
2014-05-07 15:34:45 +00:00
// Method used to send a message to this client
2014-05-04 18:55:37 +00:00
public void send(String msg) {
try {
streamOut.writeUTF(msg);
streamOut.flush();
} catch(IOException e) {
System.out.println(id + " Error sending: " + e.getMessage());
server.remove(id);
interrupt();
}
}
2014-05-07 15:34:45 +00:00
// The run method which will run in a loop
2014-05-04 18:55:37 +00:00
public void run() {
System.out.println("Server thread " + id + " running.");
while(run) {
try {
server.handle(id, username, streamIn.readUTF());
} catch(IOException e) {
System.out.println(id + " error reading: " + e.getMessage());
server.remove(id);
interrupt();
}
}
}
2014-05-07 15:34:45 +00:00
// Run a command
2014-05-04 18:55:37 +00:00
public void runCommand(String command) {
if(command.equals("/help")) {
send(" - /admin [passwd] -- Gain admin privileges.\n" +
" - /list -- Returns all the user's ID's and usernames.\n" +
" - /myID -- Returns your ID.\n" +
" - /myUserName -- Returns your username.\n" +
" - /pm [id] [msg] -- Sends a message to only one of the clients.\n" +
" - /setUserName [newusername] -- Change your username.\n" +
" - /clientVersion -- Returns the version of the client.\n" +
" - /serverVersion -- Returns the version of the server.\n" +
2014-05-04 18:55:37 +00:00
" - /help -- Show this information.\n" +
" - /quit -- Quit.\n");
if(admin) {
send("Admin commands:\n" +
" - /chgpasswd [newpasswd] -- Change the server admin password.\n" +
" - /giveadmin [id] -- Give another client admin as well.\n" +
" - /kick [id] [reason] -- Kick out a user and state a reason for the kick.\n");
}
} else if(command.length() > 6 && command.substring(0, 6).equals("/admin")) {
if(command.substring(7).equals(server.getPasswd())) {
admin = true;
send("You are now admin.\n");
} else {
send("Wrong password.\n");
}
} else if(command.equals("/myID")) {
send(Integer.toString(id) + "\n");
} else if(command.equals("/myUserName")) {
send(username + "\n");
} else if(command.length() > 12 && command.substring(0, 12).equals("/setUserName")) {
if(command.length() <= 13) {
send("You did not enter a new username.");
} else {
username = command.substring(13);
send("Your username is now: " + username + "\n");
}
} else if(admin && command.length() > 10 && command.substring(0, 10).equals("/chgpasswd")) {
if(command.length() > 11) {
server.setPasswd(command.substring(11));
} else {
send("You did not enter a new password.\n");
}
} else {
send("You have either entered an invalid command, or used an improper form.\n");
}
}
2014-05-07 15:34:45 +00:00
// Open the streams
2014-05-04 18:55:37 +00:00
public void open() throws IOException {
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
streamOut = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
run = true;
}
2014-05-07 15:34:45 +00:00
// Close the streams
2014-05-04 18:55:37 +00:00
public void close() throws IOException {
if(socket != null) { socket.close(); }
if(streamIn != null) { streamIn.close(); }
if(streamOut != null) { streamOut.close(); }
run = false;
}
2014-05-07 15:34:45 +00:00
// Getter methods
2014-05-04 18:55:37 +00:00
public int getID() { return id; }
public String getUsername() { return username; }
public boolean isAdmin() { return admin; }
2014-05-07 15:34:45 +00:00
// Setter methods
2014-05-04 18:55:37 +00:00
public void setAdmin(boolean a) { this.admin = a; }
2014-09-18 02:07:36 +00:00
}