use of suite.os.Listen in project suite by stupidsing.
the class LoadBalancer method run.
public void run() {
var running = BooMutable.true_();
var probe = new Thread(() -> {
while (running.isTrue()) try {
var alives1 = new ArrayList<String>();
for (var server : servers) try (var socket = new Socket(server, port)) {
alives1.add(server);
} catch (SocketException ex) {
}
alives = alives1;
Thread.sleep(500l);
} catch (Exception ex) {
Log_.error(ex);
}
});
Io io = (is, os) -> {
var count = counter.getAndIncrement();
var alives0 = alives;
var server = alives0.get(count % alives0.size());
try (var socket = new Socket(server, port)) {
var sis = socket.getInputStream();
var sos = socket.getOutputStream();
Read.each(Copy.streamByThread(is, sos), Copy.streamByThread(sis, os)).collect(Start::thenJoin);
}
};
try {
probe.start();
new Listen().io(port, io);
} finally {
running.setFalse();
}
}
use of suite.os.Listen in project suite by stupidsing.
the class ServeSocket method listen.
private void listen(Fun<Bytes, Bytes> handle) {
new Listen().io(5151, (is, os) -> {
var in = read(is, Defaults.bufferLimit);
var out = handle.apply(in);
os.write(out.bs);
});
}
use of suite.os.Listen in project suite by stupidsing.
the class HttpProxy method serve0.
public void serve0() {
new Listen().io(port, (is, os) -> {
var line = ReadLine.from(is);
Log_.info("PROXY " + line);
var url = line.split(" ")[1];
var pp = Split.string(url, "://");
var path = pp != null ? Split.strl(pp.v, "/").v : url;
try (//
var socket1 = connect(path);
//
var is0 = is;
//
var os0 = os;
//
var is1 = socket1.getInputStream();
var os1 = socket1.getOutputStream()) {
os1.write((line + "\r\nConnection: close\r\n").getBytes(Utf8.charset));
Read.each(Copy.streamByThread(is0, os1), Copy.streamByThread(is1, os0)).collect(Start::thenJoin);
}
});
}
use of suite.os.Listen in project suite by stupidsing.
the class SimpleCgiServerMain method run.
private void run(Handler handler) {
new Listen().io(4000, (is, os) -> {
var headers = readHeaders(is);
os.write((//
"Status: 200 OK\r\n" + //
"Content-Type: text/html\r\n" + "\r\n").getBytes(Utf8.charset));
handler.handle(headers, os);
});
}
use of suite.os.Listen in project suite by stupidsing.
the class HttpProxy method serve.
public void serve() {
var httpIo = new HttpIo();
new Listen().io(port, (is, os) -> {
var request0 = httpIo.readRequest(is);
var pq = request0.path() + "?" + request0.query;
Log_.info("PROXY " + pq);
var headers1 = request0.headers.remove("Connection").put("Connection", "close");
var request1 = new //
Request(//
request0.method, //
request0.server, //
request0.paths, //
request0.query, //
headers1, request0.in);
try (//
var socket1 = connect(pq);
//
var is0 = is;
//
var os0 = os;
//
var is1 = socket1.getInputStream();
var os1 = socket1.getOutputStream()) {
//
Start.thenJoin(//
() -> httpIo.writeRequest(os1, request1), () -> httpIo.writeResponse(os0, httpIo.readResponse(is1)));
}
});
}
Aggregations