/*
* 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;
result = tempKey;
netStream._key = tempKey;
netStream.execute();
- System.err.println("Added new stream with key: " + tempKey);
}
else {
if ((userID < 0 && userID > TEMPCLIENT) || userID > 0) {
result = userID;
netStream._key = userID;
netStream.execute();
- System.err.println("Added new stream with predefined key: " + userID);
}
}
}
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;
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;
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;
if (_tcpStreams.containsKey(key)) {
_tcpStreams.remove(key);
result = true;
- System.err.println("Removed stream with key: " + key);
}
return result;
}