Search in sources :

Example 1 with ControlMessage

use of org.apache.storm.messaging.netty.ControlMessage in project storm by apache.

the class ThriftDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    long available = buf.readableBytes();
    if (available < INTEGER_SIZE) {
        return null;
    }
    buf.markReaderIndex();
    int thriftLen = buf.readInt();
    available -= INTEGER_SIZE;
    if (available < thriftLen) {
        // We haven't received the entire object yet, return and wait for more bytes.
        buf.resetReaderIndex();
        return null;
    }
    byte[] serialized = new byte[thriftLen];
    buf.readBytes(serialized, 0, thriftLen);
    HBMessage m = (HBMessage) Utils.thriftDeserialize(HBMessage.class, serialized);
    if (m.get_type() == HBServerMessageType.CONTROL_MESSAGE) {
        ControlMessage cm = ControlMessage.read(m.get_data().get_message_blob());
        return cm;
    } else if (m.get_type() == HBServerMessageType.SASL_MESSAGE_TOKEN) {
        SaslMessageToken sm = SaslMessageToken.read(m.get_data().get_message_blob());
        return sm;
    } else {
        return m;
    }
}
Also used : SaslMessageToken(org.apache.storm.messaging.netty.SaslMessageToken) HBMessage(org.apache.storm.generated.HBMessage) ControlMessage(org.apache.storm.messaging.netty.ControlMessage)

Example 2 with ControlMessage

use of org.apache.storm.messaging.netty.ControlMessage in project storm by apache.

the class ThriftEncoder method encode.

@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) {
    if (msg == null) {
        return null;
    }
    LOG.debug("Trying to encode: " + msg.getClass().toString() + " : " + msg.toString());
    HBMessage m;
    if (msg instanceof INettySerializable) {
        INettySerializable nettyMsg = (INettySerializable) msg;
        HBServerMessageType type;
        if (msg instanceof ControlMessage) {
            type = HBServerMessageType.CONTROL_MESSAGE;
        } else if (msg instanceof SaslMessageToken) {
            type = HBServerMessageType.SASL_MESSAGE_TOKEN;
        } else {
            LOG.error("Didn't recognise INettySerializable: " + nettyMsg.toString());
            throw new RuntimeException("Unrecognized INettySerializable.");
        }
        m = encodeNettySerializable(nettyMsg, type);
    } else {
        m = (HBMessage) msg;
    }
    try {
        byte[] serialized = Utils.thriftSerialize(m);
        ChannelBuffer ret = ChannelBuffers.directBuffer(serialized.length + 4);
        ret.writeInt(serialized.length);
        ret.writeBytes(serialized);
        return ret;
    } catch (RuntimeException e) {
        LOG.error("Failed to serialize.", e);
        throw e;
    }
}
Also used : HBServerMessageType(org.apache.storm.generated.HBServerMessageType) SaslMessageToken(org.apache.storm.messaging.netty.SaslMessageToken) INettySerializable(org.apache.storm.messaging.netty.INettySerializable) HBMessage(org.apache.storm.generated.HBMessage) ControlMessage(org.apache.storm.messaging.netty.ControlMessage) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

HBMessage (org.apache.storm.generated.HBMessage)2 ControlMessage (org.apache.storm.messaging.netty.ControlMessage)2 SaslMessageToken (org.apache.storm.messaging.netty.SaslMessageToken)2 HBServerMessageType (org.apache.storm.generated.HBServerMessageType)1 INettySerializable (org.apache.storm.messaging.netty.INettySerializable)1 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1