From c8693857ceaf837cebf63a3e2d5e73dbfbfafd26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Mon, 21 May 2012 13:41:53 +0200 Subject: Add stats server. --- .../java/com/orbekk/stats/Experiment1Impl.java | 41 ++++++++++++++++++++++ .../src/main/java/com/orbekk/stats/Server.java | 29 +++++++++++++++ .../src/main/java/com/orbekk/stats/TestClient.java | 40 +++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 statserver/src/main/java/com/orbekk/stats/Experiment1Impl.java create mode 100644 statserver/src/main/java/com/orbekk/stats/Server.java create mode 100644 statserver/src/main/java/com/orbekk/stats/TestClient.java (limited to 'statserver/src/main') diff --git a/statserver/src/main/java/com/orbekk/stats/Experiment1Impl.java b/statserver/src/main/java/com/orbekk/stats/Experiment1Impl.java new file mode 100644 index 0000000..1ec4ff3 --- /dev/null +++ b/statserver/src/main/java/com/orbekk/stats/Experiment1Impl.java @@ -0,0 +1,41 @@ +package com.orbekk.stats; + +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; + +import com.google.protobuf.RpcCallback; +import com.google.protobuf.RpcController; +import com.orbekk.stats.Experiments.Empty; +import com.orbekk.stats.Experiments.Experiment1; +import com.orbekk.stats.Experiments.SimpleTiming; + +public class Experiment1Impl extends Experiment1 { + List samples = new ArrayList(); + + @Override + public synchronized void registerSample(RpcController controller, SimpleTiming request, + RpcCallback done) { + samples.add(request); + done.run(Empty.getDefaultInstance()); + } + + public void writeSamples(String filename) { + PrintWriter printer = null; + try { + printer = new PrintWriter(filename); + System.out.println("Writing results to " + filename); + printer.println("# Automatically generated by Experiment1Impl.java"); + for (SimpleTiming timing : samples) { + printer.println(timing); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } finally { + if (printer != null) { + printer.close(); + } + } + } +} diff --git a/statserver/src/main/java/com/orbekk/stats/Server.java b/statserver/src/main/java/com/orbekk/stats/Server.java new file mode 100644 index 0000000..4f5ff32 --- /dev/null +++ b/statserver/src/main/java/com/orbekk/stats/Server.java @@ -0,0 +1,29 @@ +package com.orbekk.stats; + +import com.orbekk.protobuf.SimpleProtobufServer; + +public class Server { + final Experiment1Impl exp1 = new Experiment1Impl(); + + public static void main(String[] args) { + new Server().run(); + } + + public void run() { + addShutdownHook(); + SimpleProtobufServer server = SimpleProtobufServer.create( + Common.PORT); + server.registerService(exp1); + System.out.println("Waiting for samples..."); + server.start(); + } + + public void addShutdownHook() { + class ShutdownTask implements Runnable { + @Override public void run() { + exp1.writeSamples("experiment1.data"); + } + } + Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownTask())); + } +} diff --git a/statserver/src/main/java/com/orbekk/stats/TestClient.java b/statserver/src/main/java/com/orbekk/stats/TestClient.java new file mode 100644 index 0000000..b7fb595 --- /dev/null +++ b/statserver/src/main/java/com/orbekk/stats/TestClient.java @@ -0,0 +1,40 @@ +package com.orbekk.stats; + +import java.io.IOException; +import java.net.UnknownHostException; + +import com.google.protobuf.RpcCallback; +import com.orbekk.protobuf.Rpc; +import com.orbekk.protobuf.RpcChannel; +import com.orbekk.stats.Experiments.Empty; +import com.orbekk.stats.Experiments.Experiment1; +import com.orbekk.stats.Experiments.SimpleTiming; + +public class TestClient { + public static void main(String[] args) { + RpcChannel channel = null; + try { + RpcCallback done = new RpcCallback() { + @Override public void run(Empty unused) { + } + }; + channel = RpcChannel.create("localhost", Common.PORT); + Experiment1 exp1 = Experiment1Impl.newStub(channel); + Rpc rpc = new Rpc(); + rpc.setTimeout(5000); + SimpleTiming timing = SimpleTiming.newBuilder() + .setTiming(1337.0) + .setNumDevices(0) + .build(); + exp1.registerSample(rpc, timing, done); + } catch (UnknownHostException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (channel != null) { + channel.close(); + } + } + } +} -- cgit v1.2.3