OSDN Git Service

ファイル名の変更
authorcaprice <caprice@users.sourceforge.jp>
Thu, 1 May 2014 07:09:39 +0000 (16:09 +0900)
committercaprice <caprice@users.sourceforge.jp>
Thu, 1 May 2014 07:09:39 +0000 (16:09 +0900)
src/post-office.cpp [new file with mode: 0644]

diff --git a/src/post-office.cpp b/src/post-office.cpp
new file mode 100644 (file)
index 0000000..a6b430f
--- /dev/null
@@ -0,0 +1,87 @@
+#include <sstream>
+
+#include <boost/range/algorithm.hpp>
+
+#include "reference-counter.hpp"
+
+#include "communication/communication.hpp"
+#include "communication/message/request.hpp"
+#include "communication/message/failure.hpp"
+#include "communication/message/succeed.hpp"
+
+namespace ipc = boost::interprocess;
+
+namespace monazilla { namespace GikoMona { namespace core {
+
+communication::communication(const communicate_id self_id) : self(self_id) {
+    gm_shmem = shared_memory(ipc::open_or_create, shared_memory_name, 1024 * 4);
+
+    my_mailbox = construct_object<mailbox>(create_mailbox_name(self));
+    
+    if(auto obj = find_object<reference_counter>(ref_counter_name)) {
+        auto locker = obj->scoped_lock();
+        ++(*obj);
+    } else {
+        construct_object<reference_counter>(ref_counter_name);
+    }
+}
+
+communication::~communication() {
+    disconnect_all();
+    
+    if(auto obj = find_object<reference_counter>(ref_counter_name)) {
+        if(obj->count() == 1) {
+            ipc::shared_memory_object::remove(shared_memory_name);
+        } else {
+            auto locker = obj->scoped_lock();
+            --(*obj);
+        }
+    } else {
+        /* error!!!!! */
+    }
+}
+
+bool communication::connect(const communicate_id connect_to) {
+    auto obj = find_object<mailbox>(gm_shmem, create_mailbox_name(connect_to));
+    
+    if(obj) {
+        mailbox_map[connect_to] = *obj;
+        send(connect_to, succeed::find_your_mailbox("").to_string());
+    
+        do {
+            mona_string mail = receive(connect_to);
+        } while(is_same_mail(mail, succeed::allow_you_to_send_mail("")));
+    }
+    
+    return obj;
+}
+
+bool communication::disconnect(const communicate_id disconnect_from) {
+    return mailbox_map.erase(disconnect_from);
+}
+
+mona_string communication::receive(const communicate_id originator_id) {
+    boost::find_if
+}
+    
+bool communication::send_string(const communicate_id to, mona_string&& src) {
+    auto obj = find_object<mailbox>(gm_shmem, create_mailbox_name(to));
+    
+    if(obj) {
+        std::ostringstream str_builder;
+        str_builder << to_string(self) << "-" << src;
+        
+        obj->push_back(str_builder.str());
+    }
+    
+    return obj;
+}
+
+std::string communication::create_mailbox_name(const communicate_id mb_user_id) {
+    std::ostringstream str_builder;
+    str_builder << "mailbox:" << to_string(mb_user_id);
+    
+    return str_builder.str();
+}
+
+} } }