-
Notifications
You must be signed in to change notification settings - Fork 16
/
lvp.java
235 lines (203 loc) · 8.93 KB
/
lvp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
// To run this code type `jshell -R-ea --enable-preview`
enum SSEType { WRITE, CALL, SCRIPT, LOAD, CLEAR, RELEASE; }
class LiveView {
final HttpServer server;
final int port;
static int defaultPort = 50_001;
static final String index = "./web/index.html";
static Map<Integer,LiveView> views = new ConcurrentHashMap<>();
List<String> paths = new ArrayList<>();
static void setDefaultPort(int port) { defaultPort = port != 0 ? Math.abs(port) : 50_001; }
static int getDefaultPort() { return defaultPort; }
List<HttpExchange> sseClientConnections;
// lock required to temporarily block processing of `SSEType.LOAD`
Lock lock = new ReentrantLock();
Condition loadEventOccurredCondition = lock.newCondition();
boolean loadEventOccured = false;
static LiveView onPort(int port) {
port = Math.abs(port);
try {
if (!views.containsKey(port))
views.put(port, new LiveView(port));
return views.get(port);
} catch (IOException e) {
System.err.printf("Error starting Server: %s\n", e.getMessage());
e.printStackTrace();
return null;
}
}
static LiveView onPort() { return onPort(defaultPort); }
private LiveView(int port) throws IOException {
this.port = port;
sseClientConnections = new CopyOnWriteArrayList<>(); // thread-safe variant of ArrayList
server = HttpServer.create(new InetSocketAddress("localhost", port), 0);
System.out.println("Open http://localhost:" + port + " in your browser");
// loaded-Request to signal successful processing of SSEType.LOAD
server.createContext("/loaded", exchange -> {
if (!exchange.getRequestMethod().equalsIgnoreCase("post")) {
exchange.sendResponseHeaders(405, -1); // Method Not Allowed
return;
}
exchange.sendResponseHeaders(200, 0);
exchange.close();
lock.lock();
try { // try/finally pattern for locks
loadEventOccured = true;
loadEventOccurredCondition.signalAll();
} finally {
lock.unlock();
}
});
// SSE context
server.createContext("/events", exchange -> {
if (!exchange.getRequestMethod().equalsIgnoreCase("get")) {
exchange.sendResponseHeaders(405, -1); // Method Not Allowed
return;
}
exchange.getResponseHeaders().add("Content-Type", "text/event-stream");
exchange.getResponseHeaders().add("Cache-Control", "no-cache");
exchange.getResponseHeaders().add("Connection", "keep-alive");
exchange.sendResponseHeaders(200, 0);
sseClientConnections.add(exchange);
});
// initial html site
server.createContext("/", exchange -> {
if (!exchange.getRequestMethod().equalsIgnoreCase("get")) {
exchange.sendResponseHeaders(405, -1); // Method Not Allowed
return;
}
final String path = exchange.getRequestURI().getPath().equals("/") ? index
: "." + exchange.getRequestURI().getPath();
try (final InputStream stream = new FileInputStream(path)) {
final byte[] bytes = stream.readAllBytes();
exchange.getResponseHeaders().add("Content-Type", Files.probeContentType(Path.of(path)) + "; charset=utf-8");
exchange.sendResponseHeaders(200, bytes.length);
exchange.getResponseBody().write(bytes);
exchange.getResponseBody().flush();
} finally {
exchange.close();
}
});
server.setExecutor(Executors.newFixedThreadPool(5));
server.start();
}
void sendServerEvent(SSEType sseType, String data) {
List<HttpExchange> deadConnections = new ArrayList<>();
for (HttpExchange connection : sseClientConnections) {
if (sseType == SSEType.LOAD) lock.lock();
try {
byte[] binaryData = data.getBytes(StandardCharsets.UTF_8);
String base64Data = Base64.getEncoder().encodeToString(binaryData);
String message = "data: " + sseType + ":" + base64Data + "\n\n";
connection.getResponseBody()
.write(message.getBytes());
connection.getResponseBody().flush();
if (sseType == SSEType.LOAD && !loadEventOccured) {
loadEventOccurredCondition.await(1_000, TimeUnit.MILLISECONDS);
if (loadEventOccured) paths.add(data);
else System.err.println("LOAD-Timeout: " + data);
}
} catch (IOException e) {
deadConnections.add(connection);
} catch (InterruptedException e) {
System.err.println("LOAD-Timeout: " + data + ", " + e);
} finally {
if (sseType == SSEType.LOAD) {
loadEventOccured = false;
lock.unlock();
}
}
}
sseClientConnections.removeAll(deadConnections);
}
void createResponseContext(String path, Consumer<String> delegate) {
createResponseContext(path, delegate, "-1");
}
void createResponseContext(String path, Consumer<String> delegate, String id) {
server.createContext(path, exchange -> {
if (!exchange.getRequestMethod().equalsIgnoreCase("post")) {
exchange.sendResponseHeaders(405, -1); // Method Not Allowed
return;
}
String content_length = exchange.getRequestHeaders().getFirst("Content-length");
if (content_length == null) {
exchange.sendResponseHeaders(400, -1);
return;
}
try {
int length = Integer.parseInt(content_length);
byte[] data = new byte[length];
exchange.getRequestBody().read(data);
delegate.accept(new String(data));
sendServerEvent(SSEType.RELEASE, id);
} catch (NumberFormatException e) {
exchange.sendResponseHeaders(400, -1);
return;
}
exchange.sendResponseHeaders(200, 0);
exchange.close();
});
}
public void stop() {
sseClientConnections.clear();
views.remove(port);
server.stop(0);
}
static void shutdown() {
views.forEach((k, v) -> v.stop());
}
}
interface Clerk {
static String generateID(int n) { // random alphanumeric string of size n
return new Random().ints(n, 0, 36).
mapToObj(i -> Integer.toString(i, 36)).
collect(Collectors.joining());
}
static String getHashID(Object o) { return Integer.toHexString(o.hashCode()); }
static LiveView view(int port) { return LiveView.onPort(port); }
static LiveView view() { return view(LiveView.getDefaultPort()); }
static void write(LiveView view, String html) { view.sendServerEvent(SSEType.WRITE, html); }
static void call(LiveView view, String javascript) { view.sendServerEvent(SSEType.CALL, javascript); }
static void script(LiveView view, String javascript) { view.sendServerEvent(SSEType.SCRIPT, javascript); }
static void load(LiveView view, String path) {
if (!view.paths.contains(path.trim())) view.sendServerEvent(SSEType.LOAD, path);
}
static void load(LiveView view, String onlinePath, String offlinePath) {
load(view, onlinePath + ", " + offlinePath);
}
static void clear(LiveView view) { view.sendServerEvent(SSEType.CLEAR, ""); }
static void clear() { clear(view()); };
static void markdown(String text) { new MarkdownIt(view()).write(text); }
}
/open skills/Text/Text.java
/open skills/ObjectInspector/ObjectInspector.java
/open views/Turtle/Turtle.java
/open views/Markdown/Marked.java
/open views/Markdown/MarkdownIt.java
/open views/TicTacToe/TicTacToe.java
/open views/Dot/Dot.java
/open views/Input/Slider.java
// LiveView view = Clerk.view();