OSDN Git Service

* 関数の名称変更に伴う書き換え (construct, destruct, find)
[gikomona/libcore.git] / src / post-office.cpp
1 #include <sstream>
2
3 #include <boost/range/algorithm.hpp>
4
5 #include "post-office.hpp"
6 #include "reference-counter.hpp"
7
8 #include "request.hpp"
9 #include "failure.hpp"
10 #include "succeed.hpp"
11
12 namespace monazilla { namespace GikoMona { namespace core { namespace communication {
13
14 post_office::post_office(const post_code& self_) : self_code(self_) {
15     gm_shmem = shared_memory(ipc::open_or_create, shared_memory_name, 1024 * 4);
16     auto my_mailbox = construct<mailbox>(gm_shmem, self_code);
17     
18     if(auto obj = find<reference_counter>(gm_shmem, ref_counter_name)) {
19         auto locker = obj->scoped_lock();
20         ++(*obj);
21     } else {
22         construct<reference_counter>(gm_shmem, ref_counter_name);
23     }
24 }
25
26 post_office::~post_office() noexcept {
27     disconnect_all();
28     
29     if(auto obj = find<reference_counter>(gm_shmem, ref_counter_name)) {
30         if(obj->count() == 1) {
31             destruct(gm_shmem, shared_memory_name);
32         } else {
33             auto locker = obj->scoped_lock();
34             --(*obj);
35         }
36     } else {
37         /* error!!!!! */
38     }
39 }
40
41 bool post_office::connect(const post_code& connect_to) {
42     auto obj = find<mailbox>(gm_shmem, connect_to);
43     
44     if(obj) {
45         //mailbox_map[connect_to] = *obj;
46         send(connect_to, succeed::find_your_mailbox("").to_raw());
47     }
48     
49     return obj;
50 }
51
52 bool post_office::disconnect(const post_code& disconnect_from) {
53     return /*mailbox_map.erase(disconnect_from);*/ true;
54 }
55
56 mona_string post_office::receive(const post_code& originator_id) {
57     //boost::find_if
58 }
59     
60 bool post_office::send_string(const post_code& to, mona_string&& src) {
61     auto obj = find<mailbox>(gm_shmem, to);
62     
63     if(obj) {
64         std::ostringstream str_builder;
65         //str_builder << self_ << "-" << src;
66     }
67     
68     return obj;
69 }
70
71 } } } }