Providing the service

Finally here's the implementation of HelloService which actually provides the service to the client netlet. Recall this is what was returned from the bindService() method above:


    /** This is the real implementation of the hello world service.  It's
     * handed out only through the bindService() method of the public
     * (netlet) class.
     */
    private class HelloService implements IService
    {
        /** Respond to the service connection failing. If it does fail
         * this netlet is useless, so we terminate ourselves.
         */
        public void terminate()
        {
            System.out.println("Service terminated, terminate ourselves");
            _node.requestTermination();
        }

        /** See what facets this service offers the binding (consumer) netlet.
         */
        public String[] getFacets()
        {
            return new String[] { _facet_name };
        }

        /** Allows the consumer netlet to bind a specific facet.
         * @param facet  The name of the facet to bind.
         * @return  An interface to the requested facet.
         */
        public IRemote bindFacet(String facet) throws FacetBindException
        {
            System.out.println("Instantiating "+facet);

            if(!facet.equals(_facet_name))
            {
                throw new FacetBindException();
            }

            System.out.println("Facet instantiated");
            return new FacetHandle(new HelloFacet(), facet);
        }

    } // HelloService

} // HelloServer

Notice this class implements all the methods we mentioned in our earlier discussion: getFacets(), bindFacets() and terminate().

The terminate() method responds to the client netlet severing the service connection, and does so by self-destructing the netlet.

The getFacets() methods tells the client netlet what facets it's offering. In this case, just the one, the IHelloFacet.

But most interesting is the bindFacet() method, which binds any facet requested by the client. Of course, the first thing it does is to check the client is asking for a legitimate facet--if it's not asking for the one facet it offers then it throws an exception.

But notice how it returns the HelloFacet object, the implementation of IHelloFacet. In a simpler world it would just return a new HelloFacet() but instead it wraps it in a FacetHandle. If you want a simple view of the world, just remember that whenever a netlet needs to return a facet it should wrap it in a FacetHandle, giving the object and the facet interface name. If you want to know a bit more, read on...

Nik Silver 2002-03-09