Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
</properties>

<dependencies>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/mycompany/tictactoeserver/App.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.mycompany.tictactoeserver;

import com.mycompany.tictactoeserver.datasource.database.Database;
import com.mycompany.tictactoeserver.domain.services.communication.MessageRouter;
import com.mycompany.tictactoeserver.domain.utils.exception.DatabaseConnectionException;
import com.mycompany.tictactoeserver.domain.utils.exception.ExceptionHandlerMiddleware;
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;

public class App extends Application {

private static Scene scene;
Expand Down Expand Up @@ -59,6 +57,6 @@ private void setupDependencies() {
ExceptionHandlerMiddleware.getInstance().handleException(e);
}

MessageRouter.getInstance();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.mycompany.tictactoeserver.domain.entity;/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/


/**
*
* @author Hazem
*/
public class AuthRequestEntity {
private String userName ;
private String password;

public AuthRequestEntity(String userName, String password) {
this.userName = userName;
this.password = password;
}

public void setUserName(String userName) {
this.userName = userName;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "AuthRequestEntity{" + "userName=" + userName + ", password=" + password + '}';
}

public AuthRequestEntity() {
}

public String getUserName() {
return userName;
}

public String getPassword() {
return password;
}

public static AuthRequestEntity createAuthEntity(String username , String password)
{
return new AuthRequestEntity(username , password );
}



}
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package com.mycompany.tictactoeserver.domain.entity;

import com.mycompany.tictactoeserver.datasource.model.Player;
import org.json.JSONObject;

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/

/**
*
* @author Hazem
*/
public class AuthResponseEntity {
String id;
String username;
int score;

public AuthResponseEntity() {
private String id;
private String username;
private int score;

public AuthResponseEntity(String id, String userName, int Score) {
this.id = id;
this.username = userName;
this.score = Score;
}

public AuthResponseEntity(Player player) {
Expand All @@ -17,15 +29,40 @@ public AuthResponseEntity(Player player) {
score = player.getScore();
}

public JSONObject toJson() {
JSONObject json = new JSONObject();
public AuthResponseEntity() {
}

json.put("id", id);
json.put("username", username);
json.put("score", score);
public void setId(String id) {
this.id = id;
}

public void setUserName(String userName) {
this.username = userName;
}

return json;
public void setScore(int Score) {
this.score = Score;
}

public String getId() {
return id;
}

public String getUserName() {
return username;
}

public int getScore() {
return score;
}

public static AuthResponseEntity createAuthEntity(String id, String username, int score) {
return new AuthResponseEntity(id, username, score);
}

@Override
public String toString() {
return "AuthRequestEntity{" + "id=" + id + ", userName=" + username + ", Score=" + score + '}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mycompany.tictactoeserver.domain.utils.exception.ExceptionHandlerMiddleware;
import com.mycompany.tictactoeserver.domain.utils.exception.PlayerSendMessageException;
import com.mycompany.tictactoeserver.domain.utils.exception.ServerInterruptException;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -75,12 +74,12 @@ public void sendMessage(String message, PlayerConnectionHandler player) throws P
}

public void onReceiveMessage(String message, PlayerConnectionHandler sender) {
MessageRouter router = MessageRouter.getInstance();

try {
MessageRouter router = MessageRouter.getInstance();
System.out.println("testingggggggggg");
router.navigateMessage(message, sender);
} catch (PlayerSendMessageException e) {
ExceptionHandlerMiddleware.getInstance().handleException(e);
} catch (PlayerSendMessageException ex) {
System.getLogger(GameServerManager.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex);
}
}

Expand Down Expand Up @@ -151,8 +150,11 @@ public void run() {
System.out.println("Server Accepting...");
Socket clientSocket = serverSocket.accept();
if (isRunning) {
onAdd.call(new PlayerConnectionHandler(clientSocket));
PlayerConnectionHandler player = new PlayerConnectionHandler(clientSocket);
onAdd.call(player);
System.out.println("Client Connected: " + clientSocket.getInetAddress().getHostAddress());


} else {
clientSocket.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.mycompany.tictactoeserver.domain.server;

import com.google.gson.Gson;
import com.mycompany.tictactoeserver.datasource.model.Player;
import com.mycompany.tictactoeserver.domain.entity.AuthRequestEntity;
import com.mycompany.tictactoeserver.domain.services.communication.Message;
import com.mycompany.tictactoeserver.domain.utils.callbacks.StringCallback;
import com.mycompany.tictactoeserver.domain.utils.callbacks.VoidCallback;
import com.mycompany.tictactoeserver.domain.utils.exception.ExceptionHandlerMiddleware;
import com.mycompany.tictactoeserver.domain.utils.exception.PlayerConnectionException;
import com.mycompany.tictactoeserver.domain.utils.exception.PlayerSendMessageException;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
Expand All @@ -26,8 +28,12 @@ public class PlayerConnectionHandler {
thread.start();
}

public void sendMessageToPlayer(String message) throws PlayerSendMessageException {
runnable.sendMessageToSocket(message);
public void sendMessageToPlayer(String message) {
try {
runnable.sendMessageToSocket(message);
} catch (PlayerSendMessageException e) {
System.out.println("Excetion sending message");
}
}

public void receiveMessageFromPlayer(String message) {
Expand Down Expand Up @@ -100,6 +106,7 @@ public void run() {
}
System.out.println("Player Reading...");
String msg = in.readUTF();

System.out.println("in: ---> " + msg);
onReceive.call(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

import com.mycompany.tictactoeserver.datasource.database.dao.PlayerDAO;
import com.mycompany.tictactoeserver.datasource.model.Player;
import com.mycompany.tictactoeserver.domain.entity.AuthRequestEntity;
import com.mycompany.tictactoeserver.domain.entity.AuthResponseEntity;
import com.mycompany.tictactoeserver.domain.services.communication.Action;
import com.mycompany.tictactoeserver.domain.services.communication.Header;
import com.mycompany.tictactoeserver.domain.services.communication.Message;
import com.mycompany.tictactoeserver.domain.services.communication.MessageType;
import com.mycompany.tictactoeserver.domain.services.playerSession.PlayerSessionService;
import com.mycompany.tictactoeserver.domain.services.security.ServerSecurityManager;
import com.mycompany.tictactoeserver.domain.utils.exception.ExceptionHandlerMiddleware;
import com.mycompany.tictactoeserver.domain.utils.exception.HashingException;
import com.mycompany.tictactoeserver.domain.utils.exception.ServerInterruptException;
import org.json.JSONObject;

/**
*
* @author Hazem
*
*/
public class AuthenticationService {


private static AuthenticationService instance;
private final PlayerDAO playerDao;
private final PlayerSessionService playerSessionService;
Expand All @@ -27,82 +30,77 @@ private AuthenticationService() {
}

public static AuthenticationService getInstance() {
if (instance == null)
if (instance == null) {
instance = new AuthenticationService();

}
return instance;
}

public Message register(AuthRequestEntity credential) {
Message response;

public Message register(String username, String plainTextPassword) {
System.out.println(credential.getUserName() + " " + credential.getPassword());
if (credential.getUserName() == null || credential.getPassword() == null) {
response = Message.createMessage(MessageType.ERROR, Action.USERNAME_NOT_FOUND, credential);
}

Player player = playerDao.findByUsername(username);
Player player = playerDao.findByUsername(credential.getUserName());

if (player != null) {
JSONObject json = new JSONObject();
json.put("error", "Username already exists");
return new Message(new Header(MessageType.ERROR, Action.REGISTER), json);
}

AuthResponseEntity responseEntity;
response = Message.createMessage(MessageType.ERROR, Action.USERNAME_ALREADY_EXIST, credential);
}

try {

String hashedPassword = ServerSecurityManager.hashText(plainTextPassword);
Player newPlayer = new Player(username, hashedPassword);
String hashedPassword = ServerSecurityManager.hashText(credential.getPassword());
Player newPlayer = new Player(credential.getUserName(), hashedPassword);
playerDao.insert(newPlayer);
responseEntity = new AuthResponseEntity(newPlayer);

} catch (HashingException ex) {
AuthResponseEntity responseEntity = new AuthResponseEntity(newPlayer);
response = Message.createMessage(MessageType.RESPONSE, Action.REGISTERATION_SUCCESS, responseEntity);

} catch (HashingException ex) {
ServerInterruptException customException = new ServerInterruptException(ex.getStackTrace());
ExceptionHandlerMiddleware.getInstance().handleException(customException);

JSONObject json = new JSONObject();
json.put("error", "Internal Server Error");
return new Message(new Header(MessageType.ERROR, Action.REGISTER), json);

response = Message.createMessage(MessageType.ERROR, Action.INTERNAL_SERVER_ERROR, credential);
}


return new Message(new Header(MessageType.RESPONSE, Action.REGISTER), responseEntity.toJson());

return response;
}

public Message login(String username, String plainTextPassword) {
public Message login(AuthRequestEntity credential) {
Message response;
if (credential == null || credential.getUserName() == null || credential.getPassword() == null) {
response = Message.createMessage(MessageType.ERROR, Action.USERNAME_NOT_FOUND, credential);
}

try {

Player player = playerDao.findByUsername(username);
Player player = playerDao.findByUsername(credential.getUserName());

if (player == null) {
JSONObject json = new JSONObject();
json.put("error", "Invalid Credentials");
return new Message(new Header(MessageType.ERROR, Action.LOGIN), json);
}

response = Message.createMessage(MessageType.ERROR, Action.USERNAME_NOT_FOUND, credential);
return response;
}

String hashedPassword = ServerSecurityManager.hashText(plainTextPassword);
String hashedPassword = ServerSecurityManager.hashText(credential.getPassword());
if (!hashedPassword.equals(player.getPassword())) {
JSONObject json = new JSONObject();
json.put("error", "Invalid Credentials");
return new Message(new Header(MessageType.ERROR, Action.LOGIN), json);

response = Message.createMessage(MessageType.ERROR, Action.INVALID_CREDENTIAL, credential);
}

playerSessionService.startPlayerSession(player.getId());

AuthResponseEntity responseEntity = new AuthResponseEntity(player);
return new Message(new Header(MessageType.RESPONSE, Action.LOGIN), responseEntity.toJson());
response = Message.createMessage(MessageType.RESPONSE, Action.LOGIN_SUCCESS, responseEntity);

} catch (HashingException ex) {
ServerInterruptException customException = new ServerInterruptException(ex.getStackTrace());
ExceptionHandlerMiddleware.getInstance().handleException(customException);

JSONObject json = new JSONObject();
json.put("error", "Internal Server Error");
return new Message(new Header(MessageType.ERROR, Action.LOGIN), json);
response = Message.createMessage(MessageType.ERROR, Action.INTERNAL_SERVER_ERROR, credential);

}
return response;
}


}
Loading