From 4708ba199166d777d0b60879398a8e000f7aaf56 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Thu, 14 Nov 2019 19:13:32 +0100 Subject: Setup simple api --- build.gradle | 5 +++ scripts/registry | 4 ++ .../java/pl/edu/mimuw/cloudatlas/agent/Agent.java | 22 ++++++++++ .../java/pl/edu/mimuw/cloudatlas/agent/Api.java | 11 +++++ .../mimuw/cloudatlas/agent/ApiImplementation.java | 23 ++++++++++ .../pl/edu/mimuw/cloudatlas/agent/AgentTest.java | 50 ++++++++++++++++++++++ 6 files changed, 115 insertions(+) create mode 100755 scripts/registry create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/Api.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java create mode 100644 src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java diff --git a/build.gradle b/build.gradle index 34701c0..8294b27 100644 --- a/build.gradle +++ b/build.gradle @@ -42,3 +42,8 @@ application { run { standardInput = System.in } + +task runAgent(type: JavaExec) { + classpath = sourceSets.main.runtimeClasspath + main = 'pl.edu.mimuw.cloudatlas.agent.Agent' +} diff --git a/scripts/registry b/scripts/registry new file mode 100755 index 0000000..afca7d6 --- /dev/null +++ b/scripts/registry @@ -0,0 +1,4 @@ +#!/bin/bash + +cd build/classes/java/main +rmiregistry -J-Djava.rmi.server.codebase="http://localhost:1099/" diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java new file mode 100644 index 0000000..79df04e --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java @@ -0,0 +1,22 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import java.rmi.RemoteException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; + +public class Agent { + public static void main(String[] args) { + try { + ApiImplementation api = new ApiImplementation(); + Api apiStub = + (Api) UnicastRemoteObject.exportObject(api, 0); + Registry registry = LocateRegistry.getRegistry(); + registry.rebind("Api", apiStub); + System.out.println("Api bound"); + } catch (Exception e) { + System.err.println("Agent exception:"); + e.printStackTrace(); + } + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Api.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Api.java new file mode 100644 index 0000000..c0003a8 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Api.java @@ -0,0 +1,11 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +import pl.edu.mimuw.cloudatlas.model.ValueSet; + +public interface Api extends Remote { + public int ping(int n) throws RemoteException; + public ValueSet getZones() throws RemoteException; +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java new file mode 100644 index 0000000..fc7532d --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/ApiImplementation.java @@ -0,0 +1,23 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import java.rmi.RemoteException; + +import java.util.Set; +import java.util.HashSet; + +import pl.edu.mimuw.cloudatlas.model.Value; +import pl.edu.mimuw.cloudatlas.model.ValueSet; +import pl.edu.mimuw.cloudatlas.model.ValueNull; +import pl.edu.mimuw.cloudatlas.model.TypePrimitive; + +public class ApiImplementation implements Api { + public int ping(int n) throws RemoteException { + return n + 1; + } + + public ValueSet getZones() throws RemoteException { + Set set = new HashSet(); + set.add(ValueNull.getInstance()); + return new ValueSet(set, TypePrimitive.STRING); + } +} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java new file mode 100644 index 0000000..a185649 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/AgentTest.java @@ -0,0 +1,50 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +import java.lang.Runtime; +import java.lang.Process; +import java.lang.Thread; + +import java.io.InputStream; + +import java.rmi.registry.LocateRegistry; +import java.rmi.RemoteException; +import java.rmi.registry.Registry; +import java.math.BigDecimal; + +public class AgentTest { + private static Process registryProcess; + private static Process agentProcess; + + @BeforeClass + public static void bindApi() throws Exception { + registryProcess = Runtime.getRuntime().exec("./scripts/registry"); + Thread.sleep(1000); + agentProcess = Runtime.getRuntime().exec("./gradlew runAgent"); + Thread.sleep(1000); + } + + @AfterClass + public static void killProcesses() throws Exception { + registryProcess.destroy(); + agentProcess.destroy(); + } + + @Test + public void testPing() { + try { + Registry registry = LocateRegistry.getRegistry("localhost"); + Api api = (Api) registry.lookup("Api"); + int res = api.ping(10); + assertEquals(11, res); + } catch (Exception e) { + System.err.println("FibonacciClient exception:"); + e.printStackTrace(); + assertTrue(false); + } + } +} -- cgit v1.2.3