org.apache.mina.common.ByteBuffer Java Examples
The following examples show how to use
org.apache.mina.common.ByteBuffer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NioConfig.java From cougar with Apache License 2.0 | 6 votes |
protected void configureProtocol(BaseIoServiceConfig config, boolean isServer) throws IOException { ByteBuffer.setUseDirectBuffers(useDirectBuffersInMina); config.getFilterChain().addLast("slowHandling", new SessionWriteQueueMonitoring(nioLogger, maxWriteQueueSize)); config.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CougarProtocolEncoder(nioLogger), new CougarProtocolDecoder(nioLogger))); if (isServer) { config.getFilterChain().addLast("protocol", CougarProtocol.getServerInstance(nioLogger, keepAliveInterval, keepAliveTimeout, null, false, false)); } else { config.getFilterChain().addLast("protocol", CougarProtocol.getClientInstance(nioLogger, keepAliveInterval, keepAliveTimeout, null, false, false, rpcTimeoutMillis)); } config.setThreadModel(ThreadModel.MANUAL); }
Example #2
Source File: WelderDecoder.java From camelinaction with Apache License 2.0 | 6 votes |
protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { if (in.remaining() >= PAYLOAD_SIZE) { byte[] buf = new byte[in.remaining()]; in.get(buf); // first 7 bytes are the sensor ID, last is the status // and the result message will look something like // MachineID=2371748;Status=Good StringBuilder sb = new StringBuilder(); sb.append("MachineID=") .append(new String(buf, 0, PAYLOAD_SIZE - 1)).append(";") .append("Status="); if (buf[PAYLOAD_SIZE - 1] == '1') { sb.append("Good"); } else { sb.append("Failure"); } out.write(sb.toString()); return true; } else { return false; } }
Example #3
Source File: StructDecoder.java From javastruct with GNU Lesser General Public License v3.0 | 6 votes |
@Override protected boolean doDecode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { if(in.prefixedDataAvailable(4, MAX_MESSAGE_SIZE)){ int len = in.getInt(); int type = in.getInt(); byte[] b = new byte[len]; in.get(b); StructMessage m = Messages.getMessage(type); JavaStruct.unpack(m, b); out.write(m); return true; } return false; }
Example #4
Source File: StructEncoder.java From javastruct with GNU Lesser General Public License v3.0 | 5 votes |
public void encode(IoSession session, Object obj, ProtocolEncoderOutput out) throws Exception { StructMessage m = (StructMessage)obj; byte[] buffer = JavaStruct.pack(m); ByteBuffer b = ByteBuffer.allocate(buffer.length + 8, false); b.putInt(buffer.length + 4); b.putInt(m.getID()); b.put(buffer); b.flip(); out.write(b); }
Example #5
Source File: WelderEncoder.java From camelinaction with Apache License 2.0 | 5 votes |
public void encode(IoSession ioSession, Object message, ProtocolEncoderOutput out) throws Exception { ByteBuffer buf = ByteBuffer.allocate(PAYLOAD_SIZE); String s = (String) message; buf.put(s.getBytes()); buf.flip(); out.write(buf); }
Example #6
Source File: MinaCodecAdapter.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
@Override public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024); MinaChannel channel = MinaChannel.getOrAddChannel(session, url, handler); try { codec.encode(channel, buffer, msg); } finally { MinaChannel.removeChannelIfDisconnected(session); } out.write(ByteBuffer.wrap(buffer.toByteBuffer())); out.flush(); }
Example #7
Source File: MinaCodecAdapter.java From dubbox with Apache License 2.0 | 5 votes |
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024); MinaChannel channel = MinaChannel.getOrAddChannel(session, url, handler); try { codec.encode(channel, buffer, msg); } finally { MinaChannel.removeChannelIfDisconnectd(session); } out.write(ByteBuffer.wrap(buffer.toByteBuffer())); out.flush(); }
Example #8
Source File: MinaCodecAdapter.java From dubbox with Apache License 2.0 | 5 votes |
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024); MinaChannel channel = MinaChannel.getOrAddChannel(session, url, handler); try { codec.encode(channel, buffer, msg); } finally { MinaChannel.removeChannelIfDisconnectd(session); } out.write(ByteBuffer.wrap(buffer.toByteBuffer())); out.flush(); }
Example #9
Source File: MinaCodecAdapter.java From dubbox-hystrix with Apache License 2.0 | 5 votes |
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024); MinaChannel channel = MinaChannel.getOrAddChannel(session, url, handler); try { codec.encode(channel, buffer, msg); } finally { MinaChannel.removeChannelIfDisconnectd(session); } out.write(ByteBuffer.wrap(buffer.toByteBuffer())); out.flush(); }
Example #10
Source File: MinaCodecAdapter.java From dubbox with Apache License 2.0 | 5 votes |
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024); MinaChannel channel = MinaChannel.getOrAddChannel(session, url, handler); try { codec.encode(channel, buffer, msg); } finally { MinaChannel.removeChannelIfDisconnectd(session); } out.write(ByteBuffer.wrap(buffer.toByteBuffer())); out.flush(); }
Example #11
Source File: NioUtils.java From cougar with Apache License 2.0 | 4 votes |
public static byte[] getVersionSet(ByteBuffer buffer) { int numElements = buffer.get(); byte[] versions = new byte[numElements]; buffer.get(versions); return versions; }
Example #12
Source File: MinaCodecAdapter.java From dubbox with Apache License 2.0 | 4 votes |
public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { int readable = in.limit(); if (readable <= 0) return; ChannelBuffer frame; if (buffer.readable()) { if (buffer instanceof DynamicChannelBuffer) { buffer.writeBytes(in.buf()); frame = buffer; } else { int size = buffer.readableBytes() + in.remaining(); frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize); frame.writeBytes(buffer, buffer.readableBytes()); frame.writeBytes(in.buf()); } } else { frame = ChannelBuffers.wrappedBuffer(in.buf()); } Channel channel = MinaChannel.getOrAddChannel(session, url, handler); Object msg; int savedReadIndex; try { do { savedReadIndex = frame.readerIndex(); try { msg = codec.decode(channel, frame); } catch (Exception e) { buffer = ChannelBuffers.EMPTY_BUFFER; throw e; } if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { frame.readerIndex(savedReadIndex); break; } else { if (savedReadIndex == frame.readerIndex()) { buffer = ChannelBuffers.EMPTY_BUFFER; throw new Exception("Decode without read data."); } if (msg != null) { out.write(msg); } } } while (frame.readable()); } finally { if (frame.readable()) { frame.discardReadBytes(); buffer = frame; } else { buffer = ChannelBuffers.EMPTY_BUFFER; } MinaChannel.removeChannelIfDisconnectd(session); } }
Example #13
Source File: NioUtils.java From cougar with Apache License 2.0 | 4 votes |
public static void writeVersionSet(ByteBuffer buffer, byte[] applicationVersions) { buffer.put((byte) applicationVersions.length); buffer.put(applicationVersions); }
Example #14
Source File: CougarProtocolEncoder.java From cougar with Apache License 2.0 | 4 votes |
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { final ByteBuffer buffer; if (message instanceof ProtocolMessage) { ProtocolMessage pm = (ProtocolMessage) message; nioLogger.log(PROTOCOL, session, "CougarProtocolEncoder: Writing protocol message %s", pm.getProtocolMessageType()); Byte version = (Byte) session.getAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME); // go for lowest likely common denominator, since this will likely only occur for RejectMessages if (version == null) { version = CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MIN_SUPPORTED; } buffer = pm.getSerialisedForm(version); if (buffer == null) { badMessagesRequested.incrementAndGet(); throw new IllegalArgumentException("Couldn't serialise ProtocolMessage [" + ((ProtocolMessage) message).getProtocolMessageType() + "]"); } switch (pm.getProtocolMessageType()) { case ACCEPT: acceptsSent.incrementAndGet(); break; case CONNECT: connectsSent.incrementAndGet(); break; case REJECT: rejectsSent.incrementAndGet(); break; case KEEP_ALIVE: keepAlivesSent.incrementAndGet(); break; case DISCONNECT: disconnectsSent.incrementAndGet(); break; case MESSAGE_REQUEST: messageRequestsSent.incrementAndGet(); nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing message of length %s", (((RequestMessage) pm).getPayload().length + 8)); break; case MESSAGE_RESPONSE: messageRequestsSent.incrementAndGet(); nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing message of length %s", ((ResponseMessage) pm).getPayload().length); break; case EVENT: eventsSent.incrementAndGet(); nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing event of length %s", ((EventMessage) pm).getPayload().length); break; case SUSPEND: suspendsSent.incrementAndGet(); break; case START_TLS_REQUEST: tlsRequestsSent.incrementAndGet(); break; case START_TLS_RESPONSE: tlsResponsesSent.incrementAndGet(); break; default: badMessagesRequested.incrementAndGet(); throw new IllegalArgumentException("Unknown ProtocolMessage [" + ((ProtocolMessage) message).getProtocolMessageType() + "] received"); } } else { throw new IllegalArgumentException("Unknown message type " + message); } buffer.flip(); out.write(buffer); out.flush(); }
Example #15
Source File: NioUtils.java From cougar with Apache License 2.0 | 4 votes |
public static ByteBuffer createMessageHeader(int numBytes, ProtocolMessage message) { return createMessageHeader(numBytes, message.getProtocolMessageType()); }
Example #16
Source File: NioUtils.java From cougar with Apache License 2.0 | 4 votes |
public static ByteBuffer createMessageHeader(int numBytes, ProtocolMessageType type) { ByteBuffer bb = ByteBuffer.allocate(5 + numBytes); bb.putInt(numBytes + 1); bb.put(type.getMessageType()); return bb; }
Example #17
Source File: CougarProtocolEncoder.java From cougar with Apache License 2.0 | 4 votes |
public static ByteBuffer encode(ProtocolMessage pm, byte protocolVersion) { final ByteBuffer buffer; switch (pm.getProtocolMessageType()) { case ACCEPT: buffer = NioUtils.createMessageHeader(1, pm); buffer.put(((AcceptMessage) pm).getAcceptedVersion()); break; case CONNECT: ConnectMessage cm = (ConnectMessage) pm; buffer = NioUtils.createMessageHeader(1 + cm.getApplicationVersions().length, pm); NioUtils.writeVersionSet(buffer, cm.getApplicationVersions()); break; case REJECT: RejectMessage rm = (RejectMessage) pm; buffer = NioUtils.createMessageHeader(2 + rm.getAcceptableVersions().length, pm); buffer.put(rm.getRejectReason().getReasonCode()); NioUtils.writeVersionSet(buffer, rm.getAcceptableVersions()); break; case KEEP_ALIVE: buffer = NioUtils.createMessageHeader(0, pm); break; case DISCONNECT: buffer = NioUtils.createMessageHeader(0, pm); break; case MESSAGE_REQUEST: RequestMessage req = (RequestMessage) pm; ProtocolMessageType reqMsgType = protocolVersion == CougarProtocol.TRANSPORT_PROTOCOL_VERSION_CLIENT_ONLY_RPC ? ProtocolMessageType.MESSAGE : ProtocolMessageType.MESSAGE_REQUEST; buffer = NioUtils.createMessageHeader(req.getPayload().length + 8, reqMsgType); buffer.putLong(req.getCorrelationId()); buffer.put(req.getPayload()); break; case MESSAGE_RESPONSE: ResponseMessage resp = (ResponseMessage) pm; // backwards compatibility with version 1 protocol ProtocolMessageType responseType = protocolVersion == CougarProtocol.TRANSPORT_PROTOCOL_VERSION_CLIENT_ONLY_RPC ? ProtocolMessageType.MESSAGE : ProtocolMessageType.MESSAGE_RESPONSE; buffer = NioUtils.createMessageHeader(resp.getPayload().length + 8, responseType); buffer.putLong(resp.getCorrelationId()); buffer.put(resp.getPayload()); break; case EVENT: EventMessage em = (EventMessage) pm; if (protocolVersion < CougarProtocol.TRANSPORT_PROTOCOL_VERSION_BIDIRECTION_RPC) { return null; } buffer = NioUtils.createMessageHeader(em.getPayload().length, em); buffer.put(em.getPayload()); break; case SUSPEND: if (protocolVersion < CougarProtocol.TRANSPORT_PROTOCOL_VERSION_BIDIRECTION_RPC) { return null; } buffer = NioUtils.createMessageHeader(0, pm); break; case START_TLS_REQUEST: if (protocolVersion < CougarProtocol.TRANSPORT_PROTOCOL_VERSION_START_TLS) { return null; } buffer = NioUtils.createMessageHeader(1, pm); buffer.put(((StartTLSRequestMessage) pm).getRequirement().getValue()); break; case START_TLS_RESPONSE: if (protocolVersion < CougarProtocol.TRANSPORT_PROTOCOL_VERSION_START_TLS) { return null; } buffer = NioUtils.createMessageHeader(1, pm); buffer.put(((StartTLSResponseMessage) pm).getResult().getValue()); break; default: throw new IllegalArgumentException("Unknown ProtocolMessage [" + pm.getProtocolMessageType() + "] received"); } return buffer; }
Example #18
Source File: AbstractMessage.java From cougar with Apache License 2.0 | 4 votes |
@Override public ByteBuffer getSerialisedForm(byte protocolVersion) { return CougarProtocolEncoder.encode(this, protocolVersion); }
Example #19
Source File: EventMessage.java From cougar with Apache License 2.0 | 4 votes |
@Override public ByteBuffer getSerialisedForm(byte protocolVersion) { return serialisedForms.get(protocolVersion).duplicate(); }
Example #20
Source File: MinaStructPacker.java From javastruct with GNU Lesser General Public License v3.0 | 4 votes |
public MinaStructPacker(ByteBuffer buffer){ out = buffer; }
Example #21
Source File: MinaStructPacker.java From javastruct with GNU Lesser General Public License v3.0 | 4 votes |
public MinaStructPacker(){ out = ByteBuffer.allocate(64, false); out.setAutoExpand(true); }
Example #22
Source File: MinaCodecAdapter.java From dubbox with Apache License 2.0 | 4 votes |
public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { int readable = in.limit(); if (readable <= 0) return; ChannelBuffer frame; if (buffer.readable()) { if (buffer instanceof DynamicChannelBuffer) { buffer.writeBytes(in.buf()); frame = buffer; } else { int size = buffer.readableBytes() + in.remaining(); frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize); frame.writeBytes(buffer, buffer.readableBytes()); frame.writeBytes(in.buf()); } } else { frame = ChannelBuffers.wrappedBuffer(in.buf()); } Channel channel = MinaChannel.getOrAddChannel(session, url, handler); Object msg; int savedReadIndex; try { do { savedReadIndex = frame.readerIndex(); try { msg = codec.decode(channel, frame); } catch (Exception e) { buffer = ChannelBuffers.EMPTY_BUFFER; throw e; } if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { frame.readerIndex(savedReadIndex); break; } else { if (savedReadIndex == frame.readerIndex()) { buffer = ChannelBuffers.EMPTY_BUFFER; throw new Exception("Decode without read data."); } if (msg != null) { out.write(msg); } } } while (frame.readable()); } finally { if (frame.readable()) { frame.discardReadBytes(); buffer = frame; } else { buffer = ChannelBuffers.EMPTY_BUFFER; } MinaChannel.removeChannelIfDisconnectd(session); } }
Example #23
Source File: MinaCodecAdapter.java From dubbox-hystrix with Apache License 2.0 | 4 votes |
public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { int readable = in.limit(); if (readable <= 0) return; ChannelBuffer frame; if (buffer.readable()) { if (buffer instanceof DynamicChannelBuffer) { buffer.writeBytes(in.buf()); frame = buffer; } else { int size = buffer.readableBytes() + in.remaining(); frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize); frame.writeBytes(buffer, buffer.readableBytes()); frame.writeBytes(in.buf()); } } else { frame = ChannelBuffers.wrappedBuffer(in.buf()); } Channel channel = MinaChannel.getOrAddChannel(session, url, handler); Object msg; int savedReadIndex; try { do { savedReadIndex = frame.readerIndex(); try { msg = codec.decode(channel, frame); } catch (Exception e) { buffer = ChannelBuffers.EMPTY_BUFFER; throw e; } if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { frame.readerIndex(savedReadIndex); break; } else { if (savedReadIndex == frame.readerIndex()) { buffer = ChannelBuffers.EMPTY_BUFFER; throw new Exception("Decode without read data."); } if (msg != null) { out.write(msg); } } } while (frame.readable()); } finally { if (frame.readable()) { frame.discardReadBytes(); buffer = frame; } else { buffer = ChannelBuffers.EMPTY_BUFFER; } MinaChannel.removeChannelIfDisconnectd(session); } }
Example #24
Source File: MinaCodecAdapter.java From dubbox with Apache License 2.0 | 4 votes |
public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { int readable = in.limit(); if (readable <= 0) return; ChannelBuffer frame; if (buffer.readable()) { if (buffer instanceof DynamicChannelBuffer) { buffer.writeBytes(in.buf()); frame = buffer; } else { int size = buffer.readableBytes() + in.remaining(); frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize); frame.writeBytes(buffer, buffer.readableBytes()); frame.writeBytes(in.buf()); } } else { frame = ChannelBuffers.wrappedBuffer(in.buf()); } Channel channel = MinaChannel.getOrAddChannel(session, url, handler); Object msg; int savedReadIndex; try { do { savedReadIndex = frame.readerIndex(); try { msg = codec.decode(channel, frame); } catch (Exception e) { buffer = ChannelBuffers.EMPTY_BUFFER; throw e; } if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { frame.readerIndex(savedReadIndex); break; } else { if (savedReadIndex == frame.readerIndex()) { buffer = ChannelBuffers.EMPTY_BUFFER; throw new Exception("Decode without read data."); } if (msg != null) { out.write(msg); } } } while (frame.readable()); } finally { if (frame.readable()) { frame.discardReadBytes(); buffer = frame; } else { buffer = ChannelBuffers.EMPTY_BUFFER; } MinaChannel.removeChannelIfDisconnectd(session); } }
Example #25
Source File: MinaCodecAdapter.java From dubbo-2.6.5 with Apache License 2.0 | 4 votes |
@Override public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { int readable = in.limit(); if (readable <= 0) return; ChannelBuffer frame; if (buffer.readable()) { if (buffer instanceof DynamicChannelBuffer) { buffer.writeBytes(in.buf()); frame = buffer; } else { int size = buffer.readableBytes() + in.remaining(); frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize); frame.writeBytes(buffer, buffer.readableBytes()); frame.writeBytes(in.buf()); } } else { frame = ChannelBuffers.wrappedBuffer(in.buf()); } Channel channel = MinaChannel.getOrAddChannel(session, url, handler); Object msg; int savedReadIndex; try { do { savedReadIndex = frame.readerIndex(); try { msg = codec.decode(channel, frame); } catch (Exception e) { buffer = ChannelBuffers.EMPTY_BUFFER; throw e; } if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { frame.readerIndex(savedReadIndex); break; } else { if (savedReadIndex == frame.readerIndex()) { buffer = ChannelBuffers.EMPTY_BUFFER; throw new Exception("Decode without read data."); } if (msg != null) { out.write(msg); } } } while (frame.readable()); } finally { if (frame.readable()) { frame.discardReadBytes(); buffer = frame; } else { buffer = ChannelBuffers.EMPTY_BUFFER; } MinaChannel.removeChannelIfDisconnected(session); } }
Example #26
Source File: ProtocolMessage.java From cougar with Apache License 2.0 | votes |
public ByteBuffer getSerialisedForm(byte protocolVersion);