Added a sensor data receiving thread.
This commit is contained in:
@@ -20,6 +20,7 @@ import ve.ucv.ciens.ccg.nxtar.interfaces.MulticastEnabler;
|
|||||||
import ve.ucv.ciens.ccg.nxtar.interfaces.NetworkConnectionListener;
|
import ve.ucv.ciens.ccg.nxtar.interfaces.NetworkConnectionListener;
|
||||||
import ve.ucv.ciens.ccg.nxtar.interfaces.Toaster;
|
import ve.ucv.ciens.ccg.nxtar.interfaces.Toaster;
|
||||||
import ve.ucv.ciens.ccg.nxtar.network.RobotControlThread;
|
import ve.ucv.ciens.ccg.nxtar.network.RobotControlThread;
|
||||||
|
import ve.ucv.ciens.ccg.nxtar.network.SensorReportThread;
|
||||||
import ve.ucv.ciens.ccg.nxtar.network.ServiceDiscoveryThread;
|
import ve.ucv.ciens.ccg.nxtar.network.ServiceDiscoveryThread;
|
||||||
import ve.ucv.ciens.ccg.nxtar.network.VideoStreamingThread;
|
import ve.ucv.ciens.ccg.nxtar.network.VideoStreamingThread;
|
||||||
import ve.ucv.ciens.ccg.nxtar.states.BaseState;
|
import ve.ucv.ciens.ccg.nxtar.states.BaseState;
|
||||||
@@ -76,10 +77,12 @@ public class NxtARCore extends Game implements NetworkConnectionListener{
|
|||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The current application state.
|
* The current application state.
|
||||||
*/
|
*/
|
||||||
private game_states_t currState;
|
private game_states_t currState;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>The state to change to.</p>
|
* <p>The state to change to.</p>
|
||||||
* <p> Usually null. A state change is scheduled by setting this field to a {@link game_states_t} value.</p>
|
* <p> Usually null. A state change is scheduled by setting this field to a {@link game_states_t} value.</p>
|
||||||
@@ -100,6 +103,7 @@ public class NxtARCore extends Game implements NetworkConnectionListener{
|
|||||||
private ServiceDiscoveryThread serviceDiscoveryThread;
|
private ServiceDiscoveryThread serviceDiscoveryThread;
|
||||||
private VideoStreamingThread videoThread;
|
private VideoStreamingThread videoThread;
|
||||||
private RobotControlThread robotThread;
|
private RobotControlThread robotThread;
|
||||||
|
private SensorReportThread sensorThread;
|
||||||
|
|
||||||
// Overlays.
|
// Overlays.
|
||||||
private OrthographicCamera pixelPerfectCamera;
|
private OrthographicCamera pixelPerfectCamera;
|
||||||
@@ -144,8 +148,10 @@ public class NxtARCore extends Game implements NetworkConnectionListener{
|
|||||||
public void create(){
|
public void create(){
|
||||||
// Create the state objects.
|
// Create the state objects.
|
||||||
states = new BaseState[3];
|
states = new BaseState[3];
|
||||||
if(Ouya.runningOnOuya)states[game_states_t.MAIN_MENU.getValue()] = new OuyaMainMenuState(this);
|
if(Ouya.runningOnOuya)
|
||||||
else states[game_states_t.MAIN_MENU.getValue()] = new TabletMainMenuState(this);
|
states[game_states_t.MAIN_MENU.getValue()] = new OuyaMainMenuState(this);
|
||||||
|
else
|
||||||
|
states[game_states_t.MAIN_MENU.getValue()] = new TabletMainMenuState(this);
|
||||||
states[game_states_t.IN_GAME.getValue()] = new InGameState(this);
|
states[game_states_t.IN_GAME.getValue()] = new InGameState(this);
|
||||||
states[game_states_t.PAUSED.getValue()] = new PauseState(this);
|
states[game_states_t.PAUSED.getValue()] = new PauseState(this);
|
||||||
|
|
||||||
@@ -179,6 +185,7 @@ public class NxtARCore extends Game implements NetworkConnectionListener{
|
|||||||
serviceDiscoveryThread = ServiceDiscoveryThread.getInstance();
|
serviceDiscoveryThread = ServiceDiscoveryThread.getInstance();
|
||||||
videoThread = VideoStreamingThread.getInstance();
|
videoThread = VideoStreamingThread.getInstance();
|
||||||
robotThread = RobotControlThread.getInstance();
|
robotThread = RobotControlThread.getInstance();
|
||||||
|
sensorThread = SensorReportThread.getInstance();
|
||||||
|
|
||||||
serviceDiscoveryThread.start();
|
serviceDiscoveryThread.start();
|
||||||
|
|
||||||
@@ -189,6 +196,9 @@ public class NxtARCore extends Game implements NetworkConnectionListener{
|
|||||||
robotThread.addNetworkConnectionListener(this);
|
robotThread.addNetworkConnectionListener(this);
|
||||||
robotThread.start();
|
robotThread.start();
|
||||||
|
|
||||||
|
sensorThread.addNetworkConnectionListener(this);
|
||||||
|
sensorThread.start();
|
||||||
|
|
||||||
// Set the current and next states.
|
// Set the current and next states.
|
||||||
currState = game_states_t.MAIN_MENU;
|
currState = game_states_t.MAIN_MENU;
|
||||||
nextState = null;
|
nextState = null;
|
||||||
@@ -275,6 +285,7 @@ public class NxtARCore extends Game implements NetworkConnectionListener{
|
|||||||
font.draw(batch, String.format("Render FPS: %d", Gdx.graphics.getFramesPerSecond()), fontX, fontY);
|
font.draw(batch, String.format("Render FPS: %d", Gdx.graphics.getFramesPerSecond()), fontX, fontY);
|
||||||
font.draw(batch, String.format("Total stream FPS: %d", videoThread.getFps()), fontX, fontY - font.getCapHeight() - 5);
|
font.draw(batch, String.format("Total stream FPS: %d", videoThread.getFps()), fontX, fontY - font.getCapHeight() - 5);
|
||||||
font.draw(batch, String.format("Lost stream FPS: %d", videoThread.getLostFrames()), fontX, fontY - (2 * font.getCapHeight()) - 10);
|
font.draw(batch, String.format("Lost stream FPS: %d", videoThread.getLostFrames()), fontX, fontY - (2 * font.getCapHeight()) - 10);
|
||||||
|
font.draw(batch, String.format("Light sensor data: %d", sensorThread.getLightSensorReading()), fontX, fontY - (3 * font.getCapHeight()) - 15);
|
||||||
}batch.end();
|
}batch.end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -309,10 +320,10 @@ public class NxtARCore extends Game implements NetworkConnectionListener{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void networkStreamConnected(String streamName){
|
public synchronized void networkStreamConnected(String streamName){
|
||||||
//if(streamName.equals(VideoStreamingThread.THREAD_NAME) || streamName.equals(RobotControlThread.THREAD_NAME))
|
|
||||||
Gdx.app.log(TAG, CLASS_NAME + ".networkStreamConnected() :: Stream " + streamName + " connected.");
|
Gdx.app.log(TAG, CLASS_NAME + ".networkStreamConnected() :: Stream " + streamName + " connected.");
|
||||||
connections += 1;
|
connections += 1;
|
||||||
if(connections >= 2){
|
|
||||||
|
if(connections >= 3){
|
||||||
Gdx.app.debug(TAG, CLASS_NAME + ".networkStreamConnected() :: Stopping service broadcast.");
|
Gdx.app.debug(TAG, CLASS_NAME + ".networkStreamConnected() :: Stopping service broadcast.");
|
||||||
serviceDiscoveryThread.finish();
|
serviceDiscoveryThread.finish();
|
||||||
mcastEnabler.disableMulticast();
|
mcastEnabler.disableMulticast();
|
||||||
|
@@ -15,10 +15,44 @@
|
|||||||
*/
|
*/
|
||||||
package ve.ucv.ciens.ccg.nxtar.network;
|
package ve.ucv.ciens.ccg.nxtar.network;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
import ve.ucv.ciens.ccg.nxtar.interfaces.NetworkConnectionListener;
|
||||||
|
import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
|
||||||
public class SensorReportThread extends Thread {
|
public class SensorReportThread extends Thread {
|
||||||
|
public static final String THREAD_NAME = "SensorReportThread";
|
||||||
|
private static final String TAG = "NXTAR_CORE_ROBOTTHREAD";
|
||||||
|
private static final String CLASS_NAME = SensorReportThread.class.getSimpleName();
|
||||||
|
|
||||||
|
private NetworkConnectionListener netListener;
|
||||||
|
private ServerSocket server;
|
||||||
|
private Socket client;
|
||||||
|
private Object pauseMonitor;
|
||||||
|
private boolean paused;
|
||||||
|
private boolean done;
|
||||||
|
private InputStream reader;
|
||||||
|
private Byte lightReading;
|
||||||
|
|
||||||
private SensorReportThread(){
|
private SensorReportThread(){
|
||||||
|
paused = false;
|
||||||
|
done = false;
|
||||||
|
netListener = null;
|
||||||
|
pauseMonitor = null;
|
||||||
|
client = null;
|
||||||
|
lightReading = -1;
|
||||||
|
|
||||||
|
try{
|
||||||
|
server = new ServerSocket(ProjectConstants.SENSOR_REPORT_PORT);
|
||||||
|
}catch(IOException io){
|
||||||
|
Gdx.app.error(TAG, CLASS_NAME + ".RobotControlThread() :: Error creating server: " + io.getMessage(), io);
|
||||||
|
server = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SingletonHolder{
|
private static class SingletonHolder{
|
||||||
@@ -29,8 +63,64 @@ public class SensorReportThread extends Thread {
|
|||||||
return SingletonHolder.INSTANCE;
|
return SingletonHolder.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addNetworkConnectionListener(NetworkConnectionListener listener){
|
||||||
|
netListener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pauseThread(){
|
||||||
|
synchronized(pauseMonitor){
|
||||||
|
paused = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resumeThread(){
|
||||||
|
synchronized(pauseMonitor){
|
||||||
|
paused = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void finish(){
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte getLightSensorReading(){
|
||||||
|
byte data;
|
||||||
|
|
||||||
|
synchronized(lightReading){
|
||||||
|
data = lightReading.byteValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(){
|
public void run(){
|
||||||
|
byte[] reading = new byte[1];
|
||||||
|
|
||||||
|
try{
|
||||||
|
client = server.accept();
|
||||||
|
client.setTcpNoDelay(true);
|
||||||
|
if(netListener != null) netListener.networkStreamConnected(THREAD_NAME);
|
||||||
|
reader = client.getInputStream();
|
||||||
|
|
||||||
|
}catch(IOException io){
|
||||||
|
Gdx.app.error(TAG, CLASS_NAME + ".run() :: Error accepting client: " + io.getMessage(), io);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(!paused){
|
||||||
|
if(done) break;
|
||||||
|
|
||||||
|
try{
|
||||||
|
reader.read(reading);
|
||||||
|
}catch(IOException io){
|
||||||
|
Gdx.app.error(TAG, CLASS_NAME + ".run() :: IOException during sensor read: " + io.getMessage(), io);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (lightReading) {
|
||||||
|
lightReading = reading[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user