com.threerings.crowd.chat.server
Class ChatChannelManager

java.lang.Object
  extended by com.threerings.crowd.chat.server.ChatChannelManager
All Implemented Interfaces:
ChannelSpeakProvider, InvocationProvider

public abstract class ChatChannelManager
extends Object
implements ChannelSpeakProvider

Handles chat channel services.


Nested Class Summary
protected static class ChatChannelManager.ChannelAction
          Forwards a channel speak request from the server hosting the message originator to the server that is hosting the channel.
protected static class ChatChannelManager.ChannelInfo
          Contains metadata for a particular channel.
static class ChatChannelManager.ChatHistoryResult
          Value asynchronously returned by collectChatHistory(com.threerings.util.Name, com.samskivert.util.ResultListener) after polling all peer nodes.
 
Field Summary
protected  Map<ChatChannel,ChatChannelManager.ChannelInfo> _channels
          A map of resolved channels to metadata records.
protected  ChatHistory _chatHistory
          Used for recording chat history.
protected  BodyLocator _locator
          Used for acquiring BodyObject references from Names and ClientObjects.
protected  CrowdPeerManager _peerMan
          Provides peer services.
protected  Map<ChatChannel,List<UserMessage>> _resolving
          Contains pending messages for all channels currently being resolved.
protected static long IDLE_CHANNEL_CHECK_PERIOD
          The period on which we check for idle channels.
protected static long IDLE_CHANNEL_CLOSE_TIME
          The amount of idle time (in milliseconds) after which we close a channel.
protected static Predicate<ChatHistory.Entry> IS_USER_MESSAGE
           
protected static Comparator<ChatHistory.Entry> SORT_BY_TIMESTAMP
           
 
Constructor Summary
protected ChatChannelManager(PresentsDObjectMgr omgr, InvocationManager invmgr)
          Creates our singleton manager and registers our invocation service.
 
Method Summary
 void bodyAddedToChannel(ChatChannel channel, int bodyId)
          When a body becomes a member of a channel, this method should be called so that any server that happens to be hosting that channel can be told that the body in question is now a participant.
 void bodyRemovedFromChannel(ChatChannel channel, int bodyId)
          When a body loses channel membership, this method should be called so that any server that happens to be hosting that channel can be told that the body in question is now a participant.
protected  void closeIdleChannels()
          Called periodically to check for and close any channels that have been idle too long.
 void collectChatHistory(Name user, ResultListener<ChatChannelManager.ChatHistoryResult> lner)
          Collects all chat messages heard by the given user on all peers.
protected  void deliverSpeak(ChatChannel channel, UserMessage message, int[] bodyIds)
          Delivers the supplied chat channel message to the specified bodies.
protected  void dispatchSpeak(ChatChannel channel, UserMessage message)
          Requests that we dispatch the supplied message to all participants of the specified chat channel.
protected  void finishResolveAndDispatch(ChatChannel channel)
          Resolves the participant set for the specified chat channel and dispatches all pending messages to the channel.
protected abstract  int getBodyId(Name speaker)
          Converts a speaker's visible name into a unique integer id.
protected abstract  BodyObject getBodyObject(int bodyId)
          Locates a body object from the given unique id.
protected  ChatChannel intern(ChatChannel channel)
          Returns a widely referenced instance equivalent to the given channel, if one is available.
protected  void resolutionComplete(ChatChannel channel, Set<Integer> parts)
          This should be called when a channel's participant set has been resolved.
protected  void resolutionFailed(ChatChannel channel, Exception cause)
          This should be called if channel resolution fails.
protected  void resolveAndDispatch(ChatChannel channel)
          Resolves the channel specified in the supplied action and then dispatches it.
protected  boolean shouldDeliverSpeak(ChatChannel channel, UserMessage message, BodyObject body)
          Ratifies the delivery of the supplied chat channel message to the specified body.
 void speak(ClientObject caller, ChatChannel channel, String message, byte mode)
          Handles a ChannelSpeakService.speak(com.threerings.crowd.chat.data.ChatChannel, java.lang.String, byte) request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IS_USER_MESSAGE

protected static final Predicate<ChatHistory.Entry> IS_USER_MESSAGE

SORT_BY_TIMESTAMP

protected static final Comparator<ChatHistory.Entry> SORT_BY_TIMESTAMP

_resolving

protected Map<ChatChannel,List<UserMessage>> _resolving
Contains pending messages for all channels currently being resolved.


_channels

protected Map<ChatChannel,ChatChannelManager.ChannelInfo> _channels
A map of resolved channels to metadata records.


_peerMan

@Inject
protected CrowdPeerManager _peerMan
Provides peer services.


_locator

@Inject
protected BodyLocator _locator
Used for acquiring BodyObject references from Names and ClientObjects.


_chatHistory

@Inject
protected ChatHistory _chatHistory
Used for recording chat history.


IDLE_CHANNEL_CHECK_PERIOD

protected static final long IDLE_CHANNEL_CHECK_PERIOD
The period on which we check for idle channels.

See Also:
Constant Field Values

IDLE_CHANNEL_CLOSE_TIME

protected static final long IDLE_CHANNEL_CLOSE_TIME
The amount of idle time (in milliseconds) after which we close a channel.

See Also:
Constant Field Values
Constructor Detail

ChatChannelManager

@Inject
protected ChatChannelManager(PresentsDObjectMgr omgr,
                                    InvocationManager invmgr)
Creates our singleton manager and registers our invocation service.

Method Detail

bodyAddedToChannel

public void bodyAddedToChannel(ChatChannel channel,
                               int bodyId)
When a body becomes a member of a channel, this method should be called so that any server that happens to be hosting that channel can be told that the body in question is now a participant.


bodyRemovedFromChannel

public void bodyRemovedFromChannel(ChatChannel channel,
                                   int bodyId)
When a body loses channel membership, this method should be called so that any server that happens to be hosting that channel can be told that the body in question is now a participant.


collectChatHistory

public void collectChatHistory(Name user,
                               ResultListener<ChatChannelManager.ChatHistoryResult> lner)
Collects all chat messages heard by the given user on all peers.


speak

public void speak(ClientObject caller,
                  ChatChannel channel,
                  String message,
                  byte mode)
Description copied from interface: ChannelSpeakProvider
Handles a ChannelSpeakService.speak(com.threerings.crowd.chat.data.ChatChannel, java.lang.String, byte) request.

Specified by:
speak in interface ChannelSpeakProvider

resolveAndDispatch

protected void resolveAndDispatch(ChatChannel channel)
Resolves the channel specified in the supplied action and then dispatches it.


finishResolveAndDispatch

protected void finishResolveAndDispatch(ChatChannel channel)
Resolves the participant set for the specified chat channel and dispatches all pending messages to the channel. End users of the chat channel system should override this method and do what is necessary to resolve the channel's participant set and call resolutionComplete(com.threerings.crowd.chat.data.ChatChannel, java.util.Set) or resolutionFailed(com.threerings.crowd.chat.data.ChatChannel, java.lang.Exception).


resolutionComplete

protected void resolutionComplete(ChatChannel channel,
                                  Set<Integer> parts)
This should be called when a channel's participant set has been resolved.


resolutionFailed

protected void resolutionFailed(ChatChannel channel,
                                Exception cause)
This should be called if channel resolution fails.


dispatchSpeak

protected void dispatchSpeak(ChatChannel channel,
                             UserMessage message)
Requests that we dispatch the supplied message to all participants of the specified chat channel. The speaker will be validated prior to dispatching the message as the originating server does not have the information it needs to validate the speaker and must leave that to us, the channel hosting server.


deliverSpeak

protected void deliverSpeak(ChatChannel channel,
                            UserMessage message,
                            int[] bodyIds)
Delivers the supplied chat channel message to the specified bodies.


closeIdleChannels

protected void closeIdleChannels()
Called periodically to check for and close any channels that have been idle too long.


shouldDeliverSpeak

protected boolean shouldDeliverSpeak(ChatChannel channel,
                                     UserMessage message,
                                     BodyObject body)
Ratifies the delivery of the supplied chat channel message to the specified body. Derived classes can override this method to implement channel disabling, mute lists or any other suppression they might need.


intern

protected ChatChannel intern(ChatChannel channel)
Returns a widely referenced instance equivalent to the given channel, if one is available. This reduces memory usage since clients send new channel instances with each message.


getBodyId

protected abstract int getBodyId(Name speaker)
Converts a speaker's visible name into a unique integer id. This is not the oid for this speaker but rather a persistent integer identifier that can be passed between servers and used to look up the speaker on the target server via a call to getBodyObject(int). We use this rather than names to avoid having to send (large) Name objects for every channel participant to each individual peer that will be forwarding messages.


getBodyObject

protected abstract BodyObject getBodyObject(int bodyId)
Locates a body object from the given unique id. May return null.