Here is how multiplex works with respect to server sockets. The MasterServerSocket wraps a real server socket, and when a client connects to it (the first time) by calling a VirtualSocket constructor targeted at the MasterServerSocket, the MasterServerSocket creates a real socket and creates a VirtualSocket associated with that socket. This is how a multiplex connection is created from the client to the server.
Let's call the multiplex connection MC. Now, you can attach a VirtualServerSocket to either end of MC, which allows new virtual connections to be added to MC. To attach a VirtualServerSocket to the server side, you need to take an existing VirtualSocket associated with MC and pass it to the constructor
public VirtualServerSocket(VirtualSocket socket, Map configuration) throws IOException
Then the VirtualServerSocket will be bound to the same host and port as the MasterServerSocket, and subsequent connections to that host and port from the other end of MC will be handled by the VirtualServerSocket. That is, new virtual connections will be added to the single physical connection. You can see this idea at work in MultiplexServerInvoker.run().
In your situation it sounds like you are restricted to configuring your third party library with a server socket factory, and the existing VirtualServerSocketFactory probably won't work for you, since it was intended for other purposes. However, you could write your own. Initialize it with a VirtualSocket and then in each createServerSocket() method call the VirtualServerSocket constructor shown above.
- Do I have to create VirtualServerSocket on the client side although I don't need any "callbacks", i.e. VirtualSockets on the server connecting explicitly to clients?
No. You would need a VirtualServerSocket on the client side only if you intended to create virtual connections from the server to the client.
- How to allow multiple physical clients to connect to one MasterServerSocket?
That is enabled automatically, since the MasterServerSocket wraps a real server socket.
Thank you very much for your answer. It works! I have another two questions:
- What kind of configuration can be passed into VirtualServerSocket constructor you mentioned? (Now i pass null :))
- Let's suppose I have 100 physical connections from 100 different clients. I want to react if there is a new virtual connection on any of VirtualServerSockets bound to these physical connections or if there is a new physical connection. The only idea is to have 101 threads, 100 waiting on accept() on the VirtualServerSockets, and one waiting on accept() on the MasterServerSocket. I'd like to have a kind of listener on VirtualServerSockets to observe new coming connections without blocking. I would use NIO, but I doubt it will work with VirtualServerSocket.
Without using virtual sockets I can have only one thread waiting on one real ServerSocket.accept() method (but I have far too many connections :).
What kind of configuration can be passed into VirtualServerSocket constructor you mentioned?
In the normal course of events, this configuration map would be the one that the Connector is configured with.
I'd like to have a kind of listener on VirtualServerSockets to observe new coming connections without blocking.
I see, you want a kind of "virtual Selector". The multiplex transport doesn't have that facility. An NIO-like virtual socket would be a nice project, but I don't think there will be much of a demand for it. Of course, if you are interesting in contributing to the Remoting project, you would certainly be welcome.