NetworkStreamManager: add closeAll(), missing javadocs
authorEddie <dev@fun2be.me>
Sat, 6 Jun 2015 08:58:40 +0000 (09:58 +0100)
committerEddie <dev@fun2be.me>
Sat, 6 Jun 2015 09:37:19 +0000 (10:37 +0100)
Introduce closeAll(). Provides public method for cleanly closing all
managed NetworkStreams.

src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkStreamManager.java

index 2d131cb..c5640a1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * The MIT License
  *
- * Copyright 2015 eddie.
+ * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  */
 package uk.ac.ntu.n0521366.wsyd.libs.net;
 
+import java.util.Enumeration;
 import java.util.Random;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
- *
- * @author eddie
+ * Manages active TCP connections.
+ * 
+ * @author Eddie Berrisford-Lynch <dev@fun2be.me>
  */
 public class NetworkStreamManager implements NetworkSocketClosing {
-    
+   
+    /** 
+     * Unique ID for Social Server.
+     */
     public static final long SERVERSOCIAL = -98;
     
+    /**
+     * Unique ID for Chat Server.
+     */
     public static final long SERVERCHAT = -99;
     
+    /**
+     * Temporary IDs below this value allocated to anonymous clients before user has logged in.
+     */
     static final long TEMPCLIENT = -200;
     
+    /**
+     * Random number generator for anonymous client IDs.
+     */
     Random random;
     
     /**
-     *
+     * Map of UserIDs to TCP streams.
      */
     public ConcurrentHashMap<Long, NetworkStream> _tcpStreams;
     
+    /**
+     * Default constructor.
+     */
     public NetworkStreamManager() {
         random = new Random();
-        _tcpStreams = new ConcurrentHashMap<Long, NetworkStream>();
+        _tcpStreams = new ConcurrentHashMap<>();
     }
     
+    /**
+     *  Add a NetworkStream to the collection.
+     * 
+     * @param userID 0 == allocate temporary ID
+     * @param netStream the stream associated with the userID
+     * @return the allocated userID (key)
+     */
     public long addStream(long userID, NetworkStream netStream) {
         long result = 0;
         
@@ -64,7 +88,6 @@ public class NetworkStreamManager implements NetworkSocketClosing {
                     result = tempKey;
                     netStream._key = tempKey;
                     netStream.execute();
-                    System.err.println("Added new stream with key: " + tempKey);
                 }
                 else {
                     if ((userID < 0 && userID > TEMPCLIENT) || userID > 0) {
@@ -72,7 +95,6 @@ public class NetworkStreamManager implements NetworkSocketClosing {
                         result = userID;
                         netStream._key = userID;
                         netStream.execute();
-                        System.err.println("Added new stream with predefined key: " + userID);
                     }
                 }
             }
@@ -81,6 +103,13 @@ public class NetworkStreamManager implements NetworkSocketClosing {
         return result;
     }
     
+    /**
+     * Update anonymous client ID after user has logged in.
+     * 
+     * @param oldID The temporary ID
+     * @param newID The valid user ID
+     * @return true if the key was updated
+     */
     public boolean updateKey(long oldID, long newID) {
         boolean result = false;
         
@@ -89,7 +118,6 @@ public class NetworkStreamManager implements NetworkSocketClosing {
                 NetworkStream temp = _tcpStreams.get(oldID);
                 temp._key = newID;
                 _tcpStreams.put(newID, temp);
-                System.err.println("Added new stream with key: " + newID);
                 _tcpStreams.remove(oldID);
                 
                 result = true;
@@ -99,10 +127,30 @@ public class NetworkStreamManager implements NetworkSocketClosing {
         return result;
     }
     
+    /**
+     * Generate a random key ID.
+     * @return random ID
+     */
     private long createTempKey() {
         return TEMPCLIENT + -random.nextInt(1000000);
     }
 
+    public boolean closeAll() {
+        boolean result = false;
+        for (Enumeration<Long> keys = _tcpStreams.keys(); keys.hasMoreElements();) {
+            long key = keys.nextElement();
+            _tcpStreams.get(key).close(); // close the network stream
+            _tcpStreams.remove(key);
+        }
+        return result;
+    }
+
+    /**
+     * Indicate whether a NetworkStream has been removed from the collection.
+     * 
+     * @param key ID of the stream that is closing
+     * @return true if the stream was removed
+     */
     @Override
     public boolean canCloseSocket(long key) {
         boolean result = false;
@@ -110,7 +158,6 @@ public class NetworkStreamManager implements NetworkSocketClosing {
         if (_tcpStreams.containsKey(key)) {
             _tcpStreams.remove(key);
             result = true;
-            System.err.println("Removed stream with key: " + key);
         }
         return result;
     }