not been able to validate with the tow throw IOException conditions for both the methods.
package com.codegym.task.task30.task3008;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;;
import java.net.SocketAddress;
public class Server {
private static Map<String, Connection> connectionMap;
static {
connectionMap = new ConcurrentHashMap();
}
public static void main(String[] args) throws IOException{
int port = ConsoleHelper.readInt();
ServerSocket ss = new ServerSocket(port);
if(ss != null) {
System.out.println("Server is running");
}
Socket s = null;
try
{
while(true){
// socket object to receive incoming client requests
s = ss.accept();
// create a new thread object
Thread t = new Handler(s);
t.start();
}
}
catch (Exception e){
System.out.println(e.getMessage());
ss.close();
s.close();
}
}
public static void sendBroadcastMessage(Message message){
for(Connection c : connectionMap.values()){
try {
c.send(message);
} catch (IOException e) {
ConsoleHelper.writeMessage("Message couldn't be sent.");
}
}
}
private static class Handler extends Thread {
Socket socket;
public Handler(Socket socket) {
this.socket = socket;
}
private String serverHandshake(Connection connection) throws IOException,ClassNotFoundException {
//String str;
//boolean nameAlreadyExist = false;
Message response;
while(true){
connection.send(new Message(MessageType.NAME_REQUEST));
response = connection.receive();
if (response.getType().equals(MessageType.USER_NAME)) {
String data = response.getData();
if (data != null && !(data.isEmpty())) {
if(!(connectionMap.containsKey(data))) {
connectionMap.put(data, connection);
connection.send(new Message(MessageType.NAME_ACCEPTED));
return data;
}
}
}
}
}
private void notifyUsers(Connection connection,String UserName) throws IOException {
for(Map.Entry<String, Connection> entry : connectionMap.entrySet()){
if(!entry.getKey().equals(UserName)){
connection.send(new Message(MessageType.USER_ADDED,entry.getKey()));
}
}
}
private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException {
while(true){
try{
Message response = connection.receive();
if(response.getType().equals(MessageType.TEXT)){
String data = userName + ": " + response.getData();
sendBroadcastMessage(new Message(MessageType.TEXT,data));
}else{
ConsoleHelper.writeMessage("Error in receiving message");
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
public void run(){
Connection connection = null;
String userName = null;
do {
try {
ConsoleHelper.writeMessage("New connection was established with a remote address " + socket.getRemoteSocketAddress());
connection = new Connection(socket);
} catch (Exception ig) {
break;
}
try {
userName = serverHandshake(connection);
} catch (Exception ig) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try {
sendBroadcastMessage(new Message(MessageType.USER_ADDED, userName));
notifyUsers(connection, userName);
serverMainLoop(connection, userName);
} catch (Exception e) {
} finally {
connectionMap.remove(userName);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, userName));
try {
connection.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} while (false);
ConsoleHelper.writeMessage("Connection closed: " + socket.getRemoteSocketAddress());
/*Connection connection = null;
String userName = null;
try{
//get remote address
ConsoleHelper.writeMessage("A connection has been established with the remote adddress " + socket.getRemoteSocketAddress());
//Make the connection
connection = new Connection(socket);
//establishes connection with new user
userName = serverHandshake(connection);
sendBroadcastMessage(new Message(MessageType.USER_ADDED,userName));
notifyUsers(connection,userName);
serverMainLoop(connection,userName);
}catch(IOException e){
//System.out.println(e.getMessage());
try{
/*if(userName != null ){
connectionMap.remove(userName);
connection.send(new Message(MessageType.USER_REMOVED));
}
if(connection != null){
connection.close();
ConsoleHelper.writeMessage("The connection with the remote address is closed");
}
}catch(Exception except){
except.printStackTrace();
}
}catch(ClassNotFoundException e1) {
System.out.println("An error occured while communication with the remote address");
try{
/*if(userName != null){
connectionMap.remove(userName);
connection.send(new Message(MessageType.USER_REMOVED));
}
if(connection != null){
try{
connection.close();
ConsoleHelper.writeMessage("The connection with the remote address is closed");
}catch(Exception ex1){
ex1.printStackTrace();
}
}
}catch(Exception en){
en.printStackTrace();
}
}finally{
try{
if(userName != null){
connectionMap.remove(userName);
connection.send(new Message(MessageType.USER_REMOVED));
}
}catch(Exception x){
}
}
ConsoleHelper.writeMessage("Connection closed: " + socket.getRemoteSocketAddress());*/
}
}
}