Added ability to add/remove custom connections to the server backed,

to allow for custom connection types (such as web-based) that we want
 our server to be able to manage + dispatch listeners
This commit is contained in:
nathan 2016-03-25 17:54:58 +01:00
parent fa07a1b56a
commit ad34679e71
2 changed files with 67 additions and 17 deletions

View File

@ -476,23 +476,7 @@ class ConnectionManager<C extends Connection> implements ListenerBridge, ISessio
@Override
public
void connectionConnected(final C connection) {
// synchronized is used here to ensure the "single writer principle", and make sure that ONLY one thread at a time can enter this
// section. Because of this, we can have unlimited reader threads all going at the same time, without contention (which is our
// use-case 99% of the time)
synchronized (singleWriterLock2) {
// access a snapshot of the subscriptions (single-writer-principle)
ConcurrentEntry head = connectionsREF.get(this);
if (!connectionEntries.containsKey(connection)) {
head = new ConcurrentEntry<Object>(connection, head);
connectionEntries.put(connection, head);
// save this snapshot back to the original (single writer principle)
connectionsREF.lazySet(this, head);
}
}
addConnection(connection);
final IdentityMap<Type, ConcurrentIterator> listeners = listenersREF.get(this);
@ -593,6 +577,45 @@ class ConnectionManager<C extends Connection> implements ListenerBridge, ISessio
removeListenerManager(connection);
}
removeConnection(connection);
}
/**
* Adds a custom connection to the server.
* <p>
* This should only be used in situations where there can be DIFFERENT types of connections (such as a 'web-based' connection) and
* you want *this* server instance to manage listeners + message dispatch
*
* @param connection the connection to add
*/
void addConnection(final C connection) {
// synchronized is used here to ensure the "single writer principle", and make sure that ONLY one thread at a time can enter this
// section. Because of this, we can have unlimited reader threads all going at the same time, without contention (which is our
// use-case 99% of the time)
synchronized (singleWriterLock2) {
// access a snapshot of the subscriptions (single-writer-principle)
ConcurrentEntry head = connectionsREF.get(this);
if (!connectionEntries.containsKey(connection)) {
head = new ConcurrentEntry<Object>(connection, head);
connectionEntries.put(connection, head);
// save this snapshot back to the original (single writer principle)
connectionsREF.lazySet(this, head);
}
}
}
/**
* Removes a custom connection to the server.
* <p>
* This should only be used in situations where there can be DIFFERENT types of connections (such as a 'web-based' connection) and
* you want *this* server instance to manage listeners + message dispatch
*
* @param connection the connection to remove
*/
void removeConnection(C connection) {
// synchronized is used here to ensure the "single writer principle", and make sure that ONLY one thread at a time can enter this
// section. Because of this, we can have unlimited reader threads all going at the same time, without contention (which is our
// use-case 99% of the time)
@ -619,6 +642,7 @@ class ConnectionManager<C extends Connection> implements ListenerBridge, ISessio
}
}
/**
* Returns a non-modifiable list of active connections. This is extremely slow, and not recommended!
*/

View File

@ -70,4 +70,30 @@ class EndPointServer<C extends Connection> extends EndPoint<C> {
void removeListenerManager(final C connection) {
this.connectionManager.removeListenerManager(connection);
}
/**
* Adds a custom connection to the server.
* <p>
* This should only be used in situations where there can be DIFFERENT types of connections (such as a 'web-based' connection) and
* you want *this* server instance to manage listeners + message dispatch
*
* @param connection the connection to add
*/
public
void addConnection(C connection) {
connectionManager.addConnection(connection);
}
/**
* Removes a custom connection to the server.
* <p>
* This should only be used in situations where there can be DIFFERENT types of connections (such as a 'web-based' connection) and
* you want *this* server instance to manage listeners + message dispatch
*
* @param connection the connection to remove
*/
public
void removeConnection(C connection) {
connectionManager.addConnection(connection);
}
}