2 #ifndef GIKOMONA_CORE_INTERPROCESS_COMMUNICATION_HPP
3 #define GIKOMONA_CORE_INTERPROCESS_COMMUNICATION_HPP
5 #include <unordered_map>
7 #include <boost/optional.hpp>
8 #include <boost/interprocess/managed_shared_memory.hpp>
9 #include <boost/interprocess/containers/vector.hpp>
10 #include <boost/interprocess/allocators/allocator.hpp>
12 #include "GikoMona.hpp"
14 #include "message.hpp"
15 #include "application-type.hpp"
17 namespace monazilla { namespace GikoMona { namespace core {
21 explicit communication(const communicate_id self_id);
24 bool connect(const communicate_id connect_to);
25 bool disconnect(const communicate_id disconnect_from);
26 bool disconnect_all();
28 template <class MessageType>
29 bool send(const communicate_id to, MessageType&& msg);
31 mona_string receive(const communicate_id originator_id);
34 typedef boost::interprocess::managed_shared_memory shared_memory;
37 using shared_memory_allocator
38 = boost::interprocess::allocator<T, shared_memory::segment_manager>;
42 = boost::interprocess::list<T, shared_memory_allocator<T>>;
44 typedef shared_list<mona_string> mailbox;
48 T *find_object(const std::string& name) {
49 return gm_shmem.find<T>(name.c_str()).first;
53 T *construct_object(const std::string& name) {
54 return gm_shmem.construct<T>(name.c_str())(shm.get_segment_manager());
57 bool send_string(const communicate_id to, mona_string&& src);
58 std::string create_mailbox_name(const communicate_id mb_user_id);
60 const communicate_id self;
61 shared_memory gm_shmem; // GikoMona全体で共有されているメモリ
63 std::unordered_map<communicate_id, mailbox> mailbox_map;
64 boost::interprocess::named_mutex my_mailbox_mutex;
66 const char *shared_memory_name = "monazilla.GikoMona.shared-memory";
67 const char *ref_counter_name = "monazilla.GikoMona.reference-counter";
70 template <class MessageType>
71 bool communication::send(const communicate_id to, MessageType&& msg) {
72 static_assert(is_satisfied_message_concept_v<MessageType>(),
73 "`MessageType' does not satisfy the `message_concept'");
75 return send_string(to, msg.to_string());
78 template <class MessageType>
79 bool communication::send_async(const communicate_id to, MessageType&& msg) {
80 static_assert(is_satisfied_message_concept_v<MessageType>(),
81 "`MessageType' does not satisfy the `message_concept'");
83 return send_string(to, msg.to_string());
88 #endif // GIKOMONA_CORE_INTERPROCESS_COMMUNICATION_HPP