Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / asio / example / invocation / prioritised_handlers.cpp
1 //
2 // prioritised_handlers.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #include <boost/asio.hpp>
12 #include <boost/function.hpp>
13 #include <iostream>
14 #include <queue>
15
16 using boost::asio::ip::tcp;
17
18 class handler_priority_queue
19 {
20 public:
21   void add(int priority, boost::function<void()> function)
22   {
23     handlers_.push(queued_handler(priority, function));
24   }
25
26   void execute_all()
27   {
28     while (!handlers_.empty())
29     {
30       queued_handler handler = handlers_.top();
31       handler.execute();
32       handlers_.pop();
33     }
34   }
35
36   // A generic wrapper class for handlers to allow the invocation to be hooked.
37   template <typename Handler>
38   class wrapped_handler
39   {
40   public:
41     wrapped_handler(handler_priority_queue& q, int p, Handler h)
42       : queue_(q), priority_(p), handler_(h)
43     {
44     }
45
46     void operator()()
47     {
48       handler_();
49     }
50
51     template <typename Arg1>
52     void operator()(Arg1 arg1)
53     {
54       handler_(arg1);
55     }
56
57     template <typename Arg1, typename Arg2>
58     void operator()(Arg1 arg1, Arg2 arg2)
59     {
60       handler_(arg1, arg2);
61     }
62
63   //private:
64     handler_priority_queue& queue_;
65     int priority_;
66     Handler handler_;
67   };
68
69   template <typename Handler>
70   wrapped_handler<Handler> wrap(int priority, Handler handler)
71   {
72     return wrapped_handler<Handler>(*this, priority, handler);
73   }
74
75 private:
76   class queued_handler
77   {
78   public:
79     queued_handler(int p, boost::function<void()> f)
80       : priority_(p), function_(f)
81     {
82     }
83
84     void execute()
85     {
86       function_();
87     }
88
89     friend bool operator<(const queued_handler& a,
90         const queued_handler& b)
91     {
92       return a.priority_ < b.priority_;
93     }
94
95   private:
96     int priority_;
97     boost::function<void()> function_;
98   };
99
100   std::priority_queue<queued_handler> handlers_;
101 };
102
103 // Custom invocation hook for wrapped handlers.
104 template <typename Function, typename Handler>
105 void asio_handler_invoke(Function f,
106     handler_priority_queue::wrapped_handler<Handler>* h)
107 {
108   h->queue_.add(h->priority_, f);
109 }
110
111 //----------------------------------------------------------------------
112
113 void high_priority_handler(const boost::system::error_code& /*ec*/)
114 {
115   std::cout << "High priority handler\n";
116 }
117
118 void middle_priority_handler(const boost::system::error_code& /*ec*/)
119 {
120   std::cout << "Middle priority handler\n";
121 }
122
123 void low_priority_handler()
124 {
125   std::cout << "Low priority handler\n";
126 }
127
128 int main()
129 {
130   boost::asio::io_service io_service;
131
132   handler_priority_queue pri_queue;
133
134   // Post a completion handler to be run immediately.
135   io_service.post(pri_queue.wrap(0, low_priority_handler));
136
137   // Start an asynchronous accept that will complete immediately.
138   tcp::endpoint endpoint(boost::asio::ip::address_v4::loopback(), 0);
139   tcp::acceptor acceptor(io_service, endpoint);
140   tcp::socket server_socket(io_service);
141   acceptor.async_accept(server_socket,
142       pri_queue.wrap(100, high_priority_handler));
143   tcp::socket client_socket(io_service);
144   client_socket.connect(acceptor.local_endpoint());
145
146   // Set a deadline timer to expire immediately.
147   boost::asio::deadline_timer timer(io_service);
148   timer.expires_at(boost::posix_time::neg_infin);
149   timer.async_wait(pri_queue.wrap(42, middle_priority_handler));
150
151   while (io_service.run_one())
152   {
153     // The custom invocation hook adds the handlers to the priority queue
154     // rather than executing them from within the poll_one() call.
155     while (io_service.poll_one())
156       ;
157
158     pri_queue.execute_all();
159   }
160
161   return 0;
162 }