use of suite.os.SocketUtil in project suite by stupidsing.
the class LoadBalancer method run.
public void run() throws IOException {
BooMutable running = BooMutable.true_();
Thread probe = new Thread(() -> {
while (running.isTrue()) try {
List<String> alives1 = new ArrayList<>();
for (String server : servers) try (Socket socket = new Socket(server, port)) {
alives1.add(server);
} catch (SocketException ex) {
}
alives = alives1;
Thread.sleep(500l);
} catch (Exception ex) {
LogUtil.error(ex);
}
});
Io io = (is, os) -> {
int count = counter.getAndIncrement();
List<String> alives0 = alives;
String server = alives0.get(count % alives0.size());
try (Socket socket = new Socket(server, port)) {
InputStream sis = socket.getInputStream();
OutputStream sos = socket.getOutputStream();
List<Thread> threads = List.of(Copy.streamByThread(is, sos), Copy.streamByThread(sis, os));
Thread_.startJoin(threads);
}
};
try {
probe.start();
new SocketUtil().listenIo(port, io);
} finally {
running.setFalse();
}
}
use of suite.os.SocketUtil in project suite by stupidsing.
the class SimpleCgiServerMain method run.
private void run(Handler handler) throws IOException {
new SocketUtil().listenIo(4000, (is, os) -> {
Map<String, String> headers = readHeaders(is);
os.write((//
"Status: 200 OK\r\n" + //
"Content-Type: text/html\r\n" + "\r\n").getBytes(Constants.charset));
handler.handle(headers, os);
});
}
use of suite.os.SocketUtil in project suite by stupidsing.
the class SocketServer method listen.
private void listen(Fun<Bytes, Bytes> handle) throws IOException {
new SocketUtil().listenIo(5151, (is, os) -> {
Bytes in = read(is, 65536);
Bytes out = handle.apply(in);
os.write(out.bs);
});
}
use of suite.os.SocketUtil in project suite by stupidsing.
the class HttpServer method run_.
private void run_(HttpHandler handler) throws IOException {
new SocketUtil().listenIo(8051, (is, os) -> {
String line;
String[] ls = Util.readLine(is).split(" ");
String method = ls[0], url = ls[1], protocol = ls[2];
String server, pqs;
Pair<String, String> pp = String_.split2(url, "://");
if (String_.isNotBlank(pp.t1)) {
Pair<String, String> sp = String_.split2(pp.t1, "/");
server = sp.t0;
pqs = sp.t1;
} else {
server = "";
pqs = url;
}
Pair<String, String> pq = String_.split2(pqs, "?");
String path = pq.t0;
String query = pq.t1;
String path1 = path.startsWith("/") ? path : "/" + path;
String path2 = URLDecoder.decode(path1, "UTF-8");
if (!String_.equals(protocol, "HTTP/1.1"))
Fail.t("only HTTP/1.1 is supported");
Map<String, String> requestHeaders = new HashMap<>();
while (!(line = Util.readLine(is)).isEmpty()) {
Pair<String, String> pair = String_.split2(line, ":");
requestHeaders.put(pair.t0, pair.t1);
}
String cls = requestHeaders.get("Content-Length");
int contentLength = cls != null ? Integer.parseInt(cls) : 0;
InputStream cis = sizeLimitedInputStream(is, contentLength);
HttpRequest request = new HttpRequest(method, server, path2, query, requestHeaders, cis);
HttpResponse response = null;
try {
response = handler.handle(request);
} catch (Exception ex) {
LogUtil.error(ex);
response = HttpResponse.of(HttpResponse.HTTP500);
} finally {
LogUtil.info(request.getLogString() + " " + response.getLogString());
}
StringBuilder sb = new StringBuilder();
sb.append("HTTP/1.1 " + response.status + "\r\n");
for (Pair<String, String> e : response.headers) sb.append(e.t0 + ": " + e.t1 + "\r\n");
sb.append("\r\n");
os.write(sb.toString().getBytes(Constants.charset));
Copy.stream(To.inputStream(response.out), os);
});
}
Aggregations