Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Usage
Include HAP-Java in your project using maven:
```
<dependency>
<groupId>com.beowulfe.hap</groupId>
<groupId>io.github.hap-java</groupId>
<artifactId>hap</artifactId>
<version>1.1.3</version>
<version>1.2.0-SNAPSHOT</version>
</dependency>
```

Expand Down
27 changes: 10 additions & 17 deletions src/main/java/io/github/hapjava/impl/http/impl/BinaryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.io.HexDump;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,9 +25,9 @@ public BinaryHandler(HomekitClientConnection connection) {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
if (started) {
debugData("Sending data", msg, ctx);
byte[] b = new byte[msg.readableBytes()];
msg.readBytes(b);
traceData("Sending data", b, ctx);
out.writeBytes(connection.encryptResponse(b));
} else {
out.writeBytes(msg);
Expand All @@ -41,8 +39,9 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
byte[] b = new byte[in.readableBytes()];
in.readBytes(b);
byte[] decrypted = connection.decryptRequest(b);
traceData("Received data", decrypted, ctx);
out.add(Unpooled.copiedBuffer(decrypted));
ByteBuf outBuf = Unpooled.copiedBuffer(decrypted);
debugData("Received data", outBuf, ctx);
out.add(outBuf);
started = true;
}

Expand All @@ -57,18 +56,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
super.exceptionCaught(ctx, cause);
}

private void traceData(String msg, byte[] b, ChannelHandlerContext ctx) throws Exception {
if (logger.isTraceEnabled() && b.length > 0) {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
HexDump.dump(b, 0, stream, 0);
stream.flush();
logger.trace(
String.format(
"%s [%s]:%n%s%n",
msg,
ctx.channel().remoteAddress().toString(),
stream.toString(StandardCharsets.UTF_8.name())));
}
private void debugData(String msg, ByteBuf b, ChannelHandlerContext ctx) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug(
String.format(
"%s [%s]:%n%s",
msg, ctx.channel().remoteAddress().toString(), b.toString(StandardCharsets.UTF_8)));
}
}
}