UPPPP FINISH
This commit is contained in:
parent
e1622dfaa1
commit
ab307b0a97
24 changed files with 857 additions and 526 deletions
|
|
@ -42,8 +42,8 @@ public class Server {
|
|||
private void startAuthService(){
|
||||
//Starting Auth service based on RMI
|
||||
try {
|
||||
auth = new AuthService(Config.AuthServerPort);
|
||||
Registry authRegistry = LocateRegistry.createRegistry(auth.serverPort);
|
||||
auth = new AuthService();
|
||||
Registry authRegistry = LocateRegistry.createRegistry(Config.AuthServerPort);
|
||||
authRegistry.bind("auth", auth);
|
||||
} catch (RemoteException e) {
|
||||
Server.logger.write("SERVER: RMI authentication service error (Remote exception)");
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import java.util.concurrent.*;
|
|||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 18/06/2017.
|
||||
* Description: Proxy Dispatcher
|
||||
* Description: Message Dispatcher
|
||||
* */
|
||||
public class MessageDispatcher implements Callable<Boolean> {
|
||||
private final ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.texttwist.server.services;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import constants.Config;
|
||||
import interfaces.IAuth;
|
||||
import interfaces.INotificationClient;
|
||||
import models.Response;
|
||||
|
|
@ -19,12 +20,9 @@ import static com.texttwist.server.Server.notificationServer;
|
|||
public class AuthService extends UnicastRemoteObject implements IAuth {
|
||||
|
||||
private SecureRandom random = new SecureRandom();
|
||||
public int serverPort = 9999;
|
||||
|
||||
|
||||
public AuthService(int serverPort) throws RemoteException{
|
||||
this.serverPort=serverPort;
|
||||
Server.logger.write("AuthService Service running at "+serverPort+" port...");
|
||||
public AuthService() throws RemoteException{
|
||||
Server.logger.write("AuthService Service running at "+ Config.AuthServerPort+" port...");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.texttwist.server.services;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import models.User;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
|
|
@ -16,6 +17,11 @@ import static com.texttwist.server.Server.jedisPool;
|
|||
*/
|
||||
public class JedisService {
|
||||
|
||||
|
||||
JedisService(){
|
||||
Server.logger.write("Jedis Service running on localhost...");
|
||||
}
|
||||
|
||||
/** Read the object from Base64 string. */
|
||||
public static Object fromString(String s) throws IOException, ClassNotFoundException {
|
||||
byte [] data = Base64.getDecoder().decode(s);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.texttwist.server.services;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import constants.Config;
|
||||
import interfaces.INotificationClient;
|
||||
import interfaces.INotificationServer;
|
||||
|
||||
|
|
@ -12,13 +14,14 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 19/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Notification Service
|
||||
*/
|
||||
public class NotificationService implements INotificationServer {
|
||||
|
||||
private List<INotificationClient> clients;
|
||||
public NotificationService() throws RemoteException {
|
||||
super();
|
||||
Server.logger.write("Notification Service running at "+ Config.NotificationServerPort+" port...");
|
||||
clients = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,34 +1,29 @@
|
|||
package com.texttwist.server.services;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.ComputeScore;
|
||||
import com.texttwist.server.tasks.TokenInvalid;
|
||||
import constants.Config;
|
||||
import models.Message;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 27/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Receive Words Service
|
||||
*/
|
||||
public class ReceiveWordsService implements Runnable {
|
||||
|
||||
private ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
|
||||
public ReceiveWordsService() {
|
||||
|
||||
Server.logger.write("ReceiveWords Service running at "+Config.WordsReceiverServerPort+" port...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
|
||||
|
||||
Message msg;
|
||||
DatagramSocket s = null;
|
||||
|
||||
|
|
@ -40,7 +35,6 @@ public class ReceiveWordsService implements Runnable {
|
|||
DatagramPacket packet;
|
||||
|
||||
while(true) {
|
||||
|
||||
byte[] buf = new byte[1024];
|
||||
|
||||
packet = new DatagramPacket(buf, buf.length);
|
||||
|
|
|
|||
|
|
@ -33,8 +33,6 @@ public class SessionsService {
|
|||
return null;
|
||||
}
|
||||
|
||||
public SessionsService(){}
|
||||
|
||||
public boolean add(String userName, String token) {
|
||||
remove(userName);
|
||||
return sessions.add(new Session(new User(userName,"",0), token));
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.services.SessionsService;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 19/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Task: Check Online Users
|
||||
*/
|
||||
public class CheckOnlineUsers implements Callable<Boolean> {
|
||||
private final DefaultListModel<String> users;
|
||||
|
|
|
|||
|
|
@ -10,12 +10,10 @@ import java.util.concurrent.Callable;
|
|||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 28/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Task: Compute Highscores
|
||||
*/
|
||||
public class ComputeHighscores implements Callable<DefaultListModel<String>> {
|
||||
|
||||
public ComputeHighscores(){}
|
||||
|
||||
@Override
|
||||
public DefaultListModel<String> call() throws Exception {
|
||||
DefaultListModel<String> l = new DefaultListModel<>();
|
||||
|
|
|
|||
|
|
@ -1,23 +1,22 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.services.AccountsService;
|
||||
import com.texttwist.server.models.Dictionary;
|
||||
import com.texttwist.server.models.Match;
|
||||
import javafx.util.Pair;
|
||||
import models.User;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 27/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Task: Ccmpute Score
|
||||
*/
|
||||
public class ComputeScore implements Callable<Integer> {
|
||||
|
||||
private DefaultListModel<String> words;
|
||||
private final String sender;
|
||||
public Match match;
|
||||
private DefaultListModel<String> wordsValid;
|
||||
private DefaultListModel<String> wordsValid = new DefaultListModel<>();
|
||||
|
||||
public ComputeScore(String sender, DefaultListModel<String> words, Match match){
|
||||
this.words = words;
|
||||
|
|
@ -27,26 +26,24 @@ public class ComputeScore implements Callable<Integer> {
|
|||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
wordsValid = new DefaultListModel<>();
|
||||
Integer score = 0;
|
||||
|
||||
for (int i = 0; i < words.size(); i++) {
|
||||
if (isValid(words.get(i), match.letters)) {
|
||||
score += words.get(i).length();
|
||||
wordsValid.addElement(words.get(i));
|
||||
}
|
||||
Integer score = 0;
|
||||
for (int i = 0; i < words.size(); i++) {
|
||||
if (isValid(words.get(i), match.letters)) {
|
||||
score += words.get(i).length();
|
||||
wordsValid.addElement(words.get(i));
|
||||
}
|
||||
match.setScore(sender, score);
|
||||
}
|
||||
match.setScore(sender, score);
|
||||
|
||||
User u = AccountsService.getInstance().findUser(sender);
|
||||
u.addScore(score);
|
||||
User u = AccountsService.getInstance().findUser(sender);
|
||||
u.addScore(score);
|
||||
|
||||
if(match.allPlayersSendedHisScore()) {
|
||||
match.matchTimeout = false;
|
||||
match.setUndefinedScorePlayersToZero();
|
||||
new SendScores(match).call();
|
||||
}
|
||||
return score;
|
||||
if(match.allPlayersSendedHisScore()) {
|
||||
match.matchTimeout = false;
|
||||
match.setUndefinedScorePlayersToZero();
|
||||
new SendScores(match).call();
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
private Boolean isValid(String word, DefaultListModel<String> letters) {
|
||||
|
|
|
|||
|
|
@ -1,24 +1,18 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.services.MessageService;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 25/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Task: Generate Letters
|
||||
*/
|
||||
public class GenerateLetters implements Callable<DefaultListModel<String>> {
|
||||
|
||||
|
||||
public GenerateLetters(){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultListModel<String> call() throws Exception {
|
||||
DefaultListModel<String> l = new DefaultListModel<String>();
|
||||
DefaultListModel<String> l = new DefaultListModel<>();
|
||||
|
||||
String word = MessageService.dict.getRandomWord(6, 7);
|
||||
for (int i = 0;i < word.length(); i++){
|
||||
|
|
|
|||
|
|
@ -8,12 +8,12 @@ import java.util.concurrent.Callable;
|
|||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 23/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Task: Join Match
|
||||
*/
|
||||
public class JoinMatch implements Callable<Boolean> {
|
||||
public final String matchName;
|
||||
public final String playerName;
|
||||
public final SocketChannel socketChannel;
|
||||
private final String matchName;
|
||||
private final String playerName;
|
||||
private final SocketChannel socketChannel;
|
||||
|
||||
public JoinMatch(String playerName, DefaultListModel<String> matchName, SocketChannel socketChannel) {
|
||||
this.playerName = playerName;
|
||||
|
|
@ -24,19 +24,19 @@ public class JoinMatch implements Callable<Boolean> {
|
|||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
final Match thisMatch = Match.findMatch(Match.activeMatches, this.matchName);
|
||||
if (thisMatch != null) {
|
||||
for (int j = 0; j < thisMatch.playersStatus.size(); j++) {
|
||||
String name = thisMatch.playersStatus.get(j).getKey();
|
||||
if (name.equals(playerName)) {
|
||||
thisMatch.playersStatus.remove(j);
|
||||
thisMatch.playersStatus.add(new Pair<>(name, 1));
|
||||
thisMatch.playersSocket.remove(j);
|
||||
thisMatch.playersSocket.add(new Pair<>(name, socketChannel));
|
||||
return allJoined(thisMatch);
|
||||
}
|
||||
if (thisMatch != null) {
|
||||
for (int j = 0; j < thisMatch.playersStatus.size(); j++) {
|
||||
String name = thisMatch.playersStatus.get(j).getKey();
|
||||
if (name.equals(playerName)) {
|
||||
thisMatch.playersStatus.remove(j);
|
||||
thisMatch.playersStatus.add(new Pair<>(name, 1));
|
||||
thisMatch.playersSocket.remove(j);
|
||||
thisMatch.playersSocket.add(new Pair<>(name, socketChannel));
|
||||
return allJoined(thisMatch);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Boolean allJoined(Match match) {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package com.texttwist.server.tasks;
|
||||
import com.texttwist.server.models.Match;
|
||||
import constants.Config;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 23/06/2017.
|
||||
* Author: Lorenzo Iovino on 23/06/2017.
|
||||
* Description: Task: Join Timeout
|
||||
*/
|
||||
public class JoinTimeout implements Callable<Boolean> {
|
||||
|
||||
|
|
@ -11,14 +14,13 @@ public class JoinTimeout implements Callable<Boolean> {
|
|||
|
||||
public JoinTimeout(Match match) {
|
||||
this.match = match;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
try {
|
||||
match.joinTimeout=true;
|
||||
Thread.currentThread().sleep(5000);
|
||||
match.joinTimeout = true;
|
||||
Thread.currentThread().sleep(Config.joinMatchTimeout);
|
||||
|
||||
if(match.joinTimeout) {
|
||||
match.joinTimeout = false;
|
||||
|
|
|
|||
|
|
@ -2,12 +2,11 @@ package com.texttwist.server.tasks;
|
|||
|
||||
import com.texttwist.server.models.Match;
|
||||
import constants.Config;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 27/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Task: Match Timeout
|
||||
*/
|
||||
public class MatchTimeout implements Callable<Boolean> {
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 19/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Task: Send Invitations
|
||||
*/
|
||||
public class SendInvitations implements Callable<Boolean> {
|
||||
private DefaultListModel<String> users;
|
||||
|
|
@ -22,11 +21,9 @@ public class SendInvitations implements Callable<Boolean> {
|
|||
public Boolean call() throws Exception {
|
||||
try {
|
||||
Server.notificationServer.sendInvitations(sender, users);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,22 +2,21 @@ package com.texttwist.server.tasks;
|
|||
|
||||
import com.texttwist.server.models.Match;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 27/06/2017.
|
||||
* Description: Jedis Service
|
||||
* Description: Task: Send Message To All Players
|
||||
*/
|
||||
public class SendMessageToAllPlayers implements Callable<Boolean> {
|
||||
|
||||
|
||||
public final Match match;
|
||||
public final Message message;
|
||||
public SocketChannel socketChannel;
|
||||
private final Message message;
|
||||
private SocketChannel socketChannel;
|
||||
|
||||
public SendMessageToAllPlayers(Match match, Message message, SocketChannel socketChannel){
|
||||
this.match = match;
|
||||
this.message = message;
|
||||
|
|
@ -37,10 +36,8 @@ public class SendMessageToAllPlayers implements Callable<Boolean> {
|
|||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ import java.net.MulticastSocket;
|
|||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 13/07/2017.
|
||||
* Author: Lorenzo Iovino on 13/07/2017.
|
||||
* Description: Task: Send Scores
|
||||
*/
|
||||
public class SendScores implements Callable<Void> {
|
||||
|
||||
|
|
@ -24,19 +25,17 @@ public class SendScores implements Callable<Void> {
|
|||
@Override
|
||||
public Void call() throws Exception {
|
||||
|
||||
while (true) {
|
||||
Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList());
|
||||
MulticastSocket multicastSocket = null;
|
||||
try {
|
||||
multicastSocket = new MulticastSocket(match.multicastId);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||
DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId);
|
||||
multicastSocket.send(hi);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator));
|
||||
return null;
|
||||
Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList());
|
||||
MulticastSocket multicastSocket = null;
|
||||
try {
|
||||
multicastSocket = new MulticastSocket(match.multicastId);
|
||||
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||
DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, match.multicastId);
|
||||
multicastSocket.send(hi);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import java.util.concurrent.Callable;
|
|||
|
||||
/**
|
||||
* Author: Lorenzo Iovino on 11/07/2017.
|
||||
* Description: Task: Token Invalid Service
|
||||
* Description: Task: Token Invalid
|
||||
*/
|
||||
public class TokenInvalid implements Callable<Boolean> {
|
||||
private String sender;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue