Search in sources :

Example 1 with Io

use of suite.os.Listen.Io 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();
    }
}
Also used : Io(suite.os.Listen.Io) Socket(java.net.Socket) SocketException(java.net.SocketException) List(java.util.List) Copy(suite.util.Copy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Start(primal.Verbs.Start) Log_(primal.os.Log_) Listen(suite.os.Listen) BooMutable(primal.adt.BooMutable) Read(primal.MoreVerbs.Read) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) Io(suite.os.Listen.Io) Listen(suite.os.Listen) Socket(java.net.Socket) SocketException(java.net.SocketException)

Aggregations

Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Read (primal.MoreVerbs.Read)1 Start (primal.Verbs.Start)1 BooMutable (primal.adt.BooMutable)1 Log_ (primal.os.Log_)1 Listen (suite.os.Listen)1 Io (suite.os.Listen.Io)1 Copy (suite.util.Copy)1