Browse Source

Connected clients size method. Issue #57

master
Nikita 12 years ago
parent
commit
c41c2ec7b6
  1. 6
      src/main/java/com/corundumstudio/socketio/CompositeIterable.java
  2. 47
      src/main/java/com/corundumstudio/socketio/IterableCollection.java
  3. 6
      src/main/java/com/corundumstudio/socketio/SocketIOPipelineFactory.java
  4. 17
      src/main/java/com/corundumstudio/socketio/transport/BaseClient.java
  5. 13
      src/main/java/com/corundumstudio/socketio/transport/NamespaceClient.java

6
src/main/java/com/corundumstudio/socketio/CompositeIterable.java

@ -35,6 +35,11 @@ public class CompositeIterable<T> implements Iterable<T>, Iterator<T> {
this.iterables = iterables;
}
public CompositeIterable(CompositeIterable<T> iterable) {
this.iterables = iterable.iterables;
this.iterablesList = iterable.iterablesList;
}
@Override
public Iterator<T> iterator() {
List<Iterator<T>> iterators = new ArrayList<Iterator<T>>();
@ -48,6 +53,7 @@ public class CompositeIterable<T> implements Iterable<T>, Iterator<T> {
}
}
listIterator = iterators.iterator();
currentIterator = null;
return this;
}

47
src/main/java/com/corundumstudio/socketio/IterableCollection.java

@ -0,0 +1,47 @@
/**
* 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;
import java.util.AbstractCollection;
import java.util.Iterator;
public class IterableCollection<T> extends AbstractCollection<T> {
private final Iterable<T> iterable;
private final Iterable<T> sizeIterable;
public IterableCollection(CompositeIterable<T> iterable) {
this.iterable = iterable;
this.sizeIterable = new CompositeIterable<T>(iterable);
}
@Override
public Iterator<T> iterator() {
return iterable.iterator();
}
@Override
public int size() {
Iterator<T> iterator = sizeIterable.iterator();
int count = 0;
while (iterator.hasNext()) {
iterator.next();
count++;
}
return count;
}
}

6
src/main/java/com/corundumstudio/socketio/SocketIOPipelineFactory.java

@ -20,6 +20,7 @@ import static org.jboss.netty.channel.Channels.pipeline;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.Security;
import java.util.Collection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
@ -118,12 +119,13 @@ public class SocketIOPipelineFactory implements ChannelPipelineFactory, Disconne
socketIOEncoder = new SocketIOEncoder(encoder);
}
public Iterable<SocketIOClient> getAllClients() {
public Collection<SocketIOClient> getAllClients() {
// TODO refactor to transport registry
Iterable<SocketIOClient> xhrClients = xhrPollingTransport.getAllClients();
Iterable<SocketIOClient> webSocketClients = webSocketTransport.getAllClients();
Iterable<SocketIOClient> flashSocketClients = flashSocketTransport.getAllClients();
return new CompositeIterable<SocketIOClient>(xhrClients, webSocketClients, flashSocketClients);
CompositeIterable<SocketIOClient> mainIterable = new CompositeIterable<SocketIOClient>(xhrClients, webSocketClients, flashSocketClients);
return new IterableCollection<SocketIOClient>(mainIterable);
}
public ChannelPipeline getPipeline() throws Exception {

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

@ -36,8 +36,8 @@ import com.corundumstudio.socketio.parser.PacketType;
/**
* Base class for main client.
*
* Each main client can have multiple namespace clients,
* when all namespace clients has disconnected then main client disconnects too.
* Each main client can have multiple namespace clients, when all namespace
* clients has disconnected then main client disconnects too.
*
*
*/
@ -51,7 +51,8 @@ public abstract class BaseClient {
private final Transport transport;
private Channel channel;
public BaseClient(UUID sessionId, AckManager ackManager, DisconnectableHub disconnectable, Transport transport) {
public BaseClient(UUID sessionId, AckManager ackManager, DisconnectableHub disconnectable,
Transport transport) {
this.sessionId = sessionId;
this.ackManager = ackManager;
this.disconnectable = disconnectable;
@ -65,7 +66,7 @@ public abstract class BaseClient {
public abstract ChannelFuture send(Packet packet);
public void removeChildClient(SocketIOClient client) {
namespaceClients.remove((Namespace)client.getNamespace());
namespaceClients.remove((Namespace) client.getNamespace());
if (namespaceClients.isEmpty()) {
disconnectable.onDisconnect(this);
}
@ -113,11 +114,11 @@ public abstract class BaseClient {
}
Channel getChannel() {
return channel;
}
return channel;
}
void setChannel(Channel channel) {
this.channel = channel;
}
this.channel = channel;
}
}

13
src/main/java/com/corundumstudio/socketio/transport/NamespaceClient.java

@ -46,12 +46,12 @@ public class NamespaceClient implements SocketIOClient {
return baseClient.getTransport();
}
@Override
public boolean isChannelOpen() {
return baseClient.getChannel().isOpen();
}
@Override
public boolean isChannelOpen() {
return baseClient.getChannel().isOpen();
}
@Override
@Override
public Namespace getNamespace() {
return namespace;
}
@ -142,7 +142,8 @@ public class NamespaceClient implements SocketIOClient {
final int prime = 31;
int result = 1;
result = prime * result + ((getSessionId() == null) ? 0 : getSessionId().hashCode());
result = prime * result + ((getNamespace().getName() == null) ? 0 : getNamespace().getName().hashCode());
result = prime * result
+ ((getNamespace().getName() == null) ? 0 : getNamespace().getName().hashCode());
return result;
}

Loading…
Cancel
Save