Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
retrieve port from server
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Procio committed May 2, 2019
1 parent ce732e1 commit 8b1824d
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 70 deletions.
11 changes: 6 additions & 5 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.concurrent.LinkedBlockingQueue;

public class Main {
public static void main(String[] args) throws Exception {
if(args == null || args.length == 0){
Expand All @@ -12,11 +14,10 @@ public static void main(String[] args) throws Exception {
} else if (args[0].equals("server")) {
/* run server */
System.out.println("Running server");
try {
server.Runner.main(args);
} catch (Exception e) {
e.printStackTrace();
}
server.Runner srv = new server.Runner();
Integer port = srv.getPort();
System.out.println("server port: " + port.toString());
srv.run();
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/client/GUI/BoardGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ public void start(Stage stage) throws Exception {
this.scene = new Scene(this.pane);
stage.setScene(this.scene);
}

public void updateGUI(Integer[][] state){
/*TODO poll each square and for each piece that doesn't
*TODO correspond update button
*/
}
}
53 changes: 31 additions & 22 deletions src/main/java/client/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,43 @@
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import logic.Board;

import java.util.concurrent.LinkedBlockingQueue;


public class Runner extends Application{
private Integer port;
private MenuScreen ms;
private BoardGUI boardGUI;
private Board board;
private Thread logicThread,
cliThread,
servThread;

private void runGame(Stage stage, Boolean remote, Boolean computer){
/* Garbage collector should clean ms */
this.ms = null;
this.boardGUI = new BoardGUI(null);
try {
this.boardGUI.start(stage);
}catch (Exception e){
e.printStackTrace();
}
if(!remote) {
/* queue for server port */
LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
/* run server */
server.Runner server = new server.Runner();
this.port = server.getPort();
this.servThread = new Thread(server);
this.servThread.start();
}
this.cliThread = new Thread(new Utils(this.port));
this.cliThread.start();
this.logicThread = new Thread(new Board());
this.logicThread.start();
}

public static void main(String[] args) throws Exception {
launch();
Expand Down Expand Up @@ -62,26 +93,4 @@ private void setPort(final Stage stage){
this.ms.getTf().setOnAction(this.ms.getEh());
}
}

private void runGame(Stage stage, Boolean remote, Boolean computer){
/* Garbage collector should clean ms */
this.ms = null;
this.boardGUI = new BoardGUI(null);
try {
this.boardGUI.start(stage);
}catch (Exception e){
e.printStackTrace();
}
if(remote) {
/* TODO connect to server */
System.out.println(this.port.toString());
new Thread(new Utils(this.port)).start();
}else{
/* run server */
new Thread(new server.Runner()).start();
/* TODO get server port and connect to it */
this.port = 8888;
new Thread(new Utils(this.port)).start();
}
}
}
23 changes: 16 additions & 7 deletions src/main/java/client/communication/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@

public class Utils implements Runnable{
private int port;
private Socket clientSocket;
private BufferedReader inFromServer;
private DataOutputStream outToServer;

public void sendState(Integer[][] state){
System.out.println(state.toString());
//outToServer.writeBytes(state.toString());
}

public void connect() throws Exception {
/**TODO
* if game selected is on 1 machine, create thread with server running in background
*/
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket("localhost", this.port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
this.clientSocket = new Socket("localhost", this.port);
this.outToServer = new DataOutputStream(
this.clientSocket.getOutputStream()
);
this.inFromServer = new BufferedReader(
new InputStreamReader(this.clientSocket.getInputStream())
);

String sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
String modifiedSentence = inFromServer.readLine();
String modifiedSentence = this.inFromServer.readLine();
System.out.println(modifiedSentence);
clientSocket.close();
}
Expand Down
37 changes: 18 additions & 19 deletions src/main/java/server/Board.java → src/main/java/logic/Board.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
package server;
package logic;

import logic.Player;

class Board {
/* solution
Player implements PlayerIF
color
pieces
time
PlayerIF
move(piece, pos)
move(pos, pos)
Piece (abstract class) implements PieceIF
posX, posY
toString() and move(x,y)
abstract class that is extended by (rook, bishop, queen, knight, pawn, king)
*/
public class Board implements Runnable{
private Player whitePlayer;
private Player blackPlayer;
private Integer[][] state;

Board(){
public Board(){
/* default setup */
this.state = new Integer[][]{
{-4,-2,-3,-5,-6,-3,-2,-4},
Expand All @@ -36,4 +20,19 @@ abstract class that is extended by (rook, bishop, queen, knight, pawn, king)
this.whitePlayer = new Player(true, this.state);
this.blackPlayer = new Player(false, this.state);
}
public Board(Integer[][] state){
this.state = state;
}

public Integer[][] getState() {
return state;
}

public void setState(Integer[][] state) {
this.state = state;
}

public void run() {
System.out.println("Running Board logic");
}
}
6 changes: 0 additions & 6 deletions src/main/java/logic/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,21 @@ public Player(Boolean color, Integer[][] state){
if(!color) state[i][j] *= -1;
switch (state[i][j]){
case 1:
/* pawns */
this.pieces.add(new Pawn(color, i, j));
break;
case 2:
/* knights */
this.pieces.add(new Knight(color, i, j));
break;
case 3:
/* bishops */
this.pieces.add(new Bishop(color, i, j));
break;
case 4:
/* rooks */
this.pieces.add(new Rook(color, i, j));
break;
case 5:
/* queen */
this.pieces.add(new Queen(color, i, j));
break;
case 6:
/* king */
this.pieces.add(new King(color, i, j));
break;
}
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/server/Runner.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
package server;

import logic.Board;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Runner implements Runnable{
public static void main(String[] args) throws Exception {
ServerSocket welcomeSocket = new ServerSocket(8888);
System.out.println(welcomeSocket.getLocalPort());
private ServerSocket serverSocket;
private Socket conSocket;
private Board board;
private Thread logicThread;

Socket connectionSocket = welcomeSocket.accept();
public Runner(){
try {
this.serverSocket = new ServerSocket(8888);
}catch(Exception e){
e.printStackTrace();
}
}
public int getPort(){
return this.serverSocket.getLocalPort();
}

private void setup() throws Exception{
this.conSocket = this.serverSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
new InputStreamReader(this.conSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
this.conSocket.getOutputStream());
String clientSentence = inFromClient.readLine();
String capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}

public void run() {
try{
main(null);
}catch (Exception e){
try {
setup();
}catch(Exception e){
e.printStackTrace();
}
}
Expand Down
Binary file modified target/classes/Main.class
Binary file not shown.
Binary file modified target/classes/client/GUI/BoardGUI.class
Binary file not shown.
Binary file modified target/classes/client/Runner$1.class
Binary file not shown.
Binary file modified target/classes/client/Runner$2.class
Binary file not shown.
Binary file modified target/classes/client/Runner$3.class
Binary file not shown.
Binary file modified target/classes/client/Runner.class
Binary file not shown.
Binary file modified target/classes/client/communication/Utils.class
Binary file not shown.
Binary file added target/classes/logic/Board.class
Binary file not shown.
Binary file modified target/classes/logic/Player.class
Binary file not shown.
Binary file removed target/classes/server/Board.class
Binary file not shown.
Binary file modified target/classes/server/Runner.class
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ client/Runner$3.class
client/Runner$2.class
Main.class
client/GUI/MenuScreen$3.class
logic/Board.class
logic/PieceIF.class
logic/PosXY.class
logic/pieces/Queen.class
Expand All @@ -19,7 +20,6 @@ logic/pieces/Bishop.class
client/communication/Utils.class
logic/Piece.class
client/GUI/BoardGUI.class
server/Board.class
client/GUI/MenuScreen$4.class
logic/pieces/Rook.class
client/GUI/MenuScreen.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
/home/adam_procio/School/PJV/prociada/src/main/java/logic/pieces/Knight.java
/home/adam_procio/School/PJV/prociada/src/main/java/logic/pieces/Rook.java
/home/adam_procio/School/PJV/prociada/src/main/java/Main.java
/home/adam_procio/School/PJV/prociada/src/main/java/server/Board.java
/home/adam_procio/School/PJV/prociada/src/main/java/client/GUI/MenuScreen.java
/home/adam_procio/School/PJV/prociada/src/main/java/logic/Board.java
/home/adam_procio/School/PJV/prociada/src/main/java/client/Runner.java

0 comments on commit 8b1824d

Please sign in to comment.