Browse Source
Merge pull request #28 from RaduCirstoiu/master
Merge pull request #28 from RaduCirstoiu/master
Java 6 feature usage removal (Android compatibility)master
7 changed files with 305 additions and 9 deletions
-
4pom.xml
-
4src/main/java/com/corundumstudio/socketio/handler/AuthorizeHandler.java
-
6src/main/java/com/corundumstudio/socketio/namespace/Namespace.java
-
188src/main/java/com/corundumstudio/socketio/namespace/Namespace.java~
-
4src/main/java/com/corundumstudio/socketio/parser/JacksonJsonSupport.java
-
10src/main/java/com/corundumstudio/socketio/parser/Packet.java
-
98src/main/java/com/corundumstudio/socketio/utils/ConcurrentHashSet.java
@ -0,0 +1,188 @@ |
|||
/** |
|||
* 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.Queue; |
|||
import java.util.Set; |
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.ConcurrentLinkedQueue; |
|||
import java.util.concurrent.ConcurrentMap; |
|||
|
|||
import com.corundumstudio.socketio.AckRequest; |
|||
import com.corundumstudio.socketio.BroadcastOperations; |
|||
import com.corundumstudio.socketio.SocketIOClient; |
|||
import com.corundumstudio.socketio.SocketIONamespace; |
|||
import com.corundumstudio.socketio.listener.ConnectListener; |
|||
import com.corundumstudio.socketio.listener.DataListener; |
|||
import com.corundumstudio.socketio.listener.DisconnectListener; |
|||
import com.corundumstudio.socketio.parser.JsonSupport; |
|||
<<<<<<< HEAD |
|||
import com.corundumstudio.socketio.transport.NamespaceClient; |
|||
import com.corundumstudio.socketio.utils.ConcurrentHashSet; |
|||
======= |
|||
>>>>>>> e797a715fffed6621cb25e781922c7b1459889f9 |
|||
|
|||
public class Namespace implements SocketIONamespace { |
|||
|
|||
public static final String DEFAULT_NAME = ""; |
|||
|
|||
private final Set<SocketIOClient> clients = new ConcurrentHashSet<SocketIOClient>(); |
|||
private final ConcurrentMap<String, EventEntry<?>> eventListeners = |
|||
new ConcurrentHashMap<String, EventEntry<?>>(); |
|||
private final ConcurrentMap<Class<?>, Queue<DataListener<?>>> jsonObjectListeners = |
|||
new ConcurrentHashMap<Class<?>, Queue<DataListener<?>>>(); |
|||
private final Queue<DataListener<String>> messageListeners = new ConcurrentLinkedQueue<DataListener<String>>(); |
|||
private final Queue<ConnectListener> connectListeners = new ConcurrentLinkedQueue<ConnectListener>(); |
|||
private final Queue<DisconnectListener> disconnectListeners = new ConcurrentLinkedQueue<DisconnectListener>(); |
|||
|
|||
private final String name; |
|||
private final JsonSupport jsonSupport; |
|||
|
|||
public Namespace(String name, JsonSupport jsonSupport) { |
|||
super(); |
|||
this.name = name; |
|||
this.jsonSupport = jsonSupport; |
|||
} |
|||
|
|||
public void addClient(SocketIOClient client) { |
|||
clients.add(client); |
|||
} |
|||
|
|||
public String getName() { |
|||
return name; |
|||
} |
|||
|
|||
@Override |
|||
@SuppressWarnings({"unchecked", "rawtypes"}) |
|||
public <T> void addEventListener(String eventName, Class<T> eventClass, DataListener<T> listener) { |
|||
EventEntry entry = eventListeners.get(eventName); |
|||
if (entry == null) { |
|||
entry = new EventEntry<T>(eventClass); |
|||
EventEntry<?> oldEntry = eventListeners.putIfAbsent(eventName, entry); |
|||
if (oldEntry != null) { |
|||
entry = oldEntry; |
|||
} |
|||
} |
|||
entry.addListener(listener); |
|||
jsonSupport.addEventMapping(eventName, eventClass); |
|||
} |
|||
|
|||
@SuppressWarnings({"rawtypes", "unchecked"}) |
|||
public void onEvent(SocketIOClient client, String eventName, Object data, AckRequest ackRequest) { |
|||
EventEntry entry = eventListeners.get(eventName); |
|||
if (entry == null) { |
|||
return; |
|||
} |
|||
Queue<DataListener> listeners = entry.getListeners(); |
|||
for (DataListener dataListener : listeners) { |
|||
dataListener.onData(client, data, ackRequest); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public <T> void addJsonObjectListener(Class<T> clazz, DataListener<T> listener) { |
|||
Queue<DataListener<?>> queue = jsonObjectListeners.get(clazz); |
|||
if (queue == null) { |
|||
queue = new ConcurrentLinkedQueue<DataListener<?>>(); |
|||
Queue<DataListener<?>> oldQueue = jsonObjectListeners.putIfAbsent(clazz, queue); |
|||
if (oldQueue != null) { |
|||
queue = oldQueue; |
|||
} |
|||
} |
|||
queue.add(listener); |
|||
jsonSupport.addJsonClass(clazz); |
|||
} |
|||
|
|||
public void onJsonObject(SocketIOClient client, Object data, AckRequest ackRequest) { |
|||
Queue<DataListener<?>> queue = jsonObjectListeners.get(data.getClass()); |
|||
if (queue == null) { |
|||
return; |
|||
} |
|||
for (DataListener dataListener : queue) { |
|||
dataListener.onData(client, data, ackRequest); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void addDisconnectListener(DisconnectListener listener) { |
|||
disconnectListeners.add(listener); |
|||
} |
|||
|
|||
public void onDisconnect(SocketIOClient client) { |
|||
for (DisconnectListener listener : disconnectListeners) { |
|||
listener.onDisconnect(client); |
|||
} |
|||
clients.remove(client); |
|||
} |
|||
|
|||
@Override |
|||
public void addConnectListener(ConnectListener listener) { |
|||
connectListeners.add(listener); |
|||
} |
|||
|
|||
public void onConnect(SocketIOClient client) { |
|||
for (ConnectListener listener : connectListeners) { |
|||
listener.onConnect(client); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void addMessageListener(DataListener<String> listener) { |
|||
messageListeners.add(listener); |
|||
} |
|||
|
|||
public Queue<DataListener<String>> getMessageListeners() { |
|||
return messageListeners; |
|||
} |
|||
|
|||
public void onMessage(SocketIOClient client, String data, AckRequest ackRequest) { |
|||
for (DataListener<String> listener : messageListeners) { |
|||
listener.onData(client, data, ackRequest); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public BroadcastOperations getBroadcastOperations() { |
|||
return new BroadcastOperations(clients); |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public int hashCode() { |
|||
final int prime = 31; |
|||
int result = 1; |
|||
result = prime * result + ((name == null) ? 0 : name.hashCode()); |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
public boolean equals(Object obj) { |
|||
if (this == obj) |
|||
return true; |
|||
if (obj == null) |
|||
return false; |
|||
if (getClass() != obj.getClass()) |
|||
return false; |
|||
Namespace other = (Namespace) obj; |
|||
if (name == null) { |
|||
if (other.name != null) |
|||
return false; |
|||
} else if (!name.equals(other.name)) |
|||
return false; |
|||
return true; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,98 @@ |
|||
/** |
|||
* 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.utils; |
|||
|
|||
import java.util.AbstractSet; |
|||
import java.util.Collection; |
|||
import java.util.Iterator; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
|
|||
/** |
|||
* Adapter for ConcurrentHashMap, a-la jdk 1.6+ Collections.newSetFromMap(new ConcurrentHashMap....) idiom |
|||
* @author radu.cirstoiu@gmail.com |
|||
*/ |
|||
public class ConcurrentHashSet<E> extends AbstractSet<E> implements Set<E> { |
|||
private final Map<E, Boolean> backingMap; |
|||
private final Set<E> backingMapSet; |
|||
|
|||
public ConcurrentHashSet() { |
|||
backingMap = (new ConcurrentHashMap<E, Boolean>()); |
|||
backingMapSet = backingMap.keySet(); |
|||
} |
|||
|
|||
public int size() { |
|||
return backingMap.size(); |
|||
} |
|||
|
|||
public boolean isEmpty() { |
|||
return backingMap.isEmpty(); |
|||
} |
|||
|
|||
public boolean contains(Object o) { |
|||
return backingMap.containsKey(o); |
|||
} |
|||
|
|||
public Iterator<E> iterator() { |
|||
return backingMapSet.iterator(); |
|||
} |
|||
|
|||
public void clear() { |
|||
backingMap.clear(); |
|||
} |
|||
|
|||
public Object[] toArray() { |
|||
return backingMapSet.toArray(); |
|||
} |
|||
|
|||
public <T> T[] toArray(T[] a) { |
|||
return backingMapSet.toArray(a); |
|||
} |
|||
|
|||
public boolean add(E e) { |
|||
return backingMap.put(e, Boolean.TRUE) == null; |
|||
} |
|||
|
|||
public boolean remove(Object o) { |
|||
return backingMap.remove(o) != null; |
|||
} |
|||
|
|||
public boolean containsAll(Collection<?> c) { |
|||
return backingMapSet.containsAll(c); |
|||
} |
|||
|
|||
public boolean removeAll(Collection<?> c) { |
|||
return backingMapSet.removeAll(c); |
|||
} |
|||
|
|||
public boolean retainAll(Collection<?> c) { |
|||
return backingMapSet.retainAll(c); |
|||
} |
|||
|
|||
public String toString() { |
|||
return backingMapSet.toString(); |
|||
} |
|||
|
|||
public int hashCode() { |
|||
return backingMapSet.hashCode(); |
|||
} |
|||
|
|||
public boolean equals(Object o) { |
|||
return o == this || backingMapSet.equals(o); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue