propane 3.4.0-java → 3.7.0.pre-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.mvn/extensions.xml +1 -2
- data/.mvn/wrapper/MavenWrapperDownloader.java +2 -2
- data/.mvn/wrapper/maven-wrapper.properties +2 -2
- data/.travis.yml +2 -2
- data/CHANGELOG.md +12 -0
- data/Gemfile +2 -0
- data/README.md +17 -8
- data/Rakefile +10 -11
- data/bin/propane +3 -1
- data/lib/propane.rb +6 -4
- data/lib/propane/app.rb +20 -10
- data/lib/propane/creators/sketch_class.rb +7 -1
- data/lib/propane/creators/sketch_factory.rb +4 -2
- data/lib/propane/creators/sketch_writer.rb +1 -0
- data/lib/propane/helper_methods.rb +23 -24
- data/lib/propane/helpers/numeric.rb +2 -0
- data/lib/propane/helpers/version_error.rb +1 -0
- data/lib/propane/library.rb +5 -1
- data/lib/propane/library_loader.rb +2 -0
- data/lib/propane/native_folder.rb +10 -9
- data/lib/propane/native_loader.rb +3 -0
- data/lib/propane/runner.rb +14 -6
- data/lib/propane/version.rb +2 -1
- data/library/boids/boids.rb +21 -11
- data/library/color_group/color_group.rb +28 -0
- data/library/control_panel/control_panel.rb +8 -5
- data/library/dxf/dxf.rb +6 -0
- data/library/file_chooser/chooser.rb +10 -9
- data/library/file_chooser/file_chooser.rb +10 -9
- data/library/library_proxy/library_proxy.rb +2 -0
- data/library/net/net.rb +7 -0
- data/library/simplex_noise/simplex_noise.rb +2 -0
- data/library/slider/slider.rb +23 -22
- data/library/vector_utils/vector_utils.rb +4 -0
- data/library/video_event/video_event.rb +4 -1
- data/pom.rb +37 -36
- data/pom.xml +7 -7
- data/propane.gemspec +16 -12
- data/src/main/java/monkstone/ColorUtil.java +13 -1
- data/src/main/java/monkstone/MathToolModule.java +253 -203
- data/src/main/java/monkstone/PropaneLibrary.java +2 -2
- data/src/main/java/monkstone/fastmath/Deglut.java +1 -1
- data/src/main/java/monkstone/filechooser/Chooser.java +2 -1
- data/src/main/java/monkstone/noise/SimplexNoise.java +2 -2
- data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
- data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SliderBar.java +1 -1
- data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
- data/src/main/java/monkstone/slider/WheelHandler.java +7 -6
- data/src/main/java/monkstone/vecmath/package-info.java +1 -1
- data/src/main/java/monkstone/vecmath/vec2/Vec2.java +1 -1
- data/src/main/java/monkstone/vecmath/vec3/Vec3.java +3 -3
- data/src/main/java/monkstone/videoevent/CaptureEvent.java +27 -0
- data/src/main/java/monkstone/videoevent/{VideoInterface.java → MovieEvent.java} +11 -27
- data/src/main/java/monkstone/videoevent/package-info.java +1 -1
- data/src/main/java/processing/awt/PGraphicsJava2D.java +781 -285
- data/src/main/java/processing/awt/PImageAWT.java +377 -0
- data/src/main/java/processing/awt/PShapeJava2D.java +56 -52
- data/src/main/java/processing/awt/PSurfaceAWT.java +309 -209
- data/src/main/java/processing/awt/ShimAWT.java +581 -0
- data/src/main/java/processing/core/PApplet.java +4510 -4503
- data/src/main/java/processing/core/PConstants.java +477 -447
- data/src/main/java/processing/core/PFont.java +914 -880
- data/src/main/java/processing/core/PGraphics.java +193 -177
- data/src/main/java/processing/core/PImage.java +611 -309
- data/src/main/java/processing/core/PMatrix.java +172 -159
- data/src/main/java/processing/core/PMatrix2D.java +478 -415
- data/src/main/java/processing/core/PMatrix3D.java +762 -735
- data/src/main/java/processing/core/PShape.java +2888 -2652
- data/src/main/java/processing/core/PShapeOBJ.java +97 -92
- data/src/main/java/processing/core/PShapeSVG.java +1705 -1490
- data/src/main/java/processing/core/PStyle.java +40 -37
- data/src/main/java/processing/core/PSurface.java +139 -97
- data/src/main/java/processing/core/PSurfaceNone.java +296 -218
- data/src/main/java/processing/core/PVector.java +997 -965
- data/src/main/java/processing/core/ThinkDifferent.java +15 -13
- data/src/main/java/processing/data/DoubleDict.java +756 -710
- data/src/main/java/processing/data/DoubleList.java +749 -696
- data/src/main/java/processing/data/FloatDict.java +748 -702
- data/src/main/java/processing/data/FloatList.java +751 -697
- data/src/main/java/processing/data/IntDict.java +720 -673
- data/src/main/java/processing/data/IntList.java +699 -633
- data/src/main/java/processing/data/JSONArray.java +931 -873
- data/src/main/java/processing/data/JSONObject.java +1262 -1165
- data/src/main/java/processing/data/JSONTokener.java +351 -341
- data/src/main/java/processing/data/LongDict.java +710 -663
- data/src/main/java/processing/data/LongList.java +701 -635
- data/src/main/java/processing/data/Sort.java +37 -41
- data/src/main/java/processing/data/StringDict.java +525 -486
- data/src/main/java/processing/data/StringList.java +626 -580
- data/src/main/java/processing/data/Table.java +3690 -3510
- data/src/main/java/processing/data/TableRow.java +182 -183
- data/src/main/java/processing/data/XML.java +957 -883
- data/src/main/java/processing/dxf/RawDXF.java +404 -0
- data/src/main/java/processing/event/Event.java +87 -67
- data/src/main/java/processing/event/KeyEvent.java +48 -41
- data/src/main/java/processing/event/MouseEvent.java +88 -113
- data/src/main/java/processing/event/TouchEvent.java +10 -6
- data/src/main/java/processing/javafx/PGraphicsFX2D.java +20 -345
- data/src/main/java/processing/javafx/PSurfaceFX.java +149 -121
- data/src/main/java/processing/net/Client.java +744 -0
- data/src/main/java/processing/net/Server.java +388 -0
- data/src/main/java/processing/opengl/FontTexture.java +289 -270
- data/src/main/java/processing/opengl/FrameBuffer.java +386 -364
- data/src/main/java/processing/opengl/LinePath.java +547 -500
- data/src/main/java/processing/opengl/LineStroker.java +588 -581
- data/src/main/java/processing/opengl/PGL.java +3047 -2914
- data/src/main/java/processing/opengl/PGraphics2D.java +408 -315
- data/src/main/java/processing/opengl/PGraphics3D.java +107 -72
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12378 -12075
- data/src/main/java/processing/opengl/PJOGL.java +1753 -1670
- data/src/main/java/processing/opengl/PShader.java +1266 -1257
- data/src/main/java/processing/opengl/PShapeOpenGL.java +4678 -4580
- data/src/main/java/processing/opengl/PSurfaceJOGL.java +1114 -1027
- data/src/main/java/processing/opengl/Texture.java +1492 -1401
- data/src/main/java/processing/opengl/VertexBuffer.java +57 -55
- data/test/create_test.rb +21 -20
- data/test/deglut_spec_test.rb +4 -2
- data/test/helper_methods_test.rb +49 -20
- data/test/math_tool_test.rb +39 -32
- data/test/native_folder.rb +47 -0
- data/test/respond_to_test.rb +3 -2
- data/test/sketches/key_event.rb +2 -2
- data/test/sketches/library/my_library/my_library.rb +3 -0
- data/test/test_helper.rb +2 -0
- data/test/vecmath_spec_test.rb +35 -22
- data/vendors/Rakefile +33 -62
- metadata +56 -48
- data/src/main/java/processing/core/util/image/ImageLoadFacade.java +0 -161
- data/src/main/java/processing/core/util/image/ImageSaveFacade.java +0 -169
- data/src/main/java/processing/core/util/image/constants/TifConstants.java +0 -45
- data/src/main/java/processing/core/util/image/load/AwtImageLoadStrategy.java +0 -80
- data/src/main/java/processing/core/util/image/load/Base64StringImageLoadStrategy.java +0 -73
- data/src/main/java/processing/core/util/image/load/FallbackImageLoadStrategy.java +0 -70
- data/src/main/java/processing/core/util/image/load/ImageIoImageLoadStrategy.java +0 -132
- data/src/main/java/processing/core/util/image/load/ImageLoadStrategy.java +0 -48
- data/src/main/java/processing/core/util/image/load/ImageLoadUtil.java +0 -45
- data/src/main/java/processing/core/util/image/load/TgaImageLoadStrategy.java +0 -255
- data/src/main/java/processing/core/util/image/load/TiffImageLoadStrategy.java +0 -98
- data/src/main/java/processing/core/util/image/save/ImageSaveStrategy.java +0 -49
- data/src/main/java/processing/core/util/image/save/ImageSaveUtil.java +0 -48
- data/src/main/java/processing/core/util/image/save/ImageWriterImageSaveStrategy.java +0 -179
- data/src/main/java/processing/core/util/image/save/SaveImageException.java +0 -41
- data/src/main/java/processing/core/util/image/save/TgaImageSaveStrategy.java +0 -198
- data/src/main/java/processing/core/util/image/save/TiffImageSaveStrategy.java +0 -91
- data/src/main/java/processing/core/util/image/save/TiffNakedFilenameImageSaveStrategy.java +0 -57
- data/src/main/java/processing/core/util/io/InputFactory.java +0 -285
- data/src/main/java/processing/core/util/io/PathUtil.java +0 -109
- data/src/main/java/processing/opengl/shaders/LightVert-brcm.glsl +0 -154
- data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +0 -154
- data/src/main/java/processing/opengl/shaders/TexLightVert-brcm.glsl +0 -160
- data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +0 -160
@@ -1,3 +1,5 @@
|
|
1
|
+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
|
+
|
1
3
|
/*
|
2
4
|
Part of the Processing project - http://processing.org
|
3
5
|
|
@@ -16,130 +18,156 @@
|
|
16
18
|
Public License along with this library; if not, write to the
|
17
19
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
18
20
|
Boston, MA 02111-1307 USA
|
19
|
-
|
21
|
+
*/
|
22
|
+
|
20
23
|
package processing.javafx;
|
21
24
|
|
22
|
-
import processing.core.PApplet;
|
23
|
-
import processing.core.PImage;
|
24
|
-
import processing.core.PSurface;
|
25
25
|
|
26
|
-
|
26
|
+
import java.io.File;
|
27
|
+
import processing.core.*;
|
27
28
|
|
28
|
-
final String message = "FX2D renderer not supported in this version of propane";
|
29
|
-
|
30
|
-
@Override
|
31
|
-
public void initOffscreen(PApplet sketch) {
|
32
|
-
throw new UnsupportedOperationException(message);
|
33
|
-
}
|
34
|
-
|
35
|
-
@Override
|
36
|
-
public void initFrame(PApplet sketch) {
|
37
|
-
throw new UnsupportedOperationException(message);
|
38
|
-
}
|
39
|
-
|
40
|
-
@Override
|
41
|
-
public Object getNative() {
|
42
|
-
throw new UnsupportedOperationException(message);
|
43
|
-
}
|
44
|
-
|
45
|
-
@Override
|
46
|
-
public void setTitle(String title) {
|
47
|
-
throw new UnsupportedOperationException(message);
|
48
|
-
}
|
49
|
-
|
50
|
-
@Override
|
51
|
-
public void setVisible(boolean visible) {
|
52
|
-
throw new UnsupportedOperationException(message);
|
53
|
-
}
|
54
|
-
|
55
|
-
@Override
|
56
|
-
public void setResizable(boolean resizable) {
|
57
|
-
throw new UnsupportedOperationException(message);
|
58
|
-
}
|
59
|
-
|
60
|
-
@Override
|
61
|
-
public void setAlwaysOnTop(boolean always) {
|
62
|
-
throw new UnsupportedOperationException(message);
|
63
|
-
}
|
64
|
-
|
65
|
-
@Override
|
66
|
-
public void setIcon(PImage icon) {
|
67
|
-
throw new UnsupportedOperationException(message);
|
68
|
-
}
|
69
|
-
|
70
|
-
@Override
|
71
|
-
public void placeWindow(int[] location, int[] editorLocation) {
|
72
|
-
throw new UnsupportedOperationException(message);
|
73
|
-
}
|
74
|
-
|
75
|
-
@Override
|
76
|
-
public void placePresent(int stopColor) {
|
77
|
-
throw new UnsupportedOperationException(message);
|
78
|
-
}
|
79
|
-
|
80
|
-
@Override
|
81
|
-
public void setupExternalMessages() {
|
82
|
-
throw new UnsupportedOperationException(message);
|
83
|
-
}
|
84
|
-
|
85
|
-
@Override
|
86
|
-
public void setLocation(int x, int y) {
|
87
|
-
throw new UnsupportedOperationException(message);
|
88
|
-
}
|
89
|
-
|
90
|
-
@Override
|
91
|
-
public void setSize(int width, int height) {
|
92
|
-
throw new UnsupportedOperationException(message);
|
93
|
-
}
|
94
|
-
|
95
|
-
@Override
|
96
|
-
public void setFrameRate(float fps) {
|
97
|
-
throw new UnsupportedOperationException(message);
|
98
|
-
}
|
99
|
-
|
100
|
-
@Override
|
101
|
-
public void setCursor(int kind) {
|
102
|
-
throw new UnsupportedOperationException(message);
|
103
|
-
}
|
104
|
-
|
105
|
-
@Override
|
106
|
-
public void setCursor(PImage image, int hotspotX, int hotspotY) {
|
107
|
-
throw new UnsupportedOperationException(message);
|
108
|
-
}
|
109
|
-
|
110
|
-
@Override
|
111
|
-
public void showCursor() {
|
112
|
-
throw new UnsupportedOperationException(message);
|
113
|
-
}
|
114
|
-
|
115
|
-
@Override
|
116
|
-
public void hideCursor() {
|
117
|
-
throw new UnsupportedOperationException(message);
|
118
|
-
}
|
119
|
-
|
120
|
-
@Override
|
121
|
-
public void startThread() {
|
122
|
-
throw new UnsupportedOperationException(message);
|
123
|
-
}
|
124
|
-
|
125
|
-
@Override
|
126
|
-
public void pauseThread() {
|
127
|
-
throw new UnsupportedOperationException(message);
|
128
|
-
}
|
129
|
-
|
130
|
-
@Override
|
131
|
-
public void resumeThread() {
|
132
|
-
throw new UnsupportedOperationException(message);
|
133
|
-
}
|
134
|
-
|
135
|
-
@Override
|
136
|
-
public boolean stopThread() {
|
137
|
-
throw new UnsupportedOperationException(message);
|
138
|
-
}
|
139
|
-
|
140
|
-
@Override
|
141
|
-
public boolean isStopped() {
|
142
|
-
throw new UnsupportedOperationException(message);
|
143
|
-
}
|
144
29
|
|
30
|
+
public class PSurfaceFX implements PSurface {
|
31
|
+
final String MESSAGE = "Not implemented by ruby-processing projects";
|
32
|
+
|
33
|
+
@Override
|
34
|
+
public void initOffscreen(PApplet sketch) {
|
35
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
36
|
+
}
|
37
|
+
|
38
|
+
@Override
|
39
|
+
public void initFrame(PApplet sketch) {
|
40
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
41
|
+
}
|
42
|
+
|
43
|
+
@Override
|
44
|
+
public PImage loadImage(String path, Object... args) {
|
45
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
46
|
+
}
|
47
|
+
|
48
|
+
@Override
|
49
|
+
public void selectInput(String prompt, String callback, File file, Object callbackObject) {
|
50
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
51
|
+
}
|
52
|
+
|
53
|
+
@Override
|
54
|
+
public void selectOutput(String prompt, String callback, File file, Object callbackObject) {
|
55
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
56
|
+
}
|
57
|
+
|
58
|
+
@Override
|
59
|
+
public void selectFolder(String prompt, String callback, File file, Object callbackObject) {
|
60
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
61
|
+
}
|
62
|
+
|
63
|
+
@Override
|
64
|
+
public Object getNative() {
|
65
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
66
|
+
}
|
67
|
+
|
68
|
+
@Override
|
69
|
+
public void setTitle(String title) {
|
70
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
71
|
+
}
|
72
|
+
|
73
|
+
@Override
|
74
|
+
public void setVisible(boolean visible) {
|
75
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
76
|
+
}
|
77
|
+
|
78
|
+
@Override
|
79
|
+
public void setResizable(boolean resizable) {
|
80
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
81
|
+
}
|
82
|
+
|
83
|
+
@Override
|
84
|
+
public void setAlwaysOnTop(boolean always) {
|
85
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
86
|
+
}
|
87
|
+
|
88
|
+
@Override
|
89
|
+
public void setIcon(PImage icon) {
|
90
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
91
|
+
}
|
92
|
+
|
93
|
+
@Override
|
94
|
+
public void placeWindow(int[] location, int[] editorLocation) {
|
95
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
96
|
+
}
|
97
|
+
|
98
|
+
@Override
|
99
|
+
public void placePresent(int stopColor) {
|
100
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
101
|
+
}
|
102
|
+
|
103
|
+
@Override
|
104
|
+
public void setupExternalMessages() {
|
105
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
106
|
+
}
|
107
|
+
|
108
|
+
@Override
|
109
|
+
public void setLocation(int x, int y) {
|
110
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
111
|
+
}
|
112
|
+
|
113
|
+
@Override
|
114
|
+
public void setSize(int width, int height) {
|
115
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
116
|
+
}
|
117
|
+
|
118
|
+
@Override
|
119
|
+
public void setFrameRate(float fps) {
|
120
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
121
|
+
}
|
122
|
+
|
123
|
+
@Override
|
124
|
+
public void setCursor(int kind) {
|
125
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
126
|
+
}
|
127
|
+
|
128
|
+
@Override
|
129
|
+
public void setCursor(PImage image, int hotspotX, int hotspotY) {
|
130
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
131
|
+
}
|
132
|
+
|
133
|
+
@Override
|
134
|
+
public void showCursor() {
|
135
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
136
|
+
}
|
137
|
+
|
138
|
+
@Override
|
139
|
+
public void hideCursor() {
|
140
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
141
|
+
}
|
142
|
+
|
143
|
+
@Override
|
144
|
+
public boolean openLink(String url) {
|
145
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
146
|
+
}
|
147
|
+
|
148
|
+
@Override
|
149
|
+
public void startThread() {
|
150
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
151
|
+
}
|
152
|
+
|
153
|
+
@Override
|
154
|
+
public void pauseThread() {
|
155
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
156
|
+
}
|
157
|
+
|
158
|
+
@Override
|
159
|
+
public void resumeThread() {
|
160
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
161
|
+
}
|
162
|
+
|
163
|
+
@Override
|
164
|
+
public boolean stopThread() {
|
165
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
166
|
+
}
|
167
|
+
|
168
|
+
@Override
|
169
|
+
public boolean isStopped() {
|
170
|
+
throw new UnsupportedOperationException(MESSAGE); //To change body of generated methods, choose Tools | Templates.
|
171
|
+
}
|
172
|
+
|
145
173
|
}
|
@@ -0,0 +1,744 @@
|
|
1
|
+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
|
+
|
3
|
+
/*
|
4
|
+
Client - basic network client implementation
|
5
|
+
Part of the Processing project - http://processing.org
|
6
|
+
|
7
|
+
Copyright (c) 2004-2007 Ben Fry and Casey Reas
|
8
|
+
The previous version of this code was developed by Hernando Barragan
|
9
|
+
|
10
|
+
This library is free software; you can redistribute it and/or
|
11
|
+
modify it under the terms of the GNU Lesser General Public
|
12
|
+
License as published by the Free Software Foundation; either
|
13
|
+
version 2.1 of the License, or (at your option) any later version.
|
14
|
+
|
15
|
+
This library is distributed in the hope that it will be useful,
|
16
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
18
|
+
Lesser General Public License for more details.
|
19
|
+
|
20
|
+
You should have received a copy of the GNU Lesser General
|
21
|
+
Public License along with this library; if not, write to the
|
22
|
+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
23
|
+
Boston, MA 02111-1307 USA
|
24
|
+
*/
|
25
|
+
|
26
|
+
package processing.net;
|
27
|
+
import processing.core.*;
|
28
|
+
|
29
|
+
import java.io.*;
|
30
|
+
import java.lang.reflect.*;
|
31
|
+
import java.net.*;
|
32
|
+
|
33
|
+
/**
|
34
|
+
* ( begin auto-generated from Client.xml )
|
35
|
+
*
|
36
|
+
* A client connects to a server and sends data back and forth. If anything
|
37
|
+
* goes wrong with the connection, for example the host is not there or is
|
38
|
+
* listening on a different port, an exception is thrown.
|
39
|
+
*
|
40
|
+
*
|
41
|
+
* @webref net
|
42
|
+
* @brief The client class is used to create client Objects which connect to a server to exchange data.
|
43
|
+
* @instanceName client any variable of type Client
|
44
|
+
* @usage Application
|
45
|
+
* @see_external LIB_net/clientEvent
|
46
|
+
*/
|
47
|
+
public class Client implements Runnable {
|
48
|
+
|
49
|
+
protected static final int MAX_BUFFER_SIZE = 1 << 27; // 128 MB
|
50
|
+
|
51
|
+
PApplet parent;
|
52
|
+
Method clientEventMethod;
|
53
|
+
Method disconnectEventMethod;
|
54
|
+
|
55
|
+
volatile Thread thread;
|
56
|
+
Socket socket;
|
57
|
+
int port;
|
58
|
+
String host;
|
59
|
+
|
60
|
+
public InputStream input;
|
61
|
+
public OutputStream output;
|
62
|
+
|
63
|
+
final Object bufferLock = new Object[0];
|
64
|
+
|
65
|
+
byte buffer[] = new byte[32768];
|
66
|
+
int bufferIndex;
|
67
|
+
int bufferLast;
|
68
|
+
|
69
|
+
boolean disposeRegistered = false;
|
70
|
+
|
71
|
+
|
72
|
+
/**
|
73
|
+
* @param parent typically use "this"
|
74
|
+
* @param host address of the server
|
75
|
+
* @param port port to read/write from on the server
|
76
|
+
*/
|
77
|
+
public Client(PApplet parent, String host, int port) {
|
78
|
+
this.parent = parent;
|
79
|
+
this.host = host;
|
80
|
+
this.port = port;
|
81
|
+
|
82
|
+
try {
|
83
|
+
socket = new Socket(this.host, this.port);
|
84
|
+
input = socket.getInputStream();
|
85
|
+
output = socket.getOutputStream();
|
86
|
+
|
87
|
+
thread = new Thread(this);
|
88
|
+
thread.start();
|
89
|
+
|
90
|
+
parent.registerMethod("dispose", this);
|
91
|
+
disposeRegistered = true;
|
92
|
+
|
93
|
+
// reflection to check whether host sketch has a call for
|
94
|
+
// public void clientEvent(processing.net.Client)
|
95
|
+
// which would be called each time an event comes in
|
96
|
+
try {
|
97
|
+
clientEventMethod =
|
98
|
+
parent.getClass().getMethod("clientEvent", Client.class);
|
99
|
+
} catch (Exception e) {
|
100
|
+
// no such method, or an error.. which is fine, just ignore
|
101
|
+
}
|
102
|
+
// do the same for disconnectEvent(Client c);
|
103
|
+
try {
|
104
|
+
disconnectEventMethod =
|
105
|
+
parent.getClass().getMethod("disconnectEvent", Client.class);
|
106
|
+
} catch (Exception e) {
|
107
|
+
// no such method, or an error.. which is fine, just ignore
|
108
|
+
}
|
109
|
+
|
110
|
+
} catch (IOException e) {
|
111
|
+
|
112
|
+
dispose();
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
|
117
|
+
/**
|
118
|
+
* @param socket any object of type Socket
|
119
|
+
* @throws IOException
|
120
|
+
*/
|
121
|
+
public Client(PApplet parent, Socket socket) throws IOException {
|
122
|
+
this.parent = parent;
|
123
|
+
this.socket = socket;
|
124
|
+
|
125
|
+
input = socket.getInputStream();
|
126
|
+
output = socket.getOutputStream();
|
127
|
+
|
128
|
+
thread = new Thread(this);
|
129
|
+
thread.start();
|
130
|
+
|
131
|
+
// reflection to check whether host sketch has a call for
|
132
|
+
// public void clientEvent(processing.net.Client)
|
133
|
+
// which would be called each time an event comes in
|
134
|
+
try {
|
135
|
+
clientEventMethod =
|
136
|
+
parent.getClass().getMethod("clientEvent", Client.class);
|
137
|
+
} catch (Exception e) {
|
138
|
+
// no such method, or an error.. which is fine, just ignore
|
139
|
+
}
|
140
|
+
// do the same for disconnectEvent(Client c);
|
141
|
+
try {
|
142
|
+
disconnectEventMethod =
|
143
|
+
parent.getClass().getMethod("disconnectEvent", Client.class);
|
144
|
+
} catch (Exception e) {
|
145
|
+
// no such method, or an error.. which is fine, just ignore
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
/**
|
151
|
+
* ( begin auto-generated from Client_stop.xml )
|
152
|
+
*
|
153
|
+
* Disconnects from the server. Use to shut the connection when you're
|
154
|
+
* finished with the Client.
|
155
|
+
*
|
156
|
+
*
|
157
|
+
* @webref client:client
|
158
|
+
* @brief Disconnects from the server
|
159
|
+
* @usage application
|
160
|
+
*/
|
161
|
+
public void stop() {
|
162
|
+
if (disconnectEventMethod != null && thread != null){
|
163
|
+
try {
|
164
|
+
disconnectEventMethod.invoke(parent, this);
|
165
|
+
} catch (Exception e) {
|
166
|
+
Throwable cause = e;
|
167
|
+
// unwrap the exception if it came from the user code
|
168
|
+
if (e instanceof InvocationTargetException && e.getCause() != null) {
|
169
|
+
cause = e.getCause();
|
170
|
+
}
|
171
|
+
cause.printStackTrace();
|
172
|
+
disconnectEventMethod = null;
|
173
|
+
}
|
174
|
+
}
|
175
|
+
if (disposeRegistered) {
|
176
|
+
parent.unregisterMethod("dispose", this);
|
177
|
+
disposeRegistered = false;
|
178
|
+
}
|
179
|
+
dispose();
|
180
|
+
}
|
181
|
+
|
182
|
+
|
183
|
+
/**
|
184
|
+
* Disconnect from the server: internal use only.
|
185
|
+
* <P>
|
186
|
+
* This should only be called by the internal functions in PApplet,
|
187
|
+
* use stop() instead from within your own applets.
|
188
|
+
*/
|
189
|
+
public void dispose() {
|
190
|
+
thread = null;
|
191
|
+
try {
|
192
|
+
if (input != null) {
|
193
|
+
input.close();
|
194
|
+
input = null;
|
195
|
+
}
|
196
|
+
} catch (Exception e) {
|
197
|
+
|
198
|
+
}
|
199
|
+
|
200
|
+
try {
|
201
|
+
if (output != null) {
|
202
|
+
output.close();
|
203
|
+
output = null;
|
204
|
+
}
|
205
|
+
} catch (Exception e) {
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
try {
|
210
|
+
if (socket != null) {
|
211
|
+
socket.close();
|
212
|
+
socket = null;
|
213
|
+
}
|
214
|
+
} catch (Exception e) {
|
215
|
+
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
@Override
|
221
|
+
public void run() {
|
222
|
+
byte[] readBuffer;
|
223
|
+
{ // make the read buffer same size as socket receive buffer so that
|
224
|
+
// we don't waste cycles calling listeners when there is more data waiting
|
225
|
+
int readBufferSize = 1 << 16; // 64 KB (default socket receive buffer size)
|
226
|
+
try {
|
227
|
+
readBufferSize = socket.getReceiveBufferSize();
|
228
|
+
} catch (SocketException ignore) { }
|
229
|
+
readBuffer = new byte[readBufferSize];
|
230
|
+
}
|
231
|
+
while (Thread.currentThread() == thread) {
|
232
|
+
try {
|
233
|
+
while (input != null) {
|
234
|
+
int readCount;
|
235
|
+
|
236
|
+
// try to read a byte using a blocking read.
|
237
|
+
// An exception will occur when the sketch is exits.
|
238
|
+
try {
|
239
|
+
readCount = input.read(readBuffer, 0, readBuffer.length);
|
240
|
+
} catch (SocketException e) {
|
241
|
+
System.err.println("Client SocketException: " + e.getMessage());
|
242
|
+
// the socket had a problem reading so don't try to read from it again.
|
243
|
+
stop();
|
244
|
+
return;
|
245
|
+
}
|
246
|
+
|
247
|
+
// read returns -1 if end-of-stream occurs (for example if the host disappears)
|
248
|
+
if (readCount == -1) {
|
249
|
+
System.err.println("Client got end-of-stream.");
|
250
|
+
stop();
|
251
|
+
return;
|
252
|
+
}
|
253
|
+
|
254
|
+
synchronized (bufferLock) {
|
255
|
+
int freeBack = buffer.length - bufferLast;
|
256
|
+
if (readCount > freeBack) {
|
257
|
+
// not enough space at the back
|
258
|
+
int bufferLength = bufferLast - bufferIndex;
|
259
|
+
byte[] targetBuffer = buffer;
|
260
|
+
if (bufferLength + readCount > buffer.length) {
|
261
|
+
// can't fit even after compacting, resize the buffer
|
262
|
+
// find the next power of two which can fit everything in
|
263
|
+
int newSize = Integer.highestOneBit(bufferLength + readCount - 1) << 1;
|
264
|
+
if (newSize > MAX_BUFFER_SIZE) {
|
265
|
+
// buffer is full because client is not reading (fast enough)
|
266
|
+
System.err.println("Client: can't receive more data, buffer is full. " +
|
267
|
+
"Make sure you read the data from the client.");
|
268
|
+
stop();
|
269
|
+
return;
|
270
|
+
}
|
271
|
+
targetBuffer = new byte[newSize];
|
272
|
+
}
|
273
|
+
// compact the buffer (either in-place or into the new bigger buffer)
|
274
|
+
System.arraycopy(buffer, bufferIndex, targetBuffer, 0, bufferLength);
|
275
|
+
bufferLast -= bufferIndex;
|
276
|
+
bufferIndex = 0;
|
277
|
+
buffer = targetBuffer;
|
278
|
+
}
|
279
|
+
// copy all newly read bytes into the buffer
|
280
|
+
System.arraycopy(readBuffer, 0, buffer, bufferLast, readCount);
|
281
|
+
bufferLast += readCount;
|
282
|
+
}
|
283
|
+
|
284
|
+
// now post an event
|
285
|
+
if (clientEventMethod != null) {
|
286
|
+
try {
|
287
|
+
clientEventMethod.invoke(parent, this);
|
288
|
+
} catch (Exception e) {
|
289
|
+
System.err.println("error, disabling clientEvent() for " + host);
|
290
|
+
Throwable cause = e;
|
291
|
+
// unwrap the exception if it came from the user code
|
292
|
+
if (e instanceof InvocationTargetException && e.getCause() != null) {
|
293
|
+
cause = e.getCause();
|
294
|
+
}
|
295
|
+
cause.printStackTrace();
|
296
|
+
clientEventMethod = null;
|
297
|
+
}
|
298
|
+
}
|
299
|
+
}
|
300
|
+
} catch (IOException e) {
|
301
|
+
//errorMessage("run", e);
|
302
|
+
|
303
|
+
}
|
304
|
+
}
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
/**
|
309
|
+
* ( begin auto-generated from Client_active.xml )
|
310
|
+
*
|
311
|
+
* Returns true if this client is still active and hasn't run
|
312
|
+
* into any trouble.
|
313
|
+
*
|
314
|
+
*
|
315
|
+
* @webref client:client
|
316
|
+
* @brief Returns true if this client is still active
|
317
|
+
* @usage application
|
318
|
+
*/
|
319
|
+
public boolean active() {
|
320
|
+
return (thread != null);
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
/**
|
325
|
+
* ( begin auto-generated from Client_ip.xml )
|
326
|
+
*
|
327
|
+
* Returns the IP address of the computer to which the Client is attached.
|
328
|
+
*
|
329
|
+
*
|
330
|
+
* @webref client:client
|
331
|
+
* @usage application
|
332
|
+
* @brief Returns the IP address of the machine as a String
|
333
|
+
*/
|
334
|
+
public String ip() {
|
335
|
+
if (socket != null){
|
336
|
+
return socket.getInetAddress().getHostAddress();
|
337
|
+
}
|
338
|
+
return null;
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
/**
|
343
|
+
* ( begin auto-generated from Client_available.xml )
|
344
|
+
*
|
345
|
+
* Returns the number of bytes available. When any client has bytes
|
346
|
+
* available from the server, it returns the number of bytes.
|
347
|
+
*
|
348
|
+
*
|
349
|
+
* @webref client:client
|
350
|
+
* @usage application
|
351
|
+
* @brief Returns the number of bytes in the buffer waiting to be read
|
352
|
+
*/
|
353
|
+
public int available() {
|
354
|
+
synchronized (bufferLock) {
|
355
|
+
return (bufferLast - bufferIndex);
|
356
|
+
}
|
357
|
+
}
|
358
|
+
|
359
|
+
|
360
|
+
/**
|
361
|
+
* ( begin auto-generated from Client_clear.xml )
|
362
|
+
*
|
363
|
+
* Empty the buffer, removes all the data stored there.
|
364
|
+
*
|
365
|
+
*
|
366
|
+
* @webref client:client
|
367
|
+
* @usage application
|
368
|
+
* @brief Clears the buffer
|
369
|
+
*/
|
370
|
+
public void clear() {
|
371
|
+
synchronized (bufferLock) {
|
372
|
+
bufferLast = 0;
|
373
|
+
bufferIndex = 0;
|
374
|
+
}
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
/**
|
379
|
+
* ( begin auto-generated from Client_read.xml )
|
380
|
+
*
|
381
|
+
* Returns a number between 0 and 255 for the next byte that's waiting in
|
382
|
+
* the buffer. Returns -1 if there is no byte, although this should be
|
383
|
+
* avoided by first cheacking <b>available()</b> to see if any data is available.
|
384
|
+
*
|
385
|
+
*
|
386
|
+
* @webref client:client
|
387
|
+
* @usage application
|
388
|
+
* @brief Returns a value from the buffer
|
389
|
+
*/
|
390
|
+
public int read() {
|
391
|
+
synchronized (bufferLock) {
|
392
|
+
if (bufferIndex == bufferLast) return -1;
|
393
|
+
|
394
|
+
int outgoing = buffer[bufferIndex++] & 0xff;
|
395
|
+
if (bufferIndex == bufferLast) { // rewind
|
396
|
+
bufferIndex = 0;
|
397
|
+
bufferLast = 0;
|
398
|
+
}
|
399
|
+
return outgoing;
|
400
|
+
}
|
401
|
+
}
|
402
|
+
|
403
|
+
|
404
|
+
/**
|
405
|
+
* ( begin auto-generated from Client_readChar.xml )
|
406
|
+
*
|
407
|
+
* Returns the next byte in the buffer as a char. Returns -1 or 0xffff if
|
408
|
+
* nothing is there.
|
409
|
+
*
|
410
|
+
*
|
411
|
+
* @webref client:client
|
412
|
+
* @usage application
|
413
|
+
* @brief Returns the next byte in the buffer as a char
|
414
|
+
*/
|
415
|
+
public char readChar() {
|
416
|
+
synchronized (bufferLock) {
|
417
|
+
if (bufferIndex == bufferLast) return (char) (-1);
|
418
|
+
return (char) read();
|
419
|
+
}
|
420
|
+
}
|
421
|
+
|
422
|
+
|
423
|
+
/**
|
424
|
+
* ( begin auto-generated from Client_readBytes.xml )
|
425
|
+
*
|
426
|
+
* Reads a group of bytes from the buffer. The version with no parameters
|
427
|
+
* returns a byte array of all data in the buffer. This is not efficient,
|
428
|
+
* but is easy to use. The version with the <b>byteBuffer</b> parameter is
|
429
|
+
* more memory and time efficient. It grabs the data in the buffer and puts
|
430
|
+
* it into the byte array passed in and returns an int value for the number
|
431
|
+
* of bytes read. If more bytes are available than can fit into the
|
432
|
+
* <b>byteBuffer</b>, only those that fit are read.
|
433
|
+
*
|
434
|
+
*
|
435
|
+
* <h3>Advanced</h3>
|
436
|
+
* Return a byte array of anything that's in the serial buffer.
|
437
|
+
* Not particularly memory/speed efficient, because it creates
|
438
|
+
* a byte array on each read, but it's easier to use than
|
439
|
+
* readBytes(byte b[]) (see below).
|
440
|
+
*
|
441
|
+
* @webref client:client
|
442
|
+
* @usage application
|
443
|
+
* @brief Reads everything in the buffer
|
444
|
+
*/
|
445
|
+
public byte[] readBytes() {
|
446
|
+
synchronized (bufferLock) {
|
447
|
+
if (bufferIndex == bufferLast) return null;
|
448
|
+
|
449
|
+
int length = bufferLast - bufferIndex;
|
450
|
+
byte outgoing[] = new byte[length];
|
451
|
+
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
|
452
|
+
|
453
|
+
bufferIndex = 0; // rewind
|
454
|
+
bufferLast = 0;
|
455
|
+
return outgoing;
|
456
|
+
}
|
457
|
+
}
|
458
|
+
|
459
|
+
|
460
|
+
/**
|
461
|
+
* <h3>Advanced</h3>
|
462
|
+
* Return a byte array of anything that's in the serial buffer
|
463
|
+
* up to the specified maximum number of bytes.
|
464
|
+
* Not particularly memory/speed efficient, because it creates
|
465
|
+
* a byte array on each read, but it's easier to use than
|
466
|
+
* readBytes(byte b[]) (see below).
|
467
|
+
*
|
468
|
+
* @param max the maximum number of bytes to read
|
469
|
+
*/
|
470
|
+
public byte[] readBytes(int max) {
|
471
|
+
synchronized (bufferLock) {
|
472
|
+
if (bufferIndex == bufferLast) return null;
|
473
|
+
|
474
|
+
int length = bufferLast - bufferIndex;
|
475
|
+
if (length > max) length = max;
|
476
|
+
byte outgoing[] = new byte[length];
|
477
|
+
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
|
478
|
+
|
479
|
+
bufferIndex += length;
|
480
|
+
if (bufferIndex == bufferLast) {
|
481
|
+
bufferIndex = 0; // rewind
|
482
|
+
bufferLast = 0;
|
483
|
+
}
|
484
|
+
|
485
|
+
return outgoing;
|
486
|
+
}
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
/**
|
491
|
+
* <h3>Advanced</h3>
|
492
|
+
* Grab whatever is in the serial buffer, and stuff it into a
|
493
|
+
* byte buffer passed in by the user. This is more memory/time
|
494
|
+
* efficient than readBytes() returning a byte[] array.
|
495
|
+
*
|
496
|
+
* Returns an int for how many bytes were read. If more bytes
|
497
|
+
* are available than can fit into the byte array, only those
|
498
|
+
* that will fit are read.
|
499
|
+
*
|
500
|
+
* @param bytebuffer passed in byte array to be altered
|
501
|
+
*/
|
502
|
+
public int readBytes(byte bytebuffer[]) {
|
503
|
+
synchronized (bufferLock) {
|
504
|
+
if (bufferIndex == bufferLast) return 0;
|
505
|
+
|
506
|
+
int length = bufferLast - bufferIndex;
|
507
|
+
if (length > bytebuffer.length) length = bytebuffer.length;
|
508
|
+
System.arraycopy(buffer, bufferIndex, bytebuffer, 0, length);
|
509
|
+
|
510
|
+
bufferIndex += length;
|
511
|
+
if (bufferIndex == bufferLast) {
|
512
|
+
bufferIndex = 0; // rewind
|
513
|
+
bufferLast = 0;
|
514
|
+
}
|
515
|
+
return length;
|
516
|
+
}
|
517
|
+
}
|
518
|
+
|
519
|
+
|
520
|
+
/**
|
521
|
+
* ( begin auto-generated from Client_readBytesUntil.xml )
|
522
|
+
*
|
523
|
+
* Reads from the port into a buffer of bytes up to and including a
|
524
|
+
* particular character. If the character isn't in the buffer, 'null' is
|
525
|
+
* returned. The version with no <b>byteBuffer</b> parameter returns a byte
|
526
|
+
* array of all data up to and including the <b>interesting</b> byte. This
|
527
|
+
* is not efficient, but is easy to use. The version with the
|
528
|
+
* <b>byteBuffer</b> parameter is more memory and time efficient. It grabs
|
529
|
+
* the data in the buffer and puts it into the byte array passed in and
|
530
|
+
* returns an int value for the number of bytes read. If the byte buffer is
|
531
|
+
* not large enough, -1 is returned and an error is printed to the message
|
532
|
+
* area. If nothing is in the buffer, 0 is returned.
|
533
|
+
*
|
534
|
+
*
|
535
|
+
* @webref client:client
|
536
|
+
* @usage application
|
537
|
+
* @brief Reads from the buffer of bytes up to and including a particular character
|
538
|
+
* @param interesting character designated to mark the end of the data
|
539
|
+
*/
|
540
|
+
public byte[] readBytesUntil(int interesting) {
|
541
|
+
byte what = (byte)interesting;
|
542
|
+
|
543
|
+
synchronized (bufferLock) {
|
544
|
+
if (bufferIndex == bufferLast) return null;
|
545
|
+
|
546
|
+
int found = -1;
|
547
|
+
for (int k = bufferIndex; k < bufferLast; k++) {
|
548
|
+
if (buffer[k] == what) {
|
549
|
+
found = k;
|
550
|
+
break;
|
551
|
+
}
|
552
|
+
}
|
553
|
+
if (found == -1) return null;
|
554
|
+
|
555
|
+
int length = found - bufferIndex + 1;
|
556
|
+
byte outgoing[] = new byte[length];
|
557
|
+
System.arraycopy(buffer, bufferIndex, outgoing, 0, length);
|
558
|
+
|
559
|
+
bufferIndex += length;
|
560
|
+
if (bufferIndex == bufferLast) {
|
561
|
+
bufferIndex = 0; // rewind
|
562
|
+
bufferLast = 0;
|
563
|
+
}
|
564
|
+
return outgoing;
|
565
|
+
}
|
566
|
+
}
|
567
|
+
|
568
|
+
|
569
|
+
/**
|
570
|
+
* <h3>Advanced</h3>
|
571
|
+
* Reads from the serial port into a buffer of bytes until a
|
572
|
+
* particular character. If the character isn't in the serial
|
573
|
+
* buffer, then 'null' is returned.
|
574
|
+
*
|
575
|
+
* If outgoing[] is not big enough, then -1 is returned,
|
576
|
+
* and an error message is printed on the console.
|
577
|
+
* If nothing is in the buffer, zero is returned.
|
578
|
+
* If 'interesting' byte is not in the buffer, then 0 is returned.
|
579
|
+
*
|
580
|
+
* @param byteBuffer passed in byte array to be altered
|
581
|
+
*/
|
582
|
+
public int readBytesUntil(int interesting, byte byteBuffer[]) {
|
583
|
+
byte what = (byte)interesting;
|
584
|
+
|
585
|
+
synchronized (bufferLock) {
|
586
|
+
if (bufferIndex == bufferLast) return 0;
|
587
|
+
|
588
|
+
int found = -1;
|
589
|
+
for (int k = bufferIndex; k < bufferLast; k++) {
|
590
|
+
if (buffer[k] == what) {
|
591
|
+
found = k;
|
592
|
+
break;
|
593
|
+
}
|
594
|
+
}
|
595
|
+
if (found == -1) return 0;
|
596
|
+
|
597
|
+
int length = found - bufferIndex + 1;
|
598
|
+
if (length > byteBuffer.length) {
|
599
|
+
System.err.println("readBytesUntil() byte buffer is" +
|
600
|
+
" too small for the " + length +
|
601
|
+
" bytes up to and including char " + interesting);
|
602
|
+
return -1;
|
603
|
+
}
|
604
|
+
//byte outgoing[] = new byte[length];
|
605
|
+
System.arraycopy(buffer, bufferIndex, byteBuffer, 0, length);
|
606
|
+
|
607
|
+
bufferIndex += length;
|
608
|
+
if (bufferIndex == bufferLast) {
|
609
|
+
bufferIndex = 0; // rewind
|
610
|
+
bufferLast = 0;
|
611
|
+
}
|
612
|
+
return length;
|
613
|
+
}
|
614
|
+
}
|
615
|
+
|
616
|
+
|
617
|
+
/**
|
618
|
+
* ( begin auto-generated from Client_readString.xml )
|
619
|
+
*
|
620
|
+
* Returns the all the data from the buffer as a String. This method
|
621
|
+
* assumes the incoming characters are ASCII. If you want to transfer
|
622
|
+
* Unicode data, first convert the String to a byte stream in the
|
623
|
+
* representation of your choice (i.e. UTF8 or two-byte Unicode data), and
|
624
|
+
* send it as a byte array.
|
625
|
+
*
|
626
|
+
*
|
627
|
+
* @webref client:client
|
628
|
+
* @usage application
|
629
|
+
* @brief Returns the buffer as a String
|
630
|
+
*/
|
631
|
+
public String readString() {
|
632
|
+
byte b[] = readBytes();
|
633
|
+
if (b == null) return null;
|
634
|
+
return new String(b);
|
635
|
+
}
|
636
|
+
|
637
|
+
|
638
|
+
/**
|
639
|
+
* ( begin auto-generated from Client_readStringUntil.xml )
|
640
|
+
*
|
641
|
+
* Combination of <b>readBytesUntil()</b> and <b>readString()</b>. Returns
|
642
|
+
* <b>null</b> if it doesn't find what you're looking for.
|
643
|
+
*
|
644
|
+
*
|
645
|
+
* <h3>Advanced</h3>
|
646
|
+
* <p/>
|
647
|
+
* If you want to move Unicode data, you can first convert the
|
648
|
+
* String to a byte stream in the representation of your choice
|
649
|
+
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
|
650
|
+
*
|
651
|
+
* @webref client:client
|
652
|
+
* @usage application
|
653
|
+
* @brief Returns the buffer as a String up to and including a particular character
|
654
|
+
* @param interesting character designated to mark the end of the data
|
655
|
+
*/
|
656
|
+
public String readStringUntil(int interesting) {
|
657
|
+
byte b[] = readBytesUntil(interesting);
|
658
|
+
if (b == null) return null;
|
659
|
+
return new String(b);
|
660
|
+
}
|
661
|
+
|
662
|
+
|
663
|
+
/**
|
664
|
+
* ( begin auto-generated from Client_write.xml )
|
665
|
+
*
|
666
|
+
* Writes data to a server specified when constructing the client.
|
667
|
+
*
|
668
|
+
*
|
669
|
+
* @webref client:client
|
670
|
+
* @usage application
|
671
|
+
* @brief Writes bytes, chars, ints, bytes[], Strings
|
672
|
+
* @param data data to write
|
673
|
+
*/
|
674
|
+
public void write(int data) { // will also cover char
|
675
|
+
try {
|
676
|
+
output.write(data & 0xff); // for good measure do the &
|
677
|
+
output.flush(); // hmm, not sure if a good idea
|
678
|
+
|
679
|
+
} catch (Exception e) { // null pointer or serial port dead
|
680
|
+
//errorMessage("write", e);
|
681
|
+
//e.printStackTrace();
|
682
|
+
//dispose();
|
683
|
+
//disconnect(e);
|
684
|
+
|
685
|
+
stop();
|
686
|
+
}
|
687
|
+
}
|
688
|
+
|
689
|
+
|
690
|
+
public void write(byte data[]) {
|
691
|
+
try {
|
692
|
+
output.write(data);
|
693
|
+
output.flush(); // hmm, not sure if a good idea
|
694
|
+
|
695
|
+
} catch (Exception e) { // null pointer or serial port dead
|
696
|
+
//errorMessage("write", e);
|
697
|
+
//e.printStackTrace();
|
698
|
+
//disconnect(e);
|
699
|
+
|
700
|
+
stop();
|
701
|
+
}
|
702
|
+
}
|
703
|
+
|
704
|
+
|
705
|
+
/**
|
706
|
+
* <h3>Advanced</h3>
|
707
|
+
* Write a String to the output. Note that this doesn't account
|
708
|
+
* for Unicode (two bytes per char), nor will it send UTF8
|
709
|
+
* characters.. It assumes that you mean to send a byte buffer
|
710
|
+
* (most often the case for networking and serial i/o) and
|
711
|
+
* will only use the bottom 8 bits of each char in the string.
|
712
|
+
* (Meaning that internally it uses String.getBytes)
|
713
|
+
*
|
714
|
+
* If you want to move Unicode data, you can first convert the
|
715
|
+
* String to a byte stream in the representation of your choice
|
716
|
+
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
|
717
|
+
*/
|
718
|
+
public void write(String data) {
|
719
|
+
write(data.getBytes());
|
720
|
+
}
|
721
|
+
|
722
|
+
|
723
|
+
/**
|
724
|
+
* Handle disconnect due to an Exception being thrown.
|
725
|
+
*/
|
726
|
+
/*
|
727
|
+
protected void disconnect(Exception e) {
|
728
|
+
dispose();
|
729
|
+
if (e != null) {
|
730
|
+
e.printStackTrace();
|
731
|
+
}
|
732
|
+
}
|
733
|
+
*/
|
734
|
+
|
735
|
+
|
736
|
+
/**
|
737
|
+
* General error reporting, all corralled here just in case
|
738
|
+
* I think of something slightly more intelligent to do.
|
739
|
+
*/
|
740
|
+
//public void errorMessage(String where, Exception e) {
|
741
|
+
//parent.die("Error inside Client." + where + "()", e);
|
742
|
+
//e.printStackTrace(System.err);
|
743
|
+
//}
|
744
|
+
}
|