Invio del token obbligatorio e gestione dell'errore nel caso non venga inviato
This commit is contained in:
parent
4a29569052
commit
406a5647b8
8 changed files with 533 additions and 422 deletions
|
|
@ -32,7 +32,10 @@ public class GameServer implements Runnable{
|
|||
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<>());
|
||||
|
|
@ -58,7 +61,7 @@ public class GameServer implements Runnable{
|
|||
datagramChannel.connect(address);
|
||||
Logger.write("GamePage Service is running at "+this.serverPort+" port...");
|
||||
|
||||
wordsReceiver = new ReceiveWords(datagramChannel, bufferWords);
|
||||
wordsReceiver = new ReceiveWords(datagramChannel, bufferWords, bufferMessages, client);
|
||||
threadPool.submit(wordsReceiver);
|
||||
|
||||
} catch (IOException e) {
|
||||
|
|
@ -75,9 +78,9 @@ public class GameServer implements Runnable{
|
|||
|
||||
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
|
||||
while (iter.hasNext()) {
|
||||
ByteBuffer bufferMessages = ByteBuffer.allocate(1024);
|
||||
bufferMessages = ByteBuffer.allocate(1024);
|
||||
bufferMessages.clear();
|
||||
SocketChannel client = null;
|
||||
client = null;
|
||||
SelectionKey key = iter.next();
|
||||
iter.remove();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.server.models.Match;
|
||||
import com.texttwist.server.tasks.*;
|
||||
import constants.Config;
|
||||
|
|
@ -38,16 +39,11 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
|
||||
}
|
||||
|
||||
|
||||
private Boolean isValidToken(String token){
|
||||
return SessionsManager.getInstance().isValidToken(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() {
|
||||
bufferMessage = ByteBuffer.allocate(1024);
|
||||
byte[] byteMessage = null;
|
||||
if(isValidToken(request.token)){
|
||||
if(SessionsManager.getInstance().isValidToken(request.token)){
|
||||
switch(request.message){
|
||||
case "START_GAME":
|
||||
Future<Boolean> onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data));
|
||||
|
|
@ -177,7 +173,7 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
System.out.println("INVIO GAME_STARTED "+ s);
|
||||
socketClient.write(bufferMessage);
|
||||
} catch (IOException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -199,10 +195,8 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
socketChannel.write(bufferMessage);
|
||||
matchNotAvailable = true;
|
||||
}
|
||||
//Match non disponibile
|
||||
|
||||
}
|
||||
//NON FARE NULLA, ASPETA GLI ALTRI
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
|
|
@ -218,7 +212,7 @@ public class ThreadProxy implements Callable<Boolean> {
|
|||
}
|
||||
|
||||
} else {
|
||||
System.out.print("TOKEN NON VALIDO");
|
||||
threadPool.submit(new TokenInvalid(request.sender, socketChannel, bufferMessage));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,26 +1,17 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import com.texttwist.server.components.GameServer;
|
||||
import com.texttwist.server.components.SessionsManager;
|
||||
import com.texttwist.server.models.Match;
|
||||
import constants.Config;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.xml.crypto.Data;
|
||||
import java.net.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static com.texttwist.server.components.GameServer.activeMatches;
|
||||
|
||||
/**
|
||||
* Created by loke on 27/06/2017.
|
||||
|
|
@ -30,12 +21,16 @@ public class ReceiveWords implements Callable<Boolean>{
|
|||
protected ExecutorService threadPool = Executors.newCachedThreadPool();
|
||||
|
||||
public DatagramChannel channel;
|
||||
ByteBuffer buffer;
|
||||
ByteBuffer bufferWords;
|
||||
ByteBuffer bufferMessages;
|
||||
public SocketChannel socketChannel;
|
||||
|
||||
|
||||
public ReceiveWords(DatagramChannel channel, ByteBuffer buffer) {
|
||||
this.buffer = buffer;
|
||||
public ReceiveWords(DatagramChannel channel, ByteBuffer buffer, ByteBuffer bufferMessages, SocketChannel socketChannel) {
|
||||
this.bufferWords = buffer;
|
||||
this.channel = channel;
|
||||
this.bufferMessages = bufferMessages;
|
||||
this.socketChannel = socketChannel;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -58,9 +53,14 @@ public class ReceiveWords implements Callable<Boolean>{
|
|||
System.out.println(rcv);
|
||||
if (rcv.startsWith("MESSAGE")) {
|
||||
msg = Message.toMessage(rcv);
|
||||
System.out.println(msg.sender);
|
||||
Match match = Match.findMatchByPlayer(msg.sender);
|
||||
threadPool.submit(new ComputeScore(msg.sender, msg.data, match));
|
||||
if(SessionsManager.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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
38
Server/src/com/texttwist/server/tasks/TokenInvalid.java
Normal file
38
Server/src/com/texttwist/server/tasks/TokenInvalid.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 11/07/2017.
|
||||
*/
|
||||
public class TokenInvalid implements Callable<Boolean> {
|
||||
private String sender;
|
||||
private ByteBuffer buffer;
|
||||
private SocketChannel channel;
|
||||
|
||||
public TokenInvalid (String sender, SocketChannel channel, ByteBuffer buffer){
|
||||
this.sender=sender;
|
||||
this.buffer=buffer;
|
||||
this.channel=channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call()throws Exception {
|
||||
System.out.print("TOKEN NON VALIDO");
|
||||
buffer = ByteBuffer.allocate(1024);
|
||||
Message msg = new Message("MATCH_NOT_AVAILABLE", "", null, new DefaultListModel<>());
|
||||
buffer.clear();
|
||||
byte[] byteMessage = msg.toString().getBytes();
|
||||
buffer = ByteBuffer.wrap(byteMessage);
|
||||
channel.write(buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue