Search in sources :

Example 46 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class Lccs method lccs.

public Pair<Segment, Segment> lccs(Bytes bytes0, Bytes bytes1) {
    int rollingSize = min(bytes0.size(), bytes1.size());
    if (0 < rollingSize) {
        IntObjMap<Segment> segments0 = hashSegments(bytes0, rollingSize);
        IntObjMap<Segment> segments1 = hashSegments(bytes1, rollingSize);
        while (true) {
            Set<Integer> keys0 = segments0.streamlet().keys().toSet();
            Set<Integer> keys1 = segments1.streamlet().keys().toSet();
            Set<Integer> keys = Set_.intersect(keys0, keys1);
            for (int key : keys) {
                Segment segment0 = segments0.get(key);
                Segment segment1 = segments1.get(key);
                Bytes b0 = bytes0.range(segment0.start, segment0.end);
                Bytes b1 = bytes1.range(segment1.start, segment1.end);
                if (Objects.equals(b0, b1))
                    return Pair.of(segment0, segment1);
            }
            segments0 = reduceSegments(segments0, bytes0, rollingSize);
            segments1 = reduceSegments(segments1, bytes1, rollingSize);
            rollingSize--;
        }
    } else
        return Pair.of(Segment.of(0, 0), Segment.of(0, 0));
}
Also used : Bytes(suite.primitive.Bytes) Segment(suite.text.Segment)

Example 47 with Bytes

use of suite.primitive.Bytes 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 48 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class ClusterImpl method requestForResponse.

@Override
public Object requestForResponse(String peer, Object request) {
    if (probe.isActive(peer)) {
        Bytes req = NetUtil.serialize(request);
        Bytes resp = matcher.requestForResponse(getChannel(peer), req);
        return NetUtil.deserialize(resp);
    } else
        return Fail.t("peer " + peer + " is not active");
}
Also used : Bytes(suite.primitive.Bytes)

Example 49 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class NioChannelFactory method requestResponse.

public static <// 
C extends RequestResponseNioChannel> // 
C requestResponse(// 
C channel0, // 
RequestResponseMatcher matcher, // 
ThreadPoolExecutor executor, Iterate<Bytes> handler) {
    C channel = packeted(channel0);
    channel.onConnected.wire(sender -> channel.setConnected(sender != null));
    channel.onReceivePacket.wire(packet -> {
        if (5 <= packet.size()) {
            char type = (char) packet.get(0);
            int token = NetUtil.bytesToInt(packet.range(1, 5));
            Bytes contents = packet.range(5);
            if (type == RESPONSE)
                matcher.onResponseReceived(token, contents);
            else if (type == REQUEST)
                executor.execute(() -> channel.send(RESPONSE, token, handler.apply(contents)));
        }
    });
    return channel;
}
Also used : Bytes(suite.primitive.Bytes)

Example 50 with Bytes

use of suite.primitive.Bytes in project suite by stupidsing.

the class RequestResponseMatcher method requestForResponse.

public Bytes requestForResponse(RequestResponseNioChannel channel, Bytes request, int timeOut) {
    Integer token = Util.temp();
    Mutable<Bytes> holder = Mutable.nil();
    Condition condition = new Condition(() -> holder.get() != null);
    return condition.waitThen(() -> {
        requests.put(token, Pair.of(holder, condition));
        channel.send(RequestResponseNioChannel.REQUEST, token, request);
    }, () -> {
        requests.remove(token);
        return holder.get();
    });
}
Also used : Condition(suite.concurrent.Condition) Bytes(suite.primitive.Bytes)

Aggregations

Bytes (suite.primitive.Bytes)56 Test (org.junit.Test)18 BytesBuilder (suite.primitive.Bytes.BytesBuilder)8 IOException (java.io.IOException)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Pair (suite.adt.pair.Pair)4 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 To (suite.util.To)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Constants (suite.Constants)2 Amd64Interpret (suite.assembler.Amd64Interpret)2 Condition (suite.concurrent.Condition)2 Extent (suite.file.ExtentAllocator.Extent)2 JournalledPageFile (suite.file.JournalledPageFile)2 ImperativeCompiler (suite.ip.ImperativeCompiler)2 DataInput_ (suite.util.DataInput_)2