FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/java/src/processing/mode/java/tweak/TweakClient.java at master · dimr/processing · GitHub
dimr
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
java
/
src
/
processing
/
mode
/
java
/
tweak
/
TweakClient.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
188 lines (158 loc) · 5.02 KB
Breadcrumbs
processing
/
java
/
src
/
processing
/
mode
/
java
/
tweak
/
TweakClient.java
Copy path
File metadata and controls
188 lines (158 loc) · 5.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package
processing
.
mode
.
java
.
tweak
;
import
java
.
net
.*;
import
java
.
nio
.
ByteBuffer
;
public
class
TweakClient
{
private
DatagramSocket
socket
;
private
InetAddress
address
;
private
boolean
initialized
;
private
int
sketchPort
;
static
final
int
VAR_INT
=
0
;
static
final
int
VAR_FLOAT
=
1
;
static
final
int
SHUTDOWN
=
0xffffffff
;
public
TweakClient
(
int
sketchPort
) {
this
.
sketchPort
=
sketchPort
;
try
{
socket
=
new
DatagramSocket
();
// only local sketch is allowed
address
=
InetAddress
.
getByName
(
"127.0.0.1"
);
initialized
=
true
;
}
catch
(
SocketException
e
) {
initialized
=
false
;
}
catch
(
UnknownHostException
e
) {
socket
.
close
();
initialized
=
false
;
}
catch
(
SecurityException
e
) {
socket
.
close
();
initialized
=
false
;
}
}
public
void
shutdown
() {
if
(
initialized
) {
// send shutdown to the sketch
sendShutdown
();
initialized
=
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
) { }
}
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
);
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
);
socket
.
send
(
new
DatagramPacket
(
buf
,
buf
.
length
,
address
,
sketchPort
));
return
true
;
}
catch
(
Exception
e
) { }
}
return
false
;
}
static
public
String
getServerCode
(
int
listenPort
,
boolean
hasInts
,
boolean
hasFloats
) {
String
serverCode
=
""
+
"class TweakModeServer extends Thread
\n
"
+
"{
\n
"
+
" protected DatagramSocket socket = null;
\n
"
+
" protected boolean running = true;
\n
"
+
" final int INT_VAR = 0;
\n
"
+
" final int FLOAT_VAR = 1;
\n
"
+
" final int SHUTDOWN = 0xffffffff;
\n
"
+
" public TweakModeServer() {
\n
"
+
" this(
\"
TweakModeServer
\"
);
\n
"
+
" }
\n
"
+
" public TweakModeServer(String name) {
\n
"
+
" super(name);
\n
"
+
" }
\n
"
+
" public void setup()
\n
"
+
" {
\n
"
+
" try {
\n
"
+
" socket = new DatagramSocket("
+
listenPort
+
");
\n
"
+
" socket.setSoTimeout(250);
\n
"
+
" } catch (IOException e) {
\n
"
+
" println(
\"
error: could not create TweakMode server socket
\"
);
\n
"
+
" }
\n
"
+
" }
\n
"
+
" public void run()
\n
"
+
" {
\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
"
+
" 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
"
+
" int val = bb.getInt(8);
\n
"
+
" tweakmode_int[index] = val;
\n
"
+
" }
\n
"
+
" else "
;
}
if
(
hasFloats
) {
serverCode
+=
" if (type == FLOAT_VAR) {
\n
"
+
" float val = bb.getFloat(8);
\n
"
+
" tweakmode_float[index] = val;
\n
"
+
" }
\n
"
+
" else"
;
}
serverCode
+=
" if (type == SHUTDOWN) {
\n
"
+
" running = false;
\n
"
+
" }
\n
"
+
" } catch (SocketTimeoutException e) {
\n
"
+
" // nothing to do here just try receiving again
\n
"
+
" } catch (Exception e) {
\n
"
+
" }
\n
"
+
" }
\n
"
+
" socket.close();
\n
"
+
" }
\n
"
+
"}
\n
\n
\n
"
;
return
serverCode
;
}
}
Back
|
FazBrowse Home
|
New Git URL