SHOW:
|
|
- or go back to the newest paste.
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; |
18 | + | private ObjectInputStream in = null; |
19 | - | private BlockingQueue queue = null; |
19 | + | private BlockingQueue queue = null; |
20 | - | private boolean running = true; |
20 | + | private boolean running = true; |
21 | ||
22 | - | private int playerNr = 0; |
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 |
33 | + | |
34 | in = new ObjectInputStream(socket.getInputStream()); | |
35 | } catch (IOException e) { | |
36 | e.printStackTrace(); | |
37 | } | |
38 | ||
39 | start(); | |
40 | } | |
41 | ||
42 | ||
43 | /** | |
44 | * Run method, called when thread is started. | |
45 | */ | |
46 | @SuppressWarnings("unchecked") | |
47 | public void run() { | |
48 | // Get playerNr and first tank vector | |
49 | try { | |
50 | playerNr = in.readInt(); | |
51 | System.out.println("playerNr " + playerNr); | |
52 | ||
53 | updatedTanks = (Vector<Tank>) in.readUnshared(); | |
54 | - | updatedTanks = (Vector<Tank>) in.readObject(); |
54 | + | |
55 | } catch (InterruptedException e2) { | |
56 | e2.printStackTrace(); | |
57 | } catch (IOException e2) { | |
58 | e2.printStackTrace(); | |
59 | } catch (ClassNotFoundException e2) { | |
60 | e2.printStackTrace(); | |
61 | } | |
62 | - | } |
62 | + | |
63 | // Get entities from server | |
64 | try { | |
65 | while (running) { | |
66 | updatedTanks = (Vector<Tank>) in.readUnshared(); | |
67 | - | updatedTanks = (Vector<Tank>) in.readObject(); |
67 | + | |
68 | System.out.println("Tank 1 is active: " + updatedTanks.get(1).isActive()); | |
69 | sleep(5000); | |
70 | } | |
71 | } catch (IOException e) { | |
72 | e.printStackTrace(); | |
73 | } catch (ClassNotFoundException e) { | |
74 | e.printStackTrace(); | |
75 | } catch (InterruptedException e) { | |
76 | e.printStackTrace(); | |
77 | } | |
78 | } // run | |
79 | ||
80 | ||
81 | /** | |
82 | * Sets 'running' to 'false', stopping the input loop. | |
83 | */ | |
84 | public void stopRunning() { | |
85 | running = false; | |
86 | } | |
87 | ||
88 | ||
89 | /** | |
90 | * Getter of the property <tt>playerNr</tt> | |
91 | * @return Returns playerNr | |
92 | * @uml.property name="playerNr" | |
93 | */ | |
94 | public int getPlayerNr() { | |
95 | return playerNr; | |
96 | } | |
97 | ||
98 | ||
99 | /** | |
100 | * Getter of the property (vector) <tt>updatedEntitites</tt> | |
101 | * @return Returns the updatedEntities vector. | |
102 | * @uml.property name="updatedEntities" | |
103 | */ | |
104 | public Vector<Tank> getUpdatedTanks() { | |
105 | return updatedTanks; | |
106 | } | |
107 | } | |
108 | ||
109 | ||
110 | Server -> OutputThread | |
111 | ---------------------- | |
112 | ||
113 | package tw.server; | |
114 | ||
115 | import java.io.IOException; | |
116 | import java.io.ObjectOutputStream; | |
117 | import java.net.Socket; | |
118 | import java.util.concurrent.BlockingQueue; | |
119 | ||
120 | ||
121 | public class OutputThread extends Thread { | |
122 | ||
123 | private ObjectOutputStream out = null; | |
124 | private boolean running = true; | |
125 | ||
126 | private int playerNr; | |
127 | private GameManager gm; | |
128 | ||
129 | ||
130 | /** | |
131 | * OutputThread constructor. | |
132 | * @param s | |
133 | * @param playerNr | |
134 | * @param gm | |
135 | * @throws IOException | |
136 | */ | |
137 | public OutputThread(Socket s, int playerNr, GameManager gm) throws IOException { | |
138 | this.playerNr = playerNr; | |
139 | this.gm = gm; | |
140 | ||
141 | out = new ObjectOutputStream(s.getOutputStream()); | |
142 | - | // Open new output stream to client |
142 | + | |
143 | start(); | |
144 | } | |
145 | ||
146 | ||
147 | public void run() { | |
148 | ||
149 | // Send player number to client | |
150 | try { | |
151 | out.writeInt(playerNr); | |
152 | } catch (IOException e) { | |
153 | e.printStackTrace(); | |
154 | } | |
155 | ||
156 | // Send entities client | |
157 | try { | |
158 | while(running) { | |
159 | out.writeUnshared(gm.getTanks()); | |
160 | System.out.println("Tank 0 is active: " + gm.getTanks().get(0).isActive()); | |
161 | - | out.writeObject(gm.getTanks()); |
161 | + | |
162 | sleep(5000); | |
163 | } | |
164 | } catch (IOException e1) { | |
165 | e1.printStackTrace(); | |
166 | } catch (InterruptedException e) { | |
167 | e.printStackTrace(); | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | ||
173 | Client: | |
174 | - | Output Client: |
174 | + | |
175 | Tank 1 is active: true | |
176 | Client output: 32 | |
177 | Tank 0 is active: false | |
178 | Tank 1 is active: true | |
179 | ||
180 | Server: | |
181 | Tank 0 is active: false | |
182 | Tank 1 is active: true | |
183 | Tank 0 is active: false | |
184 | Tank 1 is active: true | |
185 | - | Output Server: |
185 | + | |
186 | Tank 0 is active: true | |
187 | Tank 1 is active: false | |
188 | Tank 0 is active: true | |
189 | Tank 1 is active: false |