Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
only allow a feed to have one connection open
Browse files Browse the repository at this point in the history
  • Loading branch information
jmazzitelli committed Jul 14, 2015
1 parent c5b23e5 commit 6389c32
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.websocket.CloseReason;
import javax.websocket.Session;

/**
Expand Down Expand Up @@ -57,18 +58,43 @@ public Session getSession(String feedId) {
}

@Lock(LockType.WRITE)
public void addSession(String feedId, Session feedSession) {
// TODO what happens if a feed already has a session open?
this.sessions.put(feedId, feedSession);
MsgLogger.LOG.infof("A feed session has been added [%s]. There are now [%d] connected feeds",
feedId, this.sessions.size());
public void addSession(String feedId, Session newSession) {
Session oldSession = this.sessions.putIfAbsent(feedId, newSession);
if (oldSession == null) {
MsgLogger.LOG.infof("A feed session has been added [%s]. There are now [%d] connected feeds",
feedId, this.sessions.size());
} else {
// a feed already had a session open, cannot open more than one
try {
MsgLogger.LOG.errorClosingExtraFeedSession(feedId);
newSession.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY,
"Cannot have multiple sessions open, the new one will be closed"));
} catch (Exception e) {
MsgLogger.LOG.errorCannotCloseExtraFeedSession(feedId, e);
}
}
}

@Lock(LockType.WRITE)
public void removeSession(String feedId) {
this.sessions.remove(feedId);
MsgLogger.LOG.infof("A feed session has been removed [%s]. There are now [%d] connected feeds",
feedId, this.sessions.size());
public void removeSession(String feedId, Session doomedSession) {
boolean removed = false;

// If no session was passed in, remove any session associated with the given feed.
// If a session was passed in, only remove it if the feedId is associated with that session.
// This is to support the need to close extra sessions a feed might have created.
if (doomedSession == null) {
removed = this.sessions.remove(feedId) != null;
} else {
Session existingSession = this.sessions.get(feedId);
if (existingSession != null && existingSession.getId().equals(doomedSession.getId())) {
removed = this.sessions.remove(feedId) != null;
}
}

if (removed) {
MsgLogger.LOG.infof("A feed session has been removed [%s]. There are now [%d] connected feeds",
feedId, this.sessions.size());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class FeedCommWebSocket {

@OnOpen
public void feedSessionOpen(Session session, @PathParam("feedId") String feedId) {
MsgLogger.LOG.infof("Feed [%s] connected", feedId);
MsgLogger.LOG.infof("Feed [%s] session opened", feedId);
connectedFeeds.addSession(feedId, session);
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public String feedMessage(String commandAndJsonStr, Session session, @PathParam(

@OnClose
public void feedSessionClose(Session session, CloseReason reason, @PathParam("feedId") String feedId) {
MsgLogger.LOG.infof("Feed [%s] closed session. Reason=[%s]", feedId, reason);
connectedFeeds.removeSession(feedId);
MsgLogger.LOG.infof("Feed [%s] session closed. Reason=[%s]", feedId, reason);
connectedFeeds.removeSession(feedId, session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public interface MsgLogger extends BasicLogger {
@Message(id = 2, value = "Failed to execute command [%s] with json [%s] for feed [%s]")
void errorCommandExecutionFailure(String feedId, String commandName, String json, @Cause Throwable t);

@LogMessage(level = Logger.Level.ERROR)
@Message(id = 3, value = "A feed [%s] opened multiple sessions. This is a violation; closing the extra session")
void errorClosingExtraFeedSession(String feedId);

@LogMessage(level = Logger.Level.ERROR)
@Message(id = 4, value = "Cannot close the extra session created by feed [%s]")
void errorCannotCloseExtraFeedSession(String feedId, @Cause Throwable t);

}

0 comments on commit 6389c32

Please sign in to comment.