Binding the service

Inside the initialise() method we do the main work--at least in this example.

The first thing is to take the arbitrary parameter and cast it into the warrant which we know it really is. (More robust code would test the object before casting.) Then we use it to bind the service.

This process of service binding is in many ways the heart of Jtrix. Since the node is our runtime environment we must ask it to bind the service for us. We have to give it our warrant, effectively saying ``See this? I have the right to use this service. Go get it.'' The connection to the service goes into the IService variable called ``service''.

But while binding the service will give us a service connection, there's a bit more to it than that. The fact is, a service connection is two-way deal, and we can't get a connection from the hello service unless we give it a connection into us, too. So the NullService object from Libjtrix does just that. It's a simple service connection which offers absolutely nothing of interest. Yet by offering this we make a two-way service connection--the NullService class implements IService, and is given to the hello world service, just as we, the hello client, get its IService object which goes into the ``service'' variable.

Now we're bound to the service we can start to use it. We pull out a facet (i.e. an interface) by name. This is called binding, too.

And once we've got the facet we can use it, just as we can use any interface. In this case we use its getMessage() method and print out that message.

Of course, several things could go wrong along the way, so we do need to catch some exceptions. Among other things, the service might not be available. This might happen if it's died, or if our network connection has failed. Or perhaps the service is available, but our warrant is out of date. Or perhaps our NullService isn't good enough for it, and it wants us to provide a service which offers a credit card number.

And even if we do get to bind the service, perhaps binding the facet will fail. This will happen if the service doesn't offer the IHelloFacet--perhaps we picked up the wrong warrant and hence bound to the wrong service. Or perhaps the IHelloFacet is only available to those who offer a service with a credit card number. If we want to handle this situation elegantly then we can use the getFacets() method of the IService interface, and hence check to see if the IHelloFacet is supported.

Of course, these are all worst cases. If we're connected to the network properly, and we've remembered to put in the right warrant, and the warrant is for a fairly reliable service, then all will go well.

Now on to the rest of the INetlet methods.

Nik Silver 2002-03-09