Your first Styx system

In this tutorial you will create a basic Styx server and client. This will introduce you to the main classes of the JStyx software and how they are used.

A very simple Styx server

We will create a Styx server that serves up a single file. The contents of the file are held in memory on the server. The namespace of this system is extremely simple:

           /      (The root of the namespace)
           |
         readme   (The only file exposed by the server)
        

The full source is contained in the SimpleServer class, but these are the important lines (see full source for comments):

        StyxDirectory root = new StyxDirectory("/");
        InMemoryFile file = new InMemoryFile("readme");
        file.setContents("hello");
        root.addChild(file);
        new StyxServer(9876, root).start();
        

In these five lines, we create a root directory for the namespace, then create and add a file with the name "readme" that contains the string "hello". Note that the file is an InMemoryFile, which is an instance of the general superclass for all files on a Styx server, StyxFile. Finally, we create and start a Styx server, passing it the root of the namespace.

You can run the server by changing to the bin directory of your JStyx installation and running:

        JStyxRun uk.ac.rdg.resc.jstyx.tutorial.SimpleServer
        

(The JStyxRun script sets up the classpath, then runs the main method of the provided class.) You will probably see some logging messages printed to the console.

A very simple Styx client

It is just as simple to write a client program that can read the contents of the file exposed on the server. The full source is in the SimpleClient class, but these are the most important lines:

        StyxConnection conn = new StyxConnection("localhost", 9876);
        try
        {
            conn.connect();
            CStyxFile readmeFile = conn.getFile("readme");
            System.out.println(readmeFile.getContents());
        }
        catch (StyxException se)
        {
            se.printStackTrace();
        }
        finally
        {
            conn.close();
        }
        

We create a StyxConnection to the server and call the connect() method to make the connection and perform the relevant handshaking. (Note that you might need to edit the hostname and port to suit your system.) We then get a handle to the "readme" file: this handle is an instance of the CStyxFile class. (The "C" means "Client", to avoid confusion with the server-side StyxFile class.) We read the contents of the file as a String, then print them out. Finally, we close the connection.

You can run the client by changing to the bin directory of your JStyx installation and running:

        JStyxRun uk.ac.rdg.resc.jstyx.tutorial.SimpleClient
        

You should see the string "hello" printed out, perhaps in amongst some logging messages.

Serving up files on disk

TODO (talk about the FileOnDisk and DirectoryOnDisk classes)

Summary

In this section of the tutorial, we have created a simple Styx server and client and have passed some data between them. From the server point of view, the key classes are StyxFile, which is the superclass for all virtual files on a Styx server, and the StyxServer class itself. In client-side code, the most important classes are the StyxConnection class, which represents the connection to the server, and the CStyxFile class, which we use to interact with files on the server.

In the next section of the tutorial we will look at different ways of reading from and writing to Styx files.