LogCabin
RPC/OpaqueServerRPC.h
Go to the documentation of this file.
00001 /* Copyright (c) 2012 Stanford University
00002  *
00003  * Permission to use, copy, modify, and distribute this software for any
00004  * purpose with or without fee is hereby granted, provided that the above
00005  * copyright notice and this permission notice appear in all copies.
00006  *
00007  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
00008  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00009  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
00010  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00011  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00012  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00013  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00014  */
00015 
00016 #include <memory>
00017 
00018 #include "Core/Buffer.h"
00019 #include "RPC/MessageSocket.h"
00020 #include "RPC/OpaqueServer.h"
00021 
00022 #ifndef LOGCABIN_RPC_OPAQUESERVERRPC_H
00023 #define LOGCABIN_RPC_OPAQUESERVERRPC_H
00024 
00025 namespace LogCabin {
00026 namespace RPC {
00027 
00028 /**
00029  * This class represents the server side of a remote procedure call.
00030  * An OpaqueServer returns an instance when an RPC is initiated. This is used
00031  * to send the reply.
00032  *
00033  * This class may be used from any thread, but each object is meant to be
00034  * accessed by only one thread at a time.
00035  */
00036 class OpaqueServerRPC {
00037     /**
00038      * Constructor for OpaqueServerRPC. This is called by OpaqueServer.
00039      * \param socket
00040      *      The socket on which to send the reply.
00041      * \param messageId
00042      *      The message ID received with the request.
00043      * \param request
00044      *      The RPC request received from the client.
00045      */
00046     OpaqueServerRPC(
00047             std::weak_ptr<OpaqueServer::SocketWithHandler> socket,
00048             MessageSocket::MessageId messageId,
00049             Core::Buffer request);
00050 
00051   public:
00052     /**
00053      * Default constructor for empty RPC that can't be replied to.
00054      * This is useful as a placeholder for a real OpaqueServerRPC.
00055      */
00056     OpaqueServerRPC();
00057 
00058 
00059     /**
00060      * Move constructor.
00061      */
00062     OpaqueServerRPC(OpaqueServerRPC&& other);
00063 
00064     /**
00065      * Destructor.
00066      */
00067     ~OpaqueServerRPC();
00068 
00069     /**
00070      * Move assignment.
00071      */
00072     OpaqueServerRPC& operator=(OpaqueServerRPC&& other);
00073 
00074     /**
00075      * Close the session on which this request originated.
00076      * This is an impolite thing to do to a client but can be useful
00077      * occasionally, for example for testing.
00078      */
00079     void closeSession();
00080 
00081     /**
00082      * Send the response back to the client.
00083      * This will reset #response to an empty state, and further replies on this
00084      * object will not do anything.
00085      */
00086     void sendReply();
00087 
00088     /**
00089      * The RPC request received from the client.
00090      */
00091     Core::Buffer request;
00092 
00093     /**
00094      * The reply to the RPC, to send back to the client.
00095      */
00096     Core::Buffer response;
00097 
00098   private:
00099     /**
00100      * The socket on which to send the reply.
00101      */
00102     std::weak_ptr<OpaqueServer::SocketWithHandler> socket;
00103 
00104     /**
00105      * The message ID received with the request. This should be sent back to
00106      * the client with the response so that the client can match up the
00107      * response with the right ClientRPC object.
00108      */
00109     MessageSocket::MessageId messageId;
00110 
00111     /**
00112      * This is used in unit testing only. During normal operation, this is
00113      * always NULL. If this is not NULL when sendReply() is invoked, the reply
00114      * will be moved here.
00115      */
00116     Core::Buffer* responseTarget;
00117 
00118     // The OpaqueServer class uses the private members of this object.
00119     friend class OpaqueServer;
00120 
00121     // OpaqueServerRPC is non-copyable.
00122     OpaqueServerRPC(const OpaqueServerRPC&) = delete;
00123     OpaqueServerRPC& operator=(const OpaqueServerRPC&) = delete;
00124 
00125 }; // class OpaqueServerRPC
00126 
00127 } // namespace LogCabin::RPC
00128 } // namespace LogCabin
00129 
00130 #endif /* LOGCABIN_RPC_OPAQUESERVERRPC_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines