use of primal.Verbs.Start 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 primal.Verbs.Start 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 primal.Verbs.Start in project suite by stupidsing.
the class NioClusterMapTest method testClusterMap.
@Test
public void testClusterMap() throws IOException {
var nNodes = 3;
var peers = forInt(nNodes).map2(i -> "NODE" + i, i -> new InetSocketAddress(localHost, 3000 + i)).toMap();
var clusters = //
Read.from2(//
peers).keys().map2(name -> name, //
name -> ex(() -> new NioCluster(name, peers))).toMap();
for (var cluster : clusters.values()) cluster.start();
var peerNames = new ArrayList<>(peers.keySet());
var clMap = //
Read.from2(//
peers).keys().map2(name -> name, //
name -> new NioClusterMap<Integer, String>(clusters.get(name))).toMap();
Sleep.quietly(5 * 1000);
System.out.println("=== CLUSTER FORMED (" + LocalDateTime.now() + ") ===\n");
Source<NioClusterMap<Integer, String>> peerf = () -> clMap.get(peerNames.get(random.nextInt(nNodes)));
Int_Obj<Sink<Runnable>> setf = i -> cont -> peerf.g().set(i, Integer.toString(i), v0 -> cont.run(), fail);
Int_Obj<Sink<Runnable>> getf = i -> cont -> peerf.g().get(i, v -> {
assertEquals(Integer.toString(i), v);
cont.run();
}, fail);
Fun<NioCluster, Sink<Runnable>> closef = cluster -> cont -> {
try {
cluster.stop();
System.out.println("=== CLUSTER STOPPED (" + LocalDateTime.now() + ") ===\n");
} catch (IOException ex) {
fail(ex);
}
cont.run();
};
var sinks = //
Streamlet.concat(//
forInt(9).map(setf), //
forInt(9).map(getf), Read.from2(clusters).values().map(closef)).toList();
new Object() {
public void run(int i) {
if (i < sinks.size())
sinks.get(i).f(() -> run(i + 1));
}
}.run(0);
Read.from2(clusters).values().map(cluster -> New.thread(cluster::run)).collect(Start::thenJoin);
for (var cluster : clusters.values()) cluster.close();
}
Aggregations