Skip to content
Snippets Groups Projects
Commit d999d524 authored by am13g23's avatar am13g23
Browse files

Upload New File

parent cedaf17c
No related branches found
No related tags found
No related merge requests found
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Server {
private static HashMap<SocketDetails, String[]> sessions;
private static HashMap<String, ArrayList<SocketDetails>> chats;
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(2001); //initialise server socket, sessions hash map and chats hash map
sessions = new HashMap<SocketDetails, String[]>();
chats = new HashMap<String, ArrayList<SocketDetails>>();
while (true) { //continuously listen for socket connections
Socket socket = serverSocket.accept(); //accept any socket connection
BufferedReader listener = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String[] message = listener.readLine().split("/"); //reads in sent message
SocketDetails sd = new SocketDetails(socket, Integer.parseInt(message[1]));
String response = "Invalid request";
if (message[0].equals("open")){ //if message is an open session message, add the key value pair of the socket details (a unique identifier of each session) and a string array containing the username and the current chat
sessions.put(sd,new String[]{message[2],"notInAChat"});
response = "Session started"; //set response to confirm the session has started
}
else if (message[0].equals("close")){ //if message is an open session message, add the key value pair of the socket details (a unique identifier of each session) and a string array containing the username and the current chat
if (chats.containsKey(sessions.get(sd)[1])){ //if the user is in a chat
if (chats.get(sessions.get(sd)[1]).size() == 1) { //if the chat contains only 1 member, delete it
chats.remove(sessions.get(sd)[1]);
}
else{ //else remove the user from the chat and inform all members on the chat
chats.get(sessions.get(sd)[1]).remove(sd);
sendMessage(sessions.get(sd)[0] + " has left the chat", sessions.get(sd)[1]);
}
}
sessions.remove(sd);
response = "Session ended"; //set response to confirm the session has ended
}
else if (message[0].equals("join")){ //if the message is a join chat message
sessions.get(sd)[1] = message[2]; //set the chat in the session
if (chats.containsKey(message[2])){ //if the chat already exists, add the socket details to the chat members list and send a message to all chat members that the new user has joined
sendMessage(sessions.get(sd)[0] + " has joined the chat", message[2]);
chats.get(message[2]).add(sd);
response = "Joined chat " + message[2]; //set response to confirm the user has joined the chat
}
else { //else we need to create a new chat, so add a new key value pair of the chat name and list of users and add the socket details of the current user
chats.put(message[2], new ArrayList<SocketDetails>());
chats.get(message[2]).add(sd);
response = "Created chat " + message[2]; //set response to confirm the chat has been created
}
}
else if (message[0].equals("leave")){ //if the message is a join chat message
if (chats.containsKey(sessions.get(sd)[1])){ //if the user is in a chat
if (chats.get(sessions.get(sd)[1]).size() == 1) { //if the chat contains only 1 member, delete it
chats.remove(sessions.get(sd)[1]);
response = "Deleted chat " + sessions.get(sd)[1]; //set response to confirm the chat has been deleted
sessions.get(sd)[1] = "NotInAChat";
}
else{ //else remove the user from the chat and inform all members on the chat
chats.get(sessions.get(sd)[1]).remove(sd);
sendMessage(sessions.get(sd)[0] + " has left the chat", sessions.get(sd)[1]);
response = "Left chat " + sessions.get(sd)[1]; //set response to confirm the user has left the chat
sessions.get(sd)[1] = "NotInAChat";
}
}
else { //else the user is not in a chat and hence cannot leave
response = "Invalid request as not in chat"; //set response to say that you need to join a chat first
}
}
else if (message[0].equals("message")) { //if the message is a chat message
if (chats.containsKey(sessions.get(sd)[1])) { //if the user is in a chat, send the message
sendMessage(sessions.get(sd)[0] + " - " + message[2], sessions.get(sd)[1]);
response = "Sent message"; //set response to confirm the user has joined the chat
} else { //else set response to say that you need to join a chat first
response = "Invalid request as not in chat";
}
}
PrintWriter sender = new PrintWriter(socket.getOutputStream()); //once the message is processed send back a response and close the socket
sender.println(response);
sender.flush();
socket.close();
}
}
public static void sendMessage(String message, String chat) throws Exception{ //method to send a specified message to every member in a chat
for (SocketDetails sd: chats.get(chat)) { //loops through socket details
Socket socket = sd.getSocket(); //opens socket
PrintWriter sender = new PrintWriter(socket.getOutputStream()); //sends message
sender.println(message);
sender.flush();
socket.close(); //closes socket
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment