SmartObjects‎ > ‎

Quick Start

( This chapter from SmartObjects Developer Manual - full manual available to download here )

SmartObjects API and IDL is so easy to use. It's not required to read all manuals to use SmatObjects in your applications. Let's start to implement our first SmartObject - distributed dummy calculator. 
This is the plan: we (always) must start with our IDL, then we must implement server side, and finally we must implement client side.

Writing SmartIDL

Here it is:

namespace examples; 

interface DummyCalculator {
   integer add ( integer value1, integer value2 );
}

It's very simple calculator interface in the world and as you can see we defined interface name 'DummyCalculator' in the namespace 'examples' with only one operation (named 'add') with 2 integer named arguments and integer result. Let's implement calculator interface imlementation with Java.

Compiling IDL to Java

It's time to generate server/client skeletons and stubs for our IDL file with SmartIDL compiler tool:

# sidlc.sh -f calculator.sidl -o calcdir -t java

The compiler generates few classes for both client/server side implementations. The generated files contain Java interfaces and proxy stubs with low-level code (e.g. marshal/unmarshall code) and it's not required to analyze this code by yourself.

Writing first SmartObject implementation with Java (server side)

Now we already have server side stubs, and all we need to do is implement required interfaces for choosed request processing model (synchronous/or asynchronous) and publish our implementation on the endpoint. For example, we choose the synchronous server model and our calculator implementation looks like this:

public class DummyCalculatorImpl implements IDummyCalculatorSyncSmartObject {

    @Override
    public BigInteger add ( BigInteger value1, BigInteger value2 ) throws SmartBusSystemException {
        return value1.add(value2);
    }

    @Override
    public void destroy() {
    }

}

Now we must implement small code for publishing our SmartObject on the network:

import org.latestbit.smartobjects.*;
import examples.*;

public class ServerApplication {
    public static void main(String[] args) throws Exception {
        // Publishing our SmartObject

        ISmartBus bus = new SmartBus();
        IEndpointLocation location = bus.createEndpointLocation("tcp://localhost:3333");

        DummyCalculatorImpl impl = new DummyCalculatorImpl();
        DummyCalculatorSyncSkeleton skeleton = new DummyCalculatorSyncSkeleton( impl );
        skeleton.publish( bus, location);
        
        System.in.read(); // waiting for any input

        // Unpublish/destroy SmartObject and release all resources
        skeleton.destroy();
        bus.destroy();
    }
}

That's all! Our calculator is ready to process client requests.

Writing first Java client to our SmartObject

import org.latestbit.smartobjects.*;
import examples.*;

public class ClientApplication {
    public static void main(String[] args) throws Exception {
        // Resolving SmartObject, published on the network

        ISmartBus bus = new SmartBus();
        IEndpointLocation location = bus.createEndpointLocation("tcp://localhost:3333");

        IDummyCalculator calculator = new DummyCalculator();
        calculator.resolve(bus, location);
        
        // Calling the remote method
        BigInteger result = calculator.add (BigInteger.ONE, BigInteger.ONE);
    }
}