Java Example


// -------------------------------------------------
// Create an API and Service that implements the API
// -------------------------------------------------

public interface GreetingApi {
    String greet(String name);
}

public class GreetingService implements GreetingApi {
    @Override
    public String greet(String name) {
        System.out.println("Hello from " + name);
        return "Hello, " + name + "!";
    }
}

// --------------------------------
// KDC TCP Server Parameters
// --------------------------------

TcpServerParameters parameters =
        new TcpServerParameters.Builder()
                .withBindAddress("192.0.2.10", 5000)
                .withCompressed(true)
                .registerApi(GreetingApi.class, new GreetingService())
                .build();

TcpServer server = new TcpServer(parameters);

// -----------
// KDC Agent
// -----------

public class GreetingAgent extends Agent {

    private String name;

    public GreetingAgent(String name) {
        this.name = name;
    }

    public Reply visit(GreetingApi api) {

        Reply reply = new Reply();
        reply.setData("message", api.greet(name));
        return reply;

    }
}

// -------------
// KDC Client
// -------------

TcpClientParameters parameters =
        new TcpClientParameters.Builder()
                .withRemoteAddress(("192.0.2.10", 5000)
                .withCompressed(true)
                .build();

TcpClient client = new TcpClient(parameters);

GreetingAgent agent = new GreetingAgent("Chuck");
Reply reply = client.sendSync(agent);
System.out.println(reply.getData("message", String.class));

// --------------------------------------------------
// Notes:
// --------------------------------------------------
// You can create and return complex replies
// You can use enumeration for setData() and getData()
// in place of strings to avoid errors because of 
// typos.
// --------------------------------------------------