aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/fs4/PacketQueryTracer.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-search/src/main/java/com/yahoo/fs4/PacketQueryTracer.java')
-rw-r--r--container-search/src/main/java/com/yahoo/fs4/PacketQueryTracer.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/fs4/PacketQueryTracer.java b/container-search/src/main/java/com/yahoo/fs4/PacketQueryTracer.java
new file mode 100644
index 00000000000..3eeebb43a6f
--- /dev/null
+++ b/container-search/src/main/java/com/yahoo/fs4/PacketQueryTracer.java
@@ -0,0 +1,53 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.fs4;
+
+import java.nio.ByteBuffer;
+
+import com.yahoo.fs4.mplex.FS4Channel;
+import com.yahoo.search.Query;
+
+/**
+ * Adds packets to the query context
+ *
+ * @author tonytv
+ */
+public class PacketQueryTracer implements PacketListener {
+
+ private final static int traceLevel = 10;
+
+ private void addTrace(FS4Channel channel, BasicPacket packet, ByteBuffer serializedForm) {
+ Query query = channel.getQuery();
+ if (query != null && query.getTraceLevel() >= traceLevel) {
+ StringBuilder traceString = new StringBuilder();
+ traceString.append(packet.getClass().getSimpleName()).append(": ");
+ hexDump(serializedForm, traceString);
+
+ final boolean includeQuery = true;
+ query.trace(traceString.toString(), includeQuery, traceLevel);
+ }
+ }
+
+ private void hexDump(ByteBuffer serializedForm, StringBuilder traceString) {
+ HexByteIterator hexByteIterator = new HexByteIterator(serializedForm);
+
+ long count = 0;
+ final int maxNumCharacters = 80;
+ while (hexByteIterator.hasNext()) {
+ if (++count % maxNumCharacters == 0)
+ traceString.append('\n');
+ traceString.append(hexByteIterator.next());
+ }
+ }
+
+ @Override
+ public void packetSent(FS4Channel channel, BasicPacket packet, ByteBuffer serializedForm) {
+ addTrace(channel, packet, serializedForm);
+ }
+
+ @Override
+ public void packetReceived(FS4Channel channel, BasicPacket packet, ByteBuffer serializedForm) {
+ addTrace(channel, packet, serializedForm);
+ }
+
+}
+