Browse Source

Transport classes refactoring

master
Nikita 13 years ago
parent
commit
5736674829
  1. 38
      src/main/java/com/corundumstudio/socketio/transport/BaseTransport.java
  2. 9
      src/main/java/com/corundumstudio/socketio/transport/WebSocketTransport.java
  3. 17
      src/main/java/com/corundumstudio/socketio/transport/XHRPollingTransport.java

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

@ -0,0 +1,38 @@
/**
* 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.transport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import com.corundumstudio.socketio.CompositeIterable;
import com.corundumstudio.socketio.Disconnectable;
import com.corundumstudio.socketio.SocketIOClient;
public abstract class BaseTransport extends SimpleChannelUpstreamHandler implements Disconnectable {
protected Iterable<SocketIOClient> getAllClients(Collection<? extends BaseClient> clients) {
List<Iterable<SocketIOClient>> allClients = new ArrayList<Iterable<SocketIOClient>>(clients.size());
for (BaseClient client : clients) {
allClients.add(client.getAllChildClients());
}
return new CompositeIterable<SocketIOClient>(allClients);
}
}

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

@ -53,7 +53,7 @@ import com.corundumstudio.socketio.handler.AuthorizeHandler;
import com.corundumstudio.socketio.messages.PacketsMessage;
@Sharable
public class WebSocketTransport extends SimpleChannelUpstreamHandler implements Disconnectable {
public class WebSocketTransport extends BaseTransport {
public static final String NAME = "websocket";
@ -181,13 +181,8 @@ public class WebSocketTransport extends SimpleChannelUpstreamHandler implements
}
public Iterable<SocketIOClient> getAllClients() {
// TODO remove CPD!
Collection<WebSocketClient> clients = sessionId2Client.values();
List<Iterable<SocketIOClient>> allClients = new ArrayList<Iterable<SocketIOClient>>(clients.size());
for (WebSocketClient client : sessionId2Client.values()) {
allClients.add(client.getAllChildClients());
}
return new CompositeIterable<SocketIOClient>(allClients);
return getAllClients(clients);
}
}

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

@ -16,9 +16,7 @@
package com.corundumstudio.socketio.transport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -32,7 +30,6 @@ import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
@ -40,12 +37,9 @@ import org.jboss.netty.handler.codec.http.QueryStringDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.corundumstudio.socketio.CompositeIterable;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.Disconnectable;
import com.corundumstudio.socketio.DisconnectableHub;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOPipelineFactory;
import com.corundumstudio.socketio.ack.AckManager;
import com.corundumstudio.socketio.handler.AuthorizeHandler;
import com.corundumstudio.socketio.messages.PacketsMessage;
@ -60,13 +54,14 @@ import com.corundumstudio.socketio.scheduler.SchedulerKey;
import com.corundumstudio.socketio.scheduler.SchedulerKey.Type;
@Sharable
public class XHRPollingTransport extends SimpleChannelUpstreamHandler implements Disconnectable {
public class XHRPollingTransport extends BaseTransport {
public static final String NAME = "xhr-polling";
private final Logger log = LoggerFactory.getLogger(getClass());
private final Map<UUID, XHRPollingClient> sessionId2Client = new ConcurrentHashMap<UUID, XHRPollingClient>();
private final Map<UUID, XHRPollingClient> sessionId2Client =
new ConcurrentHashMap<UUID, XHRPollingClient>();
private final CancelableScheduler scheduler;
private final AckManager ackManager;
@ -222,11 +217,7 @@ public class XHRPollingTransport extends SimpleChannelUpstreamHandler implements
public Iterable<SocketIOClient> getAllClients() {
Collection<XHRPollingClient> clients = sessionId2Client.values();
List<Iterable<SocketIOClient>> allClients = new ArrayList<Iterable<SocketIOClient>>(clients.size());
for (XHRPollingClient client : sessionId2Client.values()) {
allClients.add(client.getAllChildClients());
}
return new CompositeIterable<SocketIOClient>(allClients);
return getAllClients(clients);
}
}
Loading…
Cancel
Save