Skip to content

Commit 6be0337

Browse files
committed
remove dependency on oscP5, add a UDP client and server for message passing between the sketch and the PDE
1 parent c27570f commit 6be0337

4 files changed

Lines changed: 258 additions & 54 deletions

File tree

pdex/src/galsasson/mode/tweak/Handle.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public class Handle {
3333
HProgressBar progBar = null;
3434
String textFormat;
3535

36-
int oscPort;
36+
// the client that sends the changes
37+
UDPTweakClient tweakClient;
3738

3839
public Handle(String t, String n, int vi, String v, int ti, int l, int sc, int ec, int dp)
3940
{
@@ -241,26 +242,26 @@ public void setColorBox(ColorControlBox box)
241242
colorBox = box;
242243
}
243244

244-
public void setOscPort(int port)
245+
public void setTweakClient(UDPTweakClient client)
245246
{
246-
oscPort = port;
247+
tweakClient = client;
247248
}
248249

249250
public void oscSendNewValue()
250251
{
251252
int index = varIndex;
252253
try {
253254
if (type == "int") {
254-
OSCSender.sendInt(index, newValue.intValue(), oscPort);
255+
tweakClient.sendInt(index, newValue.intValue());
255256
}
256257
else if (type == "hex") {
257-
OSCSender.sendInt(index, newValue.intValue(), oscPort);
258+
tweakClient.sendInt(index, newValue.intValue());
258259
}
259260
else if (type == "webcolor") {
260-
OSCSender.sendInt(index, newValue.intValue(), oscPort);
261+
tweakClient.sendInt(index, newValue.intValue());
261262
}
262263
else if (type == "float") {
263-
OSCSender.sendFloat(index, newValue.floatValue(), oscPort);
264+
tweakClient.sendFloat(index, newValue.floatValue());
264265
}
265266
} catch (Exception e) { System.out.println("error sending OSC message!"); }
266267
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package galsasson.mode.tweak;
2+
3+
import java.net.*;
4+
import java.nio.ByteBuffer;
5+
6+
public class UDPTweakClient {
7+
private DatagramSocket socket;
8+
private InetAddress address;
9+
private boolean initialized;
10+
private int sketchPort;
11+
12+
static final int VAR_INT = 0;
13+
static final int VAR_FLOAT = 1;
14+
static final int SHUTDOWN = 0xffffffff;
15+
16+
public UDPTweakClient(int sketchPort)
17+
{
18+
this.sketchPort = sketchPort;
19+
20+
try {
21+
socket = new DatagramSocket();
22+
// only local sketch is allowed
23+
address = InetAddress.getByName("127.0.0.1");
24+
initialized = true;
25+
}
26+
catch (SocketException e) {
27+
initialized = false;
28+
}
29+
catch (UnknownHostException e) {
30+
socket.close();
31+
initialized = false;
32+
}
33+
catch (SecurityException e) {
34+
socket.close();
35+
initialized = false;
36+
}
37+
}
38+
39+
public void shutdown()
40+
{
41+
if (!initialized) {
42+
return;
43+
}
44+
45+
// send shutdown to the sketch
46+
sendShutdown();
47+
initialized = false;
48+
}
49+
50+
public boolean sendInt(int index, int val)
51+
{
52+
if (!initialized) {
53+
return false;
54+
}
55+
56+
try {
57+
byte[] buf = new byte[12];
58+
ByteBuffer bb = ByteBuffer.wrap(buf);
59+
bb.putInt(0, VAR_INT);
60+
bb.putInt(4, index);
61+
bb.putInt(8, val);
62+
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
63+
socket.send(packet);
64+
}
65+
catch (Exception e) {
66+
return false;
67+
}
68+
69+
return true;
70+
}
71+
72+
public boolean sendFloat(int index, float val)
73+
{
74+
if (!initialized) {
75+
return false;
76+
}
77+
78+
try {
79+
byte[] buf = new byte[12];
80+
ByteBuffer bb = ByteBuffer.wrap(buf);
81+
bb.putInt(0, VAR_FLOAT);
82+
bb.putInt(4, index);
83+
bb.putFloat(8, val);
84+
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
85+
socket.send(packet);
86+
}
87+
catch (Exception e) {
88+
return false;
89+
}
90+
91+
return true;
92+
}
93+
94+
public boolean sendShutdown()
95+
{
96+
if (!initialized) {
97+
return false;
98+
}
99+
100+
try {
101+
byte[] buf = new byte[12];
102+
ByteBuffer bb = ByteBuffer.wrap(buf);
103+
bb.putInt(0, SHUTDOWN);
104+
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
105+
socket.send(packet);
106+
}
107+
catch (Exception e) {
108+
return false;
109+
}
110+
111+
return true;
112+
}
113+
114+
public static String getServerCode(int listenPort, boolean hasInts, boolean hasFloats)
115+
{
116+
String serverCode = ""+
117+
"class TweakModeServer extends Thread\n"+
118+
"{\n"+
119+
" protected DatagramSocket socket = null;\n"+
120+
" protected boolean running = true;\n"+
121+
" final int INT_VAR = 0;\n"+
122+
" final int FLOAT_VAR = 1;\n"+
123+
" final int SHUTDOWN = 0xffffffff;\n"+
124+
" public TweakModeServer() {\n"+
125+
" this(\"TweakModeServer\");\n"+
126+
" }\n"+
127+
" public TweakModeServer(String name) {\n"+
128+
" super(name);\n"+
129+
" }\n"+
130+
" public void setup()\n"+
131+
" {\n"+
132+
" try {\n"+
133+
" socket = new DatagramSocket("+listenPort+");\n"+
134+
" socket.setSoTimeout(250);\n"+
135+
" } catch (IOException e) {\n"+
136+
" println(\"error: could not create TweakMode server socket\");\n"+
137+
" }\n"+
138+
" }\n"+
139+
" public void run()\n"+
140+
" {\n"+
141+
" byte[] buf = new byte[256];\n"+
142+
" while(running)\n"+
143+
" {\n"+
144+
" try {\n"+
145+
" DatagramPacket packet = new DatagramPacket(buf, buf.length);\n"+
146+
" socket.receive(packet);\n"+
147+
" ByteBuffer bb = ByteBuffer.wrap(buf);\n"+
148+
" int type = bb.getInt(0);\n"+
149+
" int index = bb.getInt(4);\n";
150+
151+
if (hasInts) {
152+
serverCode +=
153+
" if (type == INT_VAR) {\n"+
154+
" int val = bb.getInt(8);\n"+
155+
" tweakmode_int[index] = val;\n"+
156+
" }\n"+
157+
" else ";
158+
}
159+
if (hasFloats) {
160+
serverCode +=
161+
" if (type == FLOAT_VAR) {\n"+
162+
" float val = bb.getFloat(8);\n"+
163+
" tweakmode_float[index] = val;\n"+
164+
" }\n"+
165+
" else";
166+
}
167+
serverCode +=
168+
" if (type == SHUTDOWN) {\n"+
169+
" running = false;\n"+
170+
" }\n"+
171+
" } catch (SocketTimeoutException e) {\n"+
172+
" // nothing to do here just try receiving again\n"+
173+
" } catch (Exception e) {\n"+
174+
" }\n"+
175+
" }\n"+
176+
" socket.close();\n"+
177+
" }\n"+
178+
"}\n\n\n";
179+
180+
return serverCode;
181+
}
182+
}

0 commit comments

Comments
 (0)