情境
我将尽可能简洁地说明。基本上,参考这个类图,我有一个门面,管理一组SocketManager(每个SocketManager管理一个Socket连接)。每个SocketManager使用唯一的SocketUserId登录到远程服务器。此外,每个SocketManager将接受来自客户端的消息,这些消息将发送到特定列表中的Receipients。为了讨论方便,可以将这些Receipients视为由名称标识的远程数据桶。
客户端将以以下方式发送数据:
SocketFacade facade = ...;
byte[] data = ...
facade.sendData( receipient, data );
当 SocketFacade 启动时,它将查询一个mysql表,该表返回 SocketUserId 和 Receipients 之间的1-m关系。我将使用MultiValuedMap来表示这种1-m关系。通过迭代地遍历这个map,多个SocketManager将被启动。
(1) Map< SocketUserId, List<Receipient> >
例如,假设我们有两个SocketManager,分别具有"alice"和"tom"的SocketUserId。
+----SocketManager1 ( "alice" ) for Receipients { "B", "C" }
|
SocketFacade
|
+----SocketManager2 ( "tom" ) for Receipients { "A", "D" }
问题
我不知道如何实现sendData方法。基本上,我需要一种方法将收件人(例如“B”)映射到其负责的SocketManager(例如SocketManager1)。
假设我这样做:
(2) Map< SocketUserId, SocketManager >
(3) Map< Receipient, SocketUserId >
- Would I need a SoftReference for the value in (2) ?
- Should I just map from Receipient directly to SocketManager ?
SocketFacade also supports methods that will mutate the relationship represented by (1). If I write to the database, the in-memory data structures in (1), (2), & (3) will need to change in sync. SocketFacade must be thread safe as well. Initial idea is to have some sort of a publish subscribe system whereby an add/remove to the DB will cause the changes to propagate via callbacks.
interface Callback { void receipientAdded( Receipient r ); void receipientDeleted( Receipient r ); }