Synchronization for joins the game
This commit is contained in:
parent
124b672aeb
commit
9cc0297a5b
16 changed files with 1462 additions and 443 deletions
|
|
@ -9,5 +9,6 @@
|
|||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="com.github.cliftonlabs:json-simple:2.1.2" level="project" />
|
||||
<orderEntry type="module" module-name="Commons" />
|
||||
<orderEntry type="module" module-name="Client" />
|
||||
</component>
|
||||
</module>
|
||||
|
|
@ -24,7 +24,11 @@ public class AccountsManager {
|
|||
return AccountsManager.Holder.INSTANCE;
|
||||
}
|
||||
|
||||
private AccountsManager(){}
|
||||
private AccountsManager(){
|
||||
accounts.add(new Account("a","a"));
|
||||
accounts.add(new Account("b","b"));
|
||||
accounts.add(new Account("c","c"));
|
||||
}
|
||||
|
||||
public boolean register(String userName, String password) {
|
||||
if(!exists(userName)){
|
||||
|
|
|
|||
|
|
@ -1,12 +1,15 @@
|
|||
|
||||
package com.texttwist.server.components;
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.SendInvitations;
|
||||
import com.texttwist.server.tasks.WaitForPlayers;
|
||||
import jdk.nashorn.internal.parser.JSONParser;
|
||||
import models.Message;
|
||||
import org.json.simple.JsonObject;
|
||||
import utilities.Logger;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
|
|
@ -32,6 +35,8 @@ public class GameServer implements Runnable{
|
|||
protected Selector selector = null;
|
||||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
|
||||
public static DefaultListModel<Match> activeMatches = new DefaultListModel<Match>();
|
||||
|
||||
public GameServer(int port){
|
||||
this.serverPort = port;
|
||||
}
|
||||
|
|
@ -59,7 +64,7 @@ public class GameServer implements Runnable{
|
|||
|
||||
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
|
||||
while (iter.hasNext()) {
|
||||
SocketChannel client;
|
||||
SocketChannel client = null;
|
||||
SelectionKey key = iter.next();
|
||||
iter.remove();
|
||||
|
||||
|
|
@ -82,9 +87,10 @@ public class GameServer implements Runnable{
|
|||
Message msg = Message.toMessage(line);
|
||||
proxy = new ThreadProxy(msg, client);
|
||||
Future<Boolean> identifyMessage = threadPool.submit(proxy);
|
||||
identifyMessage.get();
|
||||
key.cancel();
|
||||
}
|
||||
|
||||
|
||||
if (line.startsWith("CLOSE")) {
|
||||
client.close();
|
||||
} else if (line.startsWith("QUIT")) {
|
||||
|
|
@ -105,35 +111,15 @@ public class GameServer implements Runnable{
|
|||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
System.out.println("DDD");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
/* try {
|
||||
this.serverSocket = new ServerSocket(this.serverPort);
|
||||
Logger.write("Game Service is running at "+this.serverPort+" port...");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while(true){
|
||||
Socket clientSocket = null;
|
||||
try {
|
||||
clientSocket = this.serverSocket.accept();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
proxy = new ThreadProxy(clientSocket);
|
||||
Thread t = new Thread(proxy);
|
||||
t.start();
|
||||
|
||||
*/
|
||||
|
||||
// threadPool.shutdown(); // shutdown the pool.
|
||||
//this.threadPool.execute(new ThreadWorker(clientSocket, "TASK DI PROVA"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -30,14 +30,14 @@ public class NotificationServer implements INotificationServer {
|
|||
|
||||
public synchronized void unregisterForCallback(INotificationClient client) throws RemoteException {
|
||||
if(clients.remove(client)) {
|
||||
System.out.print("Client unregistered");
|
||||
System.out.println("Client unregistered");
|
||||
} else {
|
||||
System.out.print("Unable to unregister client");
|
||||
System.out.println("Unable to unregister client");
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void sendInvitations(String username, DefaultListModel<String> users) throws RemoteException {
|
||||
System.out.print("Starting callbacks");
|
||||
System.out.println("Starting callbacks");
|
||||
Iterator i = clients.iterator();
|
||||
while(i.hasNext()){
|
||||
INotificationClient client = (INotificationClient) i.next();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import com.texttwist.server.tasks.CheckOnlineUsers;
|
||||
import com.texttwist.server.tasks.SendInvitations;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.*;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
|
|
@ -10,6 +10,8 @@ import java.nio.ByteBuffer;
|
|||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static com.texttwist.server.components.GameServer.activeMatches;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
*/
|
||||
|
|
@ -17,6 +19,7 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
private Message request;
|
||||
private SocketChannel socketChannel;
|
||||
|
||||
ThreadProxy(Message request, SocketChannel socketChannel){
|
||||
this.request = request;
|
||||
this.socketChannel = socketChannel;
|
||||
|
|
@ -44,14 +47,41 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
Boolean invitationSended = sendInvitations.get();
|
||||
if (invitationSended) {
|
||||
|
||||
Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<String>());
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
socketChannel.close();
|
||||
return true;
|
||||
} else {
|
||||
//Crea nuova partita e attendi i giocatori
|
||||
request.data.addElement(request.sender);
|
||||
Match match = new Match(request.sender, request.data);
|
||||
activeMatches.addElement(match);
|
||||
|
||||
DefaultListModel<String> matchName = new DefaultListModel<>();
|
||||
matchName.addElement(request.sender);
|
||||
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, matchName, socketChannel));
|
||||
Boolean joinMatchRes = joinMatch.get();
|
||||
|
||||
if(!joinMatchRes){
|
||||
//NON FARE NULLA, ASPETTA GLI ALTRI
|
||||
Message message = new Message("INVITES_ALL_SENDED", "", "", new DefaultListModel<String>());
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
|
||||
|
||||
|
||||
Future<Boolean> matchTimeout = threadPool.submit(new MatchTimeout(match));
|
||||
Boolean matchTimeoutRes = matchTimeout.get();
|
||||
if(matchTimeoutRes){
|
||||
System.out.println("TUTTO OKEY, INIZIA GIOCO");
|
||||
|
||||
} else {
|
||||
System.out.println("MESSAGGIO ERRORE A TUTTI");
|
||||
//socketChannel.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
return matchTimeoutRes;
|
||||
}
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
|
|
@ -65,7 +95,7 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
socketChannel.write(buffer);
|
||||
socketChannel.close();
|
||||
//socketChannel.close();
|
||||
return false;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
|
|
@ -76,6 +106,24 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
case "JOIN_GAME":
|
||||
Future<Boolean> joinMatch = threadPool.submit(new JoinMatch(request.sender, request.data, socketChannel));
|
||||
try {
|
||||
Boolean joinMatchRes = joinMatch.get();
|
||||
if(joinMatchRes){
|
||||
System.out.print("START THE FUCKING GAME!!!!");
|
||||
//ULTIMO A JOINARE! INIZIA GIOCO
|
||||
} else {
|
||||
System.out.print("WAIT FRIENDS");
|
||||
//NON FARE NULLA, ASPETA GLI ALTRI
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} {
|
||||
|
||||
}
|
||||
default:
|
||||
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import utilities.Logger;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
|
||||
public class ThreadWorker implements Runnable{
|
||||
|
||||
protected Socket clientSocket = null;
|
||||
protected String task = null;
|
||||
|
||||
public ThreadWorker(Socket clientSocket, String task) {
|
||||
this.clientSocket = clientSocket;
|
||||
this.task = task;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Logger.write(Thread.currentThread().getName()+": Start task -> " + task);
|
||||
try {
|
||||
sleep(6000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
/* InputStream input = clientSocket.getInputStream();
|
||||
OutputStream output = clientSocket.getOutputStream();
|
||||
long time = System.currentTimeMillis();
|
||||
output.write(("HTTP/1.1 200 OK\n\nThreadWorker: " +
|
||||
this.serverText + " - " +
|
||||
time +
|
||||
"").getBytes());
|
||||
output.close();
|
||||
input.close();*/
|
||||
Logger.write(Thread.currentThread().getName()+": End of task -> " + task);
|
||||
}
|
||||
}
|
||||
29
Server/src/com/texttwist/server/models/Match.java
Normal file
29
Server/src/com/texttwist/server/models/Match.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package com.texttwist.server.models;
|
||||
|
||||
import javafx.util.Pair;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 23/06/2017.
|
||||
*/
|
||||
public class Match {
|
||||
public DefaultListModel<Pair<String,Integer>> playersStatus = new DefaultListModel<Pair<String, Integer>>();
|
||||
public DefaultListModel<Pair<String,SocketChannel>> playersSocket = new DefaultListModel<Pair<String, SocketChannel>>();
|
||||
|
||||
public String matchCreator;
|
||||
public DefaultListModel<Pair<String,Integer>> playersScore = new DefaultListModel<Pair<String, Integer>>();
|
||||
|
||||
public Match(String matchCreator, DefaultListModel<String> players){
|
||||
for (int i =0; i < players.size(); i++){
|
||||
this.playersStatus.addElement(new Pair<>(players.get(i), 0));
|
||||
this.playersSocket.addElement(new Pair<>(players.get(i), null));
|
||||
}
|
||||
|
||||
this.matchCreator = matchCreator;
|
||||
}
|
||||
|
||||
}
|
||||
76
Server/src/com/texttwist/server/tasks/JoinMatch.java
Normal file
76
Server/src/com/texttwist/server/tasks/JoinMatch.java
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.models.Match;
|
||||
import javafx.util.Pair;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import static com.texttwist.server.components.GameServer.activeMatches;
|
||||
|
||||
/**
|
||||
* Created by loke on 23/06/2017.
|
||||
*/
|
||||
public class JoinMatch implements Callable<Boolean> {
|
||||
public String matchName;
|
||||
public String playerName;
|
||||
public SocketChannel socketChannel;
|
||||
|
||||
public JoinMatch(String playerName, DefaultListModel<String> matchName, SocketChannel socketChannel) {
|
||||
this.playerName = playerName;
|
||||
this.matchName = matchName.get(0);
|
||||
this.socketChannel = socketChannel;
|
||||
}
|
||||
|
||||
private Match findMatch(String matchName){
|
||||
for(int i = 0; i<activeMatches.size(); i++) {
|
||||
if (activeMatches.get(i).matchCreator.equals(matchName)) {
|
||||
return activeMatches.get(i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
Match thisMatch= findMatch(this.matchName);
|
||||
if(thisMatch!=null){
|
||||
for(int j = 0; j<thisMatch.playersStatus.size(); j++){
|
||||
String name = thisMatch.playersStatus.get(j).getKey();
|
||||
if (name.equals(playerName)){
|
||||
printAll(thisMatch);
|
||||
|
||||
thisMatch.playersStatus.remove(j);
|
||||
thisMatch.playersStatus.addElement(new Pair<>(name,1));
|
||||
thisMatch.playersSocket.remove(j);
|
||||
thisMatch.playersSocket.addElement(new Pair<>(name,socketChannel));
|
||||
System.out.println(playerName + ": JOINED");
|
||||
printAll(thisMatch);
|
||||
return allJoined(thisMatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
return allJoined(thisMatch);
|
||||
}
|
||||
|
||||
|
||||
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());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private Boolean allJoined(Match match) {
|
||||
for (int i = 0; i < match.playersStatus.size(); i++) {
|
||||
if (match.playersStatus.get(i).getValue() == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
48
Server/src/com/texttwist/server/tasks/MatchTimeout.java
Normal file
48
Server/src/com/texttwist/server/tasks/MatchTimeout.java
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.server.models.Match;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Created by loke on 23/06/2017.
|
||||
*/
|
||||
public class MatchTimeout implements Callable<Boolean> {
|
||||
|
||||
public Match match;
|
||||
|
||||
public MatchTimeout(Match match) {
|
||||
this.match = match;
|
||||
System.out.println("Math started, countdown for join!");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(1024);
|
||||
|
||||
try {
|
||||
Thread.currentThread().sleep(700000);
|
||||
|
||||
System.out.println("TIMEOUT - MANDA MESSAGGIO ERRORE A TUTTI GLI UTENTI DEL MATCH");
|
||||
for (int i =0; i< match.playersSocket.size(); i++) {
|
||||
SocketChannel clientSocket = match.playersSocket.get(i).getValue();
|
||||
|
||||
Message message = new Message("TIMEOUT", "", "", new DefaultListModel<>());
|
||||
byte[] byteMessage = new String(message.toString()).getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
clientSocket.write(buffer);
|
||||
clientSocket.close();
|
||||
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
32
Server/src/com/texttwist/server/tasks/WaitForPlayers.java
Normal file
32
Server/src/com/texttwist/server/tasks/WaitForPlayers.java
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import com.texttwist.server.components.NotificationServer;
|
||||
import com.texttwist.server.models.Match;
|
||||
import constants.Config;
|
||||
import interfaces.INotificationServer;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 19/06/2017.
|
||||
*/
|
||||
public class WaitForPlayers implements Callable<Boolean> {
|
||||
private Match match;
|
||||
private String sender;
|
||||
|
||||
public WaitForPlayers(Match match) {
|
||||
this.match = match;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
System.out.print("Wait for players!");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue