From 187c2d61250d270e3755a55a3a58ba9c89214339 Mon Sep 17 00:00:00 2001
From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr>
Date: Wed, 27 Apr 2016 14:42:12 +0200
Subject: [PATCH] Created update thread

---
 gui/src/Controller.java                | 14 +++++++-
 gui/src/{State.java => GameState.java} |  9 +++--
 gui/src/UpdateThread.java              | 49 ++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 3 deletions(-)
 rename gui/src/{State.java => GameState.java} (51%)
 create mode 100644 gui/src/UpdateThread.java

diff --git a/gui/src/Controller.java b/gui/src/Controller.java
index 4925ef3..bb3033d 100644
--- a/gui/src/Controller.java
+++ b/gui/src/Controller.java
@@ -1,4 +1,6 @@
 import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.net.URL;
 import java.util.ResourceBundle;
 
@@ -13,8 +15,9 @@ import javafx.scene.paint.Color;
 import javafx.scene.shape.Polygon;
 public class Controller implements Initializable {
 
-	private State state;
+	private GameState gameState;
 	private Process gameProcess;
+	private PrintWriter gameInput;
 	private Plateau plateau;
 
 	@FXML
@@ -32,14 +35,23 @@ public class Controller implements Initializable {
 			c.setOnMouseEntered(new MyHexEnteredHandler());
 		}
 		this.plateau = new Plateau(cases);
+		this.gameState = new GameState();
 		try {
 			this.gameProcess = new ProcessBuilder("../bin/theturk").start();
+			gameInput = new PrintWriter(new OutputStreamWriter(gameProcess.getOutputStream()), true);
 		} catch (IOException e) {
 			Alert alert = new Alert(Alert.AlertType.ERROR,"Can't run penguin program", ButtonType.OK);
 			alert.showAndWait();
 			e.printStackTrace();
 			System.exit(1);
 		}
+		UpdateThread upT = new UpdateThread(gameProcess, this.gameState);
+		upT.setDaemon(true);
+		upT.start();
+
+		//We tell the game that we will start
+		gameInput.print("h");
+		gameInput.println();
 	}
 
 	//public Polygon getPolygonFromCoordinates(double x, double y){	}
diff --git a/gui/src/State.java b/gui/src/GameState.java
similarity index 51%
rename from gui/src/State.java
rename to gui/src/GameState.java
index 4301973..db2776f 100644
--- a/gui/src/State.java
+++ b/gui/src/GameState.java
@@ -1,6 +1,11 @@
 import org.json.*;
-public class State {
-	public void update (String json){};//TODO
+public class GameState
+{
+	public void update (String json){
+		System.out.println(json);
+
+
+	};
 	public int getPenguinPos (int i, Player player)
 	{
 		//TODO
diff --git a/gui/src/UpdateThread.java b/gui/src/UpdateThread.java
new file mode 100644
index 0000000..9408ce5
--- /dev/null
+++ b/gui/src/UpdateThread.java
@@ -0,0 +1,49 @@
+import javafx.application.Platform;
+import javafx.scene.control.Alert;
+import javafx.scene.control.ButtonType;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+public class UpdateThread extends Thread
+{
+	BufferedReader reader;
+	GameState gameState;
+
+	public UpdateThread(Process program, GameState gameState)
+	{
+		this.reader = new BufferedReader(new InputStreamReader(program.getInputStream()));
+		this.gameState = gameState;
+	}
+
+
+	public void run()
+	{
+		boolean running = true;
+		while(running)
+			try
+			{
+				String line = reader.readLine();
+				//System.out.println(line);
+				if (line.contains("{"))
+				{
+					gameState.update(line.substring(line.indexOf("{"), line.lastIndexOf("}")+1));
+					Platform.runLater(() ->
+					{
+						// TODO: 4/27/16 update GUI from state
+					});
+				}
+			} catch (IOException e)
+			{
+				running = false;
+				Platform.runLater(() ->
+				{
+					Alert alert = new Alert(Alert.AlertType.ERROR, "Error during reading from penguin program!", ButtonType.FINISH);
+					e.printStackTrace();
+					alert.showAndWait();
+					System.exit(1);
+				});
+			}
+	}
+}
-- 
GitLab