Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored UUID into SessionID interface #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 125 additions & 77 deletions src/main/java/com/corundumstudio/socketio/Configuration.java

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions src/main/java/com/corundumstudio/socketio/DefaultSessionID.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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.UUID;

/**
* Default SessionID implementation based on UUID class.
*/
public class DefaultSessionID implements SessionID {

private UUID uuid;

public DefaultSessionID(UUID id) {
this.uuid = id;
}

@Override
public String toString() {
return this.uuid.toString();
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((uuid == null) ? 0 : uuid.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;
DefaultSessionID other = (DefaultSessionID) obj;
if (uuid == null) {
if (other.uuid != null)
return false;
} else if (!uuid.equals(other.uuid))
return false;
return true;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/corundumstudio/socketio/HandshakeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class HandshakeData implements Serializable {
private String url;
private Map<String, List<String>> urlParams;
private boolean xdomain;
private SessionID sessionId;

public HandshakeData() {
}
Expand Down Expand Up @@ -84,4 +85,12 @@ public String getSingleUrlParam(String name) {
return null;
}

public void setSessionId(SessionID sessionId) {
this.sessionId = sessionId;
}

public SessionID getSessionId() {
return this.sessionId;
}

}
21 changes: 21 additions & 0 deletions src/main/java/com/corundumstudio/socketio/SessionID.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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;

public interface SessionID {

String toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@
import com.corundumstudio.socketio.handler.EncoderHandler;
import com.corundumstudio.socketio.handler.InPacketHandler;
import com.corundumstudio.socketio.handler.PacketListener;
import com.corundumstudio.socketio.handler.SessionIDFactory;
import com.corundumstudio.socketio.handler.WrongUrlHandler;
import com.corundumstudio.socketio.namespace.NamespacesHub;
import com.corundumstudio.socketio.protocol.JsonSupport;
import com.corundumstudio.socketio.protocol.PacketDecoder;
import com.corundumstudio.socketio.protocol.PacketEncoder;
import com.corundumstudio.socketio.protocol.JsonSupport;
import com.corundumstudio.socketio.scheduler.CancelableScheduler;
import com.corundumstudio.socketio.scheduler.HashedWheelScheduler;
import com.corundumstudio.socketio.store.StoreFactory;
import com.corundumstudio.socketio.store.pubsub.DisconnectMessage;
import com.corundumstudio.socketio.store.pubsub.PubSubStore;
import com.corundumstudio.socketio.transport.WebSocketTransport;
import com.corundumstudio.socketio.transport.PollingTransport;
import com.corundumstudio.socketio.transport.WebSocketTransport;

public class SocketIOChannelInitializer extends ChannelInitializer<Channel> implements DisconnectableHub {

Expand Down Expand Up @@ -94,7 +95,6 @@ public void handlerAdded(ChannelHandlerContext ctx) {

public void start(Configuration configuration, NamespacesHub namespacesHub) {
this.configuration = configuration;

ackManager = new AckManager(scheduler);

JsonSupport jsonSupport = configuration.getJsonSupport();
Expand All @@ -114,9 +114,10 @@ public void start(Configuration configuration, NamespacesHub namespacesHub) {
StoreFactory factory = configuration.getStoreFactory();
factory.init(namespacesHub, authorizeHandler, jsonSupport);

authorizeHandler = new AuthorizeHandler(connectPath, scheduler, configuration, namespacesHub, factory, this, ackManager, clientsBox);
xhrPollingTransport = new PollingTransport(decoder, authorizeHandler, clientsBox);
webSocketTransport = new WebSocketTransport(isSsl, authorizeHandler, configuration, scheduler, clientsBox);
SessionIDFactory sessionIDFactory = configuration.getSessionIDFactory();
authorizeHandler = new AuthorizeHandler(connectPath, scheduler, configuration, namespacesHub, factory, this, ackManager, clientsBox, sessionIDFactory);
xhrPollingTransport = new PollingTransport(decoder, authorizeHandler, clientsBox, sessionIDFactory);
webSocketTransport = new WebSocketTransport(isSsl, authorizeHandler, configuration, scheduler, clientsBox, sessionIDFactory);

PacketListener packetListener = new PacketListener(ackManager, namespacesHub, xhrPollingTransport, scheduler);

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/corundumstudio/socketio/SocketIOClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.net.SocketAddress;
import java.util.Set;
import java.util.UUID;

import com.corundumstudio.socketio.protocol.Packet;
import com.corundumstudio.socketio.store.Store;
Expand Down Expand Up @@ -68,11 +67,11 @@ public interface SocketIOClient extends ClientOperations, Store {
SocketIONamespace getNamespace();

/**
* Client session id, uses {@link UUID} object
* Client session id, uses {@link SessionID} object
*
* @return - session id
*/
UUID getSessionId();
SessionID getSessionId();

/**
* Get client remote address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.corundumstudio.socketio;

import java.util.Collection;
import java.util.UUID;

import com.corundumstudio.socketio.listener.ClientListeners;

Expand All @@ -41,6 +40,6 @@ public interface SocketIONamespace extends ClientListeners {
* @param uuid
* @return
*/
SocketIOClient getClient(UUID uuid);
SocketIOClient getClient(SessionID uuid);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -84,7 +83,7 @@ public Collection<SocketIOClient> getAllClients() {
* @param uuid
* @return
*/
public SocketIOClient getClient(UUID uuid) {
public SocketIOClient getClient(SessionID uuid) {
return namespacesHub.get(Namespace.DEFAULT_NAME).getClient(uuid);
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/corundumstudio/socketio/ack/AckManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -30,6 +29,7 @@
import com.corundumstudio.socketio.Disconnectable;
import com.corundumstudio.socketio.MultiTypeAckCallback;
import com.corundumstudio.socketio.MultiTypeArgs;
import com.corundumstudio.socketio.SessionID;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.handler.ClientHead;
import com.corundumstudio.socketio.protocol.Packet;
Expand Down Expand Up @@ -70,7 +70,7 @@ public void initAckIndex(long index) {

private final Logger log = LoggerFactory.getLogger(getClass());

private final Map<UUID, AckEntry> ackEntries = new ConcurrentHashMap<UUID, AckEntry>();
private final Map<SessionID, AckEntry> ackEntries = new ConcurrentHashMap<SessionID, AckEntry>();

private final CancelableScheduler scheduler;

Expand All @@ -79,12 +79,12 @@ public AckManager(CancelableScheduler scheduler) {
this.scheduler = scheduler;
}

public void initAckIndex(UUID sessionId, long index) {
public void initAckIndex(SessionID sessionId, long index) {
AckEntry ackEntry = getAckEntry(sessionId);
ackEntry.initAckIndex(index);
}

private AckEntry getAckEntry(UUID sessionId) {
private AckEntry getAckEntry(SessionID sessionId) {
AckEntry ackEntry = ackEntries.get(sessionId);
if (ackEntry == null) {
ackEntry = new AckEntry();
Expand Down Expand Up @@ -120,7 +120,7 @@ public void onAck(SocketIOClient client, Packet packet) {
}
}

private AckCallback removeCallback(UUID sessionId, long index) {
private AckCallback removeCallback(SessionID sessionId, long index) {
AckEntry ackEntry = ackEntries.get(sessionId);
// may be null if client disconnected
// before timeout occurs
Expand All @@ -130,12 +130,12 @@ private AckCallback removeCallback(UUID sessionId, long index) {
return null;
}

public AckCallback<?> getCallback(UUID sessionId, long index) {
public AckCallback<?> getCallback(SessionID sessionId, long index) {
AckEntry ackEntry = getAckEntry(sessionId);
return ackEntry.getAckCallback(index);
}

public long registerAck(UUID sessionId, AckCallback callback) {
public long registerAck(SessionID sessionId, AckCallback callback) {
AckEntry ackEntry = getAckEntry(sessionId);
ackEntry.initAckIndex(0);
long index = ackEntry.addAckCallback(callback);
Expand All @@ -149,7 +149,7 @@ public long registerAck(UUID sessionId, AckCallback callback) {
return index;
}

private void scheduleTimeout(final long index, final UUID sessionId, AckCallback callback) {
private void scheduleTimeout(final long index, final SessionID sessionId, AckCallback callback) {
if (callback.getTimeout() == -1) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package com.corundumstudio.socketio.ack;

import java.util.UUID;

import com.corundumstudio.socketio.SessionID;
import com.corundumstudio.socketio.scheduler.SchedulerKey;

public class AckSchedulerKey extends SchedulerKey {

private final long index;

public AckSchedulerKey(Type type, UUID sessionId, long index) {
public AckSchedulerKey(Type type, SessionID sessionId, long index) {
super(type, sessionId);
this.index = index;
}
Expand Down
Loading