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