libs.net: complete network functionality
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / libs / net / NetworkStreamManager.java
index dfa02ba..2d131cb 100644 (file)
@@ -32,42 +32,47 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 public class NetworkStreamManager implements NetworkSocketClosing {
     
-    static final int SERVERSOCIAL = -98;
+    public static final long SERVERSOCIAL = -98;
     
-    static final int SERVERCHAT = -99;
+    public static final long SERVERCHAT = -99;
     
-    static final int TEMPCLIENT = -200;
+    static final long TEMPCLIENT = -200;
     
     Random random;
     
     /**
      *
      */
-    public ConcurrentHashMap<Integer, NetworkStream> _tcpStreams;
+    public ConcurrentHashMap<Long, NetworkStream> _tcpStreams;
     
     public NetworkStreamManager() {
         random = new Random();
-        _tcpStreams = new ConcurrentHashMap<Integer, NetworkStream>();
+        _tcpStreams = new ConcurrentHashMap<Long, NetworkStream>();
     }
     
-    public int addStream(int userID, NetworkStream netStream) {
-        int result = 0;
+    public long addStream(long userID, NetworkStream netStream) {
+        long result = 0;
         
         if (netStream != null) {
             if (!_tcpStreams.containsKey(userID)) {
                 if (userID == 0) {
-                    int tempKey;
+                    long tempKey;
                     do {
                         tempKey = createTempKey();
                     } while (_tcpStreams.containsKey(tempKey));
                     _tcpStreams.put(tempKey, netStream);
                     result = tempKey;
+                    netStream._key = tempKey;
+                    netStream.execute();
                     System.err.println("Added new stream with key: " + tempKey);
                 }
                 else {
                     if ((userID < 0 && userID > TEMPCLIENT) || userID > 0) {
                         _tcpStreams.put(userID, netStream);
                         result = userID;
+                        netStream._key = userID;
+                        netStream.execute();
+                        System.err.println("Added new stream with predefined key: " + userID);
                     }
                 }
             }
@@ -76,12 +81,15 @@ public class NetworkStreamManager implements NetworkSocketClosing {
         return result;
     }
     
-    public boolean updateKey(int oldID, int newID) {
+    public boolean updateKey(long oldID, long newID) {
         boolean result = false;
         
         if (!_tcpStreams.containsKey(newID)) {
             if (_tcpStreams.containsKey(oldID)) {
-                _tcpStreams.put(newID, _tcpStreams.get(oldID));
+                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;
@@ -91,18 +99,18 @@ public class NetworkStreamManager implements NetworkSocketClosing {
         return result;
     }
     
-    private int createTempKey() {
+    private long createTempKey() {
         return TEMPCLIENT + -random.nextInt(1000000);
     }
 
     @Override
-    public boolean canCloseSocket(int key) {
+    public boolean canCloseSocket(long key) {
         boolean result = false;
         
         if (_tcpStreams.containsKey(key)) {
             _tcpStreams.remove(key);
             result = true;
-            System.err.println("Removed key: " + key);
+            System.err.println("Removed stream with key: " + key);
         }
         return result;
     }