Skip to content

Commit f42650c

Browse files
committed
[platform] connect node process monitor
- connect node process monitor - enable/disable app buttons via process monitor - refact source code
1 parent 403bb41 commit f42650c

File tree

6 files changed

+213
-157
lines changed

6 files changed

+213
-157
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Share application with your friends in the same Wi-Fi!
3333
#### Screenshots
3434

3535
<div>
36-
<img src="https://raw.githubusercontent.com/wiki/dna2github/NodeBase/images/v0/install_app_manager.png" width="100" />
37-
<img src="https://raw.githubusercontent.com/wiki/dna2github/NodeBase/images/v0/app_manager.png" width="100" />
38-
<img src="https://raw.githubusercontent.com/wiki/dna2github/NodeBase/images/v0/file_download_upload.png" width="100" />
39-
<img src="https://raw.githubusercontent.com/wiki/dna2github/NodeBase/images/v0/nodepad.png" width="100" />
36+
<img src="https://raw.githubusercontent.com/wiki/dna2github/NodeBase/images/v0/install_app_manager.png" width="200" />
37+
<img src="https://raw.githubusercontent.com/wiki/dna2github/NodeBase/images/v0/app_manager.png" width="200" />
38+
<img src="https://raw.githubusercontent.com/wiki/dna2github/NodeBase/images/v0/file_download_upload.png" width="200" />
39+
<img src="https://raw.githubusercontent.com/wiki/dna2github/NodeBase/images/v0/nodepad.png" width="200" />
4040
</div>

app/app/src/main/java/seven/drawalive/nodebase/NodeBaseApp.java

Lines changed: 93 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
import java.io.File;
1717
import java.util.HashMap;
1818

19-
public class NodeBaseApp extends LinearLayout implements NodeService.NodeMonitorEvent {
19+
public class NodeBaseApp extends LinearLayout implements NodeMonitorEvent {
2020
public NodeBaseApp(Context context, HashMap<String, Object> env) {
2121
super(context);
2222
setOrientation(LinearLayout.VERTICAL);
23-
_context = context;
2423
_env = env;
2524
_appdir = (File)env.get("appdir");
2625

@@ -93,7 +92,7 @@ public void prepareLayout() {
9392

9493
TextView label;
9594
label = new TextView(context);
96-
label.setText(String.format("\nApp: %s", _appdir.getName()));
95+
label.setText(String.format("\nApp: %s", getAppName()));
9796
label.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24f);
9897
contents.addView(label);
9998
label = new TextView(context);
@@ -114,7 +113,7 @@ public void prepareLayout() {
114113
tbl_r_t = new TableRow(context);
115114
_listEntries = new Spinner(context);
116115
_listEntries.setAdapter(
117-
new ArrayAdapter<String>(
116+
new ArrayAdapter<>(
118117
context, android.R.layout.simple_spinner_dropdown_item, _appentries));
119118
tbl_r_t.addView(_listEntries);
120119
_txtParams = new EditText(context);
@@ -151,19 +150,39 @@ public void prepareEvents() {
151150
_btnStart.setOnClickListener(new OnClickListener() {
152151
@Override
153152
public void onClick(View view) {
153+
String appname = getAppName();
154154
_btnStart.setEnabled(false);
155155
_btnStop.setEnabled(true);
156156
_btnOpen.setEnabled(true);
157157
_btnShare.setEnabled(true);
158+
new Thread(new Runnable() {
159+
@Override
160+
public void run() {
161+
String appname = getAppName();
162+
long timestamp = System.currentTimeMillis();
163+
while (System.currentTimeMillis() - timestamp < 3000 /* 3s timeout */) {
164+
if (NodeService.services.containsKey(appname)) {
165+
NodeMonitor monitor = NodeService.services.get(appname);
166+
if (monitor.isDead()) {
167+
// not guarantee but give `after` get chance to run
168+
// if want to guarantee, `synchronized` isDead
169+
NodeBaseApp.this.after(monitor.getCommand(), null);
170+
} else {
171+
monitor.setEvent(NodeBaseApp.this);
172+
}
173+
break;
174+
}
175+
}
176+
}
177+
}).start();
158178
NodeService.touchService(
159-
_context,
179+
getContext(),
160180
new String[]{
161181
NodeService.AUTH_TOKEN,
162-
"start",
163-
_appdir.getName(),
182+
"start", appname,
164183
String.format(
165184
"%s/node/node %s/%s %s",
166-
(String)_env.get("datadir"),
185+
_env.get("datadir").toString(),
167186
_appdir.getAbsolutePath(),
168187
String.valueOf(_listEntries.getSelectedItem()),
169188
_txtParams.getText().toString()
@@ -179,76 +198,90 @@ public void onClick(View view) {
179198
_btnStop.setEnabled(false);
180199
_btnOpen.setEnabled(false);
181200
_btnShare.setEnabled(false);
182-
NodeService.touchService(_context, new String[]{
201+
NodeService.touchService(getContext(), new String[]{
183202
NodeService.AUTH_TOKEN,
184-
"stop", _appdir.getName()
203+
"stop", getAppName()
185204
});
186205
}
187206
});
188207

189208
_btnOpen.setOnClickListener(new OnClickListener() {
190209
@Override
191210
public void onClick(View v) {
192-
String name = null, protocol = null, port = null, index = null;
193-
if (_config != null) {
194-
name = _config.get(null, "name");
195-
port = _config.get(null, "port");
196-
protocol = _config.get(null, "protocol");
197-
index = _config.get(null, "index");
198-
}
199-
if (name == null) name = "NodeBase Service";
200-
if (port == null) port = ""; else port = ":" + port;
201-
if (protocol == null) protocol = "http";
202-
if (index == null) index = "";
203-
String url = String.format(
204-
"%s://%s%s%s", protocol, Network.getWifiIpv4(getContext()), port, index
211+
String app_url = String.format(
212+
generateAppUrlTemplate(),
213+
Network.getWifiIpv4(getContext())
205214
);
206-
External.openBrowser(getContext(), url);
215+
External.openBrowser(getContext(), app_url);
207216
}
208217
});
209218

210219
_btnShare.setOnClickListener(new OnClickListener() {
211220
@Override
212221
public void onClick(View view) {
213-
String name = null, protocol = null, port = null, index = null;
214-
if (_config != null) {
215-
name = _config.get(null, "name");
216-
port = _config.get(null, "port");
217-
protocol = _config.get(null, "protocol");
218-
index = _config.get(null, "index");
219-
}
220-
if (name == null) name = "NodeBase Service";
221-
if (port == null) port = ""; else port = ":" + port;
222-
if (protocol == null) protocol = "http";
223-
if (index == null) index = "";
222+
String name = generateAppTitle();
223+
String app_url = String.format(
224+
generateAppUrlTemplate(),
225+
Network.getWifiIpv4(getContext())
226+
);
224227
External.shareInformation(
225228
getContext(), "Share", "NodeBase",
226-
String.format(
227-
"[%s] is running at %s://%s%s%s",
228-
name, protocol, Network.getWifiIpv4(getContext()), port, index
229-
), null);
229+
String.format("[%s] is running at %s", name, app_url), null
230+
);
230231
}
231232
});
232233
}
233234

235+
private String generateAppUrlTemplate() {
236+
String protocol = null, port = null, index = null;
237+
if (_config != null) {
238+
port = _config.get(null, "port");
239+
protocol = _config.get(null, "protocol");
240+
index = _config.get(null, "index");
241+
}
242+
if (port == null) port = ""; else port = ":" + port;
243+
if (protocol == null) protocol = "http";
244+
if (index == null) index = "";
245+
return protocol + "://%s" + String.format("%s%s", port, index);
246+
}
247+
248+
private String generateAppTitle() {
249+
String name = null;
250+
if (_config != null) {
251+
name = _config.get(null, "name");
252+
}
253+
if (name == null) name = "NodeBase Service";
254+
return name;
255+
}
256+
234257
public String getAppName() {
235258
return _appdir.getName();
236259
}
237260

238261
@Override
239262
public void before(String[] cmd) {
240-
_btnStart.setEnabled(false);
241-
_btnStop.setEnabled(false);
242-
_btnOpen.setEnabled(false);
243-
_btnShare.setEnabled(false);
263+
UserInterface.run(new Runnable() {
264+
@Override
265+
public void run() {
266+
_btnStart.setEnabled(false);
267+
_btnStop.setEnabled(false);
268+
_btnOpen.setEnabled(false);
269+
_btnShare.setEnabled(false);
270+
}
271+
});
244272
}
245273

246274
@Override
247275
public void started(String[] cmd, Process process) {
248-
_btnStart.setEnabled(false);
249-
_btnStop.setEnabled(true);
250-
_btnOpen.setEnabled(true);
251-
_btnShare.setEnabled(true);
276+
UserInterface.run(new Runnable() {
277+
@Override
278+
public void run() {
279+
_btnStart.setEnabled(false);
280+
_btnStop.setEnabled(true);
281+
_btnOpen.setEnabled(true);
282+
_btnShare.setEnabled(true);
283+
}
284+
});
252285
}
253286

254287
@Override
@@ -257,10 +290,19 @@ public void error(String[] cmd, Process process) {
257290

258291
@Override
259292
public void after(String[] cmd, Process process) {
260-
_btnStart.setEnabled(true);
261-
_btnStop.setEnabled(false);
262-
_btnOpen.setEnabled(false);
263-
_btnShare.setEnabled(false);
293+
UserInterface.run(new Runnable() {
294+
@Override
295+
public void run() {
296+
_btnStart.setEnabled(true);
297+
_btnStop.setEnabled(false);
298+
_btnOpen.setEnabled(false);
299+
_btnShare.setEnabled(false);
300+
Alarm.showToast(
301+
NodeBaseApp.this.getContext(),
302+
String.format("\"%s\" stopped", getAppName())
303+
);
304+
}
305+
});
264306
}
265307

266308
private HashMap<String, Object> _env;
@@ -271,5 +313,4 @@ public void after(String[] cmd, Process process) {
271313
private EditText _txtParams;
272314
private String _readme;
273315
private NodeBaseAppConfigFile _config;
274-
private Context _context;
275316
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package seven.drawalive.nodebase;
2+
3+
import android.util.Log;
4+
5+
import java.io.IOException;
6+
7+
public class NodeMonitor extends Thread {
8+
public enum STATE { BORN, READY, RUNNING, DEAD };
9+
10+
public NodeMonitor(String service_name, String[] command) {
11+
state = STATE.BORN;
12+
this.service_name = service_name;
13+
this.command = command;
14+
event = null;
15+
}
16+
17+
public NodeMonitor setEvent(NodeMonitorEvent event) {
18+
this.event = event;
19+
return this;
20+
}
21+
22+
@Override
23+
public void run() {
24+
try {
25+
state = STATE.READY;
26+
if (event != null) event.before(command);
27+
Log.i("NodeService:NodeMonitor", String.format("node process starting - %s", command));
28+
node_process = Runtime.getRuntime().exec(command);
29+
state = STATE.RUNNING;
30+
if (event != null) event.started(command, node_process);
31+
Log.i("NodeService:NodeMonitor", "node process running ...");
32+
node_process.waitFor();
33+
/*
34+
BufferedReader reader = new BufferedReader(
35+
new InputStreamReader(_process.getInputStream()));
36+
String line = null;
37+
while ((line = reader.readLine()) != null) {
38+
Log.d("NodeMonitor", line);
39+
}
40+
Log.d("-----", "==========================");
41+
reader = new BufferedReader(
42+
new InputStreamReader(_process.getErrorStream()));
43+
while ((line = reader.readLine()) != null) {
44+
Log.d("NodeMonitor", line);
45+
}
46+
*/
47+
} catch (IOException e) {
48+
Log.e("NodeService:NodeMonitor", "node process error", e);
49+
node_process = null;
50+
if (event != null) event.error(command, null);
51+
} catch (InterruptedException e) {
52+
Log.e("NodeService:NodeMonitor", "node process error", e);
53+
if (event != null) event.error(command, node_process);
54+
} finally {
55+
state = STATE.DEAD;
56+
if (event != null) event.after(command, node_process);
57+
Log.i("NodeService:NodeMonitor", "node process stopped ...");
58+
}
59+
}
60+
61+
public String getServiceName() {
62+
return service_name;
63+
}
64+
65+
public String[] getCommand() {
66+
return command;
67+
}
68+
69+
public boolean stopService() {
70+
if (state == STATE.RUNNING) node_process.destroy();
71+
return true;
72+
}
73+
74+
public NodeMonitor restartService() {
75+
stopService();
76+
NodeMonitor m = new NodeMonitor(service_name, command);
77+
if (event != null) m.setEvent(event);
78+
return m;
79+
}
80+
81+
public boolean isRunning() {
82+
return state == STATE.RUNNING;
83+
}
84+
85+
public boolean isReady() {
86+
return state == STATE.READY;
87+
}
88+
89+
public boolean isDead() {
90+
return state == STATE.DEAD;
91+
}
92+
93+
private STATE state;
94+
private String service_name;
95+
private Process node_process;
96+
private String[] command;
97+
private NodeMonitorEvent event;
98+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package seven.drawalive.nodebase;
2+
3+
public interface NodeMonitorEvent {
4+
void before(String[] cmd);
5+
void started(String[] cmd, Process process);
6+
void error(String[] cmd, Process process);
7+
void after(String[] cmd, Process process);
8+
}

0 commit comments

Comments
 (0)