jwamp
jwamp copied to clipboard
Simplify WampDispatcher / DefaultWampDispatcher
There seems to be a lot of plumbing (including reflection) to support registered message handlers, multiple message handlers, etc... in DefaultWampDispatcher.
I'm wondering if this flexibility is necessary? It seems like it would be better to keep this as simple as possible - i.e. have the handlers register at construction time and not allow changing. Or are there use-cases that I'm not considering?
Also (and this may be pushing it a bit), would it be reasonable to have each WampMessageHandler just cast to the sub-interface that it needs?
If so, then we wind up with the constructor of DefaultWampDispatcher being:
private DefaultWampDispatcher(SessionManager sessionManager, WampMessageHandler[] handlers)
then use a builder to make it easy for the user to configure which events each handler is interested in. It will be fine for the same handler to fill multiple slots in the array.
This isn't quite as fancy as the current implementation, but it is a lot simpler. Every single message is going to route through DefaultWampDispatcher#onMessage(), and there is a lot of looping and reflection going on in that method.
If we make this change, we can break the dependency between WampWebsocket and WampDispatcher (just pass the RPCSender and WampEventSubscriber into the WampWebSocket constructor). The close() method can be handled by passing the Session into the WampWebSocket constructor instead of dispatcher (which makes a lot of sense anyway, because I'm certain that it'll be desirable to get the Session associated with the socket).