Mandala Samples Code

This page provides some examples of code that uses Mandala features.

Reflective Asynchronous Method Invocation

The RAMI package provides the specification of the reflective asynchronous method invocation mechanism. Transparency is used to allow anyone to invoke a public method of an object asynchronously the most naturally as possible. See the transparency package for more informations on transparency.

Semi-transparent mechanism: the recommended way

HelloWorld

Create your class HelloWorld as usual (notice that your class must not resides in the anonymous package so be sure to create a package when using semi-transparent asynchronous method invocation (see Limitation for details):


    package helloWorld;

    public class HelloWorld {

        public String toString() {
            return "Hello World !";
        }

        public static void main(String[] args) {
            if (args.length != 0) {
                System.err.println("Usage : " + HelloWorld.class.getName());
                System.exit(1);
            }
                
            HelloWorld helloWorld = new HelloWorld();
                
            System.out.println(helloWorld);
        }
    }

    
Invoke the toString() method asynchronously using a semi-transparent asynchronous proxy (see the Jaya Compiler to know how to generate such a proxy.):

      jaya.helloWorld.HelloWorld proxy = new jaya.helloWorld.HelloWorld();

      // Asynchronous call
      FutureClient future = proxy.rami_toString();

      System.out.println("During the invocation of toString()");

      try{
          System.out.println(future.waitForResult());
      }catch(Throwable t) {
          t.printStackTrace();
      }

    

Performing an asynchronous output operation using OutputStream.

    byte[] buf = byteToWrite();
    OutputStream os = socket.getOutputStream(); // e.g.
    jaya.java.io.OutputStream rami_os = (jaya.java.io.OutputStream) 
       Framework.getSemiTransparentAsynchronousProxy(os);

    // Semi-transparent asynchronous call
    FutureClient future = rami_os.write(buf, 0, b.length); 

    doSomethingElse();   // Concurrent execution with 'write()'

    try{
       future.waitUntileResultAvailable(); 
    }catch(IOException io) {
        handleException(io);
    }catch(Throwable t) {
        t.printStackTrace();
    }
    

Total-transparent mechanism: the "easy" way

        CharSequence cs = new String("EasyButHasManyProblems");
        cs = (CharSequence) 
            mandala.rami.Framework.getTotalTransparentAsynchronousProxy(cs);
        // Asynchronous call
        cs = cs.subSequence(0, 4); // return "Easy"
        System.out.println("During subSequence!");
        // wait by necessity
        System.out.println(cs.length());  // Must be 4!
    

Note that this example run because:

You may see the limitations of the total-transparent mechanism before using it!

Remote asynchronous method invocation

HelloWorld

Using the previous HelloWorld class, you just have to do:
        // gets a proxy on a remote active map
        ActiveMap activeMap = null;
        try{
            activeMap = (mandala.jacob.remote.RemoteActiveMap) 
                new javax.naming.InitialContext().lookup(args[0]);
        }catch(Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
        
        jaya.helloWorld.HelloWorld rsor_helloWorld = 
            new jaya.helloWorld.HelloWorld(new SORFactory(activeMap));
        
        FutureClient future = rsor_helloWorld.rami_toString();
        
        try{
            System.out.println(future.waitForResult());
        }catch(Throwable t) {
            t.printStackTrace();
            System.exit(1);
        }
    
        // Removing the stored object from the remote hashmap and invokes its
        // 'toString()' method.
        StoredObjectReference rsor = 
            (StoredObjectReference) rsor_helloWorld.getAsynchronousReference();
        
        HelloWorld object = (HelloWorld) rsor.remove();
        
        System.out.println(object);
    }

    

Remote computing


        // gets a proxy on a remote active map
        ActiveMap activeMap = null;
        try{
            activeMap = (mandala.jacob.remote.RemoteActiveMap) 
                new javax.naming.InitialContext().lookup(bindName);
        }catch(NamingException ne) {
            logger.error(ne, "Can't find " + bindName);
            return;
        }
        
        // Instanciate a BigInteger, insert it into the activeMap and gets a
        // semi-transparent proxy on it.
        jaya.java.math.BigInteger i = 
               new jaya.java.math.BigInteger("12345678901234567890", 
                                             new SORFactory(activeMap));
        // Remote Asynchronous Call
        FutureClient future = i.rami_isProbablePrime(probability);
        doSomethingElse();

        try{
            Boolean isPrime = (Boolean) future.waitForResult();
        }catch(Throwable t) {
            handleException(t);
        }