multicast socket for score

This commit is contained in:
Lorenzo Iovino 2017-06-27 15:45:28 +02:00
parent 5f8fe0ae2d
commit f7c9d0ff99
21 changed files with 1621 additions and 589 deletions

View file

@ -32,12 +32,6 @@ public class Game extends Page {
return new Point(0,0);
}
public Thread startGame(){
Thread t = new Thread(new StartGame(this));
t.start();
return t;
}
public void showLetters(){
for(int i = 0; i< this.gameController.letters.size(); i++){
@ -57,7 +51,7 @@ public class Game extends Page {
gameController = new GameController();
letterSpawningPoint = setLetterSpawningPoint();
this.gameController.waitForPlayers();
startGame();
this.gameController.startGame(this);
window.setVisible(true);
}
@ -87,12 +81,6 @@ public class Game extends Page {
return l;
}
private Callable<Object> sendWords(String word){
System.out.println("SENDDDD" + word);
return null;
}
@Override
public void createUIComponents() throws IOException {
addLogo(root);
@ -118,7 +106,7 @@ public class Game extends Page {
new Callable<Object>() {
@Override
public Object call() throws Exception {
return new com.texttwist.client.pages.Menu(Page.window);
return null;
}
});
@ -126,7 +114,13 @@ public class Game extends Page {
new Font(Palette.inputBox_font.getFontName(), Font.BOLD, 40),
null,
"00:00",
120);
new Callable<Object>() {
@Override
public Object call() throws Exception {
gameController.sendWords(words);
return null;
}},
15);
}
}

View file

@ -1,12 +1,21 @@
package com.texttwist.client.pages;
import com.texttwist.client.App;
import com.texttwist.client.tasks.StartGame;
import com.texttwist.client.tasks.WaitForPlayers;
import com.texttwist.client.tasks.WaitForScore;
import com.texttwist.client.ui.TTDialog;
import com.texttwist.client.ui.TTLetter;
import constants.Config;
import models.Message;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadLocalRandom;
/**
@ -25,9 +34,52 @@ public class GameController {
return App.matchService.waitForPlayers();
}
public void waitForScore(){
SwingWorker worker = new WaitForScore();
worker.execute();
}
public boolean addWordToWordsList(String word) {
words.addElement(word);
return true;
}
public Callable<Object> sendWords(DefaultListModel<String> words){
System.out.println("SENDDDD" + words);
DatagramSocket clientSocket = null;
try {
clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(Config.WordsReceiverServerURI);
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
Message msg = new Message("WORDS",App.sessionService.account.userName,"",words);
String sentence = msg.toString();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Config.WordsReceiverServerPort);
clientSocket.send(sendPacket);
clientSocket.close();
waitForScore();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Thread startGame(Game game){
Thread t = new Thread(new StartGame(game));
t.start();
return t;
}
}

View file

@ -19,9 +19,9 @@ import java.util.concurrent.*;
*/
public class MatchService {
public Integer multicastId = 0 ;
public DefaultListModel<String> pendingList = new DefaultListModel<String>();
ByteBuffer buffer = ByteBuffer.allocate(1024);
public DefaultListModel<String> words = new DefaultListModel<String>();
SocketChannel clientSocket = null;
@ -45,6 +45,7 @@ public class MatchService {
} catch (IOException e) {
e.printStackTrace();
}
//Visualizza popup
new TTDialog("success", "New invitation from: " + userName + "!",
new Callable() {
@ -102,6 +103,10 @@ public class MatchService {
return null;
}
public void setMulticastId(Integer multicastId){
this.multicastId = multicastId;
}
public void addToPendingList(String username) throws IOException {
pendingList.addElement(username);
}

View file

@ -93,7 +93,7 @@ public class Page {
parent);
}
public Timer addTimer(TTContainer parent, Font font, Color fontColor, String caption, Integer value) {
public Timer addTimer(TTContainer parent, Font font, Color fontColor, String caption, Callable<Object> timerEndHandler, Integer value) {
TTLabel lblTimer = new TTLabel(
new Point(0, 0),
new Dimension(150, 50),
@ -108,6 +108,11 @@ public class Page {
public void actionPerformed(ActionEvent e) {
if (count <= 0) {
lblTimer.setText("00:00");
try {
timerEndHandler.call();
} catch (Exception e1) {
e1.printStackTrace();
}
((Timer)e.getSource()).stop();
} else {
int minutes = count / 60;

View file

@ -3,9 +3,12 @@ package com.texttwist.client.tasks;
import com.texttwist.client.App;
import com.texttwist.client.pages.Game;
import com.texttwist.client.pages.GameController;
import com.texttwist.client.pages.Page;
import com.texttwist.client.ui.TTDialog;
import oracle.jrockit.jfr.JFR;
import javax.swing.*;
import java.util.concurrent.Callable;
/**
* Created by loke on 25/06/2017.
@ -25,11 +28,16 @@ public class StartGame implements Runnable {
while(!(this.game.gameController.letters.size() > 0)) {
this.game.gameController.letters = App.matchService.words;
}
game.showLetters();
if(this.game.gameController.letters.size()>0){
this.game.timer.start();
}
//Mostra pannello di conferma che le lettere sono tutte arrivate
new TTDialog("success", "Game is ready. Press OK to start!",
new Callable() {
@Override
public Object call() throws Exception {
game.showLetters();
game.timer.start();
return null;
}
}, null);
}
}

View file

@ -43,7 +43,7 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
buffer.clear();
Message msg = Message.toMessage(line);
if (msg.message.equals("TIMEOUT")) {
if (msg.message.equals("JOIN_TIMEOUT")) {
socketChannel.close();
loading.dispose();
new TTDialog("alert", "TIMEOUT!",
@ -59,7 +59,15 @@ public class WaitForPlayers extends SwingWorker<DefaultListModel<String>,Default
if (msg.message.equals("GAME_STARTED")) {
loading.dispose();
DefaultListModel<String> data = msg.data;
System.out.println("HERE");
System.out.println(msg.data);
Integer multicastId = Integer.valueOf(data.remove(data.size()-2));
System.out.println(multicastId);
App.matchService.setMulticastId(multicastId);
words = msg.data;
//socketChannel.close();
App.matchService.setWords(words);
return words;

View file

@ -0,0 +1,46 @@
package com.texttwist.client.tasks;
import com.texttwist.client.App;
import constants.Config;
import javax.swing.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
/**
* Created by loke on 27/06/2017.
*/
public class WaitForScore extends SwingWorker<Void,Void> {
@Override
public Void doInBackground() {
InetAddress group = null;
try {
MulticastSocket ms = new MulticastSocket(App.matchService.multicastId);
InetAddress ia = InetAddress.getByName(Config.ScoreMulticastServerURI);
ms.joinGroup(ia);
System.out.println("Join multicast group");
byte[] buf = new byte[1024];
DatagramPacket recv = new DatagramPacket(buf, buf.length);
ms.receive(recv);
String s = new String(recv.getData());
System.out.println(s);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void done(){
System.out.println("Done");
}
}

View file

@ -12,7 +12,6 @@ import java.util.concurrent.Callable;
*/
public class TTGameBox extends TTInputField{
private DefaultListModel words = new DefaultListModel();
public TTGameBox(Point position,
Dimension dimension,
String placeholer,
@ -33,8 +32,8 @@ public class TTGameBox extends TTInputField{
if(e.getKeyCode() == 10){
try {
System.out.println(getText());
setText("");
listModel.addElement(getText());
setText("");
} catch (Exception e1) {
e1.printStackTrace();
}