Search in sources :

Example 1 with SocketUtil

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();
    }
}
Also used : OutputStream(java.io.OutputStream) Socket(java.net.Socket) SocketUtil(suite.os.SocketUtil) Copy(suite.util.Copy) LogUtil(suite.os.LogUtil) BooMutable(suite.primitive.BooMutable) Thread_(suite.util.Thread_) IOException(java.io.IOException) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) List(java.util.List) Io(suite.os.SocketUtil.Io) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InputStream(java.io.InputStream) SocketException(java.net.SocketException) Io(suite.os.SocketUtil.Io) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) SocketException(java.net.SocketException) BooMutable(suite.primitive.BooMutable) ArrayList(java.util.ArrayList) List(java.util.List) SocketUtil(suite.os.SocketUtil) Socket(java.net.Socket)

Example 2 with SocketUtil

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

Example 3 with SocketUtil

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

Example 4 with SocketUtil

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);
    });
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) SocketUtil(suite.os.SocketUtil) IOException(java.io.IOException)

Aggregations

SocketUtil (suite.os.SocketUtil)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 LogUtil (suite.os.LogUtil)1 Io (suite.os.SocketUtil.Io)1 BooMutable (suite.primitive.BooMutable)1 Bytes (suite.primitive.Bytes)1 Copy (suite.util.Copy)1 Thread_ (suite.util.Thread_)1