#include <boost/range/algorithm.hpp>
+#include "post-office.hpp"
#include "reference-counter.hpp"
-#include "communication/communication.hpp"
-#include "communication/message/request.hpp"
-#include "communication/message/failure.hpp"
-#include "communication/message/succeed.hpp"
+#include "request.hpp"
+#include "failure.hpp"
+#include "succeed.hpp"
-namespace ipc = boost::interprocess;
+namespace monazilla { namespace GikoMona { namespace core { namespace communication {
-namespace monazilla { namespace GikoMona { namespace core {
-
-communication::communication(const communicate_id self_id) : self(self_id) {
+post_office::post_office(const post_code& self_) : self_code(self_) {
gm_shmem = shared_memory(ipc::open_or_create, shared_memory_name, 1024 * 4);
-
- my_mailbox = construct_object<mailbox>(create_mailbox_name(self));
+ auto my_mailbox = construct<mailbox>(gm_shmem, self_code);
- if(auto obj = find_object<reference_counter>(ref_counter_name)) {
+ if(auto obj = find<reference_counter>(gm_shmem, ref_counter_name)) {
auto locker = obj->scoped_lock();
++(*obj);
} else {
- construct_object<reference_counter>(ref_counter_name);
+ construct<reference_counter>(gm_shmem, ref_counter_name);
}
}
-communication::~communication() {
+post_office::~post_office() noexcept {
disconnect_all();
- if(auto obj = find_object<reference_counter>(ref_counter_name)) {
+ if(auto obj = find<reference_counter>(gm_shmem, ref_counter_name)) {
if(obj->count() == 1) {
- ipc::shared_memory_object::remove(shared_memory_name);
+ destruct(gm_shmem, shared_memory_name);
} else {
auto locker = obj->scoped_lock();
--(*obj);
}
}
-bool communication::connect(const communicate_id connect_to) {
- auto obj = find_object<mailbox>(gm_shmem, create_mailbox_name(connect_to));
+bool post_office::connect(const post_code& connect_to) {
+ auto obj = find<mailbox>(gm_shmem, 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("")));
+ //mailbox_map[connect_to] = *obj;
+ send(connect_to, succeed::find_your_mailbox("").to_raw());
}
return obj;
}
-bool communication::disconnect(const communicate_id disconnect_from) {
- return mailbox_map.erase(disconnect_from);
+bool post_office::disconnect(const post_code& disconnect_from) {
+ return /*mailbox_map.erase(disconnect_from);*/ true;
}
-mona_string communication::receive(const communicate_id originator_id) {
- boost::find_if
+mona_string post_office::receive(const post_code& 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));
+bool post_office::send_string(const post_code& to, mona_string&& src) {
+ auto obj = find<mailbox>(gm_shmem, to);
if(obj) {
std::ostringstream str_builder;
- str_builder << to_string(self) << "-" << src;
-
- obj->push_back(str_builder.str());
+ //str_builder << self_ << "-" << src;
}
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();
-}
-
-} } }
+} } } }