blob: e84d41ab7d1b2114c8263935e6c96a558fb40caf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package smoots.udesign.io;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import smoots.udesign.packet.Packet;
import smoots.udesign.packet.PhoneToServerPacket;
/**
* A class in charge of taking packets from queue and parses them into JSON
* formatted messages and - Send messages to the server.
*/
public class ClientWriter extends Thread {
private static final String TAG = "ClientWriter";
private final PrintWriter out;
private BlockingQueue<Packet> packetQ;
private boolean mRun = false;
/**
* Constructor that initializes: the output writer, and a packet queue for
* sending data.
*
* @param clientSocket
* the Socket the Client's going to use.
* @throws IOException
* possible during socket i/o
*/
public ClientWriter(BlockingQueue<Packet> packetQ, Socket clientSocket)
throws IOException {
this.out = new PrintWriter(clientSocket.getOutputStream(), true);
this.packetQ = packetQ;
}
/**
* Converting a packet into text form and send it to the server.
*/
public void send() {
Packet pac;
try {
pac = this.packetQ.take();
if (pac != null) {
this.out.println(pac.packetToText());
}
// this.out.println("gosgos1");
} catch (InterruptedException e) {
// PhoneDebugger.debug(TAG, e);
}
}
/**
* Set the running status of the thread.
*
* @param b
* If the thread is running
*/
public void setRunning(boolean b) {
this.mRun = b;
}
/**
* runtime method, keep sending packets from the queue.
*/
public void run() {
while (this.mRun) {
this.send();
}
this.out.println("end");
}
}
|