reorganization of methods

This commit is contained in:
Ben Fry
2015-05-12 18:44:04 -04:00
parent 7058249a47
commit 05aae468df

View File

@@ -23,6 +23,7 @@ package processing.mode.java.tweak;
import java.net.*;
import java.nio.ByteBuffer;
public class UDPTweakClient {
private DatagramSocket socket;
private InetAddress address;
@@ -33,106 +34,91 @@ public class UDPTweakClient {
static final int VAR_FLOAT = 1;
static final int SHUTDOWN = 0xffffffff;
public UDPTweakClient(int sketchPort)
{
public UDPTweakClient(int sketchPort) {
this.sketchPort = sketchPort;
try {
socket = new DatagramSocket();
// only local sketch is allowed
address = InetAddress.getByName("127.0.0.1");
address = InetAddress.getByName("127.0.0.1");
initialized = true;
}
catch (SocketException e) {
} catch (SocketException e) {
initialized = false;
}
catch (UnknownHostException e) {
} catch (UnknownHostException e) {
socket.close();
initialized = false;
}
catch (SecurityException e) {
} catch (SecurityException e) {
socket.close();
initialized = false;
}
}
public void shutdown()
{
if (!initialized) {
return;
public void shutdown() {
if (initialized) {
// send shutdown to the sketch
sendShutdown();
initialized = false;
}
// send shutdown to the sketch
sendShutdown();
initialized = false;
}
public boolean sendInt(int index, int val)
{
if (!initialized) {
return false;
public boolean sendInt(int index, int val) {
if (initialized) {
try {
byte[] buf = new byte[12];
ByteBuffer bb = ByteBuffer.wrap(buf);
bb.putInt(0, VAR_INT);
bb.putInt(4, index);
bb.putInt(8, val);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
socket.send(packet);
return true;
} catch (Exception e) { }
}
try {
byte[] buf = new byte[12];
ByteBuffer bb = ByteBuffer.wrap(buf);
bb.putInt(0, VAR_INT);
bb.putInt(4, index);
bb.putInt(8, val);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
socket.send(packet);
}
catch (Exception e) {
return false;
}
return true;
return false;
}
public boolean sendFloat(int index, float val)
{
if (!initialized) {
return false;
}
public boolean sendFloat(int index, float val) {
if (initialized) {
try {
byte[] buf = new byte[12];
ByteBuffer bb = ByteBuffer.wrap(buf);
bb.putInt(0, VAR_FLOAT);
bb.putInt(4, index);
bb.putFloat(8, val);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
socket.send(packet);
}
catch (Exception e) {
return false;
}
return true;
}
public boolean sendShutdown()
{
if (!initialized) {
return false;
socket.send(new DatagramPacket(buf, buf.length, address, sketchPort));
return true;
} catch (Exception e) { }
}
return false;
}
public boolean sendShutdown() {
if (initialized) {
try {
byte[] buf = new byte[12];
ByteBuffer bb = ByteBuffer.wrap(buf);
bb.putInt(0, SHUTDOWN);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, sketchPort);
socket.send(packet);
}
catch (Exception e) {
return false;
}
return true;
socket.send(new DatagramPacket(buf, buf.length, address, sketchPort));
return true;
} catch (Exception e) { }
}
return false;
}
public static String getServerCode(int listenPort, boolean hasInts, boolean hasFloats)
{
static public String getServerCode(int listenPort,
boolean hasInts, boolean hasFloats) {
String serverCode = ""+
"class TweakModeServer extends Thread\n"+
"{\n"+
@@ -156,18 +142,18 @@ public class UDPTweakClient {
" println(\"error: could not create TweakMode server socket\");\n"+
" }\n"+
" }\n"+
" public void run()\n"+
" public void run()\n"+
" {\n"+
" byte[] buf = new byte[256];\n"+
" byte[] buf = new byte[256];\n"+
" while(running)\n"+
" {\n"+
" try {\n"+
" DatagramPacket packet = new DatagramPacket(buf, buf.length);\n"+
" socket.receive(packet);\n"+
" socket.receive(packet);\n"+
" ByteBuffer bb = ByteBuffer.wrap(buf);\n"+
" int type = bb.getInt(0);\n"+
" int index = bb.getInt(4);\n";
if (hasInts) {
serverCode +=
" if (type == INT_VAR) {\n"+
@@ -196,7 +182,7 @@ public class UDPTweakClient {
" socket.close();\n"+
" }\n"+
"}\n\n\n";
return serverCode;
}
}