RStudio Launcher Plugin SDK  1.1.3
A software development kit for creating plugins that work the the RStudio Launcher.
AbstractLauncherCommunicator.hpp
1 /*
2  * AbstractLauncherCommunicator.hpp
3  *
4  * Copyright (C) 2020 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant to the terms of a commercial license agreement
7  * with RStudio, then this program is licensed to you under the following terms:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
10  * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
11  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #ifndef LAUNCHER_PLUGINS_ABSTRACT_COMMUNICATOR_HPP
25 #define LAUNCHER_PLUGINS_ABSTRACT_COMMUNICATOR_HPP
26 
27 #include <Noncopyable.hpp>
28 
29 #include <utils/Functionals.hpp>
30 
31 namespace rstudio {
32 namespace launcher_plugins {
33 
34 class Error;
35 
36 namespace api {
37 
38 class Request;
39 class Response;
40 
41 } // namespace api
42 } // namespace launcher_plugins
43 } // namespace rstudio
44 
45 namespace rstudio {
46 namespace launcher_plugins {
47 namespace comms {
48 
52 typedef std::function<void(const std::shared_ptr<api::Request>&)> RequestHandler;
53 
59  public std::enable_shared_from_this<AbstractLauncherCommunicator>
60 {
61 public:
65  virtual ~AbstractLauncherCommunicator() = default;
66 
72  void registerRequestHandler(std::unique_ptr<RequestHandler>&& in_requestHandler);
73 
79  void sendResponse(const api::Response& in_response);
80 
88  virtual Error start();
89 
95  virtual void stop();
96 
102  virtual void waitForExit();
103 
104 protected:
113  AbstractLauncherCommunicator(size_t in_maxMessageSize, const OnError& in_onError);
114 
120  void reportError(const Error& in_error);
121 
128  void onDataReceived(const char* in_data, size_t in_length);
129 
130 protected:
138  template <typename Derived>
139  std::shared_ptr<Derived> shared_from_derived()
140  {
141  static_assert(
142  std::is_base_of<AbstractLauncherCommunicator, Derived>::value,
143  "Derived must inherit from AbstractLauncherCommunicator");
144 
145  return std::static_pointer_cast<Derived>(shared_from_this());
146  }
147 
148 
149 private:
157  virtual void writeResponse(const std::string& in_responseMessage) = 0;
158 
159  // The private implementation of AbstractLauncherCommunicator.
160  PRIVATE_IMPL(m_baseImpl);
161 };
162 
163 typedef std::shared_ptr<AbstractLauncherCommunicator> AbstractLauncherCommunicatorPtr;
164 
165 } // namespace comms
166 } // namespace launcher_plugins
167 } // namespace rstudio
168 
169 #endif
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::registerRequestHandler
void registerRequestHandler(std::unique_ptr< RequestHandler > &&in_requestHandler)
Registers a request handler for all requests.
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::start
virtual Error start()
Starts the communicator.
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::reportError
void reportError(const Error &in_error)
Reports an error and stops the communicator.
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::onDataReceived
void onDataReceived(const char *in_data, size_t in_length)
Handles data that is received from the RStudio Launcher.
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::~AbstractLauncherCommunicator
virtual ~AbstractLauncherCommunicator()=default
Virtual destructor to allow for inheritance.
rstudio::launcher_plugins::OnError
std::function< void(const Error &)> OnError
Definition: Functionals.hpp:43
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::AbstractLauncherCommunicator
AbstractLauncherCommunicator(size_t in_maxMessageSize, const OnError &in_onError)
Constructor.
rstudio::launcher_plugins::Error
Class which represents an error.
Definition: Error.hpp:174
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::sendResponse
void sendResponse(const api::Response &in_response)
Sends the response to the RStudio Launcher.
Functionals.hpp
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::shared_from_derived
std::shared_ptr< Derived > shared_from_derived()
Definition: AbstractLauncherCommunicator.hpp:139
rstudio::launcher_plugins::api::Response
Represents the common components of all responses which can be sent the RStudio Launcher.
Definition: Response.hpp:56
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::waitForExit
virtual void waitForExit()
Blocks until the communicator has successfully stopped.
rstudio::launcher_plugins::Noncopyable
Class which can be inherited from to disallow copying of its child classes.
Definition: Noncopyable.hpp:34
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator
Base class responsible for communicating the the launcher. The type of communication is implementatio...
Definition: AbstractLauncherCommunicator.hpp:58
rstudio::launcher_plugins::comms::AbstractLauncherCommunicator::stop
virtual void stop()
Stops the communicator.