up
This commit is contained in:
parent
8eab4d97c1
commit
ee230f911c
16 changed files with 438 additions and 483 deletions
|
|
@ -43,7 +43,7 @@ public class Server {
|
|||
//Starting Auth service based on RMI
|
||||
try {
|
||||
auth = new AuthService();
|
||||
Registry authRegistry = LocateRegistry.createRegistry(Config.AuthServerPort);
|
||||
Registry authRegistry = LocateRegistry.createRegistry(Config.AuthServicePort);
|
||||
authRegistry.bind("auth", auth);
|
||||
} catch (RemoteException e) {
|
||||
Server.logger.write("SERVER: RMI authentication service error (Remote exception)");
|
||||
|
|
@ -54,7 +54,7 @@ public class Server {
|
|||
|
||||
private void startJedisService(){
|
||||
//Starting Jedis pool for Redis connection
|
||||
jedisPool = new JedisPool(new JedisPoolConfig(), Config.RedisServerURI);
|
||||
jedisPool = new JedisPool(new JedisPoolConfig(), Config.RedisServiceURI);
|
||||
}
|
||||
|
||||
private void startMessageService(){
|
||||
|
|
@ -71,11 +71,11 @@ public class Server {
|
|||
//Starting Notification service based on RMI
|
||||
try {
|
||||
notificationServer = new NotificationService();
|
||||
INotificationServer stub = (INotificationServer) UnicastRemoteObject.exportObject(notificationServer, Config.NotificationServerPort);
|
||||
LocateRegistry.createRegistry(Config.NotificationServerStubPort);
|
||||
INotificationServer stub = (INotificationServer) UnicastRemoteObject.exportObject(notificationServer, Config.NotificationServicePort);
|
||||
LocateRegistry.createRegistry(Config.NotificationServiceStubPort);
|
||||
|
||||
Registry notificationRegistry = LocateRegistry.getRegistry(Config.NotificationServerStubPort);
|
||||
notificationRegistry.bind(Config.NotificationServerName, stub);
|
||||
Registry notificationRegistry = LocateRegistry.getRegistry(Config.NotificationServiceStubPort);
|
||||
notificationRegistry.bind(Config.NotificationServiceName, stub);
|
||||
} catch (RemoteException e) {
|
||||
Server.logger.write("SERVER: RMI notification service error (Remote exception)");
|
||||
} catch (AlreadyBoundException e) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package com.texttwist.server.proxies;
|
||||
package com.texttwist.server.dispatchers;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import com.texttwist.server.managers.SessionsManager;
|
||||
import com.texttwist.server.models.Sessions;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.*;
|
||||
import models.Message;
|
||||
|
|
@ -33,7 +33,7 @@ public class MessageDispatcher implements Callable<Boolean> {
|
|||
public Boolean call() {
|
||||
bufferMessage = ByteBuffer.allocate(1024);
|
||||
byte[] byteMessage = null;
|
||||
if(SessionsManager.getInstance().isValidToken(request.token)){
|
||||
if(Sessions.getInstance().isValidToken(request.token)){
|
||||
switch(request.message){
|
||||
|
||||
case "START_GAME":
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.texttwist.server.managers;
|
||||
package com.texttwist.server.models;
|
||||
|
||||
import com.texttwist.server.services.JedisService;
|
||||
import models.User;
|
||||
|
|
@ -8,21 +8,21 @@ import java.util.*;
|
|||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 18/06/2017.
|
||||
* Description: AccountsManager
|
||||
* Description: Accounts
|
||||
*/
|
||||
public class AccountsManager {
|
||||
public class Accounts {
|
||||
|
||||
public List<User> users = Collections.synchronizedList(new ArrayList<User>());
|
||||
|
||||
private static class Holder {
|
||||
static final AccountsManager INSTANCE = new AccountsManager();
|
||||
static final Accounts INSTANCE = new Accounts();
|
||||
}
|
||||
|
||||
public static AccountsManager getInstance() {
|
||||
return AccountsManager.Holder.INSTANCE;
|
||||
public static Accounts getInstance() {
|
||||
return Accounts.Holder.INSTANCE;
|
||||
}
|
||||
|
||||
private AccountsManager(){
|
||||
private Accounts(){
|
||||
List<Serializable> l = JedisService.get("users");
|
||||
for(int i=0; i<l.size(); i++) {
|
||||
users.add((User) l.get(i));
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.texttwist.server.managers;
|
||||
package com.texttwist.server.models;
|
||||
import models.Session;
|
||||
import models.User;
|
||||
|
||||
|
|
@ -9,17 +9,17 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 17/06/2017.
|
||||
* Description: SessionsManager. It is a singleton that provides the model and methods for manage sessions
|
||||
* Description: Sessions. It is a singleton that provides the model and methods for manage sessions
|
||||
*/
|
||||
public class SessionsManager {
|
||||
public class Sessions {
|
||||
|
||||
private List<Session> sessions = Collections.synchronizedList(new ArrayList<Session>());
|
||||
|
||||
private static class Holder {
|
||||
static final SessionsManager INSTANCE = new SessionsManager();
|
||||
static final Sessions INSTANCE = new Sessions();
|
||||
}
|
||||
|
||||
public static SessionsManager getInstance() {
|
||||
public static Sessions getInstance() {
|
||||
return Holder.INSTANCE;
|
||||
}
|
||||
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
package com.texttwist.server.services;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import com.texttwist.server.managers.AccountsManager;
|
||||
import com.texttwist.server.managers.SessionsManager;
|
||||
import com.texttwist.server.models.Accounts;
|
||||
import com.texttwist.server.models.Sessions;
|
||||
import constants.Config;
|
||||
import interfaces.IAuth;
|
||||
import interfaces.INotificationClient;
|
||||
|
|
@ -24,14 +24,14 @@ public class AuthService extends UnicastRemoteObject implements IAuth {
|
|||
private SecureRandom random = new SecureRandom();
|
||||
|
||||
public AuthService() throws RemoteException{
|
||||
Server.logger.write("AuthService Service running at "+ Config.AuthServerPort+" port...");
|
||||
Server.logger.write("AuthService Service running at "+ Config.AuthServicePort +" port...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response register(String userName, String password) throws RemoteException {
|
||||
Server.logger.write("Invoked register with username=" + userName + " AND " + " password=" + password);
|
||||
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
|
||||
if(AccountsManager.getInstance().register(userName, password)){
|
||||
if(Accounts.getInstance().register(userName, password)){
|
||||
Server.logger.write("Registration successfull");
|
||||
return new Response("Registration successfull", 200, null);
|
||||
} else {
|
||||
|
|
@ -46,11 +46,11 @@ public class AuthService extends UnicastRemoteObject implements IAuth {
|
|||
public Response login(String userName, String password) throws RemoteException {
|
||||
Server.logger.write("Invoked login with username=" + userName + " AND " + " password=" + password);
|
||||
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
|
||||
if(AccountsManager.getInstance().exists(userName) && AccountsManager.getInstance().checkPassword(userName, password)) {
|
||||
if(Accounts.getInstance().exists(userName) && Accounts.getInstance().checkPassword(userName, password)) {
|
||||
JsonObject data = new JsonObject();
|
||||
String token = nextSessionId();
|
||||
data.put("token", token);
|
||||
SessionsManager.getInstance().add(userName,token);
|
||||
Sessions.getInstance().add(userName,token);
|
||||
Server.logger.write("Login successfull");
|
||||
return new Response("Login successfull", 200, data);
|
||||
}
|
||||
|
|
@ -65,14 +65,14 @@ public class AuthService extends UnicastRemoteObject implements IAuth {
|
|||
notificationServer.unregisterForCallback(stub);
|
||||
|
||||
if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) {
|
||||
boolean res = SessionsManager.getInstance().remove(userName);
|
||||
boolean res = Sessions.getInstance().remove(userName);
|
||||
if(res) {
|
||||
Server.logger.write("Logout successfull");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SessionsManager.getInstance().remove(userName);
|
||||
Sessions.getInstance().remove(userName);
|
||||
Server.logger.write("Logout successfull (but something gone wrong)");
|
||||
return new Response("Logout successfull (but something gone wrong)", 200, null);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package com.texttwist.server.services;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import com.texttwist.server.proxies.MessageDispatcher;
|
||||
import com.texttwist.server.dispatchers.MessageDispatcher;
|
||||
import com.texttwist.server.models.Dictionary;
|
||||
import constants.Config;
|
||||
import models.Message;
|
||||
|
|
@ -27,7 +27,7 @@ public class MessageService implements Runnable{
|
|||
private String dictionaryPath = "./Server/resources/dictionary";
|
||||
public static Dictionary dict;
|
||||
|
||||
public static Integer multicastId = Config.NotificationServerStubPort;
|
||||
public static Integer multicastId = Config.NotificationServiceStubPort;
|
||||
|
||||
public MessageService()
|
||||
{
|
||||
|
|
@ -37,9 +37,9 @@ public class MessageService implements Runnable{
|
|||
|
||||
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
|
||||
serverSocketChannel.configureBlocking(false);
|
||||
serverSocketChannel.socket().bind(new InetSocketAddress(Config.GameServerPort));
|
||||
serverSocketChannel.socket().bind(new InetSocketAddress(Config.MessageServicePort));
|
||||
serverSocketChannel.register(selector, OP_ACCEPT);
|
||||
Server.logger.write("GameService Service is running at "+Config.GameServerPort+" port...");
|
||||
Server.logger.write("GameService Service is running at "+Config.MessageServicePort +" port...");
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ public class NotificationService implements INotificationServer {
|
|||
private List<INotificationClient> clients;
|
||||
public NotificationService() throws RemoteException {
|
||||
super();
|
||||
Server.logger.write("Notification Service running at "+ Config.NotificationServerPort+" port...");
|
||||
Server.logger.write("Notification Service running at "+ Config.NotificationServicePort +" port...");
|
||||
clients = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package com.texttwist.server.services;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import com.texttwist.server.managers.SessionsManager;
|
||||
import com.texttwist.server.models.Sessions;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.ComputeScore;
|
||||
import constants.Config;
|
||||
|
|
@ -20,7 +20,7 @@ public class ReceiveWordsService implements Runnable {
|
|||
private ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
|
||||
public ReceiveWordsService() {
|
||||
Server.logger.write("ReceiveWords Service running at "+Config.WordsReceiverServerPort+" port...");
|
||||
Server.logger.write("ReceiveWords Service running at "+Config.WordsReceiverServicePort +" port...");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -29,7 +29,7 @@ public class ReceiveWordsService implements Runnable {
|
|||
DatagramSocket s = null;
|
||||
|
||||
try {
|
||||
s = new DatagramSocket(Config.WordsReceiverServerPort);
|
||||
s = new DatagramSocket(Config.WordsReceiverServicePort);
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
@ -48,7 +48,7 @@ public class ReceiveWordsService implements Runnable {
|
|||
String rcv = new String(packet.getData());
|
||||
if (rcv.startsWith("MESSAGE")) {
|
||||
msg = Message.toMessage(rcv);
|
||||
if(SessionsManager.getInstance().isValidToken(msg.token)) {
|
||||
if(Sessions.getInstance().isValidToken(msg.token)) {
|
||||
Match match = Match.findMatchByPlayerName(msg.sender);
|
||||
threadPool.submit(new ComputeScore(msg.sender, msg.data, match));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.managers.SessionsManager;
|
||||
import com.texttwist.server.models.Sessions;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
|
@ -19,7 +19,7 @@ public class CheckOnlineUsers implements Callable<Boolean> {
|
|||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
for(int i = 0; i < users.size(); i++){
|
||||
if(!(SessionsManager.getInstance().exists(users.get(i)))){
|
||||
if(!(Sessions.getInstance().exists(users.get(i)))){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.managers.AccountsManager;
|
||||
import com.texttwist.server.models.Accounts;
|
||||
import com.texttwist.server.services.JedisService;
|
||||
import models.User;
|
||||
|
||||
|
|
@ -18,16 +18,16 @@ public class ComputeHighscores implements Callable<DefaultListModel<String>> {
|
|||
public DefaultListModel<String> call() throws Exception {
|
||||
DefaultListModel<String> l = new DefaultListModel<>();
|
||||
|
||||
AccountsManager.getInstance().users.sort(new Comparator<User>() {
|
||||
Accounts.getInstance().users.sort(new Comparator<User>() {
|
||||
@Override
|
||||
public int compare(User o1, User o2) {
|
||||
return o2.score.compareTo(o1.score);
|
||||
}
|
||||
});
|
||||
JedisService.removeAll("users");
|
||||
for(int i = 0; i< AccountsManager.getInstance().users.size(); i++){
|
||||
l.addElement(AccountsManager.getInstance().users.get(i).userName+":"+ AccountsManager.getInstance().users.get(i).score);
|
||||
JedisService.add("users", AccountsManager.getInstance().users.get(i));
|
||||
for(int i = 0; i< Accounts.getInstance().users.size(); i++){
|
||||
l.addElement(Accounts.getInstance().users.get(i).userName+":"+ Accounts.getInstance().users.get(i).score);
|
||||
JedisService.add("users", Accounts.getInstance().users.get(i));
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.managers.AccountsManager;
|
||||
import com.texttwist.server.models.Accounts;
|
||||
import com.texttwist.server.models.Dictionary;
|
||||
import com.texttwist.server.models.Match;
|
||||
import models.User;
|
||||
|
|
@ -35,7 +35,7 @@ public class ComputeScore implements Callable<Integer> {
|
|||
}
|
||||
match.setScore(sender, score);
|
||||
|
||||
User u = AccountsManager.getInstance().findUser(sender);
|
||||
User u = Accounts.getInstance().findUser(sender);
|
||||
u.addScore(score);
|
||||
|
||||
if(match.allPlayersSendedHisScore()) {
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ public class SendScores implements Callable<Void> {
|
|||
MulticastSocket multicastSocket = null;
|
||||
try {
|
||||
multicastSocket = new MulticastSocket(match.multicastId);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServiceURI);
|
||||
DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId);
|
||||
multicastSocket.send(hi);
|
||||
} catch (IOException e) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue