Search in sources :

Example 1 with Start

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

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

Example 3 with Start

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();
}
Also used : NioClusterMap(suite.net.cluster.impl.NioClusterMap) Fail.fail(primal.statics.Fail.fail) Fun(primal.fp.Funs.Fun) Source(primal.fp.Funs.Source) LocalDateTime(java.time.LocalDateTime) IOException(java.io.IOException) Random(java.util.Random) Start(primal.Verbs.Start) New(primal.Verbs.New) Log_(primal.os.Log_) InetSocketAddress(java.net.InetSocketAddress) Sink(primal.fp.Funs.Sink) Read(primal.MoreVerbs.Read) Streamlet_.forInt(suite.util.Streamlet_.forInt) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test) Streamlet(primal.streamlet.Streamlet) Rethrow.ex(primal.statics.Rethrow.ex) Rethrow(primal.statics.Rethrow) Int_Obj(primal.primitive.IntPrim.Int_Obj) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Sleep(primal.Verbs.Sleep) NioCluster(suite.net.cluster.impl.NioCluster) NioClusterMap(suite.net.cluster.impl.NioClusterMap) Start(primal.Verbs.Start) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NioCluster(suite.net.cluster.impl.NioCluster) Sink(primal.fp.Funs.Sink) Test(org.junit.jupiter.api.Test)

Aggregations

Start (primal.Verbs.Start)3 ArrayList (java.util.ArrayList)2 Read (primal.MoreVerbs.Read)2 Log_ (primal.os.Log_)2 Listen (suite.os.Listen)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 SocketException (java.net.SocketException)1 LocalDateTime (java.time.LocalDateTime)1 List (java.util.List)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Test (org.junit.jupiter.api.Test)1 New (primal.Verbs.New)1 Sleep (primal.Verbs.Sleep)1 BooMutable (primal.adt.BooMutable)1 Fun (primal.fp.Funs.Fun)1