当前位置: 首页>>代码示例>>Java>>正文


Java IoBuffer.getUnsignedShort方法代码示例

本文整理汇总了Java中org.apache.mina.core.buffer.IoBuffer.getUnsignedShort方法的典型用法代码示例。如果您正苦于以下问题:Java IoBuffer.getUnsignedShort方法的具体用法?Java IoBuffer.getUnsignedShort怎么用?Java IoBuffer.getUnsignedShort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.mina.core.buffer.IoBuffer的用法示例。


在下文中一共展示了IoBuffer.getUnsignedShort方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: processWriteCommand

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private boolean processWriteCommand ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException
{
    final int len = messageLength ( data );
    if ( len < 0 )
    {
        return false;
    }

    final int registerNumber = data.getUnsignedShort ();
    final int operationId = data.getInt ();
    final Variant value = decodeVariant ( session, data );

    out.write ( new WriteCommand ( registerNumber, value, operationId ) );

    return true;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:17,代码来源:ProtocolDecoderImpl.java

示例2: processWriteResult

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
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;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:24,代码来源:ProtocolDecoderImpl.java

示例3: processDataUpdate

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private boolean processDataUpdate ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException
{
    final int len = messageLength ( data );
    if ( len < 0 )
    {
        return false;
    }

    final int count = data.getUnsignedShort ();
    final List<DataUpdate.Entry> entries = new ArrayList<DataUpdate.Entry> ( count );
    for ( int i = 0; i < count; i++ )
    {
        entries.add ( decodeDataUpdateEntry ( data, session ) );
    }

    out.write ( new DataUpdate ( entries ) );

    return true;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:20,代码来源:ProtocolDecoderImpl.java

示例4: processBrowseAdded

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private boolean processBrowseAdded ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException
{
    final int len = messageLength ( data );
    if ( len < 0 )
    {
        return false;
    }

    final int count = data.getUnsignedShort ();

    final List<BrowseAdded.Entry> entries = new ArrayList<BrowseAdded.Entry> ( count );

    for ( int i = 0; i < count; i++ )
    {
        entries.add ( decodeBrowserAddEntry ( data, session ) );
    }

    out.write ( new BrowseAdded ( entries ) );

    return true;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:22,代码来源:ProtocolDecoderImpl.java

示例5: writeData

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public void writeData ( final int blockAddress, byte[] data )
{
    if ( this.request.getType () != RequestType.HOLDING )
    {
        throw new IllegalStateException ( String.format ( "Modbus can only write data when the block is of type %s", RequestType.HOLDING ) );
    }
    if ( data.length == 2 )
    {
        final IoBuffer buffer = IoBuffer.wrap ( data );
        buffer.order ( this.dataOrder );
        final int value = buffer.getUnsignedShort ();
        this.slave.writeCommand ( new WriteSingleDataRequest ( this.transactionId.incrementAndGet (), this.slave.getSlaveAddress (), Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER, toWriteAddress ( blockAddress ), value ), this.request.getTimeout () );
    }
    else
    {
        data = ModbusProtocol.encodeData ( data, this.dataOrder ); // apply requested byte order
        this.slave.writeCommand ( new WriteMultiDataRequest ( this.transactionId.incrementAndGet (), this.slave.getSlaveAddress (), Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS, toWriteAddress ( blockAddress ), data, data.length / 2 ), this.request.getTimeout () );
    }
    requestUpdate ();
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:22,代码来源:ModbusRequestBlock.java

示例6: decodeDataReadReply

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private DaveMessage decodeDataReadReply ( final IoBuffer parameters, final IoBuffer data )
{
    parameters.get (); // command code

    final short count = parameters.getUnsigned ();

    final Collection<Result> result = new LinkedList<Result> ();

    for ( int i = 0; i < count; i++ )
    {
        final short q = data.getUnsigned ();
        if ( q == 0xFF && data.remaining () > 4 )
        {
            final byte type = data.get ();
            int len = data.getUnsignedShort ();
            if ( type == 4 )
            {
                len >>= 3;
            }

            final IoBuffer valueData = IoBuffer.allocate ( len );
            data.get ( valueData.array () );
            result.add ( new Result ( valueData ) );

            if ( len % 2 != 0 && data.remaining () > 0 )
            {
                data.get ();
            }
        }
        else if ( q != 0xFF )
        {
            result.add ( new Result ( q ) );
        }
    }

    return new DaveReadResult ( result );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:38,代码来源:DaveFilter.java

示例7: processAnalogWrite

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private void processAnalogWrite ( final int startAddress, final int numRegisters, final byte[] rawData )
{
    final int[] regs = new int[numRegisters];
    final IoBuffer data = IoBuffer.wrap ( rawData );

    data.order ( this.order );

    for ( int i = 0; i < numRegisters; i++ )
    {
        regs[i] = data.getUnsignedShort ();
    }

    handleAnalogWrite ( startAddress, regs );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:15,代码来源:AbstractDataSlave.java

示例8: decodeAsMaster

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
 * Decode a PDU as a master
 *
 * @param message
 *            the message PDU
 * @return the decoded messages
 * @throws IllegalStateException
 *             if the function code is not supported
 */
public static Object decodeAsMaster ( final Pdu message )
{
    final IoBuffer data = message.getData ();

    final byte functionCode = data.get ();

    logger.trace ( "Decoding functionCode = {}", functionCode );

    if ( ( functionCode & Constants.FUNCTION_CODE_ERROR_FLAG ) != 0 )
    {
        final byte exceptionCode = data.get ();
        final byte originalFunctionCode = (byte) ( functionCode & ~Constants.FUNCTION_CODE_ERROR_FLAG );
        logger.trace ( "EC = {}, FC = {}", exceptionCode, functionCode );
        return new ErrorResponse ( message.getTransactionId (), message.getUnitIdentifier (), originalFunctionCode, exceptionCode );
    }

    switch ( functionCode )
    {
        case Constants.FUNCTION_CODE_READ_COILS:
        case Constants.FUNCTION_CODE_READ_DISCRETE_INPUTS:
        case Constants.FUNCTION_CODE_READ_HOLDING_REGISTERS:
        case Constants.FUNCTION_CODE_READ_INPUT_REGISTERS:
            return new ReadResponse ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, readBytes ( data ) );
        case Constants.FUNCTION_CODE_WRITE_SINGLE_COIL:
        case Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER:
            return new WriteSingleDataResponse ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), readBytes ( data, 2 ) );
        case Constants.FUNCTION_CODE_WRITE_MULTIPLE_COILS:
        case Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS:
            return new WriteMultiDataResponse ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), data.getUnsignedShort () );
        default:
            throw new IllegalStateException ( String.format ( "Function code %02x is not supported", functionCode ) );
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:43,代码来源:ModbusProtocol.java

示例9: decodeAsSlave

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
/**
 * Decode a PDU as a slave
 *
 * @param message
 *            the message PDU
 * @return the decoded messages
 * @throws IllegalStateException
 *             if the function code is not supported
 */
public static Object decodeAsSlave ( final Pdu message ) throws ProtocolCodecException
{
    final IoBuffer data = message.getData ();

    final byte functionCode = data.get ();

    switch ( functionCode )
    {
        case Constants.FUNCTION_CODE_READ_COILS:
        case Constants.FUNCTION_CODE_READ_DISCRETE_INPUTS:
        case Constants.FUNCTION_CODE_READ_HOLDING_REGISTERS:
        case Constants.FUNCTION_CODE_READ_INPUT_REGISTERS:
            return new ReadRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), data.getUnsignedShort () );
        case Constants.FUNCTION_CODE_WRITE_SINGLE_COIL:
        case Constants.FUNCTION_CODE_WRITE_SINGLE_REGISTER:
            return new WriteSingleDataRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, data.getUnsignedShort (), readBytes ( data, 2 ) );
        case Constants.FUNCTION_CODE_WRITE_MULTIPLE_COILS:
        case Constants.FUNCTION_CODE_WRITE_MULTIPLE_REGISTERS:
            final int startAddress = data.getUnsignedShort ();
            final int numRegisters = data.getUnsignedShort (); /* number of registers */
            final byte num = data.get ();
            if ( data.remaining () != num )
            {
                throw new ProtocolCodecException ( String.format ( "Wrong amount of data. Announced %s (bytes), found %s (bytes)", num, data.remaining () ) );
            }
            final byte[] b = new byte[data.remaining ()];
            data.get ( b );
            return new WriteMultiDataRequest ( message.getTransactionId (), message.getUnitIdentifier (), functionCode, startAddress, b, numRegisters );
        default:
            throw new IllegalStateException ( String.format ( "Function code %02x is not supported", functionCode ) );
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:42,代码来源:ModbusProtocol.java

示例10: decodeTimeoutFrame

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private void decodeTimeoutFrame ( final IoSession session, final IoBuffer currentFrame, final ProtocolDecoderOutput out )
{
    logger.trace ( "timeout () frame = {}", currentFrame.getHexDump () );

    if ( currentFrame.limit () <= Constants.RTU_HEADER_SIZE )
    {
        throw new ModbusProtocolError ( "frame must be at least 4 bytes long (address + data[] + crc low + crc high" );
    }

    currentFrame.order ( ByteOrder.LITTLE_ENDIAN );
    final int receivedCrc = currentFrame.getUnsignedShort ( currentFrame.limit () - 2 );
    currentFrame.order ( ByteOrder.BIG_ENDIAN );

    final int actualCrc = Checksum.crc16 ( currentFrame.array (), 0, currentFrame.limit () - 2 );
    if ( receivedCrc != actualCrc )
    {
        final String hd = currentFrame.getHexDump ();
        logger.info ( "CRC error - received: {}, calculated: {} - data: {}", receivedCrc, actualCrc, hd );
        throw new ChecksumProtocolException ( String.format ( "CRC error. received: %04x, but actually was: %04x - Data: %s", receivedCrc, actualCrc, hd ) );
    }

    currentFrame.position ( 0 );

    // get unit id
    final byte unitIdentifier = currentFrame.get ();

    final int len = currentFrame.limit () - ( 2 /*crc*/+ 1/*unit id*/);

    final IoBuffer pdu = IoBuffer.allocate ( len );
    for ( int i = 0; i < len; i++ )
    {
        pdu.put ( currentFrame.get () );
    }
    pdu.flip ();

    // decode and send
    logger.trace ( "Decoded PDU - data: {}", pdu.getHexDump () );
    out.write ( new Pdu ( 0, unitIdentifier, pdu ) );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:40,代码来源:ModbusRtuDecoder.java

示例11: decodeDataUpdateEntry

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private DataUpdate.Entry decodeDataUpdateEntry ( final IoBuffer data, final IoSession session ) throws ProtocolCodecException
{
    final int register = data.getUnsignedShort ();
    final byte missedUpdates = data.get ();
    final long timestamp = data.getLong ();
    final Set<DataUpdate.State> states = data.getEnumSetShort ( DataUpdate.State.class );

    final Variant value = decodeVariant ( session, data );

    return new DataUpdate.Entry ( register, value, timestamp, states, missedUpdates );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:12,代码来源:ProtocolDecoderImpl.java

示例12: decodeBrowserAddEntry

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
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 );
    }

}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:31,代码来源:ProtocolDecoderImpl.java

示例13: messageLength

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private int messageLength ( final IoBuffer data )
{
    final int len = data.getUnsignedShort ( data.position () + 3 );
    if ( data.remaining () < 5 + len )
    {
        return -1;
    }

    data.skip ( 5 );

    return len;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:13,代码来源:ProtocolDecoderImpl.java

示例14: decode

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
private DaveMessage decode ( final IoBuffer buffer )
{
    final byte start = buffer.get ();
    if ( start != PACKET_START_MAGIC )
    {
        throw new IllegalStateException ( String.format ( "packet header must be 0x32 but is %02x", start ) );
    }

    final byte type = buffer.get ();
    logger.debug ( "Type: {}" );

    buffer.get (); // unknown
    buffer.get (); // unknown

    buffer.getUnsignedShort (); // request id

    final int plen = buffer.getUnsignedShort ();
    final int dlen = buffer.getUnsignedShort ();

    int errorInfo;
    if ( type == 2 || type == 3 )
    {
        errorInfo = buffer.getUnsignedShort ();
        logger.debug ( "Error info: {}", errorInfo );
    }
    else
    {
        errorInfo = 0;
    }

    final IoBuffer parameters = IoBuffer.allocate ( plen );
    final IoBuffer data = IoBuffer.allocate ( dlen );

    buffer.get ( parameters.array () );
    buffer.get ( data.array () );

    logger.debug ( "plen: {}, dlen: {}, remain: {}", new Object[] { plen, dlen, buffer.remaining () } );
    logger.debug ( "Parameters: {}", parameters );
    logger.debug ( "Data: {}", data );

    if ( parameters.remaining () == 2 && parameters.get ( 0 ) == (byte)4 )
    {
        // Data read reply
        return decodeDataReadReply ( parameters, data );
    }
    else if ( parameters.remaining () == 8 && parameters.get ( 0 ) == (byte)0xF0 )
    {
        // FIXME: better detection
        final int maxPDU = parameters.getUnsignedShort ( 6 );
        return new DaveConnectionEstablishedMessage ( maxPDU );
    }
    else
    {
        return new DaveGenericMessage ( parameters, data );
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:57,代码来源:DaveFilter.java

示例15: messageReceived

import org.apache.mina.core.buffer.IoBuffer; //导入方法依赖的package包/类
@Override
public void messageReceived ( final NextFilter nextFilter, final IoSession session, final Object message ) throws Exception
{
    if ( ! ( message instanceof IoBuffer ) )
    {
        nextFilter.messageReceived ( session, message );
        return;
    }

    final IoBuffer in = (IoBuffer)message;
    final IoBuffer sessionBuffer = getSessionBuffer ( session );

    // first add to the session buffer (may be optimized later)
    sessionBuffer.position ( sessionBuffer.limit () );
    sessionBuffer.put ( in );
    sessionBuffer.flip ();

    while ( sessionBuffer.remaining () >= 4 )
    {
        final int len = sessionBuffer.getUnsignedShort ( 2 );
        if ( sessionBuffer.remaining () < len )
        {
            logger.debug ( "Next packet requires {} bytes", new Object[] { len } );
            // not enough data for body
            return;
        }

        // convert
        final IoBuffer data = IoBuffer.allocate ( len - 4 );
        sessionBuffer.get (); // version
        sessionBuffer.get (); // reserved
        sessionBuffer.getUnsignedShort (); // length

        sessionBuffer.get ( data.array () );

        nextFilter.messageReceived ( session, data );

        logger.debug ( "{} bytes left in session buffer", sessionBuffer.remaining () );
    }

    if ( sessionBuffer.hasRemaining () )
    {
        sessionBuffer.compact ();
    }
    else
    {
        sessionBuffer.clear ().flip ();
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:50,代码来源:TPKTFilter.java


注:本文中的org.apache.mina.core.buffer.IoBuffer.getUnsignedShort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。