YEAAAAAAHHHHH

This commit is contained in:
Lorenzo Iovino 2017-07-07 01:12:25 +02:00
parent 09ced44428
commit ada17053d8
8 changed files with 1013 additions and 175 deletions

View file

@ -15,10 +15,7 @@ import utilities.Logger;
import javax.swing.*;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.*;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.*;
@ -42,7 +39,9 @@ public class GameServer implements Runnable{
protected ServerSocketChannel serverSocketChannel = null;
protected ThreadProxy proxy;
ByteBuffer buffer = ByteBuffer.allocate(1024);
DatagramSocket datagramSocket;
ByteBuffer buffer2 = ByteBuffer.allocate(1024);
DatagramChannel datagramChannel;
protected Selector selector = null;
protected ExecutorService threadPool = Executors.newCachedThreadPool();
private String dictionaryPath = "./Server/resources/dictionary";
@ -65,7 +64,12 @@ public class GameServer implements Runnable{
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(serverPort));
serverSocketChannel.register(selector, OP_ACCEPT);
datagramSocket = new DatagramSocket(Config.WordsReceiverServerPort);
// datagramSocket = new DatagramSocket(Config.WordsReceiverServerPort);
InetSocketAddress address = new InetSocketAddress(Config.WordsReceiverServerPort);
datagramChannel = DatagramChannel.open();
DatagramSocket datagramSocket = datagramChannel.socket();
datagramSocket.bind(address);
Logger.write("GamePage Service is running at "+this.serverPort+" port...");
} catch (IOException e) {
e.printStackTrace();
@ -103,7 +107,7 @@ public class GameServer implements Runnable{
if (line.startsWith("MESSAGE")) {
SessionsManager.getInstance().printAll();
Message msg = Message.toMessage(line);
proxy = new ThreadProxy(msg, client, datagramSocket);
proxy = new ThreadProxy(msg, client, datagramChannel, buffer2);
Future<Boolean> identifyMessage = threadPool.submit(proxy);
System.out.println(line);
}

View file

@ -12,6 +12,7 @@ import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.*;
@ -24,13 +25,15 @@ public class ThreadProxy implements Callable<Boolean> {
protected final ExecutorService threadPool = Executors.newCachedThreadPool();
private final Message request;
private final SocketChannel socketChannel;
private final DatagramSocket datagramSocket;
private final DatagramChannel datagramChannel;
private ByteBuffer buffer;
ThreadProxy(Message request, SocketChannel socketChannel, DatagramSocket datagramSocket){
ThreadProxy(Message request, SocketChannel socketChannel, DatagramChannel datagramChannel, ByteBuffer buffer){
this.request = request;
this.socketChannel = socketChannel;
this.datagramSocket = datagramSocket;
this.datagramChannel = datagramChannel;
this.buffer = buffer;
}
@ -168,7 +171,7 @@ public class ThreadProxy implements Callable<Boolean> {
}
//Start receive words: tempo masimo 5 minuti per completare l'invio delle lettere.
Future<Boolean> receiveWords = threadPool.submit(new ReceiveWords(match, datagramSocket));
Future<Boolean> receiveWords = threadPool.submit(new ReceiveWords(match, datagramChannel, buffer));
Boolean receiveWordsRes = receiveWords.get();
if(receiveWordsRes){
@ -190,6 +193,8 @@ public class ThreadProxy implements Callable<Boolean> {
System.out.println(msg.toString());
multicastSocket.send(hi);
System.out.println(Match.findMatchIndex(activeMatches, match.matchCreator));
activeMatches.remove(Match.findMatchIndex(activeMatches, match.matchCreator));
//multicastSocket.disconnect();
//multicastSocket.close();

View file

@ -11,6 +11,9 @@ import javax.xml.crypto.Data;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -23,44 +26,61 @@ public class ReceiveWords implements Callable<Boolean>{
protected ExecutorService threadPool = Executors.newCachedThreadPool();
public final DatagramSocket datagramSocket;
public DatagramChannel DatagramChannel;
public final Match match;
byte[] receiveData = new byte[1024];
ByteBuffer buffer;
public ReceiveWords(Match match, DatagramSocket datagramSocket) {
public ReceiveWords(Match match, DatagramChannel DatagramChannel, ByteBuffer buffer) {
this.match = match;
this.datagramSocket = datagramSocket;
this.buffer = buffer;
this.DatagramChannel = DatagramChannel;
}
@Override
public Boolean call() throws Exception {
System.out.print("READY TO Receive words !!!!");
byte[] receiveData = new byte[1024];
Future<Boolean> matchTimeout = threadPool.submit(new MatchTimeout());
receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
datagramSocket.receive(receivePacket);
String rcv = new String( receivePacket.getData());
System.out.println("RECEIVED: " + rcv);
Message msg = Message.toMessage(rcv);
Message msg;
while(true) {
DatagramChannel.receive(buffer);
buffer.flip();
System.out.println(buffer.limit());
int limits = buffer.limit();
byte bytes[] = new byte[limits];
buffer.get(bytes, 0, limits);
String rcv = new String(bytes);
System.out.println("RECEIVED: " + rcv);
buffer.rewind();
msg = Message.toMessage(rcv);
if(msg.message.equals("WORDS")){
break;
}
}
Future<Integer> computeScore = threadPool.submit(new ComputeScore(msg.sender, match, msg.data));
//Se tutti hanno inviato le parole, blocca il timer e restituisci true
computeScore.get();
System.out.println(match.matchCreator);
System.out.println(match.allPlayersSendedHisScore());
if(match.allPlayersSendedHisScore()){
System.out.println("TIMEOUT BLOCCATO, OK");
// match.setUndefinedScorePlayersToZero();
matchTimeout.cancel(true);
// datagramSocket.close();
DatagramChannel.close();
return true;
}
return false;
}
}