2323import java .net .*;
2424import java .nio .ByteBuffer ;
2525
26+
2627public class UDPTweakClient {
2728 private DatagramSocket socket ;
2829 private InetAddress address ;
@@ -33,106 +34,91 @@ public class UDPTweakClient {
3334 static final int VAR_FLOAT = 1 ;
3435 static final int SHUTDOWN = 0xffffffff ;
3536
36- public UDPTweakClient ( int sketchPort )
37- {
37+
38+ public UDPTweakClient ( int sketchPort ) {
3839 this .sketchPort = sketchPort ;
39-
40+
4041 try {
4142 socket = new DatagramSocket ();
4243 // only local sketch is allowed
43- address = InetAddress .getByName ("127.0.0.1" );
44+ address = InetAddress .getByName ("127.0.0.1" );
4445 initialized = true ;
45- }
46- catch (SocketException e ) {
46+
47+ } catch (SocketException e ) {
4748 initialized = false ;
48- }
49- catch (UnknownHostException e ) {
49+
50+ } catch (UnknownHostException e ) {
5051 socket .close ();
5152 initialized = false ;
52- }
53- catch (SecurityException e ) {
53+
54+ } catch (SecurityException e ) {
5455 socket .close ();
5556 initialized = false ;
5657 }
5758 }
58-
59- public void shutdown ()
60- {
61- if (!initialized ) {
62- return ;
59+
60+
61+ public void shutdown () {
62+ if (initialized ) {
63+ // send shutdown to the sketch
64+ sendShutdown ();
65+ initialized = false ;
6366 }
64-
65- // send shutdown to the sketch
66- sendShutdown ();
67- initialized = false ;
6867 }
69-
70- public boolean sendInt (int index , int val )
71- {
72- if (!initialized ) {
73- return false ;
68+
69+
70+ public boolean sendInt (int index , int val ) {
71+ if (initialized ) {
72+ try {
73+ byte [] buf = new byte [12 ];
74+ ByteBuffer bb = ByteBuffer .wrap (buf );
75+ bb .putInt (0 , VAR_INT );
76+ bb .putInt (4 , index );
77+ bb .putInt (8 , val );
78+ DatagramPacket packet = new DatagramPacket (buf , buf .length , address , sketchPort );
79+ socket .send (packet );
80+ return true ;
81+
82+ } catch (Exception e ) { }
7483 }
75-
76- try {
77- byte [] buf = new byte [12 ];
78- ByteBuffer bb = ByteBuffer .wrap (buf );
79- bb .putInt (0 , VAR_INT );
80- bb .putInt (4 , index );
81- bb .putInt (8 , val );
82- DatagramPacket packet = new DatagramPacket (buf , buf .length , address , sketchPort );
83- socket .send (packet );
84- }
85- catch (Exception e ) {
86- return false ;
87- }
88-
89- return true ;
84+ return false ;
9085 }
91-
92- public boolean sendFloat (int index , float val )
93- {
94- if (!initialized ) {
95- return false ;
96- }
97-
86+
87+
88+ public boolean sendFloat (int index , float val ) {
89+ if (initialized ) {
9890 try {
9991 byte [] buf = new byte [12 ];
10092 ByteBuffer bb = ByteBuffer .wrap (buf );
10193 bb .putInt (0 , VAR_FLOAT );
10294 bb .putInt (4 , index );
10395 bb .putFloat (8 , val );
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 boolean sendShutdown ()
115- {
116- if (!initialized ) {
117- return false ;
96+ socket .send (new DatagramPacket (buf , buf .length , address , sketchPort ));
97+ return true ;
98+
99+ } catch (Exception e ) { }
118100 }
119-
101+ return false ;
102+ }
103+
104+
105+ public boolean sendShutdown () {
106+ if (initialized ) {
120107 try {
121108 byte [] buf = new byte [12 ];
122109 ByteBuffer bb = ByteBuffer .wrap (buf );
123110 bb .putInt (0 , SHUTDOWN );
124- DatagramPacket packet = new DatagramPacket (buf , buf .length , address , sketchPort );
125- socket .send (packet );
126- }
127- catch (Exception e ) {
128- return false ;
129- }
130-
131- return true ;
111+ socket .send (new DatagramPacket (buf , buf .length , address , sketchPort ));
112+ return true ;
113+
114+ } catch (Exception e ) { }
115+ }
116+ return false ;
132117 }
133-
134- public static String getServerCode (int listenPort , boolean hasInts , boolean hasFloats )
135- {
118+
119+
120+ static public String getServerCode (int listenPort ,
121+ boolean hasInts , boolean hasFloats ) {
136122 String serverCode = "" +
137123 "class TweakModeServer extends Thread\n " +
138124 "{\n " +
@@ -156,18 +142,18 @@ public static String getServerCode(int listenPort, boolean hasInts, boolean hasF
156142 " println(\" error: could not create TweakMode server socket\" );\n " +
157143 " }\n " +
158144 " }\n " +
159- " public void run()\n " +
145+ " public void run()\n " +
160146 " {\n " +
161- " byte[] buf = new byte[256];\n " +
147+ " byte[] buf = new byte[256];\n " +
162148 " while(running)\n " +
163149 " {\n " +
164150 " try {\n " +
165151 " DatagramPacket packet = new DatagramPacket(buf, buf.length);\n " +
166- " socket.receive(packet);\n " +
152+ " socket.receive(packet);\n " +
167153 " ByteBuffer bb = ByteBuffer.wrap(buf);\n " +
168154 " int type = bb.getInt(0);\n " +
169155 " int index = bb.getInt(4);\n " ;
170-
156+
171157 if (hasInts ) {
172158 serverCode +=
173159 " if (type == INT_VAR) {\n " +
@@ -196,7 +182,7 @@ public static String getServerCode(int listenPort, boolean hasInts, boolean hasF
196182 " socket.close();\n " +
197183 " }\n " +
198184 "}\n \n \n " ;
199-
185+
200186 return serverCode ;
201187 }
202188}
0 commit comments