Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Client -> InputThread
- ---------------------
- package tw.client;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.net.Socket;
- import java.util.Vector;
- import java.util.concurrent.BlockingQueue;
- import tw.Projectile;
- import tw.Tank;
- public class InputThread extends Thread {
- private ObjectInputStream in = null;
- private BlockingQueue queue = 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, BlockingQueue queue) {
- this.queue = queue;
- try {
- in = new ObjectInputStream(socket.getInputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- start();
- }
- /**
- * Run method, called when thread is started.
- */
- @SuppressWarnings("unchecked")
- public void run() {
- // Get playerNr and first tank vector
- try {
- playerNr = in.readInt();
- System.out.println("playerNr " + playerNr);
- updatedTanks = (Vector<Tank>) in.readUnshared();
- queue.put(updatedTanks);
- } catch (InterruptedException e2) {
- e2.printStackTrace();
- } catch (IOException e2) {
- e2.printStackTrace();
- } catch (ClassNotFoundException e2) {
- e2.printStackTrace();
- }
- // Get entities from server
- try {
- while (running) {
- updatedTanks = (Vector<Tank>) in.readUnshared();
- System.out.println("Tank 0 is active: " + updatedTanks.get(0).isActive());
- System.out.println("Tank 1 is active: " + updatedTanks.get(1).isActive());
- sleep(5000);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (InterruptedException 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;
- }
- }
- Server -> OutputThread
- ----------------------
- package tw.server;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.concurrent.BlockingQueue;
- public class OutputThread extends Thread {
- private ObjectOutputStream out = null;
- private boolean running = true;
- private int playerNr;
- private GameManager gm;
- /**
- * OutputThread constructor.
- * @param s
- * @param playerNr
- * @param gm
- * @throws IOException
- */
- public OutputThread(Socket s, int playerNr, GameManager gm) throws IOException {
- this.playerNr = playerNr;
- this.gm = gm;
- out = new ObjectOutputStream(s.getOutputStream());
- start();
- }
- public void run() {
- // Send player number to client
- try {
- out.writeInt(playerNr);
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Send entities client
- try {
- while(running) {
- out.writeUnshared(gm.getTanks());
- System.out.println("Tank 0 is active: " + gm.getTanks().get(0).isActive());
- System.out.println("Tank 1 is active: " + gm.getTanks().get(1).isActive());
- sleep(5000);
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- Client:
- Tank 0 is active: false
- Tank 1 is active: true
- Client output: 32
- Tank 0 is active: false
- Tank 1 is active: true
- Server:
- Tank 0 is active: false
- Tank 1 is active: true
- Tank 0 is active: false
- Tank 1 is active: true
- GameManager: Action triggered!
- Tank 0 is active: true
- Tank 1 is active: false
- Tank 0 is active: true
- Tank 1 is active: false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement