Compare commits
1 commit
master
...
alternativ
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04f900c389 |
51 changed files with 21698 additions and 1480 deletions
10
.idea/artifacts/Client_jar.xml
generated
10
.idea/artifacts/Client_jar.xml
generated
|
|
@ -1,10 +0,0 @@
|
||||||
<component name="ArtifactManager">
|
|
||||||
<artifact type="jar" name="Client:jar">
|
|
||||||
<output-path>$PROJECT_DIR$/out/artifacts/Client_jar</output-path>
|
|
||||||
<root id="archive" name="Client.jar">
|
|
||||||
<element id="module-output" name="Client" />
|
|
||||||
<element id="module-output" name="Commons" />
|
|
||||||
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/github/cliftonlabs/json-simple/2.1.2/json-simple-2.1.2.jar" path-in-jar="/" />
|
|
||||||
</root>
|
|
||||||
</artifact>
|
|
||||||
</component>
|
|
||||||
14
.idea/artifacts/Server_jar.xml
generated
14
.idea/artifacts/Server_jar.xml
generated
|
|
@ -1,14 +0,0 @@
|
||||||
<component name="ArtifactManager">
|
|
||||||
<artifact type="jar" name="Server:jar">
|
|
||||||
<output-path>$PROJECT_DIR$/out/artifacts/Server_jar</output-path>
|
|
||||||
<root id="archive" name="Server.jar">
|
|
||||||
<element id="module-output" name="Server" />
|
|
||||||
<element id="module-output" name="Commons" />
|
|
||||||
<element id="module-output" name="Client" />
|
|
||||||
<element id="library" level="application" name="jedis-2.1.0-sources" />
|
|
||||||
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/redis/clients/jedis/2.9.0/jedis-2.9.0.jar" path-in-jar="/" />
|
|
||||||
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/org/apache/commons/commons-pool2/2.4.2/commons-pool2-2.4.2.jar" path-in-jar="/" />
|
|
||||||
<element id="extracted-dir" path="$MAVEN_REPOSITORY$/com/github/cliftonlabs/json-simple/2.1.2/json-simple-2.1.2.jar" path-in-jar="/" />
|
|
||||||
</root>
|
|
||||||
</artifact>
|
|
||||||
</component>
|
|
||||||
1565
.idea/workspace.xml
generated
1565
.idea/workspace.xml
generated
File diff suppressed because it is too large
Load diff
BIN
Client/resources/images/logout.png
Normal file
BIN
Client/resources/images/logout.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.8 KiB |
BIN
Client/resources/images/scores.png
Normal file
BIN
Client/resources/images/scores.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
|
|
@ -1,3 +0,0 @@
|
||||||
Manifest-Version: 1.0
|
|
||||||
Main-Class: com.texttwist.client.Main
|
|
||||||
|
|
||||||
|
|
@ -32,8 +32,8 @@ import java.rmi.server.UnicastRemoteObject;
|
||||||
*/
|
*/
|
||||||
public class App extends JFrame {
|
public class App extends JFrame {
|
||||||
|
|
||||||
private static InetSocketAddress clientTCPSocketAddress = new InetSocketAddress(Config.MessageServiceURI, Config.MessageServicePort);
|
private static InetSocketAddress clientTCPSocketAddress = new InetSocketAddress(Config.GameServerURI, Config.GameServerPort);
|
||||||
public static InetSocketAddress clientUDPSocketAddress = new InetSocketAddress(Config.WordsReceiverServiceURI, Config.WordsReceiverServicePort);
|
public static InetSocketAddress clientUDPSocketAddress = new InetSocketAddress(Config.WordsReceiverServerURI, Config.WordsReceiverServerPort);
|
||||||
private static InetAddress clientMulticastSocketAddress;
|
private static InetAddress clientMulticastSocketAddress;
|
||||||
|
|
||||||
public static AuthService authService;
|
public static AuthService authService;
|
||||||
|
|
@ -68,7 +68,7 @@ public class App extends JFrame {
|
||||||
logger.write("APP: Font not found!");
|
logger.write("APP: Font not found!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*services*/
|
/*Services*/
|
||||||
gameService = new GameService();
|
gameService = new GameService();
|
||||||
authService = new AuthService();
|
authService = new AuthService();
|
||||||
|
|
||||||
|
|
@ -79,10 +79,10 @@ public class App extends JFrame {
|
||||||
|
|
||||||
public static void registerForNotifications() throws RemoteException, NotBoundException {
|
public static void registerForNotifications() throws RemoteException, NotBoundException {
|
||||||
|
|
||||||
Registry registry = LocateRegistry.getRegistry(Config.NotificationServiceStubPort);
|
Registry registry = LocateRegistry.getRegistry(Config.NotificationServerStubPort);
|
||||||
INotificationClient callbackObj = new NotificationClientService();
|
INotificationClient callbackObj = new NotificationClientService();
|
||||||
notificationStub = (INotificationClient) UnicastRemoteObject.exportObject(callbackObj, 0);
|
notificationStub = (INotificationClient) UnicastRemoteObject.exportObject(callbackObj, 0);
|
||||||
INotificationServer notificationServer = (INotificationServer) registry.lookup(Config.NotificationServiceName);
|
INotificationServer notificationServer = (INotificationServer) registry.lookup(Config.NotificationServerName);
|
||||||
notificationServer.registerForCallback(notificationStub);
|
notificationServer.registerForCallback(notificationStub);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -98,7 +98,7 @@ public class App extends JFrame {
|
||||||
public static void openClientMulticastSocket(Integer multicastId){
|
public static void openClientMulticastSocket(Integer multicastId){
|
||||||
try {
|
try {
|
||||||
App.gameService.setMulticastId(multicastId);
|
App.gameService.setMulticastId(multicastId);
|
||||||
clientMulticastSocketAddress = InetAddress.getByName(Config.ScoreMulticastServiceURI);
|
clientMulticastSocketAddress = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||||
clientMulticast = new MulticastSocket(gameService.multicastId);
|
clientMulticast = new MulticastSocket(gameService.multicastId);
|
||||||
clientMulticast.joinGroup(clientMulticastSocketAddress);
|
clientMulticast.joinGroup(clientMulticastSocketAddress);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
@ -109,7 +109,7 @@ public class App extends JFrame {
|
||||||
public static void closeClientMulticastSocket(){
|
public static void closeClientMulticastSocket(){
|
||||||
//Leave group and close multicast socket
|
//Leave group and close multicast socket
|
||||||
try {
|
try {
|
||||||
App.clientMulticast.leaveGroup(InetAddress.getByName(Config.ScoreMulticastServiceURI));
|
App.clientMulticast.leaveGroup(InetAddress.getByName(Config.ScoreMulticastServerURI));
|
||||||
App.clientMulticast.close();
|
App.clientMulticast.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,6 @@ public class HomeController {
|
||||||
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||||
Response res = authService.login(userName,password);
|
Response res = authService.login(userName,password);
|
||||||
if (res.code == 200){
|
if (res.code == 200){
|
||||||
try {
|
|
||||||
App.registerForNotifications();
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
App.logger.write("AUTH SERVICE: Can't register for notification");
|
|
||||||
}
|
|
||||||
App.session = (new Session(new User(userName,password,0), res.data.get("token").toString()));
|
App.session = (new Session(new User(userName,password,0), res.data.get("token").toString()));
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
package com.texttwist.client.controllers;
|
package com.texttwist.client.controllers;
|
||||||
|
|
||||||
import com.texttwist.client.App;
|
import com.texttwist.client.App;
|
||||||
import com.texttwist.client.tasks.JoinMatch;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -16,6 +14,6 @@ public class MatchRequestController {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void joinMatch(String matchName){
|
public void joinMatch(String matchName){
|
||||||
new JoinMatch(matchName).execute();
|
App.gameService.joinMatch(matchName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
package com.texttwist.client.controllers;
|
package com.texttwist.client.controllers;
|
||||||
|
|
||||||
import com.texttwist.client.tasks.InvitePlayers;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import static com.texttwist.client.App.gameService;
|
import static com.texttwist.client.App.gameService;
|
||||||
|
|
||||||
|
|
@ -12,7 +10,6 @@ import static com.texttwist.client.App.gameService;
|
||||||
public class MatchSetupController {
|
public class MatchSetupController {
|
||||||
|
|
||||||
public void play(DefaultListModel<String> userNames) {
|
public void play(DefaultListModel<String> userNames) {
|
||||||
new InvitePlayers(userNames).execute();
|
gameService.beginMatch(userNames);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ public class MatchSetupPage extends Page{
|
||||||
new Callable<Object>() {
|
new Callable<Object>() {
|
||||||
@Override
|
@Override
|
||||||
public Void call() throws Exception {
|
public Void call() throws Exception {
|
||||||
//If notificationServer response ok, start newMatch, else error
|
//If notificationServer response ok, start beginMatch, else error
|
||||||
matchSetupController.play(searchUserBar.list);
|
matchSetupController.play(searchUserBar.list);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,14 @@ import java.rmi.RemoteException;
|
||||||
*/
|
*/
|
||||||
public class AuthService {
|
public class AuthService {
|
||||||
|
|
||||||
private String baseUrl = Config.getAuthServiceURI().concat("/auth");
|
private String baseUrl = Config.getAuthServerURI().concat("/auth");
|
||||||
|
|
||||||
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
public Response login(String userName, String password) throws RemoteException, NotBoundException, MalformedURLException {
|
||||||
|
try {
|
||||||
|
App.registerForNotifications();
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
App.logger.write("AUTH SERVICE: Can't register for notification");
|
||||||
|
}
|
||||||
IAuth auth = (IAuth) Naming.lookup(baseUrl);
|
IAuth auth = (IAuth) Naming.lookup(baseUrl);
|
||||||
return auth.login(userName, password);
|
return auth.login(userName, password);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import com.texttwist.client.pages.GamePage;
|
||||||
import com.texttwist.client.pages.MenuPage;
|
import com.texttwist.client.pages.MenuPage;
|
||||||
import com.texttwist.client.pages.Page;
|
import com.texttwist.client.pages.Page;
|
||||||
import com.texttwist.client.tasks.InvitePlayers;
|
import com.texttwist.client.tasks.InvitePlayers;
|
||||||
import com.texttwist.client.tasks.JoinMatch;
|
|
||||||
import com.texttwist.client.ui.TTDialog;
|
import com.texttwist.client.ui.TTDialog;
|
||||||
import javafx.util.Pair;
|
import javafx.util.Pair;
|
||||||
import models.Message;
|
import models.Message;
|
||||||
|
|
@ -27,10 +26,9 @@ public class GameService {
|
||||||
public DefaultListModel<String> letters = new DefaultListModel<>();
|
public DefaultListModel<String> letters = new DefaultListModel<>();
|
||||||
public DefaultListModel<Pair<String,Integer>> globalRanks = new DefaultListModel<>();
|
public DefaultListModel<Pair<String,Integer>> globalRanks = new DefaultListModel<>();
|
||||||
public DefaultListModel<Pair<String,Integer>> ranks = new DefaultListModel<>();
|
public DefaultListModel<Pair<String,Integer>> ranks = new DefaultListModel<>();
|
||||||
public Boolean gameIsStarted = false;
|
private Boolean gameIsStarted = false;
|
||||||
public Boolean isWaiting = false;
|
|
||||||
|
|
||||||
public void addToPendingList(String username) throws IOException {
|
private void addToPendingList(String username) throws IOException {
|
||||||
pendingList.addElement(username);
|
pendingList.addElement(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,6 +36,33 @@ public class GameService {
|
||||||
App.openClientTCPSocket();
|
App.openClientTCPSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void newMatch(String userName) {
|
||||||
|
//Add to pending invites list
|
||||||
|
try {
|
||||||
|
this.addToPendingList(userName);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!App.gameService.gameIsStarted) {
|
||||||
|
//Show invite popup
|
||||||
|
new TTDialog("success", "New invite from: " + userName + "!",
|
||||||
|
new Callable() {
|
||||||
|
@Override
|
||||||
|
public Object call() throws Exception {
|
||||||
|
App.gameService.joinMatch(userName);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new Callable() {
|
||||||
|
@Override
|
||||||
|
public Object call() throws Exception {
|
||||||
|
return new MenuPage(Page.window);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public DefaultListModel<String> getLetters(){
|
public DefaultListModel<String> getLetters(){
|
||||||
return App.gameService.letters;
|
return App.gameService.letters;
|
||||||
}
|
}
|
||||||
|
|
@ -50,6 +75,32 @@ public class GameService {
|
||||||
this.letters = letters;
|
this.letters = letters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void joinMatch(String matchName) {
|
||||||
|
|
||||||
|
//Clear pending invitation list and join selected match
|
||||||
|
if(!gameIsStarted) {
|
||||||
|
this.pendingList.clear();
|
||||||
|
try {
|
||||||
|
DefaultListModel<String> matchNames = new DefaultListModel<>();
|
||||||
|
matchNames.addElement(matchName);
|
||||||
|
Message message = new Message("JOIN_GAME", App.session.account.userName, App.session.token, matchNames);
|
||||||
|
|
||||||
|
byte[] byteMessage = message.toString().getBytes();
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(byteMessage);
|
||||||
|
App.clientTCP.write(buffer);
|
||||||
|
new GamePage(Page.window);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Start game and wait for other players
|
||||||
|
public void beginMatch(DefaultListModel<String> userNames) {
|
||||||
|
new InvitePlayers(userNames).execute();
|
||||||
|
}
|
||||||
|
|
||||||
public void start(){
|
public void start(){
|
||||||
gameIsStarted = true;
|
gameIsStarted = true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package com.texttwist.client.services;
|
package com.texttwist.client.services;
|
||||||
|
|
||||||
import com.texttwist.client.App;
|
import com.texttwist.client.App;
|
||||||
import com.texttwist.client.tasks.BeginMatch;
|
|
||||||
import interfaces.INotificationClient;
|
import interfaces.INotificationClient;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.rmi.RemoteException;
|
import java.rmi.RemoteException;
|
||||||
|
|
@ -19,7 +18,7 @@ public class NotificationClientService implements INotificationClient {
|
||||||
if(App.session != null) {
|
if(App.session != null) {
|
||||||
if (users.contains(App.session.account.userName)) {
|
if (users.contains(App.session.account.userName)) {
|
||||||
App.logger.write(userName + " send a invitation!");
|
App.logger.write(userName + " send a invitation!");
|
||||||
new BeginMatch(userName).execute();
|
App.gameService.newMatch(userName);
|
||||||
} else {
|
} else {
|
||||||
App.logger.write("User " + userName + " is slogged");
|
App.logger.write("User " + userName + " is slogged");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,52 +0,0 @@
|
||||||
package com.texttwist.client.tasks;
|
|
||||||
|
|
||||||
import com.texttwist.client.App;
|
|
||||||
import com.texttwist.client.pages.MenuPage;
|
|
||||||
import com.texttwist.client.pages.Page;
|
|
||||||
import com.texttwist.client.ui.TTDialog;
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Author: Lorenzo Iovino on 15/07/2017.
|
|
||||||
* Description: Task: BeginMatch
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class BeginMatch extends SwingWorker<Void,Void> {
|
|
||||||
|
|
||||||
private String userName;
|
|
||||||
|
|
||||||
public BeginMatch(String userName) {
|
|
||||||
this.userName = userName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void doInBackground() {
|
|
||||||
//Add to pending invites list
|
|
||||||
try {
|
|
||||||
App.gameService.addToPendingList(userName);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!App.gameService.gameIsStarted && !App.gameService.isWaiting) {
|
|
||||||
//Show invite popup
|
|
||||||
new TTDialog("success", "New invite from: " + userName + "!",
|
|
||||||
new Callable() {
|
|
||||||
@Override
|
|
||||||
public Object call() throws Exception {
|
|
||||||
new JoinMatch(userName).execute();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
new Callable() {
|
|
||||||
@Override
|
|
||||||
public Object call() throws Exception {
|
|
||||||
return new MenuPage(Page.window);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,47 +0,0 @@
|
||||||
package com.texttwist.client.tasks;
|
|
||||||
|
|
||||||
|
|
||||||
import com.texttwist.client.App;
|
|
||||||
import com.texttwist.client.pages.GamePage;
|
|
||||||
import com.texttwist.client.pages.Page;
|
|
||||||
import models.Message;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Author: Lorenzo Iovino on 25/06/2017.
|
|
||||||
* Description: Task: JoinMatch
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class JoinMatch extends SwingWorker<Void,Void> {
|
|
||||||
|
|
||||||
private String matchName;
|
|
||||||
|
|
||||||
public JoinMatch(String matchName) {
|
|
||||||
this.matchName = matchName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void doInBackground() {
|
|
||||||
//Clear pending invitation list and join selected match
|
|
||||||
if(!App.gameService.gameIsStarted) {
|
|
||||||
App.gameService.pendingList.clear();
|
|
||||||
try {
|
|
||||||
DefaultListModel<String> matchNames = new DefaultListModel<>();
|
|
||||||
matchNames.addElement(matchName);
|
|
||||||
Message message = new Message("JOIN_GAME", App.session.account.userName, App.session.token, matchNames);
|
|
||||||
|
|
||||||
byte[] byteMessage = message.toString().getBytes();
|
|
||||||
ByteBuffer buffer = ByteBuffer.wrap(byteMessage);
|
|
||||||
App.clientTCP.write(buffer);
|
|
||||||
new GamePage(Page.window);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -36,6 +36,8 @@ public class SendWords extends SwingWorker<Void,Void> {
|
||||||
buffer.flip();
|
buffer.flip();
|
||||||
|
|
||||||
App.clientUDP.send(buffer, App.clientUDPSocketAddress);
|
App.clientUDP.send(buffer, App.clientUDPSocketAddress);
|
||||||
|
|
||||||
|
return null;
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
App.logger.write("SEND WORDS: Host address not correct");
|
App.logger.write("SEND WORDS: Host address not correct");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ public class WaitForPlayers extends SwingWorker<Void,Void> {
|
||||||
@Override
|
@Override
|
||||||
public Void doInBackground() {
|
public Void doInBackground() {
|
||||||
try {
|
try {
|
||||||
App.gameService.isWaiting = true;
|
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||||
TTDialog loading = new TTDialog("alert", "Waiting for users joins",null,null);
|
TTDialog loading = new TTDialog("alert", "Waiting for users joins",null,null);
|
||||||
buffer.flip();
|
buffer.flip();
|
||||||
|
|
@ -43,7 +42,6 @@ public class WaitForPlayers extends SwingWorker<Void,Void> {
|
||||||
Message msg = Message.toMessage(line);
|
Message msg = Message.toMessage(line);
|
||||||
if (msg.message.equals("JOIN_TIMEOUT")) {
|
if (msg.message.equals("JOIN_TIMEOUT")) {
|
||||||
loading.dispose();
|
loading.dispose();
|
||||||
App.gameService.isWaiting = false;
|
|
||||||
joinTimeout = true;
|
joinTimeout = true;
|
||||||
|
|
||||||
new TTDialog("alert", "TIMEOUT!",
|
new TTDialog("alert", "TIMEOUT!",
|
||||||
|
|
@ -60,7 +58,6 @@ public class WaitForPlayers extends SwingWorker<Void,Void> {
|
||||||
if (msg.message.equals("MATCH_NOT_AVAILABLE")) {
|
if (msg.message.equals("MATCH_NOT_AVAILABLE")) {
|
||||||
loading.dispose();
|
loading.dispose();
|
||||||
joinTimeout = true;
|
joinTimeout = true;
|
||||||
App.gameService.isWaiting = false;
|
|
||||||
|
|
||||||
new TTDialog("alert", "THE GAME IS NOT MORE AVAILABLE!",
|
new TTDialog("alert", "THE GAME IS NOT MORE AVAILABLE!",
|
||||||
new Callable() {
|
new Callable() {
|
||||||
|
|
@ -75,11 +72,9 @@ public class WaitForPlayers extends SwingWorker<Void,Void> {
|
||||||
|
|
||||||
if (msg.message.equals("GAME_STARTED")) {
|
if (msg.message.equals("GAME_STARTED")) {
|
||||||
loading.dispose();
|
loading.dispose();
|
||||||
App.gameService.isWaiting = false;
|
|
||||||
|
|
||||||
if(msg.data !=null ) {
|
if(msg.data !=null ) {
|
||||||
DefaultListModel<String> data = msg.data;
|
DefaultListModel<String> data = msg.data;
|
||||||
|
|
||||||
App.openClientMulticastSocket(Integer.valueOf(data.remove(data.size()-2)));
|
App.openClientMulticastSocket(Integer.valueOf(data.remove(data.size()-2)));
|
||||||
App.gameService.setLetters(msg.data);
|
App.gameService.setLetters(msg.data);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -6,29 +6,29 @@ package constants;
|
||||||
*/
|
*/
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
public static String AuthServiceURI = "localhost";
|
public static String AuthServerURI = "localhost";
|
||||||
public static Integer AuthServicePort = 9999;
|
public static Integer AuthServerPort = 9999;
|
||||||
|
|
||||||
public static String MessageServiceURI = "localhost";
|
public static String GameServerURI = "localhost";
|
||||||
public static Integer MessageServicePort = 10000;
|
public static Integer GameServerPort = 10000;
|
||||||
|
|
||||||
public static String WordsReceiverServiceURI = "localhost";
|
public static String WordsReceiverServerURI = "localhost";
|
||||||
public static Integer WordsReceiverServicePort = 10001;
|
public static Integer WordsReceiverServerPort = 10001;
|
||||||
|
|
||||||
public static String ScoreMulticastServiceURI = "226.226.226.226";
|
public static String ScoreMulticastServerURI = "226.226.226.226";
|
||||||
|
|
||||||
public static Integer NotificationServicePort = 20000;
|
public static Integer NotificationServerPort = 20000;
|
||||||
public static Integer NotificationServiceStubPort = 30000;
|
public static Integer NotificationServerStubPort = 30000;
|
||||||
public static String NotificationServiceName ="notification";
|
public static String NotificationServerName ="notification";
|
||||||
|
|
||||||
public static String RedisServiceURI = "localhost";
|
public static String RedisServerURI = "localhost";
|
||||||
|
|
||||||
public static int gameTimeout = 120; //2 minuti in sec
|
public static int gameTimeout = 10; //2 minuti in sec
|
||||||
public static int joinMatchTimeout = 7*1000*60; //7 minuti in millisec
|
public static int joinMatchTimeout = 5000; //7 minuti in millisec
|
||||||
public static int sendWordsTimeout = 5*1000*60; //5 minuti in millisec
|
public static int sendWordsTimeout = 3000; //5 minuti in millisec
|
||||||
|
|
||||||
public static String getAuthServiceURI(){
|
public static String getAuthServerURI(){
|
||||||
return "rmi://".concat(AuthServiceURI).concat(":").concat(AuthServicePort.toString());
|
return "rmi://".concat(AuthServerURI).concat(":").concat(AuthServerPort.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package interfaces;
|
package interfaces;
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.rmi.Remote;
|
import java.rmi.Remote;
|
||||||
import java.rmi.RemoteException;
|
import java.rmi.RemoteException;
|
||||||
|
|
||||||
|
|
@ -11,5 +10,4 @@ import java.rmi.RemoteException;
|
||||||
public interface INotificationServer extends Remote {
|
public interface INotificationServer extends Remote {
|
||||||
void registerForCallback (INotificationClient ClientInterface) throws RemoteException;
|
void registerForCallback (INotificationClient ClientInterface) throws RemoteException;
|
||||||
void unregisterForCallback (INotificationClient ClientInterface) throws RemoteException;
|
void unregisterForCallback (INotificationClient ClientInterface) throws RemoteException;
|
||||||
void sendInvitations(String username, DefaultListModel<String> users) throws RemoteException;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
Manifest-Version: 1.0
|
|
||||||
Main-Class: com.texttwist.server.Main
|
|
||||||
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
package com.texttwist.server;
|
package com.texttwist.server;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
package com.texttwist.server;
|
package com.texttwist.server;
|
||||||
|
|
||||||
import com.texttwist.server.models.Dictionary;
|
|
||||||
import com.texttwist.server.services.AuthService;
|
import com.texttwist.server.services.AuthService;
|
||||||
import com.texttwist.server.services.MessageService;
|
import com.texttwist.server.services.MessageService;
|
||||||
import com.texttwist.server.services.NotificationService;
|
import com.texttwist.server.services.NotificationService;
|
||||||
import com.texttwist.server.services.ReceiveWordsService;
|
|
||||||
import constants.Config;
|
import constants.Config;
|
||||||
import interfaces.INotificationServer;
|
import interfaces.INotificationServer;
|
||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
@ -28,29 +26,22 @@ public class Server {
|
||||||
public static JedisPool jedisPool;
|
public static JedisPool jedisPool;
|
||||||
public static Logger logger;
|
public static Logger logger;
|
||||||
public static AuthService auth;
|
public static AuthService auth;
|
||||||
private String dictionaryPath = "./Server/resources/dictionary";
|
|
||||||
public static Dictionary dict;
|
|
||||||
public static Integer multicastId = Config.NotificationServiceStubPort;
|
|
||||||
|
|
||||||
public Server() throws IOException {
|
public Server() throws IOException {
|
||||||
logger = new Logger(new File("./server.log"), "Server", true);
|
logger = new Logger(new File("./notificationServer.log"), "Server", true);
|
||||||
Server.logger.write("Services starting ...");
|
Server.logger.write("Services starting ...");
|
||||||
startAuthService();
|
startAuthService();
|
||||||
startJedisService();
|
startJedisService();
|
||||||
startMessageService();
|
startMessageService();
|
||||||
startWordsReceiverService();
|
|
||||||
startNotificationService();
|
startNotificationService();
|
||||||
|
|
||||||
dict = new Dictionary(dictionaryPath);
|
|
||||||
|
|
||||||
Server.logger.write("Services started correctly ...");
|
Server.logger.write("Services started correctly ...");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startAuthService(){
|
private void startAuthService(){
|
||||||
//Starting Auth service based on RMI
|
//Starting AuthService service based on RMI
|
||||||
try {
|
try {
|
||||||
auth = new AuthService();
|
auth = new AuthService(Config.AuthServerPort);
|
||||||
Registry authRegistry = LocateRegistry.createRegistry(Config.AuthServicePort);
|
Registry authRegistry = LocateRegistry.createRegistry(auth.serverPort);
|
||||||
authRegistry.bind("auth", auth);
|
authRegistry.bind("auth", auth);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
Server.logger.write("SERVER: RMI authentication service error (Remote exception)");
|
Server.logger.write("SERVER: RMI authentication service error (Remote exception)");
|
||||||
|
|
@ -61,28 +52,23 @@ public class Server {
|
||||||
|
|
||||||
private void startJedisService(){
|
private void startJedisService(){
|
||||||
//Starting Jedis pool for Redis connection
|
//Starting Jedis pool for Redis connection
|
||||||
jedisPool = new JedisPool(new JedisPoolConfig(), Config.RedisServiceURI);
|
jedisPool = new JedisPool(new JedisPoolConfig(), Config.RedisServerURI);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startMessageService(){
|
private void startMessageService(){
|
||||||
//Starting the Message service based on TCP
|
//Starting the Message service based on TCP
|
||||||
new Thread(new MessageService()).start();
|
new Thread(new MessageService(Config.GameServerPort)).start();
|
||||||
}
|
|
||||||
|
|
||||||
private void startWordsReceiverService(){
|
|
||||||
//Starting the Receive Words service based on UDP
|
|
||||||
new Thread(new ReceiveWordsService()).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startNotificationService(){
|
private void startNotificationService(){
|
||||||
//Starting Notification service based on RMI
|
//Starting Notification service based on RMI
|
||||||
try {
|
try {
|
||||||
notificationServer = new NotificationService();
|
notificationServer = new NotificationService();
|
||||||
INotificationServer stub = (INotificationServer) UnicastRemoteObject.exportObject(notificationServer, Config.NotificationServicePort);
|
INotificationServer stub = (INotificationServer) UnicastRemoteObject.exportObject(notificationServer, Config.NotificationServerPort);
|
||||||
LocateRegistry.createRegistry(Config.NotificationServiceStubPort);
|
LocateRegistry.createRegistry(Config.NotificationServerStubPort);
|
||||||
|
|
||||||
Registry notificationRegistry = LocateRegistry.getRegistry(Config.NotificationServiceStubPort);
|
Registry notificationRegistry = LocateRegistry.getRegistry(Config.NotificationServerStubPort);
|
||||||
notificationRegistry.bind(Config.NotificationServiceName, stub);
|
notificationRegistry.bind(Config.NotificationServerName, stub);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
Server.logger.write("SERVER: RMI notification service error (Remote exception)");
|
Server.logger.write("SERVER: RMI notification service error (Remote exception)");
|
||||||
} catch (AlreadyBoundException e) {
|
} catch (AlreadyBoundException e) {
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,23 @@
|
||||||
package com.texttwist.server.models;
|
package com.texttwist.server.models;
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 26/06/2017.
|
* Created by loke on 26/06/2017.
|
||||||
* Description: Dictionary Model. Provides the dictionary and methods for manage it
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Dictionary {
|
public class Dictionary {
|
||||||
|
|
||||||
private static DefaultListModel<String> wordList = new DefaultListModel<>();
|
static DefaultListModel<String> wordList = new DefaultListModel<>();
|
||||||
|
private Random randomGenerator;
|
||||||
|
|
||||||
public Dictionary(String dictionaryPath) {
|
public Dictionary(String dictionaryPath) {
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(new File(dictionaryPath)))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(new File(dictionaryPath)))) {
|
||||||
|
|
@ -21,16 +25,15 @@ public class Dictionary {
|
||||||
wordList.addElement(line);
|
wordList.addElement(line);
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
Server.logger.write("DICTIONARY: Dictionary file not found!");
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Server.logger.write("DICTIONARY: Can't read dictionary file!");
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get a random word in wordsList with minimumWordSize < size < maximumWordSize
|
|
||||||
public String getRandomWord(int minimumWordSize, int maximumWordSize){
|
public String getRandomWord(int minimumWordSize, int maximumWordSize){
|
||||||
|
|
||||||
Random randomGenerator = new Random();
|
randomGenerator = new Random();
|
||||||
int index = randomGenerator.nextInt(wordList.size());
|
int index = randomGenerator.nextInt(wordList.size());
|
||||||
String word = wordList.get(index);
|
String word = wordList.get(index);
|
||||||
if(word.length() >= minimumWordSize && word.length() <= maximumWordSize) {
|
if(word.length() >= minimumWordSize && word.length() <= maximumWordSize) {
|
||||||
|
|
@ -52,7 +55,6 @@ public class Dictionary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if a word is contained in dictionary
|
|
||||||
public static Boolean isContainedInDictionary(String word){
|
public static Boolean isContainedInDictionary(String word){
|
||||||
if(word.equals("")){
|
if(word.equals("")){
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -64,4 +66,5 @@ public class Dictionary {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,58 +1,38 @@
|
||||||
package com.texttwist.server.models;
|
package com.texttwist.server.models;
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
|
||||||
import com.texttwist.server.services.MessageService;
|
import com.texttwist.server.services.MessageService;
|
||||||
import com.texttwist.server.tasks.TimeoutMatch;
|
import com.texttwist.server.tasks.MatchTimeout;
|
||||||
|
import constants.Config;
|
||||||
import javafx.util.Pair;
|
import javafx.util.Pair;
|
||||||
|
import models.Message;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.*;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
import static com.texttwist.server.services.MessageService.activeMatches;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 23/06/2017.
|
* Created by loke on 23/06/2017.
|
||||||
* Description: Match Model. Methods for manage the matches and model of single match.
|
|
||||||
* Single point of concurrent access.
|
|
||||||
*/
|
*/
|
||||||
public class Match {
|
public class Match {
|
||||||
|
public final List<Pair<String,Integer>> playersStatus = Collections.synchronizedList(new ArrayList<>()); //Usare Liste!!!!!!!
|
||||||
/****GLOBAL AREA OF ALL MATCHES****/
|
|
||||||
//Players status: A list of pair where elements are <playerName, status>.
|
|
||||||
// status is 0 if user is not currently in a match, and 1 otherwise
|
|
||||||
public final List<Pair<String,Integer>> playersStatus = Collections.synchronizedList(new ArrayList<>());
|
|
||||||
|
|
||||||
//Players socket: A list of pair where elements are <playerName, socketChannel>.
|
|
||||||
// socketChannel is the TCP socket associated with client for messages exchange
|
|
||||||
public final List<Pair<String,SocketChannel>> playersSocket = Collections.synchronizedList(new ArrayList<>());
|
public final List<Pair<String,SocketChannel>> playersSocket = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
|
||||||
//Players score: A list of pair where elements are <playerName, score>.
|
|
||||||
public final List<Pair<String,Integer>> playersScore = Collections.synchronizedList(new ArrayList<>());
|
|
||||||
|
|
||||||
//Players score: A list of active matches.
|
|
||||||
public static List<Match> activeMatches = Collections.synchronizedList(new ArrayList<>());
|
|
||||||
|
|
||||||
|
|
||||||
/****SINGLE INSTANCE OF MATCH****/
|
|
||||||
//If match is started
|
|
||||||
private boolean started = false;
|
private boolean started = false;
|
||||||
|
|
||||||
//Name of the creator of the match
|
|
||||||
public final String matchCreator;
|
public final String matchCreator;
|
||||||
|
|
||||||
//MulticastID associated with this match
|
|
||||||
public Integer multicastId;
|
public Integer multicastId;
|
||||||
|
public Future<Boolean> timeout;
|
||||||
//True if happen timeout, false otherwise
|
public boolean matchTimeout = true;
|
||||||
public boolean matchTimeout = false;
|
public boolean joinTimeout =true;
|
||||||
public boolean joinTimeout = false;
|
|
||||||
|
|
||||||
//Letters of the match
|
|
||||||
public DefaultListModel<String> letters;
|
public DefaultListModel<String> letters;
|
||||||
|
protected ExecutorService threadPool = Executors.newSingleThreadExecutor();
|
||||||
|
|
||||||
private ExecutorService matchTimeoutThread = Executors.newSingleThreadExecutor();
|
public final List<Pair<String,Integer>> playersScore = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
|
||||||
public Match(String matchCreator, DefaultListModel<String> players){
|
public Match(String matchCreator, DefaultListModel<String> players){
|
||||||
for (int i =0; i < players.size(); i++){
|
for (int i =0; i < players.size(); i++){
|
||||||
|
|
@ -63,17 +43,43 @@ public class Match {
|
||||||
|
|
||||||
this.multicastId = this.generateMulticastId();
|
this.multicastId = this.generateMulticastId();
|
||||||
this.matchCreator = matchCreator;
|
this.matchCreator = matchCreator;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Void sendScores(){
|
||||||
|
while (true) {
|
||||||
|
System.out.println("SENDING");
|
||||||
|
Message msg = new Message("FINALSCORE", "SERVER", "", this.getMatchPlayersScoreAsStringList());
|
||||||
|
|
||||||
|
MulticastSocket multicastSocket = null;
|
||||||
|
try {
|
||||||
|
multicastSocket = new MulticastSocket(this.multicastId);
|
||||||
|
InetAddress ia = null;
|
||||||
|
ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
|
||||||
|
DatagramPacket hi = new DatagramPacket(msg.toString().getBytes(), msg.toString().length(), ia, this.multicastId);
|
||||||
|
multicastSocket.send(hi);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
activeMatches.remove(Match.findMatchIndex(activeMatches, this.matchCreator));
|
||||||
|
}
|
||||||
|
}
|
||||||
public static Match findMatch(List<Match> matches, String matchName){
|
public static Match findMatch(List<Match> matches, String matchName){
|
||||||
for (Match match : matches) {
|
for (int i = 0; i < matches.size(); i++) {
|
||||||
if (match.matchCreator.equals(matchName)) {
|
if (matches.get(i).matchCreator.equals(matchName)) {
|
||||||
return match;
|
return matches.get(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void printAll(){
|
||||||
|
for (int i = 0; i < playersScore.size(); i++) {
|
||||||
|
System.out.println(playersScore.get(i).getKey() + " : " +playersScore.get(i).getValue());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
public static int findMatchIndex(List<Match> matches, String matchName){
|
public static int findMatchIndex(List<Match> matches, String matchName){
|
||||||
for (int i = 0; i < matches.size(); i++) {
|
for (int i = 0; i < matches.size(); i++) {
|
||||||
if (matches.get(i).matchCreator.equals(matchName)) {
|
if (matches.get(i).matchCreator.equals(matchName)) {
|
||||||
|
|
@ -87,36 +93,42 @@ public class Match {
|
||||||
return started;
|
return started;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Match findMatchByPlayerName(String player){
|
public static Match findMatchByPlayer(String player){
|
||||||
for (Match activeMatch : activeMatches) {
|
for (int i = 0; i < activeMatches.size(); i++) {
|
||||||
for (int j = 0; j < activeMatch.playersStatus.size(); j++) {
|
for (int j = 0; j < activeMatches.get(i).playersStatus.size(); j++) {
|
||||||
if (activeMatch.playersStatus.get(j).getKey().equals(player)) {
|
if (activeMatches.get(i).playersStatus.get(j).getKey().equals(player)) {
|
||||||
return activeMatch;
|
return activeMatches.get(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* if (matches.get(i).matchCreator.equals(matchName)) {
|
||||||
|
return i;
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startGame(){
|
public void startGame(){
|
||||||
this.started=true;
|
this.started=true;
|
||||||
matchTimeoutThread.submit(new TimeoutMatch(this));
|
threadPool.submit(new MatchTimeout(this));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setScore(String player, Integer score){
|
public void setScore(String player, Integer score){
|
||||||
Match m = findMatchByPlayerName(player);
|
Match m = findMatchByPlayer(player);
|
||||||
if(m!=null) {
|
m.printAll();
|
||||||
|
|
||||||
for (int i = 0; i < m.playersScore.size(); i++) {
|
for (int i = 0; i < m.playersScore.size(); i++) {
|
||||||
if (m.playersScore.get(i).getKey().equals(player)) {
|
if (m.playersScore.get(i).getKey().equals(player)) {
|
||||||
m.playersScore.set(i, new Pair<>(player, score));
|
m.playersScore.set(i, new Pair<String, Integer>(player, score));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean allPlayersSendedHisScore(){
|
public Boolean allPlayersSendedHisScore(){
|
||||||
for (Pair<String, Integer> player : playersScore) {
|
System.out.println(matchCreator);
|
||||||
if (player.getValue() == -1) {
|
printAll();
|
||||||
|
for (int i = 0; i < playersScore.size(); i++) {
|
||||||
|
if (playersScore.get(i).getValue() == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -126,23 +138,24 @@ public class Match {
|
||||||
public void setUndefinedScorePlayersToZero(){
|
public void setUndefinedScorePlayersToZero(){
|
||||||
for (int i = 0; i < playersScore.size(); i++) {
|
for (int i = 0; i < playersScore.size(); i++) {
|
||||||
if (playersScore.get(i).getValue() == -1) {
|
if (playersScore.get(i).getValue() == -1) {
|
||||||
playersScore.set(i, new Pair<>(playersScore.get(i).getKey(), 0));
|
playersScore.set(i, new Pair<String, Integer>(playersScore.get(i).getKey(), 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DefaultListModel<String> getMatchPlayersScoreAsStringList(){
|
public DefaultListModel<String> getMatchPlayersScoreAsStringList(){
|
||||||
DefaultListModel<String> l = new DefaultListModel<>();
|
DefaultListModel<String> l = new DefaultListModel<>();
|
||||||
for (Pair<String, Integer> player : playersScore) {
|
for (int i = 0; i < playersScore.size(); i++) {
|
||||||
l.addElement(player.getKey() + ":" + player.getValue());
|
l.addElement(playersScore.get(i).getKey() + ":" + playersScore.get(i).getValue());
|
||||||
}
|
}
|
||||||
return l;
|
return l;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int generateMulticastId(){
|
private int generateMulticastId(){
|
||||||
return Server.multicastId++;
|
return MessageService.multicastID++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLetters(DefaultListModel<String> letters){
|
public void setLetters(DefaultListModel<String> letters){
|
||||||
this.letters = letters;
|
this.letters = letters;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,68 +0,0 @@
|
||||||
package com.texttwist.server.models;
|
|
||||||
import models.Session;
|
|
||||||
import models.User;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Author: Lorenzo Iovino on 17/06/2017.
|
|
||||||
* Description: Sessions. It is a singleton that provides the model and methods for manage sessions
|
|
||||||
*/
|
|
||||||
public class Sessions {
|
|
||||||
|
|
||||||
private List<Session> sessions = Collections.synchronizedList(new ArrayList<Session>());
|
|
||||||
|
|
||||||
private static class Holder {
|
|
||||||
static final Sessions INSTANCE = new Sessions();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Sessions getInstance() {
|
|
||||||
return Holder.INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Session getSession(String userName) {
|
|
||||||
for (Session elem : sessions) {
|
|
||||||
if (elem.account.userName.equals(userName)) {
|
|
||||||
return elem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean add(String userName, String token) {
|
|
||||||
remove(userName);
|
|
||||||
return sessions.add(new Session(new User(userName,"",0), token));
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean remove(String userName){
|
|
||||||
if(exists(userName)) {
|
|
||||||
Session s = getSession(userName);
|
|
||||||
if(s != null) {
|
|
||||||
sessions.remove(s);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean exists(String userName) {
|
|
||||||
for (Session elem : sessions) {
|
|
||||||
if (elem.account.userName.equals(userName)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isValidToken(String token) {
|
|
||||||
for (Session session : sessions) {
|
|
||||||
if (session.token.equals(token)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,109 +1,116 @@
|
||||||
package com.texttwist.server.tasks;
|
package com.texttwist.server.models.servers;
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
|
||||||
import com.texttwist.server.models.Sessions;
|
|
||||||
import com.texttwist.server.models.Match;
|
import com.texttwist.server.models.Match;
|
||||||
|
import com.texttwist.server.services.SessionsService;
|
||||||
import com.texttwist.server.tasks.*;
|
import com.texttwist.server.tasks.*;
|
||||||
import models.Message;
|
import models.Message;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
import static com.texttwist.server.services.MessageService.activeMatches;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 18/06/2017.
|
* Created by loke on 18/06/2017.
|
||||||
* Description: Message Dispatcher
|
*/
|
||||||
* */
|
public class ThreadProxy implements Callable<Boolean> {
|
||||||
public class MessageDispatcher implements Callable<Boolean> {
|
protected final ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||||
private final ExecutorService threadPool = Executors.newCachedThreadPool();
|
|
||||||
private final Message request;
|
private final Message request;
|
||||||
private final SocketChannel socketChannel;
|
private final SocketChannel socketChannel;
|
||||||
private ByteBuffer bufferMessage;
|
private ByteBuffer bufferMessage;
|
||||||
private boolean matchNotAvailable = false;
|
boolean matchNotAvailable =false;
|
||||||
|
|
||||||
public MessageDispatcher(Message request, SocketChannel socketChannel, ByteBuffer bufferMessage) {
|
|
||||||
|
public ThreadProxy(Message request, SocketChannel socketChannel, ByteBuffer bufferMessage) {
|
||||||
this.request = request;
|
this.request = request;
|
||||||
this.socketChannel = socketChannel;
|
this.socketChannel = socketChannel;
|
||||||
this.bufferMessage = bufferMessage;
|
this.bufferMessage = bufferMessage;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean call() {
|
public Boolean call() {
|
||||||
bufferMessage = ByteBuffer.allocate(1024);
|
bufferMessage = ByteBuffer.allocate(1024);
|
||||||
byte[] byteMessage = null;
|
byte[] byteMessage = null;
|
||||||
if(Sessions.getInstance().isValidToken(request.token)){
|
if(SessionsService.getInstance().isValidToken(request.token)){
|
||||||
switch(request.message){
|
switch(request.message){
|
||||||
|
|
||||||
case "START_GAME":
|
case "START_GAME":
|
||||||
Future<Boolean> onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data));
|
Future<Boolean> onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data));
|
||||||
try {
|
try {
|
||||||
//Check if invited users are online
|
|
||||||
Boolean usersOnline = onlineUsers.get();
|
Boolean usersOnline = onlineUsers.get();
|
||||||
if(usersOnline){
|
if(usersOnline){
|
||||||
Future<Boolean> sendInvitations = threadPool.submit(new SendInvitations(request.sender, request.data));
|
Future<Boolean> sendInvitations = threadPool.submit(new SendInvitations(request.sender, request.data));
|
||||||
|
try {
|
||||||
Boolean invitationSended = sendInvitations.get();
|
Boolean invitationSended = sendInvitations.get();
|
||||||
if (invitationSended) {
|
if (invitationSended) {
|
||||||
|
|
||||||
//Server create new match
|
//Crea nuova partita e attendi i giocatori
|
||||||
request.data.addElement(request.sender);
|
request.data.addElement(request.sender);
|
||||||
final Match match = new Match(request.sender, request.data);
|
final Match match = new Match(request.sender, request.data);
|
||||||
Match.activeMatches.add(match);
|
match.printAll();
|
||||||
|
|
||||||
|
activeMatches.add(match);
|
||||||
|
|
||||||
DefaultListModel<String> matchName = new DefaultListModel<>();
|
DefaultListModel<String> matchName = new DefaultListModel<>();
|
||||||
matchName.addElement(request.sender);
|
matchName.addElement(request.sender);
|
||||||
|
|
||||||
//Match creator join match
|
|
||||||
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel));
|
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel));
|
||||||
Boolean joinMatchRes = joinMatch.get();
|
Boolean joinMatchRes = joinMatch.get();
|
||||||
|
|
||||||
//Notify to the client that invites was sents correctly
|
|
||||||
if(!joinMatchRes){
|
if(!joinMatchRes){
|
||||||
bufferMessage = ByteBuffer.allocate(1024);
|
bufferMessage = ByteBuffer.allocate(1024);
|
||||||
|
|
||||||
|
//NON FARE NULLA, ASPETTA GLI ALTRI
|
||||||
Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<>());
|
Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<>());
|
||||||
byteMessage = message.toString().getBytes();
|
byteMessage = message.toString().getBytes();
|
||||||
bufferMessage = ByteBuffer.wrap(byteMessage);
|
bufferMessage = ByteBuffer.wrap(byteMessage);
|
||||||
socketChannel.write(bufferMessage);
|
socketChannel.write(bufferMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Starts to wait until all player joins
|
Future<Boolean> joinTimeout = threadPool.submit(new JoinTimeout(match));
|
||||||
Future<Boolean> joinTimeout = threadPool.submit(new TimeoutJoin(match));
|
joinTimeout.get();
|
||||||
Boolean joinTimeoutRes = joinTimeout.get();
|
if(match.joinTimeout){
|
||||||
//If joinTimeoutRes==true timeout happen, need to notify to all waiting clients
|
Future<Boolean> sendMessageJoinTimeout = threadPool.submit(
|
||||||
if(joinTimeoutRes){
|
new SendMessageToAllPlayers(match, new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel));
|
||||||
Future<Boolean> sendMessageToAllPlayers = threadPool.submit(
|
Boolean sendMessageJoinTimeoutRes = sendMessageJoinTimeout.get();
|
||||||
new SendMessageToAllPlayers(match,
|
if(!sendMessageJoinTimeoutRes){
|
||||||
new Message("JOIN_TIMEOUT", "", "", new DefaultListModel<>()), socketChannel));
|
activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator));
|
||||||
Boolean sendMessageToAllPlayersRes = sendMessageToAllPlayers.get();
|
return sendMessageJoinTimeoutRes;
|
||||||
if(!sendMessageToAllPlayersRes){
|
|
||||||
Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator));
|
|
||||||
return sendMessageToAllPlayersRes;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//All done, all player joined
|
System.out.println("TIMEOUT FINITO SENZA EFFETTI");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//Some user in the list is not online
|
|
||||||
Message message = new Message("USER_NOT_ONLINE", "", "", new DefaultListModel<>());
|
Message message = new Message("USER_NOT_ONLINE", "", "", new DefaultListModel<>());
|
||||||
byteMessage = message.toString().getBytes();
|
byteMessage = new String(message.toString()).getBytes();
|
||||||
bufferMessage.clear();
|
bufferMessage.clear();
|
||||||
bufferMessage = ByteBuffer.wrap(byteMessage);
|
bufferMessage = ByteBuffer.wrap(byteMessage);
|
||||||
this.socketChannel.write(bufferMessage);
|
this.socketChannel.write(bufferMessage);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Server.logger.write("MESSAGE DISPATCHER - START GAME: InterruptedException");
|
e.printStackTrace();
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
Server.logger.write("MESSAGE DISPATCHER - START GAME: ExecutionException");
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Server.logger.write("MESSAGE DISPATCHER - START GAME: IOException");
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
case "FETCH_HIGHSCORES":
|
case "FETCH_HIGHSCORES":
|
||||||
//Fetch hisghscore and send back to client
|
|
||||||
Future<DefaultListModel<String>> computeHighscores = threadPool.submit(new ComputeHighscores());
|
Future<DefaultListModel<String>> computeHighscores = threadPool.submit(new ComputeHighscores());
|
||||||
try {
|
try {
|
||||||
DefaultListModel<String> computeHighscoresRes = computeHighscores.get();
|
DefaultListModel<String> computeHighscoresRes = computeHighscores.get();
|
||||||
|
|
@ -115,31 +122,27 @@ public class MessageDispatcher implements Callable<Boolean> {
|
||||||
|
|
||||||
bufferMessage = ByteBuffer.wrap(byteMessage);
|
bufferMessage = ByteBuffer.wrap(byteMessage);
|
||||||
try {
|
try {
|
||||||
|
String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining());
|
||||||
|
System.out.println("INVIO HIGHSCORES "+ s);
|
||||||
socketChannel.write(bufferMessage);
|
socketChannel.write(bufferMessage);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Server.logger.write("MESSAGE DISPATCHER - FETCH HIGHSCORES: InterruptedException");
|
e.printStackTrace();
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
Server.logger.write("MESSAGE DISPATCHER - FETCH HIGHSCORES: ExecutionException");
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
case "JOIN_GAME":
|
case "JOIN_GAME":
|
||||||
//An user joined the game
|
|
||||||
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, request.data, socketChannel));
|
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, request.data, socketChannel));
|
||||||
try {
|
try {
|
||||||
Match match = Match.findMatch(Match.activeMatches, request.data.get(0));;
|
Match match = Match.findMatch(activeMatches, request.data.get(0));;
|
||||||
Boolean joinMatchRes = joinMatch.get();
|
Boolean joinMatchRes = joinMatch.get();
|
||||||
|
|
||||||
//If joinMatchRes=true start the game! Because all player joined
|
|
||||||
if(joinMatchRes){
|
if(joinMatchRes){
|
||||||
|
|
||||||
//If match not fired join timeout, notify all player that game is started
|
|
||||||
if(!match.joinTimeout) {
|
if(!match.joinTimeout) {
|
||||||
|
|
||||||
//Generate letters to send to clients
|
|
||||||
Future<DefaultListModel<String>> generateLetters = threadPool.submit(new GenerateLetters());
|
Future<DefaultListModel<String>> generateLetters = threadPool.submit(new GenerateLetters());
|
||||||
match.setLetters(generateLetters.get());
|
match.setLetters(generateLetters.get());
|
||||||
match.letters.addElement(String.valueOf(match.multicastId));
|
match.letters.addElement(String.valueOf(match.multicastId));
|
||||||
|
|
@ -152,28 +155,31 @@ public class MessageDispatcher implements Callable<Boolean> {
|
||||||
|
|
||||||
Message message = new Message("GAME_STARTED", "", "", match.letters);
|
Message message = new Message("GAME_STARTED", "", "", match.letters);
|
||||||
match.startGame();
|
match.startGame();
|
||||||
|
|
||||||
|
System.out.println("TIMEOUT CANCELLEd");
|
||||||
byteMessage = message.toString().getBytes();
|
byteMessage = message.toString().getBytes();
|
||||||
|
|
||||||
bufferMessage = ByteBuffer.wrap(byteMessage);
|
bufferMessage = ByteBuffer.wrap(byteMessage);
|
||||||
try {
|
try {
|
||||||
String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining());
|
String s = new String(bufferMessage.array(), bufferMessage.position(), bufferMessage.remaining());
|
||||||
|
System.out.println("INVIO GAME_STARTED "+ s);
|
||||||
socketClient.write(bufferMessage);
|
socketClient.write(bufferMessage);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (matchNotAvailable) {
|
if (matchNotAvailable) {
|
||||||
matchNotAvailable = false;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//Match doesn't exist more because a timeout happen
|
|
||||||
if(match == null){
|
if(match == null){
|
||||||
bufferMessage = ByteBuffer.allocate(1024);
|
bufferMessage = ByteBuffer.allocate(1024);
|
||||||
|
|
||||||
if (socketChannel != null) {
|
if (socketChannel != null) {
|
||||||
bufferMessage = ByteBuffer.allocate(1024);
|
bufferMessage = ByteBuffer.allocate(1024);
|
||||||
|
|
||||||
Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>());
|
Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>());
|
||||||
bufferMessage.clear();
|
bufferMessage.clear();
|
||||||
byteMessage = msg.toString().getBytes();
|
byteMessage = msg.toString().getBytes();
|
||||||
|
|
@ -181,23 +187,27 @@ public class MessageDispatcher implements Callable<Boolean> {
|
||||||
socketChannel.write(bufferMessage);
|
socketChannel.write(bufferMessage);
|
||||||
matchNotAvailable = true;
|
matchNotAvailable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Server.logger.write("MESSAGE DISPATCHER - JOIN GAME: InterruptedException");
|
e.printStackTrace();
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
Server.logger.write("MESSAGE DISPATCHER - JOIN GAME: ExecutionException");
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Server.logger.write("MESSAGE DISPATCHER - JOIN GAME: IOException");
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//If token is invalid, return error message to client
|
|
||||||
threadPool.submit(new TokenInvalid(request.sender, socketChannel, bufferMessage));
|
threadPool.submit(new TokenInvalid(request.sender, socketChannel, bufferMessage));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,28 +1,26 @@
|
||||||
package com.texttwist.server.models;
|
package com.texttwist.server.services;
|
||||||
|
|
||||||
import com.texttwist.server.services.JedisService;
|
|
||||||
import models.User;
|
import models.User;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 18/06/2017.
|
* Created by loke on 18/06/2017.
|
||||||
* Description: Accounts
|
|
||||||
*/
|
*/
|
||||||
public class Accounts {
|
public class AccountsService {
|
||||||
|
|
||||||
public List<User> users = Collections.synchronizedList(new ArrayList<User>());
|
public List<User> users = Collections.synchronizedList(new ArrayList<User>());
|
||||||
|
|
||||||
private static class Holder {
|
private static class Holder {
|
||||||
static final Accounts INSTANCE = new Accounts();
|
static final AccountsService INSTANCE = new AccountsService();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Accounts getInstance() {
|
public static AccountsService getInstance() {
|
||||||
return Accounts.Holder.INSTANCE;
|
return AccountsService.Holder.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Accounts(){
|
private AccountsService(){
|
||||||
List<Serializable> l = JedisService.get("users");
|
List<Serializable> l = JedisService.get("users");
|
||||||
for(int i=0; i<l.size(); i++) {
|
for(int i=0; i<l.size(); i++) {
|
||||||
users.add((User) l.get(i));
|
users.add((User) l.get(i));
|
||||||
|
|
@ -73,4 +71,9 @@ public class Accounts {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int size(){
|
||||||
|
return users.size();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
package com.texttwist.server.services;
|
package com.texttwist.server.services;
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
import com.texttwist.server.Server;
|
||||||
import com.texttwist.server.models.Accounts;
|
|
||||||
import com.texttwist.server.models.Sessions;
|
|
||||||
import constants.Config;
|
|
||||||
import interfaces.IAuth;
|
import interfaces.IAuth;
|
||||||
import interfaces.INotificationClient;
|
import interfaces.INotificationClient;
|
||||||
import models.Response;
|
import models.Response;
|
||||||
|
|
@ -16,22 +13,24 @@ import java.security.SecureRandom;
|
||||||
import static com.texttwist.server.Server.notificationServer;
|
import static com.texttwist.server.Server.notificationServer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 15/06/2017.
|
* Created by loke on 15/06/2017.
|
||||||
* Description: AuthService
|
|
||||||
*/
|
*/
|
||||||
public class AuthService extends UnicastRemoteObject implements IAuth {
|
public class AuthService extends UnicastRemoteObject implements IAuth {
|
||||||
|
|
||||||
private SecureRandom random = new SecureRandom();
|
private SecureRandom random = new SecureRandom();
|
||||||
|
public int serverPort = 9999;
|
||||||
|
|
||||||
public AuthService() throws RemoteException{
|
|
||||||
Server.logger.write("AuthService Service running at "+ Config.AuthServicePort +" port...");
|
public AuthService(int serverPort) throws RemoteException{
|
||||||
|
this.serverPort=serverPort;
|
||||||
|
Server.logger.write("AuthService Service running at "+serverPort+" port...");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response register(String userName, String password) throws RemoteException {
|
public Response register(String userName, String password) throws RemoteException {
|
||||||
Server.logger.write("Invoked register with username=" + userName + " AND " + " password=" + password);
|
Server.logger.write("Invoked register with username=" + userName + " AND " + " password=" + password);
|
||||||
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
|
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
|
||||||
if(Accounts.getInstance().register(userName, password)){
|
if(AccountsService.getInstance().register(userName, password)){
|
||||||
Server.logger.write("Registration successfull");
|
Server.logger.write("Registration successfull");
|
||||||
return new Response("Registration successfull", 200, null);
|
return new Response("Registration successfull", 200, null);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -46,11 +45,11 @@ public class AuthService extends UnicastRemoteObject implements IAuth {
|
||||||
public Response login(String userName, String password) throws RemoteException {
|
public Response login(String userName, String password) throws RemoteException {
|
||||||
Server.logger.write("Invoked login with username=" + userName + " AND " + " password=" + password);
|
Server.logger.write("Invoked login with username=" + userName + " AND " + " password=" + password);
|
||||||
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
|
if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
|
||||||
if(Accounts.getInstance().exists(userName) && Accounts.getInstance().checkPassword(userName, password)) {
|
if(AccountsService.getInstance().exists(userName) && AccountsService.getInstance().checkPassword(userName, password)) {
|
||||||
JsonObject data = new JsonObject();
|
JsonObject data = new JsonObject();
|
||||||
String token = nextSessionId();
|
String token = nextSessionId();
|
||||||
data.put("token", token);
|
data.put("token", token);
|
||||||
Sessions.getInstance().add(userName,token);
|
SessionsService.getInstance().add(userName,token);
|
||||||
Server.logger.write("Login successfull");
|
Server.logger.write("Login successfull");
|
||||||
return new Response("Login successfull", 200, data);
|
return new Response("Login successfull", 200, data);
|
||||||
}
|
}
|
||||||
|
|
@ -65,14 +64,14 @@ public class AuthService extends UnicastRemoteObject implements IAuth {
|
||||||
notificationServer.unregisterForCallback(stub);
|
notificationServer.unregisterForCallback(stub);
|
||||||
|
|
||||||
if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) {
|
if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) {
|
||||||
boolean res = Sessions.getInstance().remove(userName);
|
boolean res = SessionsService.getInstance().remove(userName);
|
||||||
if(res) {
|
if(res) {
|
||||||
Server.logger.write("Logout successfull");
|
Server.logger.write("Logout successfull");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Sessions.getInstance().remove(userName);
|
SessionsService.getInstance().remove(userName);
|
||||||
Server.logger.write("Logout successfull (but something gone wrong)");
|
Server.logger.write("Logout successfull (but something gone wrong)");
|
||||||
return new Response("Logout successfull (but something gone wrong)", 200, null);
|
return new Response("Logout successfull (but something gone wrong)", 200, null);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package com.texttwist.server.services;
|
package com.texttwist.server.services;
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
|
||||||
import models.User;
|
import models.User;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
|
|
||||||
|
|
@ -12,14 +11,12 @@ import java.util.List;
|
||||||
import static com.texttwist.server.Server.jedisPool;
|
import static com.texttwist.server.Server.jedisPool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 11/07/2017.
|
* Created by loke on 11/07/2017.
|
||||||
* Description: Jedis Service
|
|
||||||
*/
|
*/
|
||||||
public class JedisService {
|
public class JedisService {
|
||||||
|
|
||||||
|
public JedisService(){
|
||||||
|
|
||||||
JedisService(){
|
|
||||||
Server.logger.write("Jedis Service running on localhost...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Read the object from Base64 string. */
|
/** Read the object from Base64 string. */
|
||||||
|
|
@ -59,8 +56,11 @@ public class JedisService {
|
||||||
Jedis jedis = null;
|
Jedis jedis = null;
|
||||||
List<Serializable> l = new ArrayList<>();
|
List<Serializable> l = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
|
System.out.println("USER ss");
|
||||||
|
|
||||||
jedis = jedisPool.getResource();
|
jedis = jedisPool.getResource();
|
||||||
String usersString = jedis.get(key);
|
String usersString = jedis.get(key);
|
||||||
|
System.out.println("USER "+usersString);
|
||||||
if(usersString!=null) {
|
if(usersString!=null) {
|
||||||
String[] lines = usersString.split("\n");
|
String[] lines = usersString.split("\n");
|
||||||
for (int i = 0; i < lines.length; i++) {
|
for (int i = 0; i < lines.length; i++) {
|
||||||
|
|
|
||||||
|
|
@ -1,48 +1,75 @@
|
||||||
package com.texttwist.server.services;
|
|
||||||
|
|
||||||
|
package com.texttwist.server.services;
|
||||||
import com.texttwist.server.Server;
|
import com.texttwist.server.Server;
|
||||||
import com.texttwist.server.tasks.MessageDispatcher;
|
|
||||||
import com.texttwist.server.models.Dictionary;
|
import com.texttwist.server.models.Dictionary;
|
||||||
|
import com.texttwist.server.models.Match;
|
||||||
|
import com.texttwist.server.models.servers.ThreadProxy;
|
||||||
|
import com.texttwist.server.tasks.ReceiveWords;
|
||||||
import constants.Config;
|
import constants.Config;
|
||||||
import models.Message;
|
import models.Message;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.*;
|
import java.nio.channels.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import static java.nio.channels.SelectionKey.OP_ACCEPT;
|
import static java.nio.channels.SelectionKey.OP_ACCEPT;
|
||||||
import static java.nio.channels.SelectionKey.OP_READ;
|
import static java.nio.channels.SelectionKey.OP_READ;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Author: Lorenzo Iovino on 17/06/2017.
|
|
||||||
* Description: Message Service
|
|
||||||
*/
|
|
||||||
public class MessageService implements Runnable{
|
public class MessageService implements Runnable{
|
||||||
|
|
||||||
private Selector selector = null;
|
private int serverPort;
|
||||||
private ExecutorService dispatcherPool = Executors.newCachedThreadPool();
|
private ThreadProxy proxy;
|
||||||
|
private ReceiveWords wordsReceiver;
|
||||||
|
|
||||||
public MessageService()
|
private DatagramChannel datagramChannel;
|
||||||
{
|
private Selector selector = null;
|
||||||
|
private ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||||
|
private String dictionaryPath = "./Server/resources/dictionary";
|
||||||
|
public static Dictionary dict;
|
||||||
|
SocketChannel client;
|
||||||
|
ByteBuffer bufferWords = ByteBuffer.allocate(1024);
|
||||||
|
ByteBuffer bufferMessages = ByteBuffer.allocate(1024);
|
||||||
|
|
||||||
|
|
||||||
|
public static List<Match> activeMatches = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
public static Integer multicastID = 30000;
|
||||||
|
|
||||||
|
public MessageService(int port){
|
||||||
|
this.serverPort = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(){
|
||||||
|
|
||||||
|
dict = new Dictionary(dictionaryPath);
|
||||||
try {
|
try {
|
||||||
selector = Selector.open();
|
selector = Selector.open();
|
||||||
|
|
||||||
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
|
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
|
||||||
serverSocketChannel.configureBlocking(false);
|
serverSocketChannel.configureBlocking(false);
|
||||||
serverSocketChannel.socket().bind(new InetSocketAddress(Config.MessageServicePort));
|
serverSocketChannel.socket().bind(new InetSocketAddress(serverPort));
|
||||||
serverSocketChannel.register(selector, OP_ACCEPT);
|
serverSocketChannel.register(selector, OP_ACCEPT);
|
||||||
Server.logger.write("Message Service is running at "+Config.MessageServicePort +" port...");
|
InetSocketAddress address = new InetSocketAddress(Config.WordsReceiverServerURI,Config.WordsReceiverServerPort);
|
||||||
|
datagramChannel = DatagramChannel.open();
|
||||||
|
datagramChannel.configureBlocking(true);
|
||||||
|
datagramChannel.connect(address);
|
||||||
|
Server.logger.write("MessageService Service is running at "+this.serverPort+" port...");
|
||||||
|
|
||||||
|
wordsReceiver = new ReceiveWords(datagramChannel, bufferWords, bufferMessages, client);
|
||||||
|
threadPool.submit(wordsReceiver);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void run(){
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
System.out.println("WAITING FOR MSG");
|
||||||
try {
|
try {
|
||||||
selector.select();
|
selector.select();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
@ -51,9 +78,9 @@ public class MessageService implements Runnable{
|
||||||
|
|
||||||
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
|
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
ByteBuffer bufferMessages = ByteBuffer.allocate(1024);
|
bufferMessages = ByteBuffer.allocate(1024);
|
||||||
bufferMessages.clear();
|
bufferMessages.clear();
|
||||||
SocketChannel client = null;
|
client = null;
|
||||||
SelectionKey key = iter.next();
|
SelectionKey key = iter.next();
|
||||||
iter.remove();
|
iter.remove();
|
||||||
|
|
||||||
|
|
@ -70,10 +97,12 @@ public class MessageService implements Runnable{
|
||||||
if (client.read(bufferMessages) != -1) {
|
if (client.read(bufferMessages) != -1) {
|
||||||
bufferMessages.flip();
|
bufferMessages.flip();
|
||||||
String line = new String(bufferMessages.array(), bufferMessages.position(), bufferMessages.remaining());
|
String line = new String(bufferMessages.array(), bufferMessages.position(), bufferMessages.remaining());
|
||||||
|
System.out.println(line);
|
||||||
if (line.startsWith("MESSAGE")) {
|
if (line.startsWith("MESSAGE")) {
|
||||||
|
SessionsService.getInstance().printAll();
|
||||||
Message msg = Message.toMessage(line);
|
Message msg = Message.toMessage(line);
|
||||||
MessageDispatcher proxy = new MessageDispatcher(msg, client, bufferMessages);
|
proxy = new ThreadProxy(msg, client, bufferMessages);
|
||||||
dispatcherPool.submit(proxy);
|
threadPool.submit(proxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (line.startsWith("CLOSE")) {
|
if (line.startsWith("CLOSE")) {
|
||||||
|
|
@ -90,6 +119,7 @@ public class MessageService implements Runnable{
|
||||||
key.cancel();
|
key.cancel();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
package com.texttwist.server.services;
|
package com.texttwist.server.services;
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
|
||||||
import constants.Config;
|
|
||||||
import interfaces.INotificationClient;
|
import interfaces.INotificationClient;
|
||||||
import interfaces.INotificationServer;
|
import interfaces.INotificationServer;
|
||||||
|
|
||||||
|
|
@ -13,36 +11,45 @@ import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 19/06/2017.
|
* Created by loke on 19/06/2017.
|
||||||
* Description: Notification Service
|
|
||||||
*/
|
*/
|
||||||
public class NotificationService implements INotificationServer {
|
public class NotificationService implements INotificationServer {
|
||||||
|
|
||||||
private List<INotificationClient> clients;
|
private List<INotificationClient> clients;
|
||||||
public NotificationService() throws RemoteException {
|
public NotificationService() throws RemoteException {
|
||||||
super();
|
super();
|
||||||
Server.logger.write("Notification Service running at "+ Config.NotificationServicePort +" port...");
|
|
||||||
clients = new ArrayList<>();
|
clients = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void registerForCallback(INotificationClient clientInterface) throws RemoteException {
|
public synchronized void registerForCallback(INotificationClient clientInterface) throws RemoteException {
|
||||||
if(!clients.contains(clientInterface)){
|
if(!clients.contains(clientInterface)){
|
||||||
clients.add(clientInterface);
|
clients.add(clientInterface);
|
||||||
|
System.out.println(clientInterface);
|
||||||
|
System.out.println("New client registered");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void unregisterForCallback(INotificationClient client) throws RemoteException {
|
public synchronized void unregisterForCallback(INotificationClient client) throws RemoteException {
|
||||||
clients.remove(client);
|
if (clients.remove(client)) {
|
||||||
|
System.out.println("Client unregistered");
|
||||||
|
} else {
|
||||||
|
System.out.println("Unable to unregister client");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void sendInvitations(String username, DefaultListModel<String> users){
|
public synchronized void sendInvitations(String username, DefaultListModel<String> users){
|
||||||
Iterator i = clients.iterator();
|
Iterator i = clients.iterator();
|
||||||
INotificationClient client = null;
|
INotificationClient client = null;
|
||||||
|
System.out.println("Starting callbacks");
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
client = (INotificationClient) i.next();
|
client = (INotificationClient) i.next();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
System.out.println("SENDING INVITE TO "+users);
|
||||||
client.sendInvite(username, users);
|
client.sendInvite(username, users);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
|
System.out.println("Sembra down");
|
||||||
try {
|
try {
|
||||||
unregisterForCallback(client);
|
unregisterForCallback(client);
|
||||||
} catch (RemoteException e1) {
|
} catch (RemoteException e1) {
|
||||||
|
|
@ -50,5 +57,6 @@ public class NotificationService implements INotificationServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,58 +0,0 @@
|
||||||
package com.texttwist.server.services;
|
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
|
||||||
import com.texttwist.server.models.Sessions;
|
|
||||||
import com.texttwist.server.models.Match;
|
|
||||||
import com.texttwist.server.tasks.ComputeScore;
|
|
||||||
import constants.Config;
|
|
||||||
import models.Message;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.*;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Author: Lorenzo Iovino on 27/06/2017.
|
|
||||||
* Description: Receive Words Service
|
|
||||||
*/
|
|
||||||
public class ReceiveWordsService implements Runnable {
|
|
||||||
|
|
||||||
private ExecutorService threadPool = Executors.newCachedThreadPool();
|
|
||||||
|
|
||||||
public ReceiveWordsService() {
|
|
||||||
Server.logger.write("ReceiveWords Service running at "+Config.WordsReceiverServicePort +" port...");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run(){
|
|
||||||
Message msg;
|
|
||||||
DatagramSocket s = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
s = new DatagramSocket(Config.WordsReceiverServicePort);
|
|
||||||
} catch (SocketException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
DatagramPacket packet;
|
|
||||||
|
|
||||||
while(true) {
|
|
||||||
byte[] buf = new byte[1024];
|
|
||||||
|
|
||||||
packet = new DatagramPacket(buf, buf.length);
|
|
||||||
try {
|
|
||||||
s.receive(packet);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
String rcv = new String(packet.getData());
|
|
||||||
if (rcv.startsWith("MESSAGE")) {
|
|
||||||
msg = Message.toMessage(rcv);
|
|
||||||
if(Sessions.getInstance().isValidToken(msg.token)) {
|
|
||||||
Match match = Match.findMatchByPlayerName(msg.sender);
|
|
||||||
threadPool.submit(new ComputeScore(msg.sender, msg.data, match));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.texttwist.server.services;
|
||||||
|
import models.Session;
|
||||||
|
import models.User;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by loke on 17/06/2017.
|
||||||
|
*/
|
||||||
|
public class SessionsService {
|
||||||
|
|
||||||
|
private List<Session> sessions = Collections.synchronizedList(new ArrayList<Session>());
|
||||||
|
private static class Holder {
|
||||||
|
static final SessionsService INSTANCE = new SessionsService();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SessionsService getInstance() {
|
||||||
|
return Holder.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SessionsService(){}
|
||||||
|
|
||||||
|
public boolean add(String userName, String token) {
|
||||||
|
remove(userName);
|
||||||
|
return sessions.add(new Session(new User(userName,"",0), token));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printAll(){
|
||||||
|
Iterator<Session> i = sessions.iterator();
|
||||||
|
while (i.hasNext()) {
|
||||||
|
Session elem = i.next();
|
||||||
|
System.out.println(elem.account.userName + " | " + elem.token);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean remove(String userName){
|
||||||
|
if(exists(userName)) {
|
||||||
|
Session s = getSession(userName);
|
||||||
|
if(s != null) {
|
||||||
|
sessions.remove(s);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Session getSession(String userName) {
|
||||||
|
Iterator<Session> i = sessions.iterator();
|
||||||
|
while (i.hasNext()) {
|
||||||
|
Session elem = i.next();
|
||||||
|
if (elem.account.userName.equals(userName)) {
|
||||||
|
return elem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exists(String userName) {
|
||||||
|
Iterator<Session> i = sessions.iterator();
|
||||||
|
while (i.hasNext()) {
|
||||||
|
Session elem = i.next();
|
||||||
|
if (elem.account.userName.equals(userName)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isValidToken(String token) {
|
||||||
|
Iterator<Session> i = sessions.iterator();
|
||||||
|
while (i.hasNext()) {
|
||||||
|
if (i.next().token.equals(token)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
package com.texttwist.server.tasks;
|
package com.texttwist.server.tasks;
|
||||||
|
|
||||||
import com.texttwist.server.models.Sessions;
|
import com.texttwist.server.services.SessionsService;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 19/06/2017.
|
* Created by loke on 19/06/2017.
|
||||||
* Description: Task: Check Online Users
|
|
||||||
*/
|
*/
|
||||||
public class CheckOnlineUsers implements Callable<Boolean> {
|
public class CheckOnlineUsers implements Callable<Boolean> {
|
||||||
private final DefaultListModel<String> users;
|
private final DefaultListModel<String> users;
|
||||||
|
|
@ -19,7 +18,7 @@ public class CheckOnlineUsers implements Callable<Boolean> {
|
||||||
@Override
|
@Override
|
||||||
public Boolean call() throws Exception {
|
public Boolean call() throws Exception {
|
||||||
for(int i = 0; i < users.size(); i++){
|
for(int i = 0; i < users.size(); i++){
|
||||||
if(!(Sessions.getInstance().exists(users.get(i)))){
|
if(!(SessionsService.getInstance().exists(users.get(i)))){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package com.texttwist.server.tasks;
|
package com.texttwist.server.tasks;
|
||||||
|
|
||||||
import com.texttwist.server.models.Accounts;
|
import com.texttwist.server.services.AccountsService;
|
||||||
import com.texttwist.server.services.JedisService;
|
import com.texttwist.server.services.JedisService;
|
||||||
import models.User;
|
import models.User;
|
||||||
|
|
||||||
|
|
@ -9,25 +9,26 @@ import java.util.Comparator;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 28/06/2017.
|
* Created by loke on 28/06/2017.
|
||||||
* Description: Task: Compute Highscores
|
|
||||||
*/
|
*/
|
||||||
public class ComputeHighscores implements Callable<DefaultListModel<String>> {
|
public class ComputeHighscores implements Callable<DefaultListModel<String>> {
|
||||||
|
|
||||||
|
public ComputeHighscores(){}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefaultListModel<String> call() throws Exception {
|
public DefaultListModel<String> call() throws Exception {
|
||||||
DefaultListModel<String> l = new DefaultListModel<>();
|
DefaultListModel<String> l = new DefaultListModel<>();
|
||||||
|
|
||||||
Accounts.getInstance().users.sort(new Comparator<User>() {
|
AccountsService.getInstance().users.sort(new Comparator<User>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(User o1, User o2) {
|
public int compare(User o1, User o2) {
|
||||||
return o2.score.compareTo(o1.score);
|
return o2.score.compareTo(o1.score);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
JedisService.removeAll("users");
|
JedisService.removeAll("users");
|
||||||
for(int i = 0; i< Accounts.getInstance().users.size(); i++){
|
for(int i = 0; i< AccountsService.getInstance().users.size(); i++){
|
||||||
l.addElement(Accounts.getInstance().users.get(i).userName+":"+ Accounts.getInstance().users.get(i).score);
|
l.addElement(AccountsService.getInstance().users.get(i).userName+":"+ AccountsService.getInstance().users.get(i).score);
|
||||||
JedisService.add("users", Accounts.getInstance().users.get(i));
|
JedisService.add("users", AccountsService.getInstance().users.get(i));
|
||||||
}
|
}
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,21 @@
|
||||||
package com.texttwist.server.tasks;
|
package com.texttwist.server.tasks;
|
||||||
|
import com.texttwist.server.services.AccountsService;
|
||||||
import com.texttwist.server.models.Accounts;
|
|
||||||
import com.texttwist.server.models.Dictionary;
|
import com.texttwist.server.models.Dictionary;
|
||||||
import com.texttwist.server.models.Match;
|
import com.texttwist.server.models.Match;
|
||||||
import models.User;
|
import models.User;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 27/06/2017.
|
* Created by loke on 27/06/2017.
|
||||||
* Description: Task: Ccmpute Score
|
|
||||||
*/
|
*/
|
||||||
public class ComputeScore implements Callable<Integer> {
|
public class ComputeScore implements Callable<Integer> {
|
||||||
|
|
||||||
private DefaultListModel<String> words;
|
public DefaultListModel<String> words;
|
||||||
private final String sender;
|
public final String sender;
|
||||||
public Match match;
|
public Match match;
|
||||||
private DefaultListModel<String> wordsValid = new DefaultListModel<>();
|
public DefaultListModel<String> wordsValid;
|
||||||
|
|
||||||
public ComputeScore(String sender, DefaultListModel<String> words, Match match){
|
public ComputeScore(String sender, DefaultListModel<String> words, Match match){
|
||||||
this.words = words;
|
this.words = words;
|
||||||
|
|
@ -26,26 +25,36 @@ public class ComputeScore implements Callable<Integer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer call() throws Exception {
|
public Integer call() throws Exception {
|
||||||
|
wordsValid = new DefaultListModel<>();
|
||||||
//Compute the score depending on the size of the words
|
|
||||||
Integer score = 0;
|
Integer score = 0;
|
||||||
|
|
||||||
for (int i = 0; i < words.size(); i++) {
|
for (int i = 0; i < words.size(); i++) {
|
||||||
if (isValid(words.get(i), match.letters)) {
|
if (isValid(words.get(i), match.letters)) {
|
||||||
score += words.get(i).length();
|
score += words.get(i).length();
|
||||||
|
System.out.println(words.get(i) + " is valid!" + " : " + score );
|
||||||
wordsValid.addElement(words.get(i));
|
wordsValid.addElement(words.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match.setScore(sender, score);
|
|
||||||
|
|
||||||
User u = Accounts.getInstance().findUser(sender);
|
System.out.println(sender +" totalize SCORE = " + score);
|
||||||
|
match.setScore(sender, score);
|
||||||
|
System.out.println(score);
|
||||||
|
|
||||||
|
User u = AccountsService.getInstance().findUser(sender);
|
||||||
u.addScore(score);
|
u.addScore(score);
|
||||||
|
|
||||||
if(match.allPlayersSendedHisScore()) {
|
if(match.allPlayersSendedHisScore()) {
|
||||||
|
|
||||||
match.matchTimeout = false;
|
match.matchTimeout = false;
|
||||||
|
System.out.println("MATCH TIMEOUT CANCELLATO");
|
||||||
|
|
||||||
match.setUndefinedScorePlayersToZero();
|
match.setUndefinedScorePlayersToZero();
|
||||||
new SendFinalScores(match).call();
|
match.sendScores();
|
||||||
|
|
||||||
}
|
}
|
||||||
return score;
|
return score;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Boolean isValid(String word, DefaultListModel<String> letters) {
|
private Boolean isValid(String word, DefaultListModel<String> letters) {
|
||||||
|
|
@ -60,6 +69,7 @@ public class ComputeScore implements Callable<Integer> {
|
||||||
if(word.equals("")){
|
if(word.equals("")){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!isCharacterPresent || wordsValid.indexOf(word)!=-1){
|
if(!isCharacterPresent || wordsValid.indexOf(word)!=-1){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,25 @@
|
||||||
package com.texttwist.server.tasks;
|
package com.texttwist.server.tasks;
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
import com.texttwist.server.services.MessageService;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 25/06/2017.
|
* Created by loke on 25/06/2017.
|
||||||
* Description: Task: Generate Letters
|
|
||||||
*/
|
*/
|
||||||
public class GenerateLetters implements Callable<DefaultListModel<String>> {
|
public class GenerateLetters implements Callable<DefaultListModel<String>> {
|
||||||
|
|
||||||
|
|
||||||
|
public GenerateLetters(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DefaultListModel<String> call() throws Exception {
|
public DefaultListModel<String> call() throws Exception {
|
||||||
DefaultListModel<String> l = new DefaultListModel<>();
|
DefaultListModel<String> l = new DefaultListModel<String>();
|
||||||
|
|
||||||
String word = Server.dict.getRandomWord(6, 7);
|
String word = MessageService.dict.getRandomWord(6, 7);
|
||||||
for (int i = 0;i < word.length(); i++){
|
for (int i = 0;i < word.length(); i++){
|
||||||
l.addElement(String.valueOf(word.charAt(i)));
|
l.addElement(String.valueOf(word.charAt(i)));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,15 @@ import javafx.util.Pair;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
import static com.texttwist.server.services.MessageService.activeMatches;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 23/06/2017.
|
* Created by loke on 23/06/2017.
|
||||||
* Description: Task: Join Match
|
|
||||||
*/
|
*/
|
||||||
public class JoinMatch implements Callable<Boolean> {
|
public class JoinMatch implements Callable<Boolean> {
|
||||||
private final String matchName;
|
public final String matchName;
|
||||||
private final String playerName;
|
public final String playerName;
|
||||||
private final SocketChannel socketChannel;
|
public final SocketChannel socketChannel;
|
||||||
|
|
||||||
public JoinMatch(String playerName, DefaultListModel<String> matchName, SocketChannel socketChannel) {
|
public JoinMatch(String playerName, DefaultListModel<String> matchName, SocketChannel socketChannel) {
|
||||||
this.playerName = playerName;
|
this.playerName = playerName;
|
||||||
|
|
@ -23,7 +23,7 @@ public class JoinMatch implements Callable<Boolean> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean call() throws Exception {
|
public Boolean call() throws Exception {
|
||||||
final Match thisMatch = Match.findMatch(Match.activeMatches, this.matchName);
|
final Match thisMatch = Match.findMatch(activeMatches, this.matchName);
|
||||||
if (thisMatch != null) {
|
if (thisMatch != null) {
|
||||||
for (int j = 0; j < thisMatch.playersStatus.size(); j++) {
|
for (int j = 0; j < thisMatch.playersStatus.size(); j++) {
|
||||||
String name = thisMatch.playersStatus.get(j).getKey();
|
String name = thisMatch.playersStatus.get(j).getKey();
|
||||||
|
|
@ -39,6 +39,18 @@ public class JoinMatch implements Callable<Boolean> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void printAll(Match match){
|
||||||
|
for (int i = 0; i < match.playersStatus.size(); i++) {
|
||||||
|
|
||||||
|
System.out.println(match.playersStatus.get(i).getKey());
|
||||||
|
System.out.println(match.playersStatus.get(i).getValue());
|
||||||
|
System.out.println(match.playersSocket.get(i).getKey());
|
||||||
|
System.out.println(match.playersSocket.get(i).getValue());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Boolean allJoined(Match match) {
|
private Boolean allJoined(Match match) {
|
||||||
for (int i = 0; i < match.playersStatus.size(); i++) {
|
for (int i = 0; i < match.playersStatus.size(); i++) {
|
||||||
if (match.playersStatus.get(i).getValue() == 0) {
|
if (match.playersStatus.get(i).getValue() == 0) {
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,30 @@
|
||||||
package com.texttwist.server.tasks;
|
package com.texttwist.server.tasks;
|
||||||
import com.texttwist.server.models.Match;
|
import com.texttwist.server.models.Match;
|
||||||
import constants.Config;
|
|
||||||
|
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 23/06/2017.
|
* Created by loke on 23/06/2017.
|
||||||
* Description: Task: Join Timeout
|
|
||||||
*/
|
*/
|
||||||
public class TimeoutJoin implements Callable<Boolean> {
|
public class JoinTimeout implements Callable<Boolean> {
|
||||||
|
|
||||||
public final Match match;
|
public final Match match;
|
||||||
|
|
||||||
public TimeoutJoin(Match match) {
|
public JoinTimeout(Match match) {
|
||||||
this.match = match;
|
this.match = match;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean call() throws Exception {
|
public Boolean call() throws Exception {
|
||||||
try {
|
try {
|
||||||
match.joinTimeout = true;
|
Thread.currentThread().sleep(7*60*1000);
|
||||||
Thread.currentThread().sleep(Config.joinMatchTimeout);
|
System.out.println("TIMEOUTTTT");
|
||||||
|
|
||||||
if(match.joinTimeout) {
|
if(match.joinTimeout) {
|
||||||
match.joinTimeout = false;
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
match.joinTimeout = false;
|
return true;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
@ -1,17 +1,16 @@
|
||||||
package com.texttwist.server.tasks;
|
package com.texttwist.server.tasks;
|
||||||
|
|
||||||
import com.texttwist.server.models.Match;
|
import com.texttwist.server.models.Match;
|
||||||
import constants.Config;
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 27/06/2017.
|
* Created by loke on 27/06/2017.
|
||||||
* Description: Task: Match Timeout
|
|
||||||
*/
|
*/
|
||||||
public class TimeoutMatch implements Callable<Boolean> {
|
public class MatchTimeout implements Callable<Boolean> {
|
||||||
|
|
||||||
private Match match;
|
private Match match;
|
||||||
public TimeoutMatch(Match match) {
|
public MatchTimeout(Match match) {
|
||||||
this.match = match;
|
this.match = match;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -19,11 +18,12 @@ public class TimeoutMatch implements Callable<Boolean> {
|
||||||
@Override
|
@Override
|
||||||
public Boolean call() throws Exception {
|
public Boolean call() throws Exception {
|
||||||
try {
|
try {
|
||||||
Thread.currentThread().sleep(Config.sendWordsTimeout);
|
Thread.currentThread().sleep(5*60*1000); //TODO 5*60*1000
|
||||||
|
match.setUndefinedScorePlayersToZero();
|
||||||
|
|
||||||
if(match.matchTimeout) {
|
if(match.matchTimeout) {
|
||||||
match.setUndefinedScorePlayersToZero();
|
System.out.println("SEND BROADCAST BECAUSE TIMEOUT");
|
||||||
new SendFinalScores(match).call();
|
match.sendScores();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
71
Server/src/com/texttwist/server/tasks/ReceiveWords.java
Normal file
71
Server/src/com/texttwist/server/tasks/ReceiveWords.java
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
package com.texttwist.server.tasks;
|
||||||
|
|
||||||
|
import com.texttwist.server.services.SessionsService;
|
||||||
|
import com.texttwist.server.models.Match;
|
||||||
|
import constants.Config;
|
||||||
|
import models.Message;
|
||||||
|
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.DatagramChannel;
|
||||||
|
import java.nio.channels.SocketChannel;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by loke on 27/06/2017.
|
||||||
|
*/
|
||||||
|
public class ReceiveWords implements Callable<Boolean>{
|
||||||
|
|
||||||
|
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
|
public DatagramChannel channel;
|
||||||
|
ByteBuffer bufferWords;
|
||||||
|
ByteBuffer bufferMessages;
|
||||||
|
public SocketChannel socketChannel;
|
||||||
|
|
||||||
|
|
||||||
|
public ReceiveWords(DatagramChannel channel, ByteBuffer buffer, ByteBuffer bufferMessages, SocketChannel socketChannel) {
|
||||||
|
this.bufferWords = buffer;
|
||||||
|
this.channel = channel;
|
||||||
|
this.bufferMessages = bufferMessages;
|
||||||
|
this.socketChannel = socketChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean call() throws Exception {
|
||||||
|
|
||||||
|
|
||||||
|
Message msg;
|
||||||
|
DatagramSocket s = new DatagramSocket(Config.WordsReceiverServerPort);
|
||||||
|
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
System.out.println("RECEIVIN WORDS");
|
||||||
|
|
||||||
|
DatagramPacket packet = new DatagramPacket(buf, buf.length);
|
||||||
|
s.receive(packet);
|
||||||
|
|
||||||
|
System.out.println("WORDS RECEIVED");
|
||||||
|
String rcv = new String(packet.getData());
|
||||||
|
System.out.println(rcv);
|
||||||
|
if (rcv.startsWith("MESSAGE")) {
|
||||||
|
msg = Message.toMessage(rcv);
|
||||||
|
if(SessionsService.getInstance().isValidToken(msg.token)) {
|
||||||
|
System.out.println(msg.sender);
|
||||||
|
Match match = Match.findMatchByPlayer(msg.sender);
|
||||||
|
threadPool.submit(new ComputeScore(msg.sender, msg.data, match));
|
||||||
|
} else {
|
||||||
|
threadPool.submit(new TokenInvalid(msg.sender, socketChannel, bufferMessages));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
package com.texttwist.server.tasks;
|
|
||||||
|
|
||||||
import com.texttwist.server.models.Match;
|
|
||||||
import constants.Config;
|
|
||||||
import models.Message;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.DatagramPacket;
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.MulticastSocket;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Author: Lorenzo Iovino on 13/07/2017.
|
|
||||||
* Description: Task: Send Final Scores
|
|
||||||
*/
|
|
||||||
public class SendFinalScores implements Callable<Void> {
|
|
||||||
|
|
||||||
private Match match;
|
|
||||||
|
|
||||||
public SendFinalScores(Match match){
|
|
||||||
this.match = match;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void call() throws Exception {
|
|
||||||
|
|
||||||
Message msg = new Message("FINALSCORE", "SERVER", "", match.getMatchPlayersScoreAsStringList());
|
|
||||||
MulticastSocket multicastSocket = null;
|
|
||||||
try {
|
|
||||||
multicastSocket = new MulticastSocket(match.multicastId);
|
|
||||||
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) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
Match.activeMatches.remove(Match.findMatchIndex(Match.activeMatches, match.matchCreator));
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
package com.texttwist.server.tasks;
|
package com.texttwist.server.tasks;
|
||||||
|
|
||||||
import com.texttwist.server.Server;
|
import com.texttwist.server.Server;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 19/06/2017.
|
* Created by loke on 19/06/2017.
|
||||||
* Description: Task: Send Invitations
|
|
||||||
*/
|
*/
|
||||||
public class SendInvitations implements Callable<Boolean> {
|
public class SendInvitations implements Callable<Boolean> {
|
||||||
private DefaultListModel<String> users;
|
private DefaultListModel<String> users;
|
||||||
|
|
@ -20,10 +20,15 @@ public class SendInvitations implements Callable<Boolean> {
|
||||||
@Override
|
@Override
|
||||||
public Boolean call() throws Exception {
|
public Boolean call() throws Exception {
|
||||||
try {
|
try {
|
||||||
|
System.out.println("INVIA INVITO A" + users);
|
||||||
|
|
||||||
Server.notificationServer.sendInvitations(sender, users);
|
Server.notificationServer.sendInvitations(sender, users);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
System.out.println("Eccezione" + e);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,21 +2,21 @@ package com.texttwist.server.tasks;
|
||||||
|
|
||||||
import com.texttwist.server.models.Match;
|
import com.texttwist.server.models.Match;
|
||||||
import models.Message;
|
import models.Message;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 27/06/2017.
|
* Created by loke on 27/06/2017.
|
||||||
* Description: Task: Send Message To All Players
|
|
||||||
*/
|
*/
|
||||||
public class SendMessageToAllPlayers implements Callable<Boolean> {
|
public class SendMessageToAllPlayers implements Callable<Boolean> {
|
||||||
|
|
||||||
|
|
||||||
public final Match match;
|
public final Match match;
|
||||||
private final Message message;
|
public final Message message;
|
||||||
private SocketChannel socketChannel;
|
public SocketChannel socketChannel;
|
||||||
|
|
||||||
public SendMessageToAllPlayers(Match match, Message message, SocketChannel socketChannel){
|
public SendMessageToAllPlayers(Match match, Message message, SocketChannel socketChannel){
|
||||||
this.match = match;
|
this.match = match;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
|
|
@ -36,8 +36,10 @@ public class SendMessageToAllPlayers implements Callable<Boolean> {
|
||||||
buffer = ByteBuffer.wrap(byteMessage);
|
buffer = ByteBuffer.wrap(byteMessage);
|
||||||
socketChannel.write(buffer);
|
socketChannel.write(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,14 @@ import models.Message;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.Channel;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Lorenzo Iovino on 11/07/2017.
|
* Created by loke on 11/07/2017.
|
||||||
* Description: Task: Token Invalid
|
|
||||||
*/
|
*/
|
||||||
public class TokenInvalid implements Callable<Void> {
|
public class TokenInvalid implements Callable<Boolean> {
|
||||||
private String sender;
|
private String sender;
|
||||||
private ByteBuffer buffer;
|
private ByteBuffer buffer;
|
||||||
private SocketChannel channel;
|
private SocketChannel channel;
|
||||||
|
|
@ -24,14 +24,15 @@ public class TokenInvalid implements Callable<Void> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void call()throws Exception {
|
public Boolean call()throws Exception {
|
||||||
Server.logger.write("TOKEN INVALID: TOKEN USED BY "+ sender+ " IS NOT VALID");
|
System.out.print("TOKEN NON VALIDO");
|
||||||
Message msg = new Message("TOKEN_NOT_VALID", "", null, new DefaultListModel<>());
|
buffer = ByteBuffer.allocate(1024);
|
||||||
|
Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>());
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
byte[] byteMessage = msg.toString().getBytes();
|
byte[] byteMessage = msg.toString().getBytes();
|
||||||
buffer = ByteBuffer.wrap(byteMessage);
|
buffer = ByteBuffer.wrap(byteMessage);
|
||||||
channel.write(buffer);
|
channel.write(buffer);
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
BIN
TwistText.pdf
BIN
TwistText.pdf
Binary file not shown.
Binary file not shown.
9134
client_1.log
Normal file
9134
client_1.log
Normal file
File diff suppressed because it is too large
Load diff
1346
notificationServer.log
Normal file
1346
notificationServer.log
Normal file
File diff suppressed because it is too large
Load diff
9820
server.log
Normal file
9820
server.log
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue