libs.net: complete network functionality
[WeStealzYourDataz.git] / src / uk / ac / ntu / n0521366 / wsyd / libs / net / NetworkStreamManager.java
1 /*
2  * The MIT License
3  *
4  * Copyright 2015 eddie.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 package uk.ac.ntu.n0521366.wsyd.libs.net;
25
26 import java.util.Random;
27 import java.util.concurrent.ConcurrentHashMap;
28
29 /**
30  *
31  * @author eddie
32  */
33 public class NetworkStreamManager implements NetworkSocketClosing {
34     
35     public static final long SERVERSOCIAL = -98;
36     
37     public static final long SERVERCHAT = -99;
38     
39     static final long TEMPCLIENT = -200;
40     
41     Random random;
42     
43     /**
44      *
45      */
46     public ConcurrentHashMap<Long, NetworkStream> _tcpStreams;
47     
48     public NetworkStreamManager() {
49         random = new Random();
50         _tcpStreams = new ConcurrentHashMap<Long, NetworkStream>();
51     }
52     
53     public long addStream(long userID, NetworkStream netStream) {
54         long result = 0;
55         
56         if (netStream != null) {
57             if (!_tcpStreams.containsKey(userID)) {
58                 if (userID == 0) {
59                     long tempKey;
60                     do {
61                         tempKey = createTempKey();
62                     } while (_tcpStreams.containsKey(tempKey));
63                     _tcpStreams.put(tempKey, netStream);
64                     result = tempKey;
65                     netStream._key = tempKey;
66                     netStream.execute();
67                     System.err.println("Added new stream with key: " + tempKey);
68                 }
69                 else {
70                     if ((userID < 0 && userID > TEMPCLIENT) || userID > 0) {
71                         _tcpStreams.put(userID, netStream);
72                         result = userID;
73                         netStream._key = userID;
74                         netStream.execute();
75                         System.err.println("Added new stream with predefined key: " + userID);
76                     }
77                 }
78             }
79         }
80         
81         return result;
82     }
83     
84     public boolean updateKey(long oldID, long newID) {
85         boolean result = false;
86         
87         if (!_tcpStreams.containsKey(newID)) {
88             if (_tcpStreams.containsKey(oldID)) {
89                 NetworkStream temp = _tcpStreams.get(oldID);
90                 temp._key = newID;
91                 _tcpStreams.put(newID, temp);
92                 System.err.println("Added new stream with key: " + newID);
93                 _tcpStreams.remove(oldID);
94                 
95                 result = true;
96             }
97         }
98         
99         return result;
100     }
101     
102     private long createTempKey() {
103         return TEMPCLIENT + -random.nextInt(1000000);
104     }
105
106     @Override
107     public boolean canCloseSocket(long key) {
108         boolean result = false;
109         
110         if (_tcpStreams.containsKey(key)) {
111             _tcpStreams.remove(key);
112             result = true;
113             System.err.println("Removed stream with key: " + key);
114         }
115         return result;
116     }
117     
118 }