Nameservice Listener Details¶
An etch service has at its core some sort of listener for new sessions, possibly more than one. While the examples are tcp, any sort of transport will work. Here is a typical sequence of operations:
String suri = "tcp://0.0.0.0:4004"; Transport<ServerFactory> listener = PerfHelper.newListener( suri, null, factory ); listener.transportControl( Transport.START_AND_WAIT_UP, 4000 ); System.out.println( "listener ready" );
Listener is a short name for a listener transport stack. When the listener is issued the control "start", a tcp server socket is created, then a listen command is issued to the socket, then a thread is created to repeatedly call accept on the server socket and delivers any result to higher-levels of the listener transport stack. The higher levels take an accepted socket, wrap it in a session transport stack, and start the session. Thus a new connection is born. Once the listener thread is running, the listener's session is notified that the listener is "up" (the session is the top of the listener transport stack while the tcp server socket is the bottom).
Here is a diagram of the flow related to creating and starting a listener:
How to Register a Service¶
One of our tenets is that we don't want to have to modify a service to cause it to be registered with a name service. First let's look at what sort of code we're trying to get rid of:
String nsuri = "tcp://ns:4002"; String name = "Perf/foo"; String curi = "tcp://foo:4004"; RemoteNameServiceServer server = NameServiceHelper.newServer( nsuri, null, factory ); server._startAndWaitUp( 4000 ); server.register( name, curi ); server._stopAndWaitDown( 4000 ); server = null;
This code would normally be inserted just after the message "listener ready" is printed. The result is that once the listener was up and ready, it would register itself with the name service, giving its assigned api and instance name (Perf/foo) and the uri that clients should use to attempt to connect. Omitted from this code is how the server authenticates with the name service. Also omitted is the loop wrapping the sequence from start to finish with a sleep inserted to periodically re-register the name in case the name service forgets.
How to Do It Right¶
In order to eliminate the code above we need to listen using a different transport (use etch: instead of tcp:) and supply enough information on the uri to allow the etch transport to register for us. Here is a listener uri which does this:
String suri = "etch:Perf/foo?ns=tcp://ns:4002&curi=tcp://foo:4004&suri=tcp://0.0.0.0:4004";
Use this with the code from the introduction to complete the task.
Here is a diagram of the flow related to creating and starting a listener with name service registration included: