private byte[] toBytes(String str) throws ProtocolCodecException { try { return str.getBytes(charsetEncoding); } catch (UnsupportedEncodingException e) { throw new ProtocolCodecException(e); } }
private byte[] toBytes(String str) throws ProtocolCodecException { try { return str.getBytes(charsetEncoding); } catch (UnsupportedEncodingException e) { throw new ProtocolCodecException(e); } }
private byte[] toBytes(String str) throws ProtocolCodecException { try { return str.getBytes(charsetEncoding); } catch (UnsupportedEncodingException e) { throw new ProtocolCodecException(e); } }
private byte checkType ( final IoBuffer buffer, final byte expectedType, final boolean allowNull ) throws Exception { final byte type = buffer.get (); if ( allowNull && type == TYPE_NULL ) { return type; } if ( type != expectedType ) { if ( type == 0 && !allowNull ) { throw new ProtocolCodecException ( String.format ( "Failed to decode. Field is transmitted as null but defined as not-null." ) ); } throw new ProtocolCodecException ( String.format ( "Failed to decode string: Expected type %02x, found: %02x", expectedType, type ) ); } return type; }
private byte checkType ( final IoBuffer buffer, final byte expectedType, final boolean allowNull ) throws Exception { final byte type = buffer.get (); if ( allowNull && type == TYPE_NULL ) { return type; } if ( type != expectedType ) { if ( type == 0 && !allowNull ) { throw new ProtocolCodecException ( String.format ( "Failed to decode. Field is transmitted as null but defined as not-null." ) ); } throw new ProtocolCodecException ( String.format ( "Failed to decode string: Expected type %02x, found: %02x", expectedType, type ) ); } return type; }
private void encodeProperties ( final IoBuffer data, final Map<String, String> properties ) throws ProtocolCodecException { final CharsetEncoder encoder = this.defaultCharset.newEncoder (); data.putUnsignedShort ( properties.size () ); for ( final Map.Entry<String, String> entry : properties.entrySet () ) { try { data.putPrefixedString ( entry.getKey (), encoder ); data.putPrefixedString ( entry.getValue (), encoder ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } } }
@Override public void decode ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput output ) throws Exception { final short magic = data.getShort (); final byte version = data.get (); final int sequence = data.getInt (); final byte commandCode = data.get (); if ( magic != 1202 ) { throw new ProtocolCodecException ( String.format ( "Magic code should be 1202 but is %s", magic ) ); } if ( version != 1 ) { throw new ProtocolCodecException ( String.format ( "Version should be %s but is %s", 1, version ) ); } decodeMessage ( sequence, commandCode, data, output ); }
@Override public void decode ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput output ) throws Exception { final short magic = data.getShort (); final byte version = data.get (); final int sequence = data.getInt (); final byte commandCode = data.get (); if ( magic != 1202 ) { throw new ProtocolCodecException ( String.format ( "Magic code should be 1202 but is %s", magic ) ); } if ( version != 1 ) { throw new ProtocolCodecException ( String.format ( "Version should be %s but is %s", 1, version ) ); } decodeMessage ( sequence, commandCode, data, output ); }
throw new ProtocolCodecException ( e );
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { // get message bytes byte[] bytes; if (message instanceof String) { bytes = toBytes((String) message); } else if (message instanceof Message) { bytes = toBytes(message.toString()); } else { throw new ProtocolCodecException("Invalid FIX message object type: " + message.getClass()); } // write bytes to buffer and output it IoBuffer buffer = IoBuffer.allocate(bytes.length); buffer.put(bytes); buffer.flip(); out.write(buffer); } }
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { // get message bytes byte[] bytes; if (message instanceof String) { bytes = toBytes((String) message); } else if (message instanceof Message) { bytes = toBytes(message.toString()); } else { throw new ProtocolCodecException("Invalid FIX message object type: " + message.getClass()); } // write bytes to buffer and output it IoBuffer buffer = IoBuffer.allocate(bytes.length); buffer.put(bytes); buffer.flip(); out.write(buffer); } }
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { // get message bytes byte[] bytes; if (message instanceof String) { bytes = toBytes((String) message); } else if (message instanceof Message) { bytes = toBytes(message.toString()); } else { throw new ProtocolCodecException("Invalid FIX message object type: " + message.getClass()); } // write bytes to buffer and output it IoBuffer buffer = IoBuffer.allocate(bytes.length); buffer.put(bytes); buffer.flip(); out.write(buffer); } }
private boolean processHello ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException { final int len = messageLength ( data ); if ( len < 0 ) { return false; } final byte version = data.get (); if ( version != 0x01 ) { throw new ProtocolCodecException ( String.format ( "Protocol version %s is unsupported", version ) ); } final short nodeId = data.getShort (); final EnumSet<Hello.Features> features = data.getEnumSetShort ( Hello.Features.class ); out.write ( new Hello ( nodeId, features ) ); return true; }
private BrowseAdded.Entry decodeBrowserAddEntry ( final IoBuffer data, final IoSession session ) throws ProtocolCodecException { final short register = (short)data.getUnsignedShort (); // FIXME: validate if short works final byte b = data.get (); final DataType dataType = DataType.fromByte ( b ); if ( dataType == null ) { throw new ProtocolCodecException ( String.format ( "Data type %s is unkown", b ) ); } final Set<BrowseAdded.Entry.Flags> flags = data.getEnumSet ( BrowseAdded.Entry.Flags.class ); final CharsetDecoder decoder = Sessions.getCharsetDecoder ( session ); try { final String name = data.getPrefixedString ( decoder ); final String description = data.getPrefixedString ( decoder ); final String unit = data.getPrefixedString ( decoder ); return new BrowseAdded.Entry ( register, name, description, unit, dataType, flags ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } }
private Object decodeData ( final IoBuffer data ) throws ProtocolCodecException { data.order ( ByteOrder.LITTLE_ENDIAN ); final byte dataType = data.get (); switch ( dataType ) { case 0: return null; case 1: return data.get () != 0x00; case 2: return data.getInt (); case 3: return data.getLong (); case 4: return data.getFloat (); default: throw new ProtocolCodecException ( String.format ( "Data type %02x is unknown", dataType ) ); } }
private Object decodeData ( final IoBuffer data ) throws ProtocolCodecException { data.order ( ByteOrder.LITTLE_ENDIAN ); final byte dataType = data.get (); switch ( dataType ) { case 0: return null; case 1: return data.get () != 0x00; case 2: return data.getInt (); case 3: return data.getLong (); case 4: return data.getFloat (); default: throw new ProtocolCodecException ( String.format ( "Data type %02x is unknown", dataType ) ); } }
private boolean processWriteResult ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException { final int len = messageLength ( data ); if ( len < 0 ) { return false; } try { final int operationId = data.getInt (); final int errorCode = data.getUnsignedShort (); final String errorMessage = decodeString ( session, data ); out.write ( new WriteResult ( operationId, errorCode, errorMessage ) ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } return true; }
private void encodeBrowseUpdate ( final IoSession session, final Object message, final IoBuffer data ) throws ProtocolCodecException { // length data.putUnsignedShort ( ( (BrowseAdded)message ).getEntries ().size () ); final CharsetEncoder encoder = Sessions.getCharsetEncoder ( session ); // data for ( final BrowseAdded.Entry entry : ( (BrowseAdded)message ).getEntries () ) { data.putUnsignedShort ( entry.getRegister () ); data.put ( entry.getDataType ().getDataType () ); data.putEnumSet ( entry.getFlags () ); try { data.putPrefixedString ( entry.getName (), encoder ); data.putPrefixedString ( entry.getDescription (), encoder ); data.putPrefixedString ( entry.getUnit (), encoder ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } } }
throw new ProtocolCodecException ( String.format ( "Message code %02x is unkown", command ) );