Adopt for Android
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@ -11,4 +11,8 @@
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
.gradle/
|
||||
.idea/
|
||||
build/
|
||||
|
||||
local.properties
|
||||
|
@ -11,6 +11,9 @@ plugins {
|
||||
id "org.sonarqube" version "2.8"
|
||||
}
|
||||
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
|
||||
|
@ -19,10 +19,7 @@ import static nearenough.util.BytesUtil.hexToBytes;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.DatagramPacket;
|
||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||
@ -72,11 +69,14 @@ public final class NettyClient {
|
||||
|
||||
// Sends the request to the Roughtime server
|
||||
ctx.writeAndFlush(new DatagramPacket(encodedMsg, addr))
|
||||
.addListener(future -> {
|
||||
if (!future.isSuccess()) {
|
||||
System.out.println("Send failed " + future.cause().getMessage());
|
||||
}
|
||||
});
|
||||
.addListener(new ChannelFutureListener() {
|
||||
@Override
|
||||
public void operationComplete(ChannelFuture future) throws Exception {
|
||||
if (!future.isSuccess()) {
|
||||
System.out.println("Send failed " + future.cause().getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
@ -116,7 +116,12 @@ public final class NettyClient {
|
||||
System.out.println("Response INVALID: " + client.invalidResponseCause().getMessage());
|
||||
}
|
||||
|
||||
ctx.close().addListener(unused -> System.exit(0));
|
||||
ctx.close().addListener(new ChannelFutureListener() {
|
||||
@Override
|
||||
public void operationComplete(ChannelFuture unused) throws Exception {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -153,12 +158,15 @@ public final class NettyClient {
|
||||
});
|
||||
|
||||
ChannelFuture connectFuture = bootstrap.connect();
|
||||
connectFuture.addListener(future -> {
|
||||
if (!future.isSuccess()) {
|
||||
System.out.println("Connect fail:");
|
||||
System.out.println(future.cause().getMessage());
|
||||
}
|
||||
});
|
||||
connectFuture.addListener(new ChannelFutureListener() {
|
||||
@Override
|
||||
public void operationComplete(ChannelFuture future) throws Exception {
|
||||
if (!future.isSuccess()) {
|
||||
System.out.println("Connect fail:");
|
||||
System.out.println(future.cause().getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
connectFuture.channel().closeFuture().sync();
|
||||
nioEventLoopGroup.shutdownGracefully();
|
||||
|
@ -27,6 +27,8 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import nearenough.protocol.exceptions.InvalidNumTagsException;
|
||||
import nearenough.protocol.exceptions.MessageTooShortException;
|
||||
import nearenough.protocol.exceptions.MessageUnalignedException;
|
||||
@ -241,14 +243,17 @@ public final class RtMessage {
|
||||
|
||||
if (map != null) {
|
||||
map.forEach(
|
||||
(tag, value) -> {
|
||||
sb.append(indent2).append(tag.name()).append("(").append(value.length).append(") = ");
|
||||
if (tag.isNested()) {
|
||||
sb.append(fromBytes(value).toString(indentLevel + 1));
|
||||
} else {
|
||||
sb.append(ByteBufUtil.hexDump(value)).append('\n');
|
||||
}
|
||||
}
|
||||
new BiConsumer<RtTag, byte[]>() {
|
||||
@Override
|
||||
public void accept(RtTag tag, byte[] value) {
|
||||
sb.append(indent2).append(tag.name()).append("(").append(value.length).append(") = ");
|
||||
if (tag.isNested()) {
|
||||
sb.append(fromBytes(value).toString(indentLevel + 1));
|
||||
} else {
|
||||
sb.append(ByteBufUtil.hexDump(value)).append('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
sb.append(indent1).append("}\n");
|
||||
|
@ -28,7 +28,12 @@ import java.util.TreeMap;
|
||||
public final class RtMessageBuilder {
|
||||
|
||||
private final Map<RtTag, byte[]> map = new TreeMap<>(
|
||||
Comparator.comparing(RtTag::valueLE, Integer::compareUnsigned)
|
||||
new Comparator<RtTag>() {
|
||||
@Override
|
||||
public int compare(RtTag o1, RtTag o2) {
|
||||
return Integer.compareUnsigned(o1.valueLE(), o2.valueLE());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
private ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
|
||||
|
@ -22,6 +22,7 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.function.ToIntFunction;
|
||||
|
||||
/**
|
||||
* Encodes/decodes {@link RtMessage Roughtime messages} and fields to/from their on-the-wire format.
|
||||
@ -68,7 +69,12 @@ public final class RtWire {
|
||||
int numTagsSum = 4;
|
||||
int tagsSum = 4 * map.size();
|
||||
int offsetsSum = map.size() < 2 ? 0 : (4 * (map.size() - 1));
|
||||
int valuesSum = map.values().stream().mapToInt(value -> value.length).sum();
|
||||
int valuesSum = map.values().stream().mapToInt(new ToIntFunction<byte[]>() {
|
||||
@Override
|
||||
public int applyAsInt(byte[] value) {
|
||||
return value.length;
|
||||
}
|
||||
}).sum();
|
||||
|
||||
return numTagsSum + tagsSum + offsetsSum + valuesSum;
|
||||
}
|
||||
|
Reference in New Issue
Block a user