Click here to Skip to main content
15,894,460 members
Articles / Programming Languages / C++

RCF - Interprocess Communication for C++

Rate me:
Please Sign up or sign in to vote.
4.94/5 (147 votes)
25 Oct 2011CPOL20 min read 4.6M   8.4K   331  
A server/client IPC framework, using the C++ preprocessor as an IDL compiler.
//*****************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************

#ifndef INCLUDE_RCF_PUBLISHINGSERVICE_HPP
#define INCLUDE_RCF_PUBLISHINGSERVICE_HPP

#include <map>
#include <string>

#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>

#include <RCF/GetInterfaceName.hpp>
#include <RCF/Service.hpp>
#include <RCF/ThreadLibrary.hpp>

namespace RCF {

    class RcfServer;
    class I_RcfClient;
    class Session;
    class I_ClientTransport;
    typedef boost::shared_ptr<I_RcfClient> RcfClientPtr;

    struct Publisher
    {
        std::string name;
        RcfClientPtr multicastClient;
    };

    struct PublishingOptions 
    {
        unsigned int pingInterval;
    };

    /// Service for implementing the publish part of publish/subscribe functionality.
    class PublishingService : 
        public I_Service, 
        boost::noncopyable
    {
    public:
        PublishingService();

        /// Creates and stores a publisher object, i.e. a RcfClient<Interface> object with an underlying multicasting transport.
        /// \param Interface RCF interface to publish.
        /// \param publisherName Name through which this publisher will be accessed.
        /// \return True if ok, false otherwise.
        template<typename Interface>
        bool beginPublish(const std::string &publisherName = "");

        /// Obtains a reference to the requested publisher object.
        /// \param Interface RCF interface of the publisher object.
        /// \param publisherName Name of the publisher object.
        /// \return Reference to the publisher object.
        template<typename Interface>
        typename Interface::RcfClient &publish(const std::string &publisherName = "");

        /// Shuts down the given publisher object.
        /// \param Interface RCF interface of the publisher object.
        /// \param publisherName Name of the publisher object.
        /// \return True if ok, false otherwise.
        template<typename Interface>
        bool endPublish(const std::string &publisherName = "");

        // remotely accessible
        bool requestSubscription(const std::string &subscriptionName);

    private:

        void onServiceAdded(RcfServer &server);
        void onServiceRemoved(RcfServer &server);

        bool beginPublishNamed(const std::string &publisherName, RcfClientPtr rcfClientPtr);
        I_RcfClient &publishNamed(const std::string &publisherName);
        bool endPublishNamed(const std::string &publisherName);

        void addSubscriberTransport(Session &session, const std::string &publisherName, boost::shared_ptr<I_ClientTransport> clientTransport);
        ReadWriteMutex mPublishersMutex;
        std::map<std::string, boost::shared_ptr<Publisher> > mPublishers;
    };

    typedef boost::shared_ptr<PublishingService> PublishingServicePtr;

    template<typename Interface>
    inline bool PublishingService::beginPublish(const std::string &publisherName_)
    {
        const std::string &publisherName = (publisherName_ == "") ? getInterfaceName<Interface>() : publisherName_;
        typedef typename Interface::RcfClient RcfClient;
        return beginPublishNamed(publisherName, RcfClientPtr(new RcfClient(ClientStub(publisherName))));
    }

    template<typename Interface>
    inline typename Interface::RcfClient &PublishingService::publish(const std::string &publisherName_)
    {
        const std::string &publisherName = (publisherName_ == "") ? getInterfaceName<Interface>() : publisherName_;
        return dynamic_cast<typename Interface::RcfClient &>( publishNamed(publisherName) );
    }

    template<typename Interface>
    inline bool PublishingService::endPublish(const std::string &publisherName_)
    {
        const std::string &publisherName = (publisherName_ == "") ? getInterfaceName<Interface>() : publisherName_;
        return endPublishNamed(publisherName);
    }

} // namespace RCF

#endif // ! INCLUDE_RCF_PUBLISHINGSERVICE_HPP

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
Software developer, from Sweden and now living in Canberra, Australia, working on distributed C++ applications. When he is not programming, Jarl enjoys skiing and playing table tennis. He derives immense satisfaction from referring to himself in third person.

Comments and Discussions