tinymq / src / message_queue.cpp

/*
 Copyright (c) 2009 Andy Gross <andy@andygross.org>

 This file is provided to you under the Apache License,
 Version 2.0 (the "License"); you may not use this file
 except in compliance with the License.  You may obtain
 a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
*/

#include "message_queue.hpp"

namespace tinymq {

message_queue::message_queue(const char *mfile_name, std::size_t maxsize) :
     file_name_(mfile_name),
     maxsize_(maxsize),
     file_(bip::open_or_create, file_name_.c_str(), maxsize_),
     valloc_(new void_allocator(file_.get_segment_manager())),
     idgen_() {
     if (!file_.check_sanity())
         throw std::runtime_error("corrupt data file");
     deque_ = file_.find_or_construct<message_deque>("mq")(*valloc_);
}

message_queue::~message_queue() {
    sync();
    bip::shared_memory_object::remove(file_name_.c_str());
}

 void message_queue::add_waiter(client_ptr client) {
     waiters_.insert(client_wref(client));
 }

std::size_t message_queue::qsize() const {
      return deque_->size();
}

void message_queue::notify_one() {
    if (waiters_.empty()) return;
    client_wref c(*waiters_.begin());
    if (!c.expired()) {
        client_ptr cp =c.lock();
        cp->notify();
    }
    waiters_.erase(c);
}

void message_queue::grow(size_t by) {
    sync();
    maxsize_ += by;
    bip::shared_memory_object::remove(file_name_.c_str());
    file_.grow(file_name_.c_str(), maxsize_);
    tinymq_message_file m_newfile(bip::open_only, file_name_.c_str());
    file_.swap(m_newfile);
    valloc_ = boost::shared_ptr<void_allocator>(new void_allocator(file_.get_segment_manager()));
    deque_ = file_.find_or_construct<message_deque>("mq")(*valloc_);
}

message_id message_queue::enqueue_message(unsigned char *data, std::size_t len) {
    bool was_empty = (deque_->size() == 0);
    message_id id(idgen_.next_id());
    try {
        message_shared_ptr m = 
            bip::make_managed_shared_ptr(
                file_.construct<message>(
                bip::anonymous_instance)(id, len, data, valloc_.get()),
                file_
            );
        deque_->push_back(m);
        if (was_empty) notify_one();
        return id;
    }
    catch (const bip::bad_alloc&) {
        grow(maxsize_);
        message_shared_ptr m = 
            bip::make_managed_shared_ptr(
                file_.construct<message>(
                bip::anonymous_instance)(id, len, data, valloc_.get()),
                file_
            );
        deque_->push_back(m);
        if (was_empty) notify_one();
        return id;
    }
}

void message_queue::enqueue_message(message_shared_ptr m) {
    bool was_empty = (deque_->size() == 0);
    try {
        deque_->push_back(m);
        if (was_empty) notify_one();
    }
    catch (const bip::bad_alloc&) {
        grow(maxsize_);
        deque_->push_back(m);
        if (was_empty) notify_one();
    }
}

message_shared_ptr message_queue::dequeue_message() {
    if (deque_->size()) {
        message_shared_ptr m(deque_->front());
        deque_->pop_front();
        return m;
    }
    else {
        return message_shared_ptr();
    }

}

void message_queue::sync() {
    file_.flush();
}

void message_queue::print_stats() {
    printf("named objects: %lu\n", file_.get_num_named_objects());
    printf("free memory: %lu\n", file_.get_free_memory());
}

} // namespace tinymq
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.