diff --git a/src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java b/src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java index e8ddc36..453e808 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java +++ b/src/ve/ucv/ciens/ccg/nxtar/NxtARCore.java @@ -69,9 +69,9 @@ public class NxtARCore implements ApplicationListener, NetworkConnectionListener Gdx.app.debug(TAG, CLASS_NAME + ".create() :: Creating network threads"); mcastEnabler.enableMulticast(); - udpThread = new ServiceDiscoveryThread(); - videoThread = new VideoStreamingThread(toaster); - robotThread = new RobotControlThread(toaster); + udpThread = ServiceDiscoveryThread.getInstance(); + videoThread = VideoStreamingThread.getInstance().setToaster(toaster); + robotThread = RobotControlThread.getInstance().setToaster(toaster); udpThread.start(); videoThread.start(); diff --git a/src/ve/ucv/ciens/ccg/nxtar/network/RobotControlThread.java b/src/ve/ucv/ciens/ccg/nxtar/network/RobotControlThread.java index 1dbf3bc..b0bb090 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/network/RobotControlThread.java +++ b/src/ve/ucv/ciens/ccg/nxtar/network/RobotControlThread.java @@ -20,10 +20,9 @@ public class RobotControlThread extends Thread { private Socket client; private Toaster toaster; - public RobotControlThread(Toaster toaster){ + private RobotControlThread(){ super(THREAD_NAME); - this.toaster = toaster; netListener = null; try{ @@ -32,6 +31,19 @@ public class RobotControlThread extends Thread { 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){ netListener = listener; diff --git a/src/ve/ucv/ciens/ccg/nxtar/network/ServiceDiscoveryThread.java b/src/ve/ucv/ciens/ccg/nxtar/network/ServiceDiscoveryThread.java index 7f78faf..3a3a09b 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/network/ServiceDiscoveryThread.java +++ b/src/ve/ucv/ciens/ccg/nxtar/network/ServiceDiscoveryThread.java @@ -21,7 +21,6 @@ import com.badlogic.gdx.Gdx; * a row, whichever happens first.
* * @author miky - * @since 27/11/2013 */ public class ServiceDiscoveryThread extends Thread { /** @@ -41,12 +40,24 @@ public class ServiceDiscoveryThread extends Thread { */ private static final int MAX_RETRIES = 5; + /** + * A semaphore object used to synchronize acces to this thread finish flag. + */ private Object semaphore; + /** + * The finish flag. + */ private boolean done; + /** + * The UDP server socket used for the ad hoc service discovery protocol. + */ private DatagramSocket udpServer; + /** + * Holder for the multicast address used in the protocol. + */ private InetAddress group; - public ServiceDiscoveryThread(){ + private ServiceDiscoveryThread(){ // Setup this thread name. super(THREAD_NAME); @@ -71,6 +82,28 @@ public class ServiceDiscoveryThread extends Thread { 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. + * + *This method executes the ad hoc service discovery protocol implemented by this class, as + * described in the class introduction.
+ */ @Override public void run(){ int retries = 0; diff --git a/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java b/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java index 9bb70f1..fc5fd8f 100644 --- a/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java +++ b/src/ve/ucv/ciens/ccg/nxtar/network/VideoStreamingThread.java @@ -20,12 +20,10 @@ public class VideoStreamingThread extends Thread { private Socket client; private Toaster toaster; - public VideoStreamingThread(Toaster toaster){ + private VideoStreamingThread(){ super(THREAD_NAME); - this.toaster = toaster; netListener = null; - try{ server = new ServerSocket(ProjectConstants.SERVER_TCP_PORT_1); }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){ netListener = listener; }