diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /jrt/examples |
Publish
Diffstat (limited to 'jrt/examples')
-rw-r--r-- | jrt/examples/.gitignore | 1 | ||||
-rw-r--r-- | jrt/examples/SimpleClient.java | 27 | ||||
-rw-r--r-- | jrt/examples/SimpleServer.java | 34 |
3 files changed, 62 insertions, 0 deletions
diff --git a/jrt/examples/.gitignore b/jrt/examples/.gitignore new file mode 100644 index 00000000000..90b07e9d451 --- /dev/null +++ b/jrt/examples/.gitignore @@ -0,0 +1 @@ +classes diff --git a/jrt/examples/SimpleClient.java b/jrt/examples/SimpleClient.java new file mode 100644 index 00000000000..cfacb92f8ff --- /dev/null +++ b/jrt/examples/SimpleClient.java @@ -0,0 +1,27 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +import com.yahoo.jrt.*; + +public class SimpleClient { + + public static void main(String args[]) { + if (args.length != 3) { + System.err.println("usage: SimpleClient spec n1 n2"); + System.exit(1); + } + Supervisor supervisor = new Supervisor(new Transport()); + Target target = supervisor.connect(new Spec(args[0])); + Request req = new Request("add"); + req.parameters().add(new Int32Value(Integer.parseInt(args[1]))); + req.parameters().add(new Int32Value(Integer.parseInt(args[2]))); + target.invokeSync(req, 5.0); + if (req.checkReturnTypes("i")) { + System.out.println(args[1] + " + " + args[2] + " = " + + req.returnValues().get(0).asInt32()); + } else { + System.out.println("Invocation failed: " + + req.errorCode() + ": " + req.errorMessage()); + } + target.close(); + supervisor.transport().shutdown().join(); + } +} diff --git a/jrt/examples/SimpleServer.java b/jrt/examples/SimpleServer.java new file mode 100644 index 00000000000..f387e2916c1 --- /dev/null +++ b/jrt/examples/SimpleServer.java @@ -0,0 +1,34 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +import com.yahoo.jrt.*; + +public class SimpleServer { + + public void rpc_add(Request req) { + int n1 = req.parameters().get(0).asInt32(); + int n2 = req.parameters().get(1).asInt32(); + req.returnValues().add(new Int32Value(n1 + n2)); + } + + public static void main(String args[]) { + if (args.length != 1) { + System.err.println("usage: SimpleServer spec"); + System.exit(1); + } + Supervisor supervisor = new Supervisor(new Transport()); + supervisor.addMethod(new Method("add", "ii", "i", + new SimpleServer(), "rpc_add") + .methodDesc("calculate the sum of 2 integers") + .paramDesc(0, "n1", "an integer") + .paramDesc(1, "n2", "another integer") + .returnDesc(0, "ret", "n1 + n2")); + try { + Acceptor acceptor = supervisor.listen(new Spec(args[0])); + System.out.println("Listening at " + args[0]); + supervisor.transport().join(); + acceptor.shutdown().join(); + } catch (ListenFailedException e) { + System.err.println("Could not listen at " + args[0]); + supervisor.transport().shutdown().join(); + } + } +} |