OSDN Git Service

63f9907c6b4a2cb70950953c3592ff3b9b0fe89c
[gikomona/libcore.git] / include / communication / communication.hpp
1
2 #ifndef GIKOMONA_CORE_INTERPROCESS_COMMUNICATION_HPP
3 #define GIKOMONA_CORE_INTERPROCESS_COMMUNICATION_HPP
4
5 #include <unordered_map>
6
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>
11
12 #include "GikoMona.hpp"
13 #include "string.hpp"
14 #include "message.hpp"
15 #include "application-type.hpp"
16
17 namespace monazilla { namespace GikoMona { namespace core {
18
19 class communication {
20 public:
21     explicit communication(const communicate_id self_id);
22     ~communication();
23     
24     bool connect(const communicate_id connect_to);
25     bool disconnect(const communicate_id disconnect_from);
26     bool disconnect_all();
27     
28     template <class MessageType>
29     bool send(const communicate_id to, MessageType&& msg);
30     
31     mona_string receive(const communicate_id originator_id);
32     
33 private:
34     typedef boost::interprocess::managed_shared_memory shared_memory;
35     
36     template <typename T>
37     using shared_memory_allocator
38         = boost::interprocess::allocator<T, shared_memory::segment_manager>;
39     
40     template <typename T>
41     using shared_list
42         = boost::interprocess::list<T, shared_memory_allocator<T>>;
43     
44     typedef shared_list<mona_string> mailbox;
45     
46 private:
47     template <typename T>
48     T *find_object(const std::string& name) {
49         return gm_shmem.find<T>(name.c_str()).first;
50     }
51     
52     template <typename T>
53     T *construct_object(const std::string& name) {
54         return gm_shmem.construct<T>(name.c_str())(shm.get_segment_manager());
55     }
56     
57     bool send_string(const communicate_id to, mona_string&& src);
58     std::string create_mailbox_name(const communicate_id mb_user_id);
59     
60     const communicate_id self;
61     shared_memory gm_shmem; // GikoMona全体で共有されているメモリ
62     mailbox *my_mailbox;
63     std::unordered_map<communicate_id, mailbox> mailbox_map;
64     boost::interprocess::named_mutex my_mailbox_mutex;
65     
66     const char *shared_memory_name = "monazilla.GikoMona.shared-memory";
67     const char *ref_counter_name = "monazilla.GikoMona.reference-counter";
68 };
69
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'");
74     
75     return send_string(to, msg.to_string());
76 }
77
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'");
82         
83     return send_string(to, msg.to_string());
84 }
85
86 } } }
87
88 #endif // GIKOMONA_CORE_INTERPROCESS_COMMUNICATION_HPP