The main netlet methods

We begin our class as follows:


package org.jtrix.project.helloworld;

import org.jtrix.base.*;

/** Netlet which provides a Hello World service in a very simple way.
 */
public class HelloServer implements INetlet
{
    private static final String _facet_name = IHelloFacet.class.getName();
    private INode _node;

We declare two private variables, just as placeholders for later.

Since this class is a netlet we must implement the INetlet methods:


    public byte[] initialise(INode node, Object param, byte[] unsigned)
        throws InitialiseException
    {
        _node = node;
        System.out.println("Server started");
        return null;
    }

    public void terminate(long date, INetlet.IShutdownProgress progress)
    {
        // Nothing to clean up when we terminate
    }

    /** Provide the service requested by the client.
     * @param warrant   The warrant which the client used for binding
     * @param consumer  The client's reciprocating service interface.
     * @return  A service connection.
     */
    public IService bindService(Warrant warrant, IService consumer)
        throws ServiceBindException
    {
        return new HelloService();
    }

    public String[] getFacets()
    {
        // We offer only the IHelloFacet
        return new String[]{ _facet_name };
    }

    public IRemote bindFacet(String facet) throws FacetBindException
    {
        // The node shouldn't try to bind facets from here
        throw new FacetBindException();
    }

The initialise() method doesn't do much except save a reference to the node. As mentioned above, this is so we can ask it to terminate us if the connection to the client is severed. The terminate() method does even less.

We don't offer any facets to the node (we do offer facets to other netlets, but not to the node) hence the contents of getFacets() and bindFacet().

But of most interest is bindService() which is called when another netlet tries to bind a service from us. This will be the client netlet asking for the hello world service. We return a new HelloService object which implements IService, as required, and which we'll get to shortly.

Nik Silver 2002-03-09