Browse Source

SocketIOServer.getAllClients method fixed. getClient and getAllNamespaces methods added to SocketIOServer. getAllClients and getClient methods added to SocketIONamespace. #113, #99

master
Nikita 11 years ago
parent
commit
1b6ebfce19
  1. 12
      src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java
  2. 18
      src/main/java/com/corundumstudio/socketio/SocketIONamespace.java
  3. 26
      src/main/java/com/corundumstudio/socketio/SocketIOServer.java
  4. 10
      src/main/java/com/corundumstudio/socketio/namespace/Namespace.java
  5. 24
      src/main/java/com/corundumstudio/socketio/namespace/NamespacesHub.java
  6. 15
      src/main/java/com/corundumstudio/socketio/transport/BaseTransport.java
  7. 7
      src/main/java/com/corundumstudio/socketio/transport/WebSocketTransport.java
  8. 7
      src/main/java/com/corundumstudio/socketio/transport/XHRPollingTransport.java

12
src/main/java/com/corundumstudio/socketio/SocketIOChannelInitializer.java

@ -25,7 +25,6 @@ import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.ssl.SslHandler;
import java.security.KeyStore;
import java.util.Collection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
@ -44,8 +43,6 @@ import com.corundumstudio.socketio.handler.PacketHandler;
import com.corundumstudio.socketio.handler.PacketListener;
import com.corundumstudio.socketio.handler.ResourceHandler;
import com.corundumstudio.socketio.handler.WrongUrlHandler;
import com.corundumstudio.socketio.misc.CompositeIterable;
import com.corundumstudio.socketio.misc.IterableCollection;
import com.corundumstudio.socketio.namespace.NamespacesHub;
import com.corundumstudio.socketio.parser.Decoder;
import com.corundumstudio.socketio.parser.Encoder;
@ -142,15 +139,6 @@ public class SocketIOChannelInitializer extends ChannelInitializer<Channel> impl
wrongUrlHandler = new WrongUrlHandler();
}
public Collection<SocketIOClient> getAllClients() {
// TODO refactor to transport registry
Iterable<SocketIOClient> xhrClients = xhrPollingTransport.getAllClients();
Iterable<SocketIOClient> webSocketClients = webSocketTransport.getAllClients();
Iterable<SocketIOClient> flashSocketClients = flashSocketTransport.getAllClients();
CompositeIterable<SocketIOClient> mainIterable = new CompositeIterable<SocketIOClient>(xhrClients, webSocketClients, flashSocketClients);
return new IterableCollection<SocketIOClient>(mainIterable);
}
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();

18
src/main/java/com/corundumstudio/socketio/SocketIONamespace.java

@ -15,6 +15,9 @@
*/
package com.corundumstudio.socketio;
import java.util.Collection;
import java.util.UUID;
import com.corundumstudio.socketio.listener.ClientListeners;
/**
@ -25,4 +28,19 @@ public interface SocketIONamespace extends ClientListeners {
BroadcastOperations getBroadcastOperations();
/**
* Get all clients connected to namespace
*
* @return
*/
Collection<SocketIOClient> getAllClients();
/**
* Get client by uuid connected to namespace
*
* @param uuid
* @return
*/
SocketIOClient getClient(UUID uuid);
}

26
src/main/java/com/corundumstudio/socketio/SocketIOServer.java

@ -24,6 +24,7 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -67,16 +68,35 @@ public class SocketIOServer implements ClientListeners {
}
/**
* Get all clients
* Get all clients connected to default namespace
*
* @return clients collection
*/
public Collection<SocketIOClient> getAllClients() {
return pipelineFactory.getAllClients();
return namespacesHub.get(Namespace.DEFAULT_NAME).getAllClients();
}
/**
* Get client by uuid from default namespace
*
* @param uuid
* @return
*/
public SocketIOClient getClient(UUID uuid) {
return namespacesHub.get(Namespace.DEFAULT_NAME).getClient(uuid);
}
/**
* Get all namespaces
*
* @return namespaces collection
*/
public Collection<SocketIONamespace> getAllNamespaces() {
return namespacesHub.getAllNamespaces();
}
public BroadcastOperations getBroadcastOperations() {
return new BroadcastOperations(pipelineFactory.getAllClients(), configCopy.getStoreFactory());
return new BroadcastOperations(getAllClients(), configCopy.getStoreFactory());
}
/**

10
src/main/java/com/corundumstudio/socketio/namespace/Namespace.java

@ -16,6 +16,7 @@
package com.corundumstudio.socketio.namespace;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -368,9 +369,12 @@ public class Namespace implements SocketIONamespace {
return result;
}
// Utility function to check if there are anymore clients in namespace
public boolean isEmpty(){
return allClients.isEmpty();
public Collection<SocketIOClient> getAllClients() {
return allClients.values();
}
public SocketIOClient getClient(UUID uuid) {
return allClients.get(uuid);
}
}

24
src/main/java/com/corundumstudio/socketio/namespace/NamespacesHub.java

@ -16,17 +16,19 @@
package com.corundumstudio.socketio.namespace;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.misc.CompositeIterable;
public class NamespacesHub {
private final ConcurrentMap<String, Namespace> namespaces = new ConcurrentHashMap<String, Namespace>();
private final ConcurrentMap<String, SocketIONamespace> namespaces = new ConcurrentHashMap<String, SocketIONamespace>();
private final Configuration configuration;
public NamespacesHub(Configuration configuration) {
@ -34,10 +36,10 @@ public class NamespacesHub {
}
public Namespace create(String name) {
Namespace namespace = namespaces.get(name);
Namespace namespace = (Namespace) namespaces.get(name);
if (namespace == null) {
namespace = new Namespace(name, configuration);
Namespace oldNamespace = namespaces.putIfAbsent(name, namespace);
Namespace oldNamespace = (Namespace) namespaces.putIfAbsent(name, namespace);
if (oldNamespace != null) {
namespace = oldNamespace;
}
@ -47,20 +49,26 @@ public class NamespacesHub {
public Iterable<SocketIOClient> getRoomClients(String room) {
List<Iterable<SocketIOClient>> allClients = new ArrayList<Iterable<SocketIOClient>>();
for (Namespace namespace : namespaces.values()) {
Iterable<SocketIOClient> clients = namespace.getRoomClients(room);
for (SocketIONamespace namespace : namespaces.values()) {
Iterable<SocketIOClient> clients = ((Namespace)namespace).getRoomClients(room);
allClients.add(clients);
}
return new CompositeIterable<SocketIOClient>(allClients);
}
public Namespace get(String name) {
return namespaces.get(name);
return (Namespace) namespaces.get(name);
}
public void remove(String name) {
Namespace namespace = namespaces.remove(name);
namespace.getBroadcastOperations().disconnect();
SocketIONamespace namespace = namespaces.remove(name);
if (namespace != null) {
namespace.getBroadcastOperations().disconnect();
}
}
public Collection<SocketIONamespace> getAllNamespaces() {
return namespaces.values();
}
}

15
src/main/java/com/corundumstudio/socketio/transport/BaseTransport.java

@ -17,22 +17,9 @@ package com.corundumstudio.socketio.transport;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.corundumstudio.socketio.Disconnectable;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.misc.CompositeIterable;
@Deprecated
public abstract class BaseTransport extends ChannelInboundHandlerAdapter implements Disconnectable {
protected Iterable<SocketIOClient> getAllClients(Collection<? extends MainBaseClient> clients) {
List<Iterable<SocketIOClient>> allClients = new ArrayList<Iterable<SocketIOClient>>(clients.size());
for (MainBaseClient client : clients) {
allClients.add(client.getAllChildClients());
}
return new CompositeIterable<SocketIOClient>(allClients);
}
}

7
src/main/java/com/corundumstudio/socketio/transport/WebSocketTransport.java

@ -30,7 +30,6 @@ import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -40,7 +39,6 @@ import org.slf4j.LoggerFactory;
import com.corundumstudio.socketio.DisconnectableHub;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOChannelInitializer;
import com.corundumstudio.socketio.Transport;
import com.corundumstudio.socketio.ack.AckManager;
@ -204,9 +202,4 @@ public class WebSocketTransport extends BaseTransport {
}
}
public Iterable<SocketIOClient> getAllClients() {
Collection<WebSocketClient> clients = sessionId2Client.values();
return getAllClients(clients);
}
}

7
src/main/java/com/corundumstudio/socketio/transport/XHRPollingTransport.java

@ -27,7 +27,6 @@ import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.QueryStringDecoder;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -39,7 +38,6 @@ import org.slf4j.LoggerFactory;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.DisconnectableHub;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.Transport;
import com.corundumstudio.socketio.ack.AckManager;
import com.corundumstudio.socketio.handler.AuthorizeHandler;
@ -221,9 +219,4 @@ public class XHRPollingTransport extends BaseTransport {
}
}
public Iterable<SocketIOClient> getAllClients() {
Collection<XHRPollingClient> clients = sessionId2Client.values();
return getAllClients(clients);
}
}
Loading…
Cancel
Save