Advertisement
Guest User

Untitled

a guest
Oct 30th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. Client -> InputThread
  2. ---------------------
  3.  
  4. package tw.client;
  5.  
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.net.Socket;
  9. import java.util.Vector;
  10. import java.util.concurrent.BlockingQueue;
  11.  
  12. import tw.Projectile;
  13. import tw.Tank;
  14.  
  15.  
  16. public class InputThread extends Thread {
  17.    
  18.     private     ObjectInputStream   in          = null;
  19.     private     BlockingQueue       queue           = null;
  20.     private     boolean         running         = true;
  21.    
  22.     private     int         playerNr        = 0;
  23.     private     Vector<Tank>        updatedTanks        = null;
  24.     private     Vector<Projectile>  updatedProjectiles  = null;        
  25.    
  26.    
  27.     /**
  28.      * ClientThread constructor.
  29.      */
  30.     public InputThread(Socket socket, BlockingQueue queue) {
  31.         this.queue = queue;
  32.        
  33.         // Initialize input stream
  34.         try {
  35.             in = new ObjectInputStream(socket.getInputStream());
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.        
  40.         start();
  41.     }  
  42.    
  43.    
  44.     /**
  45.      * Run method, called when thread is started.
  46.      */
  47.     @SuppressWarnings("unchecked")
  48.     public void run() {    
  49.         // Get playerNr and first tank vector
  50.         try {
  51.             playerNr = in.readInt();
  52.             System.out.println("playerNr " + playerNr);
  53.            
  54.             updatedTanks = (Vector<Tank>) in.readObject();
  55.             queue.put(updatedTanks);
  56.         } catch (InterruptedException e2) {
  57.             e2.printStackTrace();
  58.         } catch (IOException e2) {
  59.             e2.printStackTrace();
  60.         } catch (ClassNotFoundException e2) {
  61.             e2.printStackTrace();
  62.         }      
  63.        
  64.         // Get entities from server
  65.         try {
  66.             while (running) {
  67.                 updatedTanks = (Vector<Tank>) in.readObject();
  68.                 System.out.println("Tank 0 is active: " + updatedTanks.get(0).isActive());
  69.                 System.out.println("Tank 1 is active: " + updatedTanks.get(1).isActive());
  70.                 sleep(5000);
  71.             }
  72.         } catch (IOException e) {
  73.             e.printStackTrace();   
  74.         } catch (ClassNotFoundException e) {
  75.             e.printStackTrace();
  76.         } catch (InterruptedException e) {
  77.             e.printStackTrace();
  78.         }
  79.     } // run   
  80.    
  81.    
  82.     /**
  83.      * Sets 'running' to 'false', stopping the input loop.
  84.      */
  85.     public void stopRunning() {
  86.         running = false;
  87.     }
  88.    
  89.    
  90.     /**
  91.      * Getter of the property <tt>playerNr</tt>
  92.      * @return Returns playerNr
  93.      * @uml.property name="playerNr"
  94.      */
  95.     public int getPlayerNr() {
  96.         return playerNr;
  97.     }
  98.    
  99.    
  100.     /**
  101.      * Getter of the property (vector) <tt>updatedEntitites</tt>
  102.      * @return  Returns the updatedEntities vector.
  103.      * @uml.property  name="updatedEntities"
  104.      */
  105.     public Vector<Tank> getUpdatedTanks() {
  106.         return updatedTanks;
  107.     }
  108. }
  109.  
  110.  
  111. Server -> OutputThread
  112. ----------------------
  113.  
  114. package tw.server;
  115.  
  116. import java.io.IOException;
  117. import java.io.ObjectOutputStream;
  118. import java.net.Socket;
  119. import java.util.concurrent.BlockingQueue;
  120.  
  121.  
  122. public class OutputThread extends Thread  {
  123.    
  124.     private ObjectOutputStream  out         = null;
  125.     private boolean             running     = true;
  126.    
  127.     private int                 playerNr;
  128.     private GameManager         gm;
  129.    
  130.    
  131.     /**
  132.      * OutputThread constructor.
  133.      * @param s
  134.      * @param playerNr
  135.      * @param gm
  136.      * @throws IOException
  137.      */
  138.     public OutputThread(Socket s, int playerNr, GameManager gm) throws IOException {
  139.         this.playerNr = playerNr;
  140.         this.gm = gm;
  141.        
  142.         // Open new output stream to client
  143.         out = new ObjectOutputStream(s.getOutputStream());
  144.        
  145.         start();
  146.     }
  147.    
  148.    
  149.     public void run() {
  150.        
  151.         // Send player number to client
  152.         try {
  153.             out.writeInt(playerNr);
  154.         } catch (IOException e) {
  155.             e.printStackTrace();
  156.         }
  157.        
  158.         // Send entities client
  159.         try {
  160.             while(running) {
  161.                 out.writeObject(gm.getTanks());
  162.                 System.out.println("Tank 0 is active: " + gm.getTanks().get(0).isActive());
  163.                 System.out.println("Tank 1 is active: " + gm.getTanks().get(1).isActive());
  164.                 sleep(5000);
  165.             }
  166.         } catch (IOException e1) {
  167.             e1.printStackTrace();
  168.         } catch (InterruptedException e) {
  169.             e.printStackTrace();
  170.         }
  171.     }
  172. }
  173.  
  174. Output Client:
  175. Tank 0 is active: false
  176. Tank 1 is active: true
  177. Tank 0 is active: false
  178. Tank 1 is active: true
  179. Client output: 32
  180. Tank 0 is active: false
  181. Tank 1 is active: true
  182. Tank 0 is active: false
  183. Tank 1 is active: true
  184.  
  185. Output Server:
  186. Tank 0 is active: false
  187. Tank 1 is active: true
  188. Tank 0 is active: false
  189. Tank 1 is active: true
  190. GameManager: Action triggered!
  191. Tank 0 is active: true
  192. Tank 1 is active: false
  193. Tank 0 is active: true
  194. Tank 1 is active: false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement