Implemented TCP streams for object serialization
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / libs / net / NetworkStreamManager.java
diff --git a/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkStreamManager.java b/src/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkStreamManager.java
new file mode 100644 (file)
index 0000000..dfa02ba
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2015 eddie.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package uk.ac.ntu.n0521366.wsyd.libs.net;
+
+import java.util.Random;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ *
+ * @author eddie
+ */
+public class NetworkStreamManager implements NetworkSocketClosing {
+    
+    static final int SERVERSOCIAL = -98;
+    
+    static final int SERVERCHAT = -99;
+    
+    static final int TEMPCLIENT = -200;
+    
+    Random random;
+    
+    /**
+     *
+     */
+    public ConcurrentHashMap<Integer, NetworkStream> _tcpStreams;
+    
+    public NetworkStreamManager() {
+        random = new Random();
+        _tcpStreams = new ConcurrentHashMap<Integer, NetworkStream>();
+    }
+    
+    public int addStream(int userID, NetworkStream netStream) {
+        int result = 0;
+        
+        if (netStream != null) {
+            if (!_tcpStreams.containsKey(userID)) {
+                if (userID == 0) {
+                    int tempKey;
+                    do {
+                        tempKey = createTempKey();
+                    } while (_tcpStreams.containsKey(tempKey));
+                    _tcpStreams.put(tempKey, netStream);
+                    result = tempKey;
+                    System.err.println("Added new stream with key: " + tempKey);
+                }
+                else {
+                    if ((userID < 0 && userID > TEMPCLIENT) || userID > 0) {
+                        _tcpStreams.put(userID, netStream);
+                        result = userID;
+                    }
+                }
+            }
+        }
+        
+        return result;
+    }
+    
+    public boolean updateKey(int oldID, int newID) {
+        boolean result = false;
+        
+        if (!_tcpStreams.containsKey(newID)) {
+            if (_tcpStreams.containsKey(oldID)) {
+                _tcpStreams.put(newID, _tcpStreams.get(oldID));
+                _tcpStreams.remove(oldID);
+                
+                result = true;
+            }
+        }
+        
+        return result;
+    }
+    
+    private int createTempKey() {
+        return TEMPCLIENT + -random.nextInt(1000000);
+    }
+
+    @Override
+    public boolean canCloseSocket(int key) {
+        boolean result = false;
+        
+        if (_tcpStreams.containsKey(key)) {
+            _tcpStreams.remove(key);
+            result = true;
+            System.err.println("Removed key: " + key);
+        }
+        return result;
+    }
+    
+}