Program Listing for File PortParameters.h

Return to documentation for file (/tmp/ws/src/fastrtps/include/fastdds/rtps/common/PortParameters.h)

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

#ifndef _FASTDDS_RTPS_PORT_PARAMETERS_H_
#define _FASTDDS_RTPS_PORT_PARAMETERS_H_

#include <fastdds/rtps/common/Types.h>
#include <fastdds/dds/log/Log.hpp>

namespace eprosima {
namespace fastrtps {
namespace rtps {

class PortParameters
{
public:

    PortParameters()
        : portBase(7400)
        , domainIDGain(250)
        , participantIDGain(2)
        , offsetd0(0)
        , offsetd1(10)
        , offsetd2(1)
        , offsetd3(11)
    {
    }

    virtual ~PortParameters()
    {
    }

    bool operator ==(
            const PortParameters& b) const
    {
        return (this->portBase == b.portBase) &&
               (this->domainIDGain == b.domainIDGain) &&
               (this->participantIDGain == b.participantIDGain) &&
               (this->offsetd0 == b.offsetd0) &&
               (this->offsetd1 == b.offsetd1) &&
               (this->offsetd2 == b.offsetd2) &&
               (this->offsetd3 == b.offsetd3);
    }

    inline uint32_t getMulticastPort(
            uint32_t domainId) const
    {
        uint32_t port = portBase + domainIDGain * domainId + offsetd0;

        if (port > 65535)
        {
            EPROSIMA_LOG_ERROR(RTPS, "Calculated port number is too high. Probably the domainId is over 232 "
                    << "or portBase is too high.");
            std::cout << "Calculated port number is too high. Probably the domainId is over 232 "
                      << "or portBase is too high." << std::endl;
            std::cout.flush();
            exit(EXIT_FAILURE);
        }

        return port;
    }

    inline uint32_t getUnicastPort(
            uint32_t domainId,
            uint32_t RTPSParticipantID) const
    {
        uint32_t port = portBase + domainIDGain * domainId + offsetd1   + participantIDGain * RTPSParticipantID;

        if (port > 65535)
        {
            EPROSIMA_LOG_ERROR(RTPS, "Calculated port number is too high. Probably the domainId is over 232, there are "
                    << "too much participants created or portBase is too high.");
            std::cout << "Calculated port number is too high. Probably the domainId is over 232, there are "
                      << "too much participants created or portBase is too high." << std::endl;
            std::cout.flush();
            exit(EXIT_FAILURE);
        }

        return port;
    }

public:

    uint16_t portBase;
    uint16_t domainIDGain;
    uint16_t participantIDGain;
    uint16_t offsetd0;
    uint16_t offsetd1;
    uint16_t offsetd2;
    uint16_t offsetd3;
};

} // namespace rtps
} /* namespace rtps */
} /* namespace eprosima */

#endif /* _FASTDDS_RTPS_PORT_PARAMETERS_H_ */