Skip to content

Commit

Permalink
added to the tests (clearer examples of functionality, additional tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
matsfunk committed Nov 11, 2015
1 parent a8c7db9 commit 66220ce
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 1 deletion.
5 changes: 4 additions & 1 deletion test/channel/TestChannelReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
85 changes: 85 additions & 0 deletions test/data/TestDataReceiver.java
Original file line number Diff line number Diff line change
@@ -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" });
}
}
57 changes: 57 additions & 0 deletions test/data/TestDataSender.java
Original file line number Diff line number Diff line change
@@ -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" });
}
}
63 changes: 63 additions & 0 deletions test/multichannel/TestMultiChannelReceiver.java
Original file line number Diff line number Diff line change
@@ -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" });
}
}
42 changes: 42 additions & 0 deletions test/multichannel/TestMultiChannelSender.java
Original file line number Diff line number Diff line change
@@ -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" });
}
}
96 changes: 96 additions & 0 deletions test/synchronization/TestSynchronization.java
Original file line number Diff line number Diff line change
@@ -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" });
}
}
31 changes: 31 additions & 0 deletions test/tools/TestClientLister.java
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 66220ce

Please sign in to comment.