Examples


µCode comes with the following toy examples that demonstrate some of its capability:

The source code for the examples is found in the distribution under the directory examples.

How to run the examples:

The examples require to run two, sometimes three different Java VMs running simultaneously. These VMs can reside on different physical hosts, or can all coexist on a single machine. In the following, we will assume that all the VMs are run on the same machine using different port numbers, namely, localhost, localhost:2000, and localhost:3000. Running the examples on different hosts consists straightforwardly of substituting the three addresses above with the addresses of the hosts.

In all the examples, the command line arguments available for launching a server from the command line are still available and can be specified for the applications you are running. Thus, for instance, you can append to the command lines below the option -debug on to each of the command lines below (e.g., to provide me with more information in case you discovered a bug). See the documentation of mucode.util.Launcher for a list of options.


Mobile Agent

This example demonstrates the capability of the class mucode.abstractions.MuAgent. It creates a mobile agent that continuously hops from one µServer to another.

How to run the example:

Start a µServer in a shell:
>java mucode.util.Launcher -port 2000
In a different shell, start the program BounceLauncher, found under examples/MobileAgent:
examples/MobileAgent>java BounceLauncher localhost:2000 localhost
The two parameters on the command line are the addresses of the two µServer the mobile agent is bouncing between.

What to expect:

On the shell running the µServer alone the following messages will appear:
>java mucode.util.Launcher -port 2000
mucode: MuServer activated on port 2000
Bouncer: step 2
Bouncer: step 3
Bouncer: step 6
Bouncer: step 7
Bouncer: step 10
Bouncer: step 11
Bouncer: step 14
Bouncer: step 15
In the shell where you started the application, you will see:
examples/MobileAgent>java BounceLauncher localhost:2000 localhost
Spawning the bouncing agent.
mucode: MuServer activated on port 1968
This is the main thread.
Bouncer: step 0
Bouncer: step 1
This is the main thread.
Bouncer: step 4
This is the main thread.
Bouncer: step 5
This is the main thread.
Bouncer: step 8
Bouncer: step 9
This is the main thread.
This is the main thread.
Bouncer: step 12
Bouncer: step 13
This is the main thread.

Worth Noting:


Remote Dynamic Linking

This example demonstrates the ability of to dynamically load classes that are not present on the local µServer from a remote µServer specified at invocation time.

How to run the example:

Start a µServer in a shell:
>java mucode.util.Launcher -port 2000
In a different shell, start the program Provider, found in examples/Provider. You may want to enable debugging this time, just to see what happens:
examples/Provider>java Provider -debug on 
In a third shell, start the program Linker, found in examples/RemoteDynamicLinking:
examples/RemoteDynamicLinking>java Linker localhost:2000 localhost
The first parameter is the destination for Linker, the second parameter is the µServer acting as a code repository for the agent, from which the missing classes will be downloaded.

What to expect:

In the shell where you run Linker, nothing happens. In fact, the Linker application just sends an agent (an instance of itself) on the host specified as the first parameters and then terminates.
In the shell where you run the Provider, you will see:
examples/Provider>java Provider -debug on 
Exporting the classes...
Classes exported. Shared class space: [ Toolkit Nail Hammer ]
mucode: MuServer activated on port 1968
mucode: Connection accepted. Reconstructing the header...
mucode: The connection contains a request for dynamic linking of class: Toolkit
mucode: Contents of shared class space: [ Toolkit Nail Hammer ]
mucode: Bytecode for Toolkit (932 bytes) sent.
mucode: Connection accepted. Reconstructing the header...
mucode: The connection contains a request for dynamic linking of class: Hammer
mucode: Contents of shared class space: [ Toolkit Nail Hammer ]
mucode: Bytecode for Hammer (263 bytes) sent.
mucode: Connection accepted. Reconstructing the header...
mucode: The connection contains a request for dynamic linking of class: Nail
mucode: Contents of shared class space: [ Toolkit Nail Hammer ]
mucode: Bytecode for Nail (259 bytes) sent.
Finally, in the shell where you started the µServer, you will see:
>java mucode.util.Launcher -port 2000
mucode: MuServer activated on port 2000
Creating a Toolkit object
Using a Hammer and a Nail from the Toolkit
Using a Hammer and a Nail from the Toolkit
Using a Hammer and a Nail from the Toolkit

Worth Noting:


Ship

This example demonstrates the ability of shipping a class to a remote µServer, thus making it proactively available to the threads that are hosted by that µServer. 

How to run the example:

In a shell, start the program Injected, found in examples/Ship/Injected:
examples/Ship/Injected>java Injected
In a different shell, start the program Injector, found in examples/Ship/Injector:
examples/Ship/Injected>java Injector localhost

What to expect:

Injector just performs the ship operation and then terminates. Thus, in the shell where you started Injector you will see:
examples/Ship/Injector>java Injector localhost
Sending the class Toolkit on localhost ...
... done.
examples/Ship/Injector>
In the shell where you started Injected you will see:
examples/Ship/Injected>java Injected
I need Toolkit.class to proceed with execution.
I'll look on my host, until it comes...
mucode: MuServer activated on port 1968
Waiting for the class ...
Waiting for the class ...
Waiting for the class ...
Class Toolkit is now in my shared class space.
Using a Hammer and a Nail from the Toolkit
Using a Hammer and a Nail from the Toolkit
Using a Hammer and a Nail from the Toolkit

Worth Noting:


Fetch

How to run the example:

In a shell, start the program Provider, found in examples/Provider:
examples/Provider>java Provider -port 2000
In a different shell, start the program Fetcher, found in examples/Fetch:
examples/Fetcher>java Fetcher localhost:2000

What to expect:

Similarly to the example about remote dynamic linking, the shell where Provider is run will look like:
examples/Provider>java Provider -port 2000
Exporting the classes...
mucode: MuServer activated on port 2000
In the shell where you run Fetcher you will see:
examples/Fetch>java Fetcher localhost:2000
I need Toolkit.class to proceed with execution.
I'm going to fetch it from localhost:2000
mucode: MuServer activated on port 1968
Waiting for the class ...
Class Toolkit is now in my shared class space.
Using a Hammer and a Nail from the Toolkit
Using a Hammer and a Nail from the Toolkit
Using a Hammer and a Nail from the Toolkit

Worth Noting:


CopyThread

This example demonstrates the capability to clone a thread and ship it to a different µServer, by retaining the thread's data state. 

How to run the example:

Start a µServer in a shell:
>java mucode.util.Launcher -port 2000
In a different shell, start the program Cloner, found under examples/CopyThread:
examples/CopyThread>java Cloner localhost:2000 
The two parameters on the command line are the addresses of the two µServer the mobile agent is bouncing between.

What to expect:

On the shell running the µServer alone the following messages will appear:
>java mucode.util.Launcher -port 2000
mucode: MuServer activated on port 1968
Cloner active, i = 4
Cloner active, i = 5
Cloner active, i = 6
Cloner active, i = 7
Cloner active, i = 8
Cloner active, i = 9
Cloner active, i = 10
...
In the shell where you started the application, you will see:
examples/MobileAgent>java Cloner localhost:2000
Cloner active, i = 0
About to clone the Cloner...
Cloner active, i = 1
Cloner active, i = 2
Cloner active, i = 3
Done.
Cloner active, i = 4
Cloner active, i = 5
Cloner active, i = 6
Cloner active, i = 7
Cloner active, i = 8
Cloner active, i = 9
Cloner active, i = 10
Cloner active, i = 11
...

Worth Noting:


ResourceSharing

The core of µCode currently does not support directly any form of communication among mobile threads. A couple of packages are under development that will provide these features. However, the MuServer class can be specialized to provide shared resources, as demonstrated in this mobile producer/consumer system. A server provides a shared integer that provides synchronized access to two mobile threads that rendez-vous on the server.

How to run the example:

Start the program Server in a shell, found in examples/ResourceSharing/Server:
examples/ResourceSharing/Server>java Server
In a different shell, start the program Consumer, found in examples/ResourceSharing/Consumer:
examples/ResourceSharing/Consumer>java Consumer localhost
In a different shell, start the program Producer, found in examples/ResourceSharing/Producer:
examples/ResourceSharing/Producer>java Producer localhost

What to expect:

Consumer and Producer will return right after they are invoked, since they only send an agent on the µServer created within the Server program. In the shell of the latter, you will see:
examples/ResourceSharing/Server>java Server
mucode: MuServer activated on port 1968
Consumer started
Consumer waiting...
Producer started
Consumer awaken.
Consumer retrieved 0
Producer produced 0
Consumer waiting...
Producer produced 1
Consumer awaken.
Consumer retrieved 1
Producer produced 2
Consumer retrieved 2
Producer produced 3
Consumer retrieved 3
Producer produced 4
Producer waiting...
Producer awaken.
Producer produced 5
Consumer retrieved 4
Consumer retrieved 5
Producer produced 6
Consumer retrieved 6
Consumer waiting...
Consumer awaken.
Consumer retrieved 7
Producer produced 7
Producer produced 8
Producer waiting...
Consumer retrieved 8
Producer awaken.
Producer produced 9
Producer terminated
Consumer retrieved 9
Consumer terminated

Worth Noting:


© 2000, Gian Pietro Picco - Last modification: 07/09/00