com.corundumstudio.socketio.namespace.Namespace Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of netty-socketio Show documentation
Show all versions of netty-socketio Show documentation
Socket.IO server implemented on Java
/**
* Copyright 2012 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import com.corundumstudio.socketio.AckMode;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.BroadcastOperations;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.MultiTypeArgs;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.annotation.ScannerEngine;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;
import com.corundumstudio.socketio.listener.ExceptionListener;
import com.corundumstudio.socketio.listener.MultiTypeEventListener;
import com.corundumstudio.socketio.protocol.JsonSupport;
import com.corundumstudio.socketio.protocol.Packet;
import com.corundumstudio.socketio.store.StoreFactory;
import com.corundumstudio.socketio.store.pubsub.JoinLeaveMessage;
import com.corundumstudio.socketio.store.pubsub.PubSubStore;
import com.corundumstudio.socketio.transport.NamespaceClient;
/**
* Hub object for all clients in one namespace.
* Namespace shares by different namespace-clients.
*
* @see com.corundumstudio.socketio.transport.NamespaceClient
*/
public class Namespace implements SocketIONamespace {
public static final String DEFAULT_NAME = "";
private final ScannerEngine engine = new ScannerEngine();
private final ConcurrentMap> eventListeners =
new ConcurrentHashMap>();
private final Queue connectListeners = new ConcurrentLinkedQueue();
private final Queue disconnectListeners = new ConcurrentLinkedQueue();
private final Map allClients = new ConcurrentHashMap();
private final ConcurrentMap> roomClients = new ConcurrentHashMap>();
private final ConcurrentMap> clientRooms = new ConcurrentHashMap>();
private final String name;
private final AckMode ackMode;
private final JsonSupport jsonSupport;
private final StoreFactory storeFactory;
private final ExceptionListener exceptionListener;
public Namespace(String name, Configuration configuration) {
super();
this.name = name;
this.jsonSupport = configuration.getJsonSupport();
this.storeFactory = configuration.getStoreFactory();
this.exceptionListener = configuration.getExceptionListener();
this.ackMode = configuration.getAckMode();
}
public void addClient(SocketIOClient client) {
allClients.put(client.getSessionId(), client);
}
public String getName() {
return name;
}
@Override
public void addMultiTypeEventListener(String eventName, MultiTypeEventListener listener,
Class>... eventClass) {
EventEntry entry = eventListeners.get(eventName);
if (entry == null) {
entry = new EventEntry();
EventEntry> oldEntry = eventListeners.putIfAbsent(eventName, entry);
if (oldEntry != null) {
entry = oldEntry;
}
}
entry.addListener(listener);
jsonSupport.addEventMapping(eventName, eventClass);
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void addEventListener(String eventName, Class eventClass, DataListener listener) {
EventEntry entry = eventListeners.get(eventName);
if (entry == null) {
entry = new EventEntry();
EventEntry> oldEntry = eventListeners.putIfAbsent(eventName, entry);
if (oldEntry != null) {
entry = oldEntry;
}
}
entry.addListener(listener);
jsonSupport.addEventMapping(eventName, eventClass);
}
@SuppressWarnings({"rawtypes", "unchecked"})
public void onEvent(NamespaceClient client, String eventName, List