Search in sources :

Example 1 with Listen

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();
    }
}
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)

Example 2 with Listen

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);
    });
}
Also used : Listen(suite.os.Listen)

Example 3 with Listen

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);
        }
    });
}
Also used : Start(primal.Verbs.Start) Listen(suite.os.Listen)

Example 4 with Listen

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);
    });
}
Also used : Listen(suite.os.Listen)

Example 5 with Listen

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)));
        }
    });
}
Also used : Request(suite.http.Http.Request) Listen(suite.os.Listen)

Aggregations

Listen (suite.os.Listen)5 Start (primal.Verbs.Start)2 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 BooMutable (primal.adt.BooMutable)1 Log_ (primal.os.Log_)1 Request (suite.http.Http.Request)1 Io (suite.os.Listen.Io)1 Copy (suite.util.Copy)1