16
16
import org .apache .commons .lang3 .SystemUtils ;
17
17
18
18
import edu .uco .cs .v2c .poc .ModuleID ;
19
+ import edu .uco .cs .v2c .poc .net .Tunnel ;
20
+ import edu .uco .cs .v2c .poc .ui .HomeComponent ;
19
21
import edu .uco .cs .v2c .poc .ui .ModuleComponent ;
20
22
21
23
public class ModuleHandler implements Runnable {
@@ -61,16 +63,19 @@ public String getBin() {
61
63
62
64
private AtomicBoolean go = new AtomicBoolean ();
63
65
private AtomicReference <Process > currentProcess = new AtomicReference <>();
66
+ private HomeComponent homeComponent = null ;
64
67
private ModuleComponent moduleComponent = null ;
65
68
private ModuleID moduleID = null ;
66
69
private Set <JButton > enabledButtonsOnLivingProcess = new HashSet <>();
67
70
private Set <JButton > enabledButtonsOnDyingProcess = new HashSet <>();
68
71
private String runtimeBin = null ;
69
72
private String moduleBin = null ;
70
73
private Thread thread = null ;
74
+ private Tunnel tunnel = null ;
71
75
72
- private ModuleHandler (ModuleComponent moduleComponent , ModuleID moduleID ) {
76
+ private ModuleHandler (HomeComponent homeComponent , ModuleComponent moduleComponent , ModuleID moduleID ) {
73
77
this .moduleID = moduleID ;
78
+ this .homeComponent = homeComponent ;
74
79
this .moduleComponent = moduleComponent ;
75
80
if (moduleID != null ) {
76
81
moduleComponent .setActive (false );
@@ -79,8 +84,8 @@ private ModuleHandler(ModuleComponent moduleComponent, ModuleID moduleID) {
79
84
}
80
85
}
81
86
82
- public static ModuleHandler build (ModuleComponent moduleComponent , ModuleID moduleID ) {
83
- ModuleHandler handler = new ModuleHandler (moduleComponent , moduleID );
87
+ public static ModuleHandler build (HomeComponent homeComponent , ModuleComponent moduleComponent , ModuleID moduleID ) {
88
+ ModuleHandler handler = new ModuleHandler (homeComponent , moduleComponent , moduleID );
84
89
handler .thread = new Thread (handler );
85
90
handler .thread .setDaemon (true );
86
91
handler .thread .start ();
@@ -141,14 +146,19 @@ public void setModuleBin(String moduleBin) {
141
146
processBuilder .redirectErrorStream (true );
142
147
143
148
try {
149
+ if (moduleID .hasTunnel () && tunnel != null && tunnel .isEnabled ())
150
+ tunnel .spinUp ();
151
+
144
152
Process process = processBuilder .start ();
145
153
currentProcess .set (process );
146
154
147
155
moduleComponent .setActive (true );
156
+ homeComponent .notifyModuleStateChange (moduleID , true );
148
157
for (JButton button : enabledButtonsOnLivingProcess )
149
158
button .setEnabled (true );
150
159
for (JButton button : enabledButtonsOnDyingProcess )
151
160
button .setEnabled (false );
161
+ moduleComponent .putLine ("Starting module..." );
152
162
153
163
try (BufferedReader streamReader = new BufferedReader (
154
164
new InputStreamReader (process .getInputStream ()))) {
@@ -163,10 +173,15 @@ public void setModuleBin(String moduleBin) {
163
173
} finally {
164
174
go .set (false );
165
175
moduleComponent .setActive (false );
176
+ homeComponent .notifyModuleStateChange (moduleID , false );
166
177
for (JButton button : enabledButtonsOnDyingProcess )
167
178
button .setEnabled (true );
168
179
for (JButton button : enabledButtonsOnLivingProcess )
169
180
button .setEnabled (false );
181
+ moduleComponent .putLine ("Module terminated." );
182
+
183
+ if (moduleID .hasTunnel () && tunnel != null )
184
+ tunnel .spinDown ();
170
185
}
171
186
}
172
187
} catch (InterruptedException e ) { }
@@ -205,4 +220,8 @@ public void addButtonToEnableOnLivingProcess(JButton button) {
205
220
public void addButtonToEnableOnDyingProcess (JButton button ) {
206
221
enabledButtonsOnDyingProcess .add (button );
207
222
}
223
+
224
+ public void setTunnel (Tunnel tunnel ) {
225
+ this .tunnel = tunnel ;
226
+ }
208
227
}
0 commit comments