Java Code Examples for org.apache.mina.core.buffer.IoBuffer#putPrefixedString()

The following examples show how to use org.apache.mina.core.buffer.IoBuffer#putPrefixedString() . 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: ProtocolEncoderImpl.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
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 );
        }
    }
}
 
Example 2
Source File: ProtocolEncoderImpl.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
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 );
        }
    }
}
 
Example 3
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void encodeStringCollection ( final IoBuffer buffer, final byte fieldNumber, final Collection<String> data ) throws Exception
{
    buffer.put ( fieldNumber );
    if ( data != null )
    {
        buffer.put ( TYPE_STRING_LIST );
        buffer.putInt ( data.size () );
        for ( final String entry : data )
        {
            buffer.putPrefixedString ( entry, STRING_PREFIX_LEN, this.encoder );
        }
    }
    else
    {
        buffer.put ( TYPE_NULL );
    }
}
 
Example 4
Source File: PrefixedStringEncoder.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
    String value = (String) message;
    IoBuffer buffer = IoBuffer.allocate(value.length()).setAutoExpand(true);
    buffer.putPrefixedString(value, prefixLength, charset.newEncoder());
    if (buffer.position() > maxDataLength) {
        throw new IllegalArgumentException("Data length: " + buffer.position());
    }
    buffer.flip();
    out.write(buffer);
}
 
Example 5
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void encodeString ( final IoBuffer buffer, final byte fieldNumber, final String data ) throws Exception
{
    buffer.put ( fieldNumber );
    if ( data != null )
    {
        buffer.put ( TYPE_STRING );
        buffer.putPrefixedString ( data, STRING_PREFIX_LEN, this.encoder );
    }
    else
    {
        buffer.put ( TYPE_NULL );
    }
}
 
Example 6
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private void inlineEncodeVariant ( final IoBuffer buffer, final Variant variant ) throws Exception
{
    final VariantType type = variant.getType ();
    buffer.putEnum ( type );

    switch ( type )
    {
        case BOOLEAN:
            buffer.put ( variant.asBoolean () ? (byte)0xFF : (byte)0x00 );
            break;
        case DOUBLE:
            buffer.putDouble ( variant.asDouble () );
            break;
        case INT32:
            buffer.putInt ( variant.asInteger () );
            break;
        case INT64:
            buffer.putLong ( variant.asLong () );
            break;
        case STRING:
            buffer.putPrefixedString ( variant.asString (), STRING_PREFIX_LEN, this.encoder );
            break;
        case NULL:
            break;
        case UNKNOWN:
            break;
    }
}
 
Example 7
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
protected void inlineEncodeVariantMap ( final IoBuffer buffer, final Map<String, Variant> data ) throws Exception
{
    buffer.putInt ( data.size () );
    for ( final Map.Entry<String, Variant> entry : data.entrySet () )
    {
        buffer.putPrefixedString ( entry.getKey (), STRING_PREFIX_LEN, this.encoder );
        inlineEncodeVariant ( buffer, entry.getValue () );
    }
}
 
Example 8
Source File: DefaultBinaryContext.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
protected void inlineEncodeProperties ( final IoBuffer buffer, final Map<String, String> data ) throws Exception
{
    buffer.putInt ( data.size () );
    for ( final Map.Entry<String, String> entry : data.entrySet () )
    {
        buffer.putPrefixedString ( entry.getKey (), STRING_PREFIX_LEN, this.encoder );
        buffer.putPrefixedString ( entry.getValue (), STRING_PREFIX_LEN, this.encoder );
    }
}
 
Example 9
Source File: SumkProtocolEncoder.java    From sumk with Apache License 2.0 5 votes vote down vote up
public static void encodeString(int code, IoSession session, CharSequence message, ProtocolEncoderOutput out)
		throws CharacterCodingException, ProtocolEncoderException {
	code = code | Protocols.FORMAT_JSON;
	int size = message.length();
	int prefixLength = size <= (Protocols.MAX_ONE / CHAR_BYTE) ? 1
			: size <= (Protocols.MAX_TWO / CHAR_BYTE) ? 2 : 4;

	IoBuffer buffer = IoBuffer.allocate((int) (size * 1.5) + 10).setAutoExpand(true);
	putProtocol(code, buffer, prefixLength);

	buffer.putPrefixedString(message, prefixLength, Profile.UTF8.newEncoder());
	buffer.flip();

	out.write(buffer);
}
 
Example 10
Source File: ProtocolEncoderImpl.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void encode ( final IoSession session, final Object message, final ProtocolEncoderOutput out ) throws Exception
{
    IoBuffer data = null;
    if ( message instanceof Hello )
    {
        data = createMessage ( session, Messages.MC_HELLO, true );

        data.put ( (byte)0x01 ); // version

        data.putShort ( ( (Hello)message ).getNodeId () );
        data.putEnumSetShort ( ( (Hello)message ).getFeatures () );

        data.putUnsignedShort ( 3, data.position () - 3 ); // fill length
    }
    else if ( message instanceof Welcome )
    {
        data = createMessage ( session, Messages.MC_WELCOME, true );

        // put bit set
        data.putEnumSetShort ( ( (Welcome)message ).getFeatures () );
        encodeProperties ( data, ( (Welcome)message ).getProperties () );

        data.putUnsignedShort ( 3, data.position () - 3 ); // fill length
    }
    else if ( message instanceof ReadAll )
    {
        data = createMessage ( session, Messages.MC_READ_ALL, false );

    }
    else if ( message instanceof DataUpdate )
    {
        data = createMessage ( session, Messages.MC_DATA_UPDATE, true );

        data.putUnsignedShort ( ( (DataUpdate)message ).getEntries ().size () );
        // put values
        for ( final DataUpdate.Entry entry : ( (DataUpdate)message ).getEntries () )
        {
            encodeEntry ( session, data, entry );
        }

        data.putUnsignedShort ( 3, data.position () - 3 ); // fill length
    }
    else if ( message instanceof SubscribeBrowse )
    {
        data = createMessage ( session, Messages.MC_START_BROWSE, false );
    }
    else if ( message instanceof UnsubscribeBrowse )
    {
        data = createMessage ( session, Messages.MC_STOP_BROWSE, false );
    }
    else if ( message instanceof BrowseAdded )
    {
        data = createMessage ( session, Messages.MC_NS_ADDED, true );

        // put browse update
        encodeBrowseUpdate ( session, message, data );

        data.putUnsignedShort ( 3, data.position () - 3 ); // fill length
    }
    else if ( message instanceof WriteCommand )
    {
        data = createMessage ( session, Messages.MC_WRITE_COMMAND, true );

        data.putUnsignedShort ( ( (WriteCommand)message ).getRegisterNumber () );
        data.putInt ( ( (WriteCommand)message ).getOperationId () );
        encodeVariant ( session, data, ( (WriteCommand)message ).getValue () );

        data.putUnsignedShort ( 3, data.position () - 3 ); // fill length
    }
    else if ( message instanceof WriteResult )
    {
        data = createMessage ( session, Messages.MC_WRITE_RESULT, true );

        data.putInt ( ( (WriteResult)message ).getOperationId () );
        data.putUnsignedShort ( ( (WriteResult)message ).getErrorCode () );

        data.putPrefixedString ( ( (WriteResult)message ).getErrorMessage (), Sessions.getCharsetEncoder ( session ) );

        data.putUnsignedShort ( 3, data.position () - 3 ); // fill length
    }

    if ( data != null )
    {
        data.flip ();
        out.write ( data );
    }
    else
    {
        throw new ProtocolCodecException ( String.format ( "Message %s is not supported", message.getClass ().getName () ) );
    }
}
 
Example 11
Source File: ProtocolEncoderImpl.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
private void encodeVariant ( final IoSession session, final IoBuffer data, final Variant value ) throws ProtocolCodecException
{
    if ( value == null )
    {
        data.put ( DataType.DEAD.getDataType () ); // dead
    }
    else if ( value.isNull () )
    {
        data.put ( DataType.NULL.getDataType () );
    }
    else if ( value.isBoolean () )
    {
        data.put ( DataType.BOOLEAN.getDataType () );
        data.put ( (byte) ( value.asBoolean () ? 0xFF : 0x00 ) );
    }
    else if ( value.isInteger () )
    {
        data.put ( DataType.INT32.getDataType () );
        data.putInt ( value.asInteger ( null ) );
    }
    else if ( value.isLong () )
    {
        data.put ( DataType.INT64.getDataType () );
        data.putLong ( value.asLong ( null ) );
    }
    else if ( value.isDouble () )
    {
        data.put ( DataType.DOUBLE.getDataType () );
        data.putDouble ( value.asDouble ( null ) );
    }
    else if ( value.isString () )
    {
        data.put ( DataType.STRING.getDataType () );
        try
        {
            data.putPrefixedString ( value.asString ( null ), Sessions.getCharsetEncoder ( session ) );
        }
        catch ( final CharacterCodingException e )
        {
            throw new ProtocolCodecException ( e );
        }
    }
}