Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**@file jserver.java
- * @author Brittany Kondo 100379346
- * @brief Represents a multi-threaded server which can have several clients
- * connected to it, its main purpose is to manage all messages sent
- * among clients.
- * */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.TreeMap;
- import java.util.Vector;
- public class jserver {
- //Stores all messages for every client created on the server
- public static TreeMap<String,Vector<String>> inbox = new TreeMap<String,Vector<String>>();
- /** Checks if a given user already exists on the server's inbox
- * @param map The tree map (which represents the inbox) to search
- * @param user The user to search for
- * @return True, if the user already exists in the map
- * False, otherwise
- * */
- public static boolean doesUserExist(TreeMap<String,Vector<String>> map,String user){
- for (String key:map.keySet()){
- if (key.equalsIgnoreCase(user))
- return true;
- }
- return false;
- }
- //Handles an individual jclient and is used later to create a thread
- //for each client
- public static class jClientHandler implements Runnable {
- private Socket client;
- /** Constructs a new jclienthandler object
- * @param client A socket representing the client's connection to
- * the server
- * */
- jClientHandler(Socket client) {
- this.client = client;
- }
- /** Processes data received from the jclient class, invoked when the
- * thread containing this handler is run in the main program.
- * */
- public void run(){
- String line;
- BufferedReader reader = null;
- PrintWriter writer = null;
- try{
- //Initialize the reader and writer so that this socket can communicate with
- //the jclient
- reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
- writer = new PrintWriter(client.getOutputStream(),true);
- } catch (IOException e) {
- e.printStackTrace();
- }
- try{
- line = reader.readLine();
- //A new user is being added if there is a '1' appended to the beginning
- //of the line
- if ((line.charAt(0)=='1')&&(!doesUserExist(inbox,line.substring(1)))){
- //Add the user to the inbox map
- Vector <String> messages = new Vector<String>();
- inbox.put(line.substring(1), messages);
- System.out.println("Adding: "+line.substring(1));
- }
- //A message is being sent to a user if there is a '2' appended to the
- //beginning of the line
- else if (line.charAt(0)=='2'){
- //Search for the first index of the double colons in the line
- int indexOfColons = line.indexOf("::");
- //Extract the sender and recipient's name from the line
- String sender = line.substring(1,indexOfColons);
- String recipient = line.substring(indexOfColons+2);
- //Find the second occurrence of the double colons
- indexOfColons = recipient.indexOf("::");
- //Extract the message
- String message = recipient.substring(indexOfColons+2);
- recipient = recipient.substring(0,indexOfColons);
- message = sender + " : " + message;
- //add the message to the user's inbox
- if (doesUserExist(inbox,recipient)){
- synchronized (inbox.get(recipient)){
- inbox.get(recipient).add(message);
- }
- System.out.println("Adding message to "+recipient+
- " from " + sender+" \""+message+"\"");
- }
- else {
- System.out.println("User does not exist");
- writer.println("User does not exist");
- }
- }
- //User wishes to retrieve their messages
- else if (line.charAt(0)=='3'){
- String userName = line.substring(1);
- if (!doesUserExist(inbox,userName)){
- writer.println("User does not exist");
- System.out.println("User does not exist");
- }
- else if (inbox.get(userName).size()>0){
- //Display the user's messages
- System.out.println("Writing " + userName + "'s messages: ");
- String messages = userName + "'s messages: \n";
- synchronized (inbox.get(userName)){
- for (String message:inbox.get(userName)){
- messages+=message + "\n";
- }
- writer.println(messages);
- //clear all messages retrieved
- inbox.get(userName).clear();
- }
- }
- else {
- System.out.println("No messages to write");
- writer.println("No messages to display");
- }
- }
- writer.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- ServerSocket main = null;
- try {
- main = new ServerSocket(1500);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- while(true) {
- jClientHandler cl;
- try {
- cl = new jClientHandler(main.accept());
- Thread t = new Thread(cl);
- t.start();
- } catch(IOException e) {
- System.out.println("Error: " + e.getMessage());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement