Skip to content

Commit 99ae780

Browse files
committed
start adapting engine to persistent interprocessing
1 parent 5cdcc14 commit 99ae780

File tree

6 files changed

+885
-1073
lines changed

6 files changed

+885
-1073
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package io.bioimage.modelrunner.tensorflow.v2.api050;
2+
3+
import java.io.IOException;
4+
import java.net.URISyntaxException;
5+
import java.util.HashMap;
6+
import java.util.LinkedHashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Scanner;
10+
11+
import io.bioimage.modelrunner.apposed.appose.Types;
12+
import io.bioimage.modelrunner.apposed.appose.Service.RequestType;
13+
import io.bioimage.modelrunner.apposed.appose.Service.ResponseType;
14+
15+
public class JavaWorker {
16+
17+
private static LinkedHashMap<String, Object> tasks = new LinkedHashMap<String, Object>();
18+
19+
private final String uuid;
20+
21+
private final Tensorflow2Interface ti;
22+
23+
private boolean cancelRequested = false;
24+
25+
public static void main(String[] args) {
26+
27+
try(Scanner scanner = new Scanner(System.in)){
28+
Tensorflow2Interface ti;
29+
try {
30+
ti = new Tensorflow2Interface(false);
31+
} catch (IOException | URISyntaxException e) {
32+
return;
33+
}
34+
35+
while (true) {
36+
String line;
37+
try {
38+
if (!scanner.hasNextLine()) break;
39+
line = scanner.nextLine().trim();
40+
} catch (Exception e) {
41+
break;
42+
}
43+
44+
if (line.isEmpty()) break;
45+
Map<String, Object> request = Types.decode(line);
46+
String uuid = (String) request.get("task");
47+
String requestType = (String) request.get("requestType");
48+
49+
if (requestType.equals(RequestType.EXECUTE.toString())) {
50+
String script = (String) request.get("script");
51+
Map<String, Object> inputs = (Map<String, Object>) request.get("inputs");
52+
JavaWorker task = new JavaWorker(uuid, ti);
53+
tasks.put(uuid, task);
54+
task.start(script, inputs);
55+
} else if (requestType.equals(RequestType.CANCEL.toString())) {
56+
JavaWorker task = (JavaWorker) tasks.get(uuid);
57+
if (task == null) {
58+
System.err.println("No such task: " + uuid);
59+
continue;
60+
}
61+
task.cancelRequested = true;
62+
} else {
63+
break;
64+
}
65+
}
66+
}
67+
68+
}
69+
70+
private JavaWorker(String uuid, Tensorflow2Interface ti) {
71+
this.uuid = uuid;
72+
this.ti = ti;
73+
}
74+
75+
private void executeScript(String script, Map<String, Object> inputs) {
76+
Map<String, Object> binding = new LinkedHashMap<String, Object>();
77+
binding.put("task", this);
78+
if (inputs != null)
79+
binding.putAll(binding);
80+
81+
this.reportLaunch();
82+
try {
83+
if (script.equals("loadModel")) {
84+
ti.loadModel((String) inputs.get("modelFolder"), null);
85+
} else if (script.equals("inference")) {
86+
ti.runFromShmas((List<String>) inputs.get("inputs"), (List<String>) inputs.get("outputs"));
87+
} else if (script.equals("close")) {
88+
ti.closeModel();
89+
}
90+
} catch(Exception ex) {
91+
this.fail(Types.stackTrace(ex));
92+
return;
93+
}
94+
this.reportCompletion();
95+
}
96+
97+
private void start(String script, Map<String, Object> inputs) {
98+
new Thread(() -> executeScript(script, inputs), "Appose-" + this.uuid).start();
99+
}
100+
101+
private void reportLaunch() {
102+
respond(ResponseType.LAUNCH, null);
103+
}
104+
105+
private void reportCompletion() {
106+
respond(ResponseType.COMPLETION, null);
107+
}
108+
109+
private void update(String message, Integer current, Integer maximum) {
110+
LinkedHashMap<String, Object> args = new LinkedHashMap<String, Object>();
111+
112+
if (message != null)
113+
args.put("message", message);
114+
115+
if (current != null)
116+
args.put("current", current);
117+
118+
if (maximum != null)
119+
args.put("maximum", maximum);
120+
this.respond(ResponseType.UPDATE, args);
121+
}
122+
123+
private void respond(ResponseType responseType, Map<String, Object> args) {
124+
Map<String, Object> response = new HashMap<String, Object>();
125+
response.put("task", uuid);
126+
response.put("responseType", responseType);
127+
if (args != null)
128+
response.putAll(args);
129+
try {
130+
System.out.println(Types.encode(response));
131+
System.out.flush();
132+
} catch(Exception ex) {
133+
this.fail(Types.stackTrace(ex.getCause()));
134+
}
135+
}
136+
137+
private void cancel() {
138+
this.respond(ResponseType.CANCELATION, null);
139+
}
140+
141+
private void fail(String error) {
142+
Map<String, Object> args = null;
143+
if (error != null) {
144+
args = new HashMap<String, Object>();
145+
args.put("error", error);
146+
}
147+
respond(ResponseType.FAILURE, args);
148+
}
149+
150+
}

0 commit comments

Comments
 (0)