Hi!
I have a multi-threaded server waiting for incoming connections. When a client connects, an input and an output thread are created to handle I/O to that client. The server’s output thread starts to send a Vector to the client.
The client, when constructed, joins the server and creates a thread to handle all input coming from the server. The thread starts when constructed and waits for the Vector to be sent. After joining, the client creates a GameBoard, giving it a reference to the input thread. The GameBoard, when constructed, tries to retrieve the Vector from the input thread.
The issue is, the GameBoard is constructed before the input thread can receive the Vector. I need the GameBoard constructor to wait for the input thread to receive it’s first update of the vector, or have the client wait for the thread to receive it before constructing the GameBoard. I hope this makes sense!
If there’s an easier solution to this mess, then please tell me. Also, if you notice anything else that’s wrong or could be better, don’t hesitate to mention it. Thanks!
-CLIENT-
package tw.client;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Client implements ActionListener {
// Frame
private JFrame frame = null;
public static final int W_WIDTH = 800;
public static final int W_HEIGHT = 600;
private JButton joinButton = null;
// Network
private static final int PORT = 2720;
private Socket socket = null;
private InputThread input = null;
private DataOutputStream output = null;
// Game loop and game board
private static final int FRAMES_PER_SECOND = 25;
private static final int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
private GameBoard gameBoard;
private boolean running = true;
/**
* Client constructor.
* Initializes a frame, game board and network connection.
*/
public Client() {
// Initialize the frame
frame = new JFrame("Tank Wars - Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(W_WIDTH, W_HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
// Initialize network connection
joinServer();
// Add game board to frame
gameBoard = new GameBoard(input, output);
//gameBoard.setVisible(false);
frame.add(gameBoard);
gameBoard.requestFocusInWindow();
// Start game loop
run();
// Initialize join button
/*joinButton = new JButton("Join");
joinButton.setActionCommand("Join");
joinButton.addActionListener(this);
frame.getContentPane().add(joinButton);*/
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Join") {
//client.joinServer();
joinButton.setEnabled(false);
joinButton.setVisible(false);
gameBoard.setVisible(true);
frame.validate();
// Start game loop
run();
}
}
/**
* Starts the game loop.
*/
private void run() {
// While client is not connected to server
/*while (!client.isConnected()) {
System.out.println("Client not connected to server.");
}*/
long nextGameTick = System.nanoTime() / 1000000;
long sleepTime;
while (running) {
gameBoard.updateGame();
gameBoard.repaint();
nextGameTick += SKIP_TICKS;
sleepTime = nextGameTick - System.nanoTime() / 1000000;
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
}
catch(Exception e){
e.getStackTrace();
}
} // if
} // while
} // run
/**
* When called, establishes connection to server.
*/
public void joinServer() {
try {
// Open socket to server
System.out.println("Initializing ...");
InetAddress addr = InetAddress.getLocalHost();
socket = new Socket(addr, PORT);
System.out.println("The new socket: " + socket);
// Initialize I/O
input = new InputThread(socket);
output = new DataOutputStream(socket.getOutputStream());
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Stops the game loop.
*/
public void stop() {
running = false;
}
/**
* MAIN METHOD
* @param args
*/
public static void main(String[] args) {
new Client();
}
}
-InputThread-
package tw.client;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.Vector;
import tw.Projectile;
import tw.Tank;
public class InputThread extends Thread {
private ObjectInputStream in = null;
private boolean running = true;
private int playerNr = 0;
private Vector<Tank> updatedTanks = null;
private Vector<Projectile> updatedProjectiles = null;
/**
* ClientThread constructor.
*/
public InputThread(Socket socket) {
// Initialize input stream
try {
in = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
// Initialize empty tank vector
updatedTanks = new Vector<Tank>();
start();
}
/**
* Run method, called when thread is started.
*/
@SuppressWarnings("unchecked")
public void run() {
// Receive player number from server
try {
playerNr = in.readInt();
System.out.println("playerNr " + playerNr);
} catch (IOException e1) {
e1.printStackTrace();
}
// Receive current turn and entity vector
try {
while (running) {
updatedTanks = (Vector<Tank>) in.readObject();
}
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
} // run
/**
* Sets 'running' to 'false', stopping the input loop.
*/
public void stopRunning() {
running = false;
}
/**
* Getter of the property <tt>playerNr</tt>
* @return Returns playerNr
* @uml.property name="playerNr"
*/
public int getPlayerNr() {
return playerNr;
}
/**
* Getter of the property (vector) <tt>updatedEntitites</tt>
* @return Returns the updatedEntities vector.
* @uml.property name="updatedEntities"
*/
public Vector<Tank> getUpdatedTanks() {
return updatedTanks;
}
}
-GameBoard-
package tw.client;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JComponent;
import tw.Projectile;
import tw.Tank;
/**
* .
*/
public class GameBoard extends JComponent implements KeyListener {
// Network
private InputThread input = null;
private DataOutputStream output = null;
// Status panel
private StatusPanel statusPanel = null;
// Players and entities
private int playerNr = 0;
private Vector<Tank> tanks = null;
private Vector<Projectile> projectiles = null;
private Tank pTank = null;
/**
* GameBoard constructor.
* @param client
*/
public GameBoard(InputThread input, DataOutputStream output) {
// Give access to input thread and output stream
this.input = input;
this.output = output;
// Setup panel
setFocusable(true);
setBackground(Color.LIGHT_GRAY);
setDoubleBuffered(true);
setSize(Client.W_WIDTH, Client.W_HEIGHT);
// Setup key listener
addKeyListener(this);
//setFocusTraversalKeysEnabled(false);
// Get playerNr and tanks from server
playerNr = input.getPlayerNr();
tanks = input.getUpdatedTanks();
// Set pTank to tank matching playerNr
for (int i = 0; i < tanks.size(); i++)
if (tanks.get(i).getPlayerNr() == playerNr)
pTank = tanks.get(i);
// Initialize status panel
statusPanel = new StatusPanel(pTank, playerNr);
add(statusPanel);
this.revalidate();
}
/**
* Update logic
*/
public void updateGame() entitieset updated entities
tanks = input.getUpdatedTanks();
// Get playerNr of currently active tank
int activeTank = 0;
for (int i = 0; i < tanks.size(); i++)
if (tanks.get(i).isActive())
activeTank = tanks.get(i).getPlayerNr();
// Update status panel
statusPanel.update(activeTank);
}
/**
* Render entities
*/
public void paintComponent(Graphics g) {
// Clear off-screen bitmap
super.paintComponent(g);
// Cast to Graphics2D for more options
Graphics2D g2d = (Graphics2D) g;
// If there are entities in the entity vector and they are alive, draw those entities
if (tanks != null) {
for (int i = 0; i < tanks.size(); i++) {
if (tanks.get(i).isAlive())
tanks.get(i).draw(g2d);
}
}
}
////////////////////////////
// - HANDLING KEY INPUT - //
////////////////////////////
/**
* Key input - when key is pressed.
*/
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (pTank.isActive())
if (key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN || key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT) {
try {
output.writeInt(key);
System.out.println("Client output: " + key);
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
/**
* Key input - when key is released.
*/
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (pTank.isActive())
if (key == KeyEvent.VK_SPACE)
try {
output.writeInt(key);
System.out.println("Client output: " + key);
}
catch (IOException e1) {
e1.printStackTrace();
}
}
public void keyTyped(KeyEvent e) {
// do nothing
}
}