Networking threads are singletons now.
This commit is contained in:
@@ -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();
|
||||
|
@@ -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{
|
||||
@@ -33,6 +32,19 @@ public class RobotControlThread extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@@ -21,7 +21,6 @@ import com.badlogic.gdx.Gdx;
|
||||
* a row, whichever happens first.</p>
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* <p>This method executes the ad hoc service discovery protocol implemented by this class, as
|
||||
* described in the class introduction.</p>
|
||||
*/
|
||||
@Override
|
||||
public void run(){
|
||||
int retries = 0;
|
||||
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user