Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class base_module {
- public:
- // Will be invoked in dedicated thread
- void main()
- {
- m_running = true;
- while (m_running)
- {
- // Get message from priority queue
- auto message = m_queue.pop_front();
- // Dispatch
- switch (message.type()) {
- case message_type::EVENT:
- // Find event handler and invoke
- event &ev = message;
- if ((auto handler = m_event_handlers.find(ev)) == m_event_handlers.end())
- // Silently drop
- continue;
- // Wrap call to leave number of arguments at event side! (dynamic argument lists possible)
- auto call_wrapper = ev.wrap(handler);
- call_wrapper(); // This will invoke handler() with arguments given by ev
- break;
- case message_type::RESPONSE:
- // There should be a pending request, find it and call the completion handler
- response &res = message;
- if ((auto handler = m_pending_requests.find(response.id)) == m_pending_requests.end())
- throw system::fatal;
- // Again, use a call wrapper
- auto call_wrapper = res.wrap(handler);
- call_wrapper(); // This will invoke handler() with arguments given by res
- break;
- case message_type::REQUEST:
- // A request has been made to an exported method, invoke handler for request
- // ...TODO...
- break;
- }
- }
- }
- void terminate()
- {
- m_running = false;
- }
- // Add message
- void deliver_message(message &msg)
- {
- m_queue.push_back(msg);
- }
- private:
- bool m_running;
- priority_queue m_queue;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement