Search in sources :

Example 21 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project weave by continuuity.

the class MessageFetcher method decodeMessage.

private void decodeMessage(int size, ChannelBuffer buffer, long nextOffset) {
    int readerIdx = buffer.readerIndex();
    int magic = buffer.readByte();
    Compression compression = magic == 0 ? Compression.NONE : Compression.fromCode(buffer.readByte());
    int crc = buffer.readInt();
    ChannelBuffer payload = buffer.readSlice(size - (buffer.readerIndex() - readerIdx));
    // Verify CRC?
    enqueueMessage(compression, payload, nextOffset);
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 22 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class ConnectionContext method handleStartupHeader.

private void handleStartupHeader(ChannelBuffer buffer, Channel channel) {
    // sslCode
    buffer.readInt();
    ChannelBuffer channelBuffer = ChannelBuffers.buffer(1);
    channelBuffer.writeByte('N');
    ChannelFuture channelFuture = channel.write(channelBuffer);
    if (LOGGER.isTraceEnabled()) {
        channelFuture.addListener(future -> LOGGER.trace("sent SSL neg: N"));
    }
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 23 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class Messages method sendDataRow.

/**
     * Byte1('D')
     * Identifies the message as a data row.
     * <p>
     * Int32
     * Length of message contents in bytes, including self.
     * <p>
     * Int16
     * The number of column values that follow (possibly zero).
     * <p>
     * Next, the following pair of fields appear for each column:
     * <p>
     * Int32
     * The length of the column value, in bytes (this count does not include itself).
     * Can be zero. As a special case, -1 indicates a NULL column value. No value bytes follow in the NULL case.
     * <p>
     * ByteN
     * The value of the column, in the format indicated by the associated format code. n is the above length.
     */
static void sendDataRow(Channel channel, Row row, List<? extends DataType> columnTypes, @Nullable FormatCodes.FormatCode[] formatCodes) {
    int length = 4 + 2;
    assert columnTypes.size() == row.numColumns() : "Number of columns in the row must match number of columnTypes. Row: " + row + " types: " + columnTypes;
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    buffer.writeByte('D');
    // will be set at the end
    buffer.writeInt(0);
    buffer.writeShort(row.numColumns());
    for (int i = 0; i < row.numColumns(); i++) {
        DataType dataType = columnTypes.get(i);
        PGType pgType = PGTypes.get(dataType);
        Object value = row.get(i);
        if (value == null) {
            buffer.writeInt(-1);
            length += 4;
        } else {
            FormatCodes.FormatCode formatCode = FormatCodes.getFormatCode(formatCodes, i);
            switch(formatCode) {
                case TEXT:
                    length += pgType.writeAsText(buffer, value);
                    break;
                case BINARY:
                    length += pgType.writeAsBinary(buffer, value);
                    break;
                default:
                    throw new AssertionError("Unrecognized formatCode: " + formatCode);
            }
        }
    }
    buffer.setInt(1, length);
    channel.write(buffer);
}
Also used : PGType(io.crate.protocols.postgres.types.PGType) DataType(io.crate.types.DataType) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 24 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class Messages method sendRowDescription.

/**
     * RowDescription (B)
     * <p>
     * | 'T' | int32 len | int16 numCols
     * <p>
     * For each field:
     * <p>
     * | string name | int32 table_oid | int16 attr_num | int32 oid | int16 typlen | int32 type_modifier | int16 format_code
     * <p>
     * See https://www.postgresql.org/docs/current/static/protocol-message-formats.html
     */
static void sendRowDescription(Channel channel, Collection<Field> columns, @Nullable FormatCodes.FormatCode[] formatCodes) {
    int length = 4 + 2;
    int columnSize = 4 + 2 + 4 + 2 + 4 + 2;
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(// use 10 as an estimate for columnName length
    length + (columns.size() * (10 + columnSize)));
    buffer.writeByte('T');
    // will be set at the end
    buffer.writeInt(0);
    buffer.writeShort(columns.size());
    int idx = 0;
    for (Field column : columns) {
        byte[] nameBytes = column.path().outputName().getBytes(StandardCharsets.UTF_8);
        length += nameBytes.length + 1;
        length += columnSize;
        writeCString(buffer, nameBytes);
        // table_oid
        buffer.writeInt(0);
        // attr_num
        buffer.writeShort(0);
        PGType pgType = PGTypes.get(column.valueType());
        buffer.writeInt(pgType.oid());
        buffer.writeShort(pgType.typeLen());
        buffer.writeInt(pgType.typeMod());
        buffer.writeShort(FormatCodes.getFormatCode(formatCodes, idx).ordinal());
        idx++;
    }
    buffer.setInt(1, length);
    ChannelFuture channelFuture = channel.write(buffer);
    if (LOGGER.isTraceEnabled()) {
        channelFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                LOGGER.trace("sentRowDescription");
            }
        });
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) Field(io.crate.analyze.symbol.Field) PGType(io.crate.protocols.postgres.types.PGType) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 25 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class Messages method sendCommandComplete.

/**
     * | 'C' | int32 len | str commandTag
     *
     * @param query    :the query
     * @param rowCount : number of rows in the result set or number of rows affected by the DML statement
     */
static void sendCommandComplete(Channel channel, String query, long rowCount) {
    query = query.split(" ", 2)[0].toUpperCase(Locale.ENGLISH);
    String commandTag;
    /*
         * from https://www.postgresql.org/docs/current/static/protocol-message-formats.html:
         *
         * For an INSERT command, the tag is INSERT oid rows, where rows is the number of rows inserted.
         * oid is the object ID of the inserted row if rows is 1 and the target table has OIDs; otherwise oid is 0.
         */
    if ("BEGIN".equals(query)) {
        commandTag = "BEGIN";
    } else if ("INSERT".equals(query)) {
        commandTag = "INSERT 0 " + rowCount;
    } else {
        commandTag = query + " " + rowCount;
    }
    byte[] commandTagBytes = commandTag.getBytes(StandardCharsets.UTF_8);
    int length = 4 + commandTagBytes.length + 1;
    ChannelBuffer buffer = ChannelBuffers.buffer(length + 1);
    buffer.writeByte('C');
    buffer.writeInt(length);
    writeCString(buffer, commandTagBytes);
    ChannelFuture channelFuture = channel.write(buffer);
    if (LOGGER.isTraceEnabled()) {
        channelFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                LOGGER.trace("sentCommandComplete");
            }
        });
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)312 Test (org.junit.Test)63 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)59 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)49 Test (org.testng.annotations.Test)49 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)46 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)43 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)37 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)34 Checkpoint (com.linkedin.databus.core.Checkpoint)27 ByteBuffer (java.nio.ByteBuffer)27 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 IOException (java.io.IOException)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)23 ArrayList (java.util.ArrayList)21 Channel (org.jboss.netty.channel.Channel)19 ChannelFuture (org.jboss.netty.channel.ChannelFuture)18 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)17 BlockLogBuffer (org.neo4j.com.BlockLogBuffer)16 Map (java.util.Map)14