@@ -27,50 +27,83 @@ public class PythonRunner {
27
27
// Threads to redirect output / error streams from process to us
28
28
Communicator communicator ;
29
29
30
- public PythonRunner (PythonBuild build , PythonEditor editor ) {
31
- this .build = build ;
30
+ boolean active ;
31
+
32
+ public PythonRunner (PythonEditor editor ) {
32
33
this .editor = editor ;
34
+ active = false ;
35
+
36
+ //make sure our partner process is dead
37
+ Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
38
+ public void run () {
39
+ if (communicator != null ) {
40
+ communicator .destroy ();
41
+ }
42
+ if (sketchProcess != null ) {
43
+ sketchProcess .destroy ();
44
+ }
45
+ }
46
+ }));
33
47
}
34
-
48
+
35
49
/*
36
50
* Run the code.
37
51
*/
38
- public void launch (boolean present ) {
39
- exec (buildArgs (present ));
52
+ public void launch (PythonBuild build , boolean present ) {
53
+ this .build = build ;
54
+ ensureParallel ();
55
+ communicator .sendSketch (buildSketchArgs (present ));
56
+ }
57
+
58
+ /*
59
+ * Kill the code.
60
+ */
61
+ public void internalClose () {
62
+ //Closed from editor button
63
+ ensureParallel ();
64
+ System .out .println ("Closing..." );
65
+ communicator .sendClose ();
40
66
}
41
67
42
68
/*
43
- * Construct the command-line command to start the sketch with
44
- *
45
- * TODO add proper machine detection & whatnot from Java Mode
46
- *
69
+ * Make sure we've got a process to run the code.
47
70
*/
48
- private String [] buildArgs (boolean present ){
49
- ArrayList <String > args = new ArrayList <String >();
50
-
51
- // Manage java
52
- appendJavaArgs (args );
53
-
54
- // Manage Python Mode
55
- args .add ("-cp" );
56
- args .add (build .getClassPath ());
57
-
58
- args .add ("info.sansgills.mode.python.wrapper.ProcessingJythonWrapper" ); // main class
59
-
60
- args .add (build .getResultFile ()); //path to script
61
-
62
- appendSketchArgs (args , present );
63
-
64
- String [] out = args .toArray (new String [0 ]);
65
-
66
- return out ;
71
+ private void ensureParallel (){
72
+ if (sketchProcess == null ){
73
+ if (build == null ){
74
+ System .err .println ("need a build" );
75
+ return ;
76
+ }
77
+ sketchProcess = PApplet .exec (buildJavaArgs ());
78
+ communicator = new Communicator (sketchProcess , this );
79
+ }
67
80
}
68
-
81
+
82
+
83
+ public void parallelStopped () {
84
+ //Closed from sketch window
85
+ if (active ){
86
+ active = false ;
87
+ System .out .println ("Closed" );
88
+ }else {
89
+ System .err .println ("something is wrong" );
90
+ }
91
+ }
92
+ public void parallelStarted (){
93
+ if (!active ){
94
+ active = true ;
95
+ }else {
96
+ System .err .println ("something is very wrong" );
97
+ }
98
+ }
99
+
69
100
/*
70
- * Create the proper args to run the jvm with
101
+ * Command to start the companion process
71
102
*/
72
- private void appendJavaArgs (ArrayList <String > args ) {
103
+ private String [] buildJavaArgs () {
104
+ ArrayList <String > args = new ArrayList <String >();
73
105
106
+ // Manage java
74
107
// special handling for base command for OS X- from Java Mode
75
108
if (!Base .isMacOS ()) {
76
109
args .add ("java" );
@@ -97,7 +130,7 @@ private void appendJavaArgs(ArrayList<String> args) {
97
130
}
98
131
}
99
132
}
100
-
133
+
101
134
// Memory
102
135
if (Preferences .getBoolean ("run.options.memory" )) {
103
136
args .add ("-Xms" + Preferences .get ("run.options.memory.initial" ) + "m" );
@@ -108,69 +141,48 @@ private void appendJavaArgs(ArrayList<String> args) {
108
141
if (Base .isMacOS ()) {
109
142
args .add ("-Xdock:name=" + build .getClassName ());
110
143
}
111
-
144
+
112
145
// Path to the libraryies we use
113
146
// TODO what's the difference between classpath and library path?
114
- args .add ("-Djava.library.path="
115
- + build .getJavaLibraryPath ()
116
- + File .pathSeparator
117
- + System .getProperty ("java.library.path" ));
147
+ args .add ("-Djava.library.path=" + build .getJavaLibraryPath ()
148
+ + File .pathSeparator + System .getProperty ("java.library.path" ));
149
+
150
+ // Manage Python Mode
151
+ args .add ("-cp" );
152
+ args .add (build .getClassPath ());
153
+
154
+ args .add ("info.sansgills.mode.python.wrapper.ProcessingJythonWrapper" ); // main class
155
+
156
+ //we parallel
157
+ args .add ("--parallel" );
158
+
159
+ return args .toArray (new String [0 ]);
118
160
119
161
}
120
162
121
163
/*
122
- * Sketch-specific stuff
164
+ * Arguments for individual sketches
123
165
*/
124
- private void appendSketchArgs (ArrayList <String > args , boolean present ){
125
- // place the sketch
126
- // TODO handle multiple displays
127
- Point windowLocation = editor .getSketchLocation ();
128
- if (windowLocation != null ) {
129
- // saved location - sketch run more than once
130
- args .add (PApplet .ARGS_LOCATION
131
- + "="
132
- + windowLocation .x + "," + windowLocation .y );
133
- } else {
134
- // tell PApplet where the editor is and let it sort itself out
135
- Point editorLocation = editor .getLocation ();
136
- args .add (PApplet .ARGS_EDITOR_LOCATION
137
- + "="
138
- + editorLocation .x + "," + editorLocation .y );
139
- }
166
+ private String [] buildSketchArgs (boolean present ) {
167
+ ArrayList <String > args = new ArrayList <String >();
168
+
169
+ args .add ("--script=" +build .getResultFile ()); // path to script
170
+
171
+ // tell PApplet where the editor is and let it sort itself out
172
+ Point editorLocation = editor .getLocation ();
173
+ args .add (PApplet .ARGS_EDITOR_LOCATION + "=" + editorLocation .x + ","
174
+ + editorLocation .y );
140
175
141
176
if (present ) {
142
177
args .add (PApplet .ARGS_FULL_SCREEN );
143
- args .add (PApplet .ARGS_STOP_COLOR + "=" + Preferences .get ("run.present.stop.color" ));
144
- args .add (PApplet .ARGS_BGCOLOR + "=" + Preferences .get ("run.present.bgcolor" ));
178
+ args .add (PApplet .ARGS_STOP_COLOR + "="
179
+ + Preferences .get ("run.present.stop.color" ));
180
+ args .add (PApplet .ARGS_BGCOLOR + "="
181
+ + Preferences .get ("run.present.bgcolor" ));
145
182
}
146
-
147
-
148
- args .add (PApplet .ARGS_EXTERNAL );
149
- args .add (build .getClassName ()); // sketch name
150
- }
151
-
152
- /*
153
- * Start the process & a thread to track it
154
- */
155
- private void exec (final String [] args ){
156
- new Thread (new Runnable (){
157
- public void run (){
158
- sketchProcess = PApplet .exec (args );
159
- communicator = new Communicator (sketchProcess , editor );
160
- try {
161
- int result = sketchProcess .waitFor ();
162
- }catch (InterruptedException e ){
163
- System .out .println ("error:" );
164
- e .printStackTrace ();
165
- }
166
- }
167
- }).start ();
168
- }
169
-
170
- /*
171
- * Kill the code.
172
- */
173
- public void close () {
174
- communicator .sendClose ();
183
+
184
+ args .add (build .getClassName ()); // sketch name MUST BE LAST
185
+
186
+ return args .toArray (new String [0 ]);
175
187
}
176
188
}
0 commit comments