summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
b5eac5d)
The code was assuming that (number of characters == number of bytes),
which is not always the case!
The notions of number of bytes and data sizes only make sense when
the strings are encoded into byte arrays. The response's getBytes()
method should the only one handling these concepts.
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
* formatted.
*/
protected static String readNextString(ByteBuffer buffer) {
* formatted.
*/
protected static String readNextString(ByteBuffer buffer) {
- int length = buffer.getInt();
- if (length < 0) {
+ int nbBytes = buffer.getInt();
+ if (nbBytes < 0) {
/* The string length should be positive */
return null;
}
/* The string length should be positive */
return null;
}
/* The string is explicitly an empty string */
return "";
}
/* The string is explicitly an empty string */
return "";
}
- byte[] stringBytes = new byte[length];
+ byte[] stringBytes = new byte[nbBytes];
buffer.get(stringBytes);
return new String(stringBytes, SESSIOND_PROTOCOL_CHARSET).trim();
}
buffer.get(stringBytes);
return new String(stringBytes, SESSIOND_PROTOCOL_CHARSET).trim();
}
@Override
public LttngAgentResponse execute(ILttngTcpClientListener agent) {
final Collection<String> loggerList = agent.listAvailableEvents();
@Override
public LttngAgentResponse execute(ILttngTcpClientListener agent) {
final Collection<String> loggerList = agent.listAvailableEvents();
- int dataSize = 0;
-
- for (String event : agent.listAvailableEvents()) {
- dataSize += event.length() + 1;
- }
-
- return new SessiondListLoggersResponse(loggerList, dataSize);
+ return new SessiondListLoggersResponse(loggerList);
}
private static class SessiondListLoggersResponse extends LttngAgentResponse {
}
private static class SessiondListLoggersResponse extends LttngAgentResponse {
private final static int SIZE = 12;
private final Collection<String> loggers;
private final static int SIZE = 12;
private final Collection<String> loggers;
- private final int dataSize;
- public SessiondListLoggersResponse(Collection<String> loggers, int dataSize) {
+ public SessiondListLoggersResponse(Collection<String> loggers) {
- this.dataSize = dataSize;
@Override
public byte[] getBytes() {
@Override
public byte[] getBytes() {
+ /*
+ * Compute the data size, which is the number of bytes of each
+ * encoded string, +1 per string for the \0
+ */
+ int dataSize = 0;
+ for (String logger : loggers) {
+ dataSize += logger.getBytes(SESSIOND_PROTOCOL_CHARSET).length + 1;
+ }
+
+ /* Prepare the buffer */
byte data[] = new byte[SIZE + dataSize];
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.BIG_ENDIAN);
byte data[] = new byte[SIZE + dataSize];
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.BIG_ENDIAN);
+ /* Write the header section of the response */
buf.putInt(getReturnCode().getCode());
buf.putInt(dataSize);
buf.putInt(loggers.size());
buf.putInt(getReturnCode().getCode());
buf.putInt(dataSize);
buf.putInt(loggers.size());
+ /* Write the payload */
for (String logger : loggers) {
buf.put(logger.getBytes(SESSIOND_PROTOCOL_CHARSET));
/* NULL terminated byte after the logger name. */
for (String logger : loggers) {
buf.put(logger.getBytes(SESSIOND_PROTOCOL_CHARSET));
/* NULL terminated byte after the logger name. */