Open3D (C++ API)  0.12.0
ReceiverBase.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2020 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <atomic>
30 #include <mutex>
31 #include <thread>
32 
33 #include "open3d/utility/Console.h"
34 
35 namespace zmq {
36 class message_t;
37 class socket_t;
38 class context_t;
39 } // namespace zmq
40 
41 namespace open3d {
42 namespace io {
43 namespace rpc {
44 
45 namespace messages {
46 struct Request;
47 struct SetMeshData;
48 struct GetMeshData;
49 struct SetCameraData;
50 struct SetProperties;
51 struct SetActiveCamera;
52 struct SetTime;
53 } // namespace messages
54 
58 class ReceiverBase {
59 public:
63  ReceiverBase(const std::string& address = "tcp://127.0.0.1:51454",
64  int timeout = 10000);
65 
66  ReceiverBase(const ReceiverBase&) = delete;
67  ReceiverBase& operator=(const ReceiverBase&) = delete;
68 
69  virtual ~ReceiverBase();
70 
72  void Start();
73 
77  void Stop();
78 
80  std::runtime_error GetLastError();
81 
82 protected:
83  // Opaque type for providing the original msgpack::object to the
84  // ProcessMessage functions
85  struct MsgpackObject;
86 
94  virtual std::shared_ptr<zmq::message_t> ProcessMessage(
95  const messages::Request& req,
96  const messages::SetMeshData& msg,
97  const MsgpackObject& obj);
98  virtual std::shared_ptr<zmq::message_t> ProcessMessage(
99  const messages::Request& req,
100  const messages::GetMeshData& msg,
101  const MsgpackObject& obj);
102  virtual std::shared_ptr<zmq::message_t> ProcessMessage(
103  const messages::Request& req,
104  const messages::SetCameraData& msg,
105  const MsgpackObject& obj);
106  virtual std::shared_ptr<zmq::message_t> ProcessMessage(
107  const messages::Request& req,
108  const messages::SetProperties& msg,
109  const MsgpackObject& obj);
110  virtual std::shared_ptr<zmq::message_t> ProcessMessage(
111  const messages::Request& req,
112  const messages::SetActiveCamera& msg,
113  const MsgpackObject& obj);
114  virtual std::shared_ptr<zmq::message_t> ProcessMessage(
115  const messages::Request& req,
116  const messages::SetTime& msg,
117  const MsgpackObject& obj);
118 
119 private:
120  void Mainloop();
121 
122  const std::string address_;
123  const int timeout_;
124  std::shared_ptr<zmq::context_t> context_;
125  std::unique_ptr<zmq::socket_t> socket_;
126  std::thread thread_;
127  std::mutex mutex_;
128  bool keep_running_;
129  std::atomic<bool> loop_running_;
130  std::atomic<int> mainloop_error_code_;
131  std::runtime_error mainloop_exception_;
132 };
133 
134 } // namespace rpc
135 } // namespace io
136 } // namespace open3d
Definition: ReceiverBase.cpp:54
Definition: Messages.h:343
Definition: Messages.h:460
Definition: Messages.h:430
Definition: PinholeCameraIntrinsic.cpp:35
struct for defining a "get_mesh_data" message, which requests mesh data.
Definition: Messages.h:362
bool SetMeshData(const core::Tensor &vertices, const std::string &path, int time, const std::string &layer, const std::map< std::string, core::Tensor > &vertex_attributes, const core::Tensor &faces, const std::map< std::string, core::Tensor > &face_attributes, const core::Tensor &lines, const std::map< std::string, core::Tensor > &line_attributes, const std::map< std::string, core::Tensor > &textures, std::shared_ptr< ConnectionBase > connection)
Definition: RemoteFunctions.cpp:163
Definition: ConnectionBase.h:31
Definition: ReceiverBase.h:58
bool SetTime(int time, std::shared_ptr< ConnectionBase > connection)
Definition: RemoteFunctions.cpp:377
bool SetActiveCamera(const std::string &path, std::shared_ptr< ConnectionBase > connection)
Definition: RemoteFunctions.cpp:394