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

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

There is a newer version: 7.6.0
Show newest version
//
// Copyright 2021 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.io.Serializable;
import java.net.InetAddress;
import net.sf.eBus.messages.EMessageKey;
import net.sf.eBus.messages.ENotificationMessage;
import net.sf.eBus.messages.ESystemMessage;
import net.sf.eBus.util.Validator;

/**
 * Reports multicast connection current state. Contains the
 * multicast connection name, multicast group address, and
 * connection state.
 *
 * @author Charles W. Rapp
 */

public final class MulticastMessage
    extends ENotificationMessage
    implements Serializable
{
//---------------------------------------------------------------
// Enums.
//

    /**
     * A multicast connection is either joined to its group or
     * disconnected.
     */
    public enum MulticastState
    {
        /**
         * eBus multicast connection is joined to its target
         * multicast group.
         */
        JOINED,

        /**
         * eBus multicast connection is disconnected from its
         * target multicast group.
         */
        DISCONNECTED
    } // end of enum MulticastState

//---------------------------------------------------------------
// Member data.
//

    //-----------------------------------------------------------
    // Constants.
    //

    /**
     * The multicast message subject is {@value}.
     */
    public static final String MCAST_SUBJECT =
        ESystemMessage.SYSTEM_SUBJECT + "/multicast";

    /**
     * Multicast connection messages are published under this
     * key.
     */
    public static final EMessageKey MESSAGE_KEY =
        new EMessageKey(MulticastMessage.class, MCAST_SUBJECT);

    /**
     * Serialization version identifier.
     */
    private static final long serialVersionUID = 0x050500L;

    //-----------------------------------------------------------
    // Locals.
    //

    /**
     * Multicast connection unique name.
     */
    public final String connectionName;

    /**
     * State applies to this multicast group address.
     */
    public final InetAddress multicastGroup;

    /**
     * Multicast connection is either joined to its group or
     * disconnected.
     */
    public final MulticastState state;

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

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

    /**
     * Creates a new multicast message instance based on the
     * builder settings.
     * @param builder multicast message builder.
     */
    private MulticastMessage(final Builder builder)
    {
        super (builder);

        this.connectionName = builder.mName;
        this.multicastGroup = builder.mGroup;
        this.state = builder.mState;
    } // end of MulticastMessage(Builder)

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

    //-----------------------------------------------------------
    // Object Method Overrides.
    //

    /**
     * Returns the multicast message as text.
     * @return textual representation of the multicast message.
     */
    @Override
    public String toString()
    {
        final StringBuilder retval = new StringBuilder();

        return (retval.append(super.toString())
                      .append("\n                   name: ")
                      .append(connectionName)
                      .append("\n                  group: ")
                      .append(multicastGroup.getHostAddress())
                      .append("\n        multicast state: ")
                      .append(state)
                      .toString());
    } // end of toString()

    //
    // end of Object Method Overrides.
    //-----------------------------------------------------------

    /**
     * Returns a multicast message builder instance.
     * @return message builder instance.
     */
    public static Builder builder()
    {
        return (new Builder());
    } // end of builder()

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

    /**
     * Use this builder to create {@code MulticastMessage}
     * instances.
     */
    public static final class Builder
        extends ENotificationMessage.Builder
    {
    //-----------------------------------------------------------
    // Member data.
    //

        //-------------------------------------------------------
        // Locals.
        //

        private String mName;
        private InetAddress mGroup;
        private MulticastState mState;

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

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

        private Builder()
        {
            super (MulticastMessage.class);
        } // end of Builder()

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

        //-------------------------------------------------------
        // Builder Method Overrides.
        //

        /**
         * Returns a multicast message based on {@code this}
         * builder's field settings.
         * @return new multicast message.
         */
        @Override
        protected MulticastMessage buildImpl()
        {
            return (new MulticastMessage(this));
        } // end of buildImpl()

        /**
         * Checks if connection name, group, and state are set.
         * @param problems used to check field validity and
         * collect discovered invalid fields.
         * @return {@code problems} to allow for method chaining.
         */
        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNullOrEmpty(mName,
                                                "connectionName")
                         .requireNotNull(mGroup,
                                         "multicastGroup")
                         .requireNotNull(mState, "state"));
        } // end of validate(Validator)

        //
        // end of Builder Method Overrides.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Set Methods.
        //

        public Builder connectionName(final String name)
        {
            mName = name;

            return (this);
        } // end of connectionName(String)

        public Builder multicastGroup(final InetAddress group)
        {
            mGroup = group;

            return (this);
        } // end of multicastGroup(InetSocketAddress)

        public Builder state(final MulticastState state)
        {
            mState = state;

            return (this);
        } // end of state(MulticastState)

        //
        // end of Set Methods.
        //-------------------------------------------------------
    } // end of class Builder
} // end of class MulticastMessage




© 2015 - 2025 Weber Informatics LLC | Privacy Policy