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");
|
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();
|
||||||
|
@@ -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{
|
||||||
@@ -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){
|
public void addNetworkConnectionListener(NetworkConnectionListener listener){
|
||||||
netListener = listener;
|
netListener = listener;
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
||||||
|
@@ -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;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user