Networking threads are singletons now.

This commit is contained in:
2013-11-28 09:16:52 -04:30
parent 5eb079fb7b
commit ddcf536e51
4 changed files with 66 additions and 10 deletions

View File

@@ -69,9 +69,9 @@ public class NxtARCore implements ApplicationListener, NetworkConnectionListener
Gdx.app.debug(TAG, CLASS_NAME + ".create() :: Creating network threads"); Gdx.app.debug(TAG, CLASS_NAME + ".create() :: Creating network threads");
mcastEnabler.enableMulticast(); mcastEnabler.enableMulticast();
udpThread = new ServiceDiscoveryThread(); udpThread = ServiceDiscoveryThread.getInstance();
videoThread = new VideoStreamingThread(toaster); videoThread = VideoStreamingThread.getInstance().setToaster(toaster);
robotThread = new RobotControlThread(toaster); robotThread = RobotControlThread.getInstance().setToaster(toaster);
udpThread.start(); udpThread.start();
videoThread.start(); videoThread.start();

View File

@@ -20,10 +20,9 @@ public class RobotControlThread extends Thread {
private Socket client; private Socket client;
private Toaster toaster; private Toaster toaster;
public RobotControlThread(Toaster toaster){ private RobotControlThread(){
super(THREAD_NAME); super(THREAD_NAME);
this.toaster = toaster;
netListener = null; netListener = null;
try{ try{
@@ -32,6 +31,19 @@ public class RobotControlThread extends Thread {
Gdx.app.error(TAG, CLASS_NAME + ".RobotControlThread() :: Error creating server: " + io.getMessage(), io); Gdx.app.error(TAG, CLASS_NAME + ".RobotControlThread() :: Error creating server: " + io.getMessage(), io);
} }
} }
private static class SingletonHolder{
public static final RobotControlThread INSTANCE = new RobotControlThread();
}
public static RobotControlThread getInstance(){
return SingletonHolder.INSTANCE;
}
public RobotControlThread setToaster(Toaster toaster){
this.toaster = toaster;
return this;
}
public void addNetworkConnectionListener(NetworkConnectionListener listener){ public void addNetworkConnectionListener(NetworkConnectionListener listener){
netListener = listener; netListener = listener;

View File

@@ -21,7 +21,6 @@ import com.badlogic.gdx.Gdx;
* a row, whichever happens first.</p> * a row, whichever happens first.</p>
* *
* @author miky * @author miky
* @since 27/11/2013
*/ */
public class ServiceDiscoveryThread extends Thread { public class ServiceDiscoveryThread extends Thread {
/** /**
@@ -41,12 +40,24 @@ public class ServiceDiscoveryThread extends Thread {
*/ */
private static final int MAX_RETRIES = 5; private static final int MAX_RETRIES = 5;
/**
* A semaphore object used to synchronize acces to this thread finish flag.
*/
private Object semaphore; private Object semaphore;
/**
* The finish flag.
*/
private boolean done; private boolean done;
/**
* The UDP server socket used for the ad hoc service discovery protocol.
*/
private DatagramSocket udpServer; private DatagramSocket udpServer;
/**
* Holder for the multicast address used in the protocol.
*/
private InetAddress group; private InetAddress group;
public ServiceDiscoveryThread(){ private ServiceDiscoveryThread(){
// Setup this thread name. // Setup this thread name.
super(THREAD_NAME); super(THREAD_NAME);
@@ -71,6 +82,28 @@ public class ServiceDiscoveryThread extends Thread {
Gdx.app.debug(TAG, CLASS_NAME + ".ServiceDiscoveryThread() :: Multicast server created."); Gdx.app.debug(TAG, CLASS_NAME + ".ServiceDiscoveryThread() :: Multicast server created.");
} }
/**
* Singleton holder for this class.
*/
private static class SingletonHolder{
public static final ServiceDiscoveryThread INSTANCE = new ServiceDiscoveryThread();
}
/**
* Get the singleton instance of this class.
*
* @return The singleton instance.
*/
public static ServiceDiscoveryThread getInstance(){
return SingletonHolder.INSTANCE;
}
/**
* This thread's run method.
*
* <p>This method executes the ad hoc service discovery protocol implemented by this class, as
* described in the class introduction.</p>
*/
@Override @Override
public void run(){ public void run(){
int retries = 0; int retries = 0;

View File

@@ -20,12 +20,10 @@ public class VideoStreamingThread extends Thread {
private Socket client; private Socket client;
private Toaster toaster; private Toaster toaster;
public VideoStreamingThread(Toaster toaster){ private VideoStreamingThread(){
super(THREAD_NAME); super(THREAD_NAME);
this.toaster = toaster;
netListener = null; netListener = null;
try{ try{
server = new ServerSocket(ProjectConstants.SERVER_TCP_PORT_1); server = new ServerSocket(ProjectConstants.SERVER_TCP_PORT_1);
}catch(IOException io){ }catch(IOException io){
@@ -33,6 +31,19 @@ public class VideoStreamingThread extends Thread {
} }
} }
private static class SingletonHolder{
public static final VideoStreamingThread INSTANCE = new VideoStreamingThread();
}
public static VideoStreamingThread getInstance(){
return SingletonHolder.INSTANCE;
}
public VideoStreamingThread setToaster(Toaster toaster){
this.toaster = toaster;
return this;
}
public void addNetworkConnectionListener(NetworkConnectionListener listener){ public void addNetworkConnectionListener(NetworkConnectionListener listener){
netListener = listener; netListener = listener;
} }