This commit is contained in:
Lorenzo Iovino 2017-07-16 00:23:40 +02:00
parent ee230f911c
commit 697d96b322
14 changed files with 562 additions and 609 deletions

View file

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.texttwist.server.Main

View file

@ -1,5 +1,6 @@
package com.texttwist.server;
import com.texttwist.server.models.Dictionary;
import com.texttwist.server.services.AuthService;
import com.texttwist.server.services.MessageService;
import com.texttwist.server.services.NotificationService;
@ -27,6 +28,9 @@ public class Server {
public static JedisPool jedisPool;
public static Logger logger;
public static AuthService auth;
private String dictionaryPath = "./Server/resources/dictionary";
public static Dictionary dict;
public static Integer multicastId = Config.NotificationServiceStubPort;
public Server() throws IOException {
logger = new Logger(new File("./notificationServer.log"), "Server", true);
@ -36,6 +40,9 @@ public class Server {
startMessageService();
startWordsReceiverService();
startNotificationService();
dict = new Dictionary(dictionaryPath);
Server.logger.write("Services started correctly ...");
}

View file

@ -1,5 +1,6 @@
package com.texttwist.server.models;
import com.texttwist.server.Server;
import com.texttwist.server.services.MessageService;
import com.texttwist.server.tasks.TimeoutMatch;
import javafx.util.Pair;
@ -139,7 +140,7 @@ public class Match {
}
private int generateMulticastId(){
return MessageService.multicastId++;
return Server.multicastId++;
}
public void setLetters(DefaultListModel<String> letters){

View file

@ -1,7 +1,7 @@
package com.texttwist.server.services;
import com.texttwist.server.Server;
import com.texttwist.server.dispatchers.MessageDispatcher;
import com.texttwist.server.tasks.MessageDispatcher;
import com.texttwist.server.models.Dictionary;
import constants.Config;
import models.Message;
@ -24,14 +24,9 @@ public class MessageService implements Runnable{
private Selector selector = null;
private ExecutorService dispatcherPool = Executors.newCachedThreadPool();
private String dictionaryPath = "./Server/resources/dictionary";
public static Dictionary dict;
public static Integer multicastId = Config.NotificationServiceStubPort;
public MessageService()
{
dict = new Dictionary(dictionaryPath);
try {
selector = Selector.open();
@ -39,7 +34,7 @@ public class MessageService implements Runnable{
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(Config.MessageServicePort));
serverSocketChannel.register(selector, OP_ACCEPT);
Server.logger.write("GameService Service is running at "+Config.MessageServicePort +" port...");
Server.logger.write("Message Service is running at "+Config.MessageServicePort +" port...");
} catch (IOException e) {
e.printStackTrace();

View file

@ -26,6 +26,8 @@ public class ComputeScore implements Callable<Integer> {
@Override
public Integer call() throws Exception {
//Compute the score depending on the size of the words
Integer score = 0;
for (int i = 0; i < words.size(); i++) {
if (isValid(words.get(i), match.letters)) {
@ -41,7 +43,7 @@ public class ComputeScore implements Callable<Integer> {
if(match.allPlayersSendedHisScore()) {
match.matchTimeout = false;
match.setUndefinedScorePlayersToZero();
new SendScores(match).call();
new SendFinalScores(match).call();
}
return score;
}

View file

@ -1,6 +1,7 @@
package com.texttwist.server.tasks;
import com.texttwist.server.services.MessageService;
import com.texttwist.server.Server;
import javax.swing.*;
import java.util.concurrent.Callable;
@ -14,7 +15,7 @@ public class GenerateLetters implements Callable<DefaultListModel<String>> {
public DefaultListModel<String> call() throws Exception {
DefaultListModel<String> l = new DefaultListModel<>();
String word = MessageService.dict.getRandomWord(6, 7);
String word = Server.dict.getRandomWord(6, 7);
for (int i = 0;i < word.length(); i++){
l.addElement(String.valueOf(word.charAt(i)));
}

View file

@ -1,4 +1,4 @@
package com.texttwist.server.dispatchers;
package com.texttwist.server.tasks;
import com.texttwist.server.Server;
import com.texttwist.server.models.Sessions;
@ -72,13 +72,13 @@ public class MessageDispatcher implements Callable<Boolean> {
Boolean joinTimeoutRes = joinTimeout.get();
//If joinTimeoutRes==true timeout happen, need to notify to all waiting clients
if(joinTimeoutRes){
Future<Boolean> sendMessageJoinTimeout = threadPool.submit(
Future<Boolean> sendMessageToAllPlayers = threadPool.submit(
new SendMessageToAllPlayers(match,
new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel));
Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get();
if(!sendMessageJoinTimeoutRes){
Boolean sendMessageToAllPlayersRes = sendMessageToAllPlayers.get();
if(!sendMessageToAllPlayersRes){
Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator));
return sendMessageJoinTimeoutRes;
return sendMessageToAllPlayersRes;
}
} else {
//All done, all player joined

View file

@ -12,13 +12,13 @@ import java.util.concurrent.Callable;
/**
* Author: Lorenzo Iovino on 13/07/2017.
* Description: Task: Send Scores
* Description: Task: Send Final Scores
*/
public class SendScores implements Callable<Void> {
public class SendFinalScores implements Callable<Void> {
private Match match;
public SendScores(Match match){
public SendFinalScores(Match match){
this.match = match;
}

View file

@ -23,7 +23,7 @@ public class TimeoutMatch implements Callable<Boolean> {
if(match.matchTimeout) {
match.setUndefinedScorePlayersToZero();
new SendScores(match).call();
new SendFinalScores(match).call();
return true;
}
return false;

View file

@ -12,7 +12,7 @@ import java.util.concurrent.Callable;
* Author: Lorenzo Iovino on 11/07/2017.
* Description: Task: Token Invalid
*/
public class TokenInvalid implements Callable<Boolean> {
public class TokenInvalid implements Callable<Void> {
private String sender;
private ByteBuffer buffer;
private SocketChannel channel;
@ -24,14 +24,14 @@ public class TokenInvalid implements Callable<Boolean> {
}
@Override
public Boolean call()throws Exception {
public Void call()throws Exception {
Server.logger.write("TOKEN INVALID: TOKEN USED BY "+ sender+ " IS NOT VALID");
Message msg = new Message("TOKEN_NOT_VALID", "", null, new DefaultListModel<>());
buffer.clear();
byte[] byteMessage = msg.toString().getBytes();
buffer = ByteBuffer.wrap(byteMessage);
channel.write(buffer);
return false;
return null;
}
}