Пример #1
0
 void on_completed() const {
     if (!is_subscribed()) {
         return;
     }
     detacher protect(this);
     destination.on_completed();
 }
Пример #2
0
 void on_error(std::exception_ptr e) const {
     if (!is_subscribed()) {
         return;
     }
     detacher protect(this);
     destination.on_error(e);
 }
Пример #3
0
 void on_next(V&& v) const {
     if (!is_subscribed()) {
         return;
     }
     detacher protect(this);
     destination.on_next(std::forward<V>(v));
     protect.that = nullptr;
 }
Пример #4
0
        virtual void schedule(clock_type::time_point when, const schedulable& scbl) const {
            if (!scbl.is_subscribed()) {
                return;
            }

            {
                // check ownership
                if (queue::owned()) {
                    // already has an owner - delegate
                    queue::get_worker_interface()->schedule(when, scbl);
                    return;
                }

                // take ownership
                queue::ensure(std::make_shared<derecurser>());
            }
            // release ownership
            RXCPP_UNWIND_AUTO([]{
                queue::destroy();
            });

            const auto& recursor = queue::get_recursion().get_recurse();
            std::this_thread::sleep_until(when);
            if (scbl.is_subscribed()) {
                scbl(recursor);
            }
            if (queue::empty()) {
                return;
            }

            // loop until queue is empty
            for (
                auto next = queue::top().when;
                (std::this_thread::sleep_until(next), true);
                next = queue::top().when
            ) {
                auto what = queue::top().what;

                queue::pop();

                if (what.is_subscribed()) {
                    what(recursor);
                }

                if (queue::empty()) {
                    break;
                }
            }
        }
Пример #5
0
void ChannelMap::unsubscribe_channel(ChannelSubscriber *p, channel_t c)
{
    std::lock_guard<std::recursive_mutex> guard(m_lock);

    if(!is_subscribed(p, c)) {
        return;
    }

    p->channels().erase(c);
    auto &subs = m_channel_subscriptions[c];

    subs.erase(p);

    if(subs.empty()) {
        on_remove_channel(c);
    }
}
Пример #6
0
void ChannelMap::subscribe_channel(ChannelSubscriber *p, channel_t c)
{
    std::lock_guard<std::recursive_mutex> guard(m_lock);

    if(is_subscribed(p, c)) {
        return;
    }

    p->channels().insert(p->channels().end(), c);
    auto &subs = m_channel_subscriptions[c];

    if(subs.empty()) {
        on_add_channel(c);
    }

    subs.insert(p);
}
Пример #7
0
bool Channel::subscribe(SharedClient client) {
    if (!is_subscribed(client)) {

        subscribers.insert(client);
        DEBUGMSG("Client FID=%d has subscribed to channel %d (%ld total)\n",
                 client->get_identifier(), get_identifier(), subscribers.size());

        SharedDictionary status = generate_event_command(get_identifier());
        status->set<int>("subscribers", subscribers.size());
        status->set<string>("type", "subscribe");
        SharedMessage message = Message::pack<Dictionary>(*status);
        MessageHandler::set_channel(message, ECHO_CONTROL_CHANNEL);
        for (std::set<SharedClient>::iterator it = watchers.begin(); it != watchers.end(); ++it) {
            (*it)->send(message);
        }

        return true;

    }

    return false;
}