Click here to Skip to main content
15,897,704 members
Articles / Programming Languages / C++

RMI for C++

Rate me:
Please Sign up or sign in to vote.
4.87/5 (113 votes)
6 Aug 2009CPOL8 min read 844.2K   4.6K   153  
User-friendly remote method invocation in C++.
//*****************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005, Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//
// Distributed under the so-called MIT license, see accompanying file license.txt.
//*****************************************************************************

#ifndef _RCF_SESSIONINFO_HPP_
#define _RCF_SESSIONINFO_HPP_

#include <RCF/ClientInfo.hpp>
#include <RCF/Exception.hpp>
#include <RCF/ThreadLibrary.hpp> // need thread_specific_ptr
#include <RCF/Tools.hpp>

namespace RCF {

    class SessionInfo
    {
    public:
        SessionInfo();
        SessionInfo(int fd);
        void setClientInfo(const ClientInfo &clientInfo) { this->clientInfo = clientInfo; }
        int getFd() const;
        const ClientInfo &getClientInfo() const;
        bool getReservedForForwarding() const;
        int getForwardFd() const;
        void setReservedForForwarding(bool reservedForForwarding);
        void setForwardFd(int forwardFd);
        friend std::ostream &operator<<(std::ostream &os, const SessionInfo &sessionInfo);

    private:
        int fd;
        ClientInfo clientInfo;
        bool reservedForForwarding;
        int forwardFd;
    };

    extern Platform::Threads::thread_specific_ptr<SessionInfo *>::Val ppCurrentSessionInfo;

    // TODO: initialize ppCurrentSessionInfo when each thread starts, instead of the lazy initialization below?

    inline void resetCurrentSessionInfo()
    {
        if (NULL == ppCurrentSessionInfo.get())
        {
            ppCurrentSessionInfo.reset( new SessionInfo*() );
        }
        *ppCurrentSessionInfo = NULL;
    }

    inline void setCurrentSessionInfo(SessionInfo &sessionInfo)
    {
        if (NULL == ppCurrentSessionInfo.get())
        {
            ppCurrentSessionInfo.reset( new SessionInfo*() );
        }
        *ppCurrentSessionInfo = &sessionInfo;
    }

    inline SessionInfo &getCurrentSessionInfo()
    {
        if (NULL == ppCurrentSessionInfo.get())
        {
            ppCurrentSessionInfo.reset( new SessionInfo*() );
        }
        if (*ppCurrentSessionInfo == NULL)
        {
            RCF_THROW(SessionInfoException, "no current session");
        }
        else
        {
            return **ppCurrentSessionInfo;
        }
    }

} // namespace RCF

#endif // ! _RCF_SESSIONINFO_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