aboutsummaryrefslogtreecommitdiff
path: root/mobileapp/src/smoots/udesign/io/ClientReader.java
blob: 7f7cbcf2844910967bb2a976156dea08527bb572 (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
72
73
74
package smoots.udesign.io;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import smoots.udesign.accelerometer.AccelerometerListener;
import smoots.udesign.accelerometer.AccelerometerManager;
import smoots.udesign.packet.MotionType;
import smoots.udesign.packet.Packet;
import smoots.udesign.packet.PhoneToServerPacket;

/**
 * ClientReader is in charge of receiving data from the view, parsing
 * the textual protocol into executable packets, and send the packets to the
 * processor. 1 per client.
 */
public class ClientReader implements Runnable, AccelerometerListener {
	private BlockingQueue<Packet> accPktQ;
	private BlockingQueue<Packet> packetQ;
	private boolean stopped;

	/**
	 * Initialize the queue of accelerometer data.
	 * 
	 * @param packetQ The queue of packets 
	 */
	public ClientReader(BlockingQueue<Packet> packetQ) {
		this.accPktQ = new LinkedBlockingQueue<Packet>();
		this.packetQ = packetQ;
		
//		if (AccelerometerManager.isSupported()) {
			AccelerometerManager.startListening(this);
//		}
	}

	/**
	 * From the queue, extract a packet of Accelerometer data and forward it to the
	 * processor, which will execute the packet accordingly.
	 * 
	 * @throws InterruptedException 
	 */
	public void forward() throws InterruptedException {
		packetQ.add(accPktQ.take());
	}

	/**
	 * runtime method, do the following actions in sequence:
	 * 		receive -> forward.
	 */
	public void run() {
		while (true) {
			try {
				Thread.sleep(1000);
				forward(); // to the processor
			} catch (InterruptedException e) {
				break;
			}
		}
	}

	public void onAccelerationChanged(float x, float y, float z) {
		// CHANGE ACCELEROMETER READINGS TO LIGHT LOCATIONS
		// ...
		
		accPktQ.add(new PhoneToServerPacket(MotionType.MOVE, (int)x, (int)y, (int)z));
	}

	public void onShake(float force) {
		// TODO Auto-generated method stub
		
	}

}