Note: At the current time Jtrix Ltd is not running a public ``hello world'' service so this example cannot be run. However, the example has been left in for demonstration. Please do read it. Later we will see how to write our own service, so the lack of a public ``hello world'' service will not matter.
Despite the fact that we've not introduced any terms yet (don't worry, we will), we present here a very small Jtrix program, or ``netlet'', with lots of comments. The point of this document is to explain the program, and whole lot more. Meanwhile, here's a brief summary.
The netlet takes a warrant, which is a right to use a service, and binds (i.e. connects) to it. In this case the service is a hello world service, so it gets the service's message interface (called a ``facet'') and then gets the message. Also, there's a lot of things it doesn't do, so several other methods are included to say as much--they just return redundant values or throw exceptions.
There are few imports, because this isn't a big application:
package org.jtrix.project.helloworld; import org.jtrix.base.*; import org.jtrix.project.libjtrix.netlet.NullService; /** Netlet which accesses a hello world service using a warrant * read from an incoming parameter. */ public class Hello1Client implements INetlet {
All the work is done when the netlet initialises, like this:
public byte[] initialise(INode node, Object param, byte[] unsigned) throws InitialiseException { try { // Get the warrant, use it bind the service, and then get the facet Warrant warrant = (Warrant)param; IService service = node.bindService(warrant, new NullService()); String hf_name = IHelloFacet.class.getName(); IHelloFacet facet = (IHelloFacet) service.bindFacet(hf_name); // Use the facet System.out.println(facet.getMessage()); return null; } catch (Throwable e) { System.out.println("Sorry, no message available"); e.printStackTrace(); throw new InitialiseException(e.toString()); } }
And we need to implement a few more methods, but we do so only minimally:
public void terminate(long date, INetlet.IShutdownProgress progress) { // Nothing to clean up when we terminate } public IService bindService(Warrant warrant, IService consumer) throws ServiceBindException { // This netlet offers no services to other netlets using warrants throw new ServiceBindException(); } public String[] getFacets() { // This netlet offers no facets to the node return new String[0]; } public IRemote bindFacet(String facet) throws FacetBindException { // The node shouldn't try to bind facets from here throw new FacetBindException(); } } // Hello1Client
The interface IHelloFacet, mentioned above, just looks like this. This is how our netlet talks to the hello world service.
package org.jtrix.project.helloworld; import org.jtrix.base.*; public interface IHelloFacet extends IRemote { public String getMessage(); }
Nik Silver 2002-03-09