From 66220ceae8a05348040aaf3867c4959cf745f33e Mon Sep 17 00:00:00 2001 From: matsfunk Date: Wed, 11 Nov 2015 08:07:00 +0100 Subject: [PATCH] added to the tests (clearer examples of functionality, additional tests) --- test/channel/TestChannelReceiver.java | 5 +- test/data/TestDataReceiver.java | 85 ++++++++++++++++ test/data/TestDataSender.java | 57 +++++++++++ .../TestMultiChannelReceiver.java | 63 ++++++++++++ test/multichannel/TestMultiChannelSender.java | 42 ++++++++ test/synchronization/TestSynchronization.java | 96 +++++++++++++++++++ test/tools/TestClientLister.java | 31 ++++++ 7 files changed, 378 insertions(+), 1 deletion(-) create mode 100644 test/data/TestDataReceiver.java create mode 100644 test/data/TestDataSender.java create mode 100644 test/multichannel/TestMultiChannelReceiver.java create mode 100644 test/multichannel/TestMultiChannelSender.java create mode 100644 test/synchronization/TestSynchronization.java create mode 100644 test/tools/TestClientLister.java diff --git a/test/channel/TestChannelReceiver.java b/test/channel/TestChannelReceiver.java index 52e4943..8f074b0 100644 --- a/test/channel/TestChannelReceiver.java +++ b/test/channel/TestChannelReceiver.java @@ -23,7 +23,10 @@ public void setup() { OOCSI oocsi = new OOCSI(this, ("receiver_" + Math.random()).toString().substring(0, 12), "localhost"); // subscribe to channel "testchannel" + // either the channel name is used for looking for a handler method... oocsi.subscribe("testchannel"); + // ... or the handler method name can be given explicitly + // oocsi.subscribe("testchannel", "testchannel"); } public void draw() { @@ -34,7 +37,7 @@ public void draw() { rect(20, position, 20, 20); } - public void handleOOCSIEvent(OOCSIEvent event) { + public void testchannel(OOCSIEvent event) { // save color value color = event.getInt("color", 0); diff --git a/test/data/TestDataReceiver.java b/test/data/TestDataReceiver.java new file mode 100644 index 0000000..6c986e3 --- /dev/null +++ b/test/data/TestDataReceiver.java @@ -0,0 +1,85 @@ +package data; + +import java.util.Date; + +import nl.tue.id.oocsi.OOCSI; +import nl.tue.id.oocsi.OOCSIEvent; +import processing.core.PApplet; +import processing.core.PVector; + +/** + * example application - receives color and position values from OOCSI, on the channel "datachannel" + * + * @author matsfunk + */ +@SuppressWarnings("serial") +public class TestDataReceiver extends PApplet { + + int numberInt = 0; + float numberFloat = 0; + double numberDouble = 0; + long numberLong = 0; + String numberString = ""; + PVector numberObject = new PVector(0, 0); + Date dateObject = new Date(); + + public void setup() { + size(200, 200); + stroke(120); + fill(120); + textSize(10f); + + // open connection to local OOCSI server + // (for more information how to run an OOCSI server refer to: https://iddi.github.io/oocsi/) + OOCSI oocsi = new OOCSI(this, ("receiver_" + Math.random()).toString().substring(0, 12), "localhost"); + + // subscribe to channel "datachannel" + // either the channel name is used for looking for a handler method... + oocsi.subscribe("datachannel"); + // ... or the handler method name can be given explicitly + // oocsi.subscribe("datachannel", "datachannel"); + } + + public void draw() { + background(255); + + fill(120); + rect(20, numberInt, 20, 20); + rect(50, numberFloat, 20, 20); + rect(80, (float) numberDouble, 20, 20); + rect(110, numberLong, 20, 20); + text(numberString, 20, 20); + text(dateObject.toString(), 20, height - 20); + fill(220); + rect(numberObject.x, numberObject.y, 20, 20); + } + + public void datachannel(OOCSIEvent event) { + + // get and save integer value + numberInt = event.getInt("integer", 0); + + // get and save float value + numberFloat = event.getFloat("float", 0); + + // get and save double value + numberDouble = event.getDouble("double", 0); + + // get and save long value + numberLong = event.getLong("long", 0); + + // get and save string value + numberString = event.getString("string"); + + // get, cast and save array value + float[] floatArray = (float[]) event.getObject("array"); + numberObject = new PVector(floatArray[0], floatArray[1]); + + // get, cast and save Date object value + dateObject = (Date) event.getObject("object"); + } + + public static void main(String[] args) { + PApplet.main(new String[] { "data.TestDataReceiver" }); + } +} diff --git a/test/data/TestDataSender.java b/test/data/TestDataSender.java new file mode 100644 index 0000000..3650037 --- /dev/null +++ b/test/data/TestDataSender.java @@ -0,0 +1,57 @@ +package data; + +import java.util.Date; + +import nl.tue.id.oocsi.OOCSI; +import processing.core.PApplet; + +/** + * example application - sends the mouse coordinates as color and position values to OOCSI, on the channel "datachannel" + * + * @author matsfunk + */ +@SuppressWarnings("serial") +public class TestDataSender extends PApplet { + + // reference to local OOCSI + OOCSI oocsi; + + public void setup() { + size(200, 200); + background(120); + frameRate(10); + + // open connection to local OOCSI + // (for more information how to run an OOCSI server refer to: https://iddi.github.io/oocsi/) + oocsi = new OOCSI(this, ("sender_" + Math.random()).toString().substring(0, 10), "localhost"); + } + + public void draw() { + + float number = map(mouseY, 0, height, 50, 150); + + // send a message to channel "datachannel" with the data items "color" + // and "position" + oocsi.channel("datachannel") + // integer type number + .data("integer", (int) number) + // float type number + .data("float", (float) number) + // double type number + .data("double", (double) number) + // long type number + .data("long", (long) number) + // number as string + .data("string", "" + number) + // number and mouseX as array + .data("array", new float[] { map(mouseX, 0, width, 20, width - 20), number }) + // number and mouseX as object + .data("object", new Date()) + // send all + .send(); + } + + public static void main(String[] args) { + PApplet.main(new String[] { "data.TestDataSender" }); + } +} diff --git a/test/multichannel/TestMultiChannelReceiver.java b/test/multichannel/TestMultiChannelReceiver.java new file mode 100644 index 0000000..29e6558 --- /dev/null +++ b/test/multichannel/TestMultiChannelReceiver.java @@ -0,0 +1,63 @@ +package multichannel; + +import nl.tue.id.oocsi.OOCSI; +import nl.tue.id.oocsi.OOCSIEvent; +import processing.core.PApplet; + +/** + * example application - receives color and position values from OOCSI, on the channel "testchannel" + * + * @author mfunk + * + */ +@SuppressWarnings("serial") +public class TestMultiChannelReceiver extends PApplet { + + int color1 = 255; + int color2 = 255; + int position1 = 0; + int position2 = 0; + + public void setup() { + size(200, 200); + + // open connection to local OOCSI + OOCSI oocsi = new OOCSI(this, ("receiver_" + Math.random()).toString().substring(0, 12), "localhost"); + + // subscribe to channel "testchannel1" and "testchannel2" + oocsi.subscribe("testchannel1"); + oocsi.subscribe("testchannel2"); + } + + public void draw() { + background(255); + + stroke(120); + fill(color1); + rect(20, position1, 20, 20); + fill(color2); + rect(width - 40, position2, 20, 20); + } + + public void testchannel1(OOCSIEvent event) { + + // save color value + color1 = event.getInt("color", 100); + + // save position value + position1 = event.getInt("position", 0); + } + + public void testchannel2(OOCSIEvent event) { + + // save color value + color2 = event.getInt("color", 0); + + // save position value + position2 = event.getInt("position", 0); + } + + public static void main(String[] args) { + PApplet.main(new String[] { "multichannel.TestMultiChannelReceiver" }); + } +} diff --git a/test/multichannel/TestMultiChannelSender.java b/test/multichannel/TestMultiChannelSender.java new file mode 100644 index 0000000..88e3121 --- /dev/null +++ b/test/multichannel/TestMultiChannelSender.java @@ -0,0 +1,42 @@ +package multichannel; + +import nl.tue.id.oocsi.OOCSI; +import processing.core.PApplet; + +/** + * example application - sends the mouse coordinates as color and position values to OOCSI, on the channel "testchannel" + * + * @author mfunk + * + */ +@SuppressWarnings("serial") +public class TestMultiChannelSender extends PApplet { + + // reference to local OOCSI + OOCSI oocsi; + + public void setup() { + size(200, 200); + background(120); + frameRate(10); + + // open connection to local OOCSI + oocsi = new OOCSI(this, ("sender_" + Math.random()).toString().substring(0, 10), "localhost"); + } + + public void draw() { + + // send a message to channel "testchannel1" with the data items "color" + // and "position" + oocsi.channel("testchannel1").data("color", (int) map(sin(frameCount / 30.f), -1, 1, 100, 255)) + .data("position", mouseY).send(); + // send a message to channel "testchannel2" with the data items "color" + // and "position" + oocsi.channel("testchannel2").data("color", (int) map(cos(frameCount / 30.f), -1, 1, 100, 255)) + .data("position", mouseX).send(); + } + + public static void main(String[] args) { + PApplet.main(new String[] { "multichannel.TestMultiChannelSender" }); + } +} diff --git a/test/synchronization/TestSynchronization.java b/test/synchronization/TestSynchronization.java new file mode 100644 index 0000000..c0736c4 --- /dev/null +++ b/test/synchronization/TestSynchronization.java @@ -0,0 +1,96 @@ +package synchronization; + +import nl.tue.id.oocsi.OOCSI; +import nl.tue.id.oocsi.OOCSIEvent; +import processing.core.PApplet; + +/** + * example application - synchronizes with all other clients in the same channel "syncChannel" via OOCSI + * + * @author matsfunk + */ +@SuppressWarnings("serial") +public class TestSynchronization extends PApplet { + + // reference to local OOCSI + OOCSI oocsi; + + // main synchronization variable + int framesTimeout = 0; + + // cycle duration + int duration = 30; + + // colors for locking visualization + int colorReset = 0; + + public void setup() { + size(200, 200); + background(0); + noStroke(); + frameRate(10); + + // open connection to local OOCSI + // (for more information how to run an OOCSI server refer to: https://iddi.github.io/oocsi/) + oocsi = new OOCSI(this, ("sync_" + Math.random()).toString().substring(0, 10) + System.currentTimeMillis(), + "localhost"); + + oocsi.subscribe("syncChannel", "syncChannel"); + } + + public void draw() { + + // fade away effect + rectMode(CORNERS); + fill(0, 20); + rect(0, 0, width, height); + + // on blink + if (framesTimeout-- <= 0) { + // draw rectangle + fill(255, 0, 100 + Math.max(0, colorReset)); + rectMode(CENTER); + rect(width / 2, height / 2, 100, 100); + + // send a message to channel "syncChannel" without any data + oocsi.channel("syncChannel").send(); + + // add randomness for variation + framesTimeout = (int) (duration + random(0, 2)); + } + } + + // sync channel handler, receives sync events and adjusts own timing accordingly + public void syncChannel(OOCSIEvent event) { + + // adjust own timing according to sync events sent by other clients + if (framesTimeout < 5) { + // subtract [0, 1] if only a little too late + framesTimeout -= random(0, 1); + } else if (framesTimeout > duration - 5) { + // add [0, 1] if a little too early + framesTimeout += random(0, 1); + } else { + // otherwise subtract more to adjust towards other timing + framesTimeout -= random(1, 2); + } + + // lock color if in sync with at least one other client + if (framesTimeout % 30 < 3) { + colorReset = 0; + } else { + // otherwise print out sync offset + println(framesTimeout); + } + } + + // allow for mouse presses to unsync a client + public void mousePressed() { + framesTimeout = 0; + colorReset = 155; + } + + public static void main(String[] args) { + PApplet.main(new String[] { "synchronization.TestSynchronization" }); + } +} diff --git a/test/tools/TestClientLister.java b/test/tools/TestClientLister.java new file mode 100644 index 0000000..1db0969 --- /dev/null +++ b/test/tools/TestClientLister.java @@ -0,0 +1,31 @@ +package tools; + +import nl.tue.id.oocsi.OOCSI; +import processing.core.PApplet; + +@SuppressWarnings("serial") +public class TestClientLister extends PApplet { + + // reference to local OOCSI + OOCSI oocsi; + + public void setup() { + size(200, 200); + noStroke(); + + // connect to OOCSI server running on the same machine (localhost) + // with the ID "clientLister" + oocsi = new OOCSI(this, "clientLister", "probe.id.tue.nl"); + + // frame rate of 1 let's this sketch check for clients every second + frameRate(1); + } + + public void draw() { + println("-----------------------------"); + + // retrieve and immediately print list of clients + println(oocsi.getClients()); + } + +}