All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.eBus.client.EReliableUDPServer Maven / Gradle / Ivy

The newest version!
//
// Copyright 2023 Charles W. Rapp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package net.sf.eBus.client;

import java.nio.ByteBuffer;
import net.sf.eBus.config.EConfigure.Service;
import net.sf.eBus.messages.EMessageHeader;

/**
 * Extends {@link EUDPServer} to handle reliable UDP message
 * header.
 *
 * @author Charles W. Rapp
 */

public final class EReliableUDPServer
    extends EUDPServer
{
//---------------------------------------------------------------
// Member data.
//

//---------------------------------------------------------------
// Member methods.
//

    //-----------------------------------------------------------
    // Constructors.
    //

    /**
     * Creates a new, closed UDP server instance.
     * @param config UDP server configuration.
     */
    public EReliableUDPServer(final Service config)
    {
        super (config, new ReliableUDPMessageWriter());
    } // end of EReliableUDPServer(Service)

    //
    // end of Constructors.
    //-----------------------------------------------------------

    /**
     * Reads in eBus message header from and to identifiers and
     * sequence number.
     * @param buffer extract eBus message header from this
     * buffer.
     */
    @Override
    protected void decodeHeader(final ByteBuffer buffer)
    {
        // Read in from and to identifiers and sequence number.
        buffer.getInt();
        buffer.getInt();
        buffer.getInt();
    } // end of decodeHeader(ByteBuffer)

//---------------------------------------------------------------
// Inner classes.
//

    /**
     * Extends {@code EDUPServer.UDPServer} to read in sequence
     * number from eBus message header.
     */
    private static final class ReliableUDPMessageWriter
        extends UDPMessageWriter
    {
    //-----------------------------------------------------------
    // Member data.
    //

    //-----------------------------------------------------------
    // Member methods.
    //

        //-------------------------------------------------------
        // Constructors.
        //

        private ReliableUDPMessageWriter()
        {}

        //
        // end of Constructors.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // UDPMessageWriter Method Overrides.
        //

        /**
         * Fills in message header (including sequence number)
         * to given buffer.
         * @param buffer serialize message header to this buffer.
         */
        @Override
        public void fillHeader(final ByteBuffer buffer)
        {
            // Move past the message size bytes and fill in the
            // rest of the header.
            // Note: buffer.position() returns Buffer and not
            // ByteBuffer.
            buffer.putInt(UDP_CONNECT_REPLY_KEY_ID)
                  .putInt(EMessageHeader.NO_ID)
                  .putInt(EMessageHeader.NO_ID)
                  .putInt(EMessageHeader.NO_ID);
        } // end of fill(ByteBuffer)

        //
        // end of UDPMessageWriter Method Overrides.
        //-------------------------------------------------------
    } // end of class ReliableUDPMessageWriter
} // end of class EReliableUDPServer




© 2015 - 2024 Weber Informatics LLC | Privacy Policy