Added client disconnect when server shuts down

This commit is contained in:
Obique
2015-05-16 20:16:01 -05:00
committed by Obique
parent ade367266d
commit 7c97a6ec57
7 changed files with 90 additions and 22 deletions
+23 -18
View File
@@ -27,6 +27,8 @@
***********************************************************************************/
package resources.control;
import intents.ServerStatusIntent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -36,15 +38,11 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import utilities.ThreadUtilities;
/**
* Don't you dare make this public.
*/
class IntentManager {
public class IntentManager {
private static final IntentManager instance = new IntentManager();
private final Runnable broadcastRunnable;
@@ -54,18 +52,20 @@ class IntentManager {
private boolean initialized = false;
private boolean terminated = false;
public IntentManager() {
private IntentManager() {
initialize();
broadcastRunnable = new Runnable() {
public void run() {
Intent i = intentQueue.poll();
if (i != null)
broadcast(i);
if (i instanceof ServerStatusIntent)
onServerStatusIntent((ServerStatusIntent) i);
}
};
}
public void initialize() {
protected void initialize() {
if (!initialized) {
broadcastThreads = Executors.newCachedThreadPool(ThreadUtilities.newThreadFactory("intent-processor-%d"));
intentRegistrations = new HashMap<String, List<IntentReceiver>>();
@@ -75,20 +75,21 @@ class IntentManager {
}
}
public void terminate() {
private void terminate() {
if (!terminated) {
broadcastThreads.shutdownNow();
try {
broadcastThreads.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
broadcastThreads.shutdown();
initialized = false;
terminated = true;
}
}
public void broadcastIntent(Intent i) {
private void onServerStatusIntent(ServerStatusIntent i) {
if (i.getStatus() == ServerStatus.TERMINATING) {
terminate();
}
}
protected void broadcastIntent(Intent i) {
if (i == null)
throw new NullPointerException("Intent cannot be null!");
intentQueue.add(i);
@@ -96,7 +97,7 @@ class IntentManager {
catch (RejectedExecutionException e) { } // This error is thrown when the server is being shut down
}
public void registerForIntent(String intentType, IntentReceiver r) {
protected void registerForIntent(String intentType, IntentReceiver r) {
if (r == null)
throw new NullPointerException("Cannot register a null value for an intent");
synchronized (intentRegistrations) {
@@ -111,7 +112,7 @@ class IntentManager {
}
}
public void unregisterForIntent(String intentType, IntentReceiver r) {
protected void unregisterForIntent(String intentType, IntentReceiver r) {
if (r == null)
return;
synchronized (intentRegistrations) {
@@ -152,7 +153,11 @@ class IntentManager {
}
}
public static IntentManager getInstance() {
public static int getIntentsQueued() {
return getInstance().intentQueue.size();
}
protected static IntentManager getInstance() {
return instance;
}