Commits

Boris Nagaev committed 5301a2a

Planning: add custom scheduler function

Deprecated set_io_service and constructor(WIOService) were restored.

Comments (0)

Files changed (3)

+2012-11-07:
+    * add custom task scheduler function (Planning)
+
 2012-10-22:
     * add class LocalStorage
 
 
 #include "config.hpp"
 
+#include <climits>
 #include <vector>
 #include <utility>
 #include <boost/bind.hpp>
 #include <Wt/WServer>
 #endif
 
+#ifdef WC_HAVE_WIOSERVICE
+#include <Wt/WIOService>
+#endif
+
 namespace Wt {
 
 namespace Wc {
     return *state_ptr_;
 }
 
-PlanningServer::PlanningServer(WIOService* /* io_service */, WObject* p):
+PlanningServer::PlanningServer(WIOService* io_service, WObject* p):
     WObject(p),
     server_(0),
-    default_notify_needed_(true)
-{ }
+    default_notify_needed_(true) {
+    set_io_service(io_service);
+}
 
 PlanningServer::PlanningServer(WObject* p):
     WObject(p),
     server_(0),
-    default_notify_needed_(true)
+    default_notify_needed_(true),
+    scheduler_(schedule_action)
 { }
 
 PlanningServer::PlanningServer(Server* notification_server, WObject* p):
     WObject(p),
     server_(notification_server),
-    default_notify_needed_(true)
+    default_notify_needed_(true),
+    scheduler_(schedule_action)
 { }
 
 bool PlanningServer::add(TaskPtr task, const WDateTime& when) {
     return add(TaskPtr(task), when);
 }
 
+void PlanningServer::set_scheduler(const Scheduler& scheduler) {
+    scheduler_ = scheduler;
+}
+
 WIOService* PlanningServer::io_service() {
 #if USE_WIOSERVICE
     return &WServer::instance()->ioService();
 #endif
 }
 
+static void WIOService_schedule(WIOService* io_service,
+                                const td::TimeDuration& wait,
+                                const boost::function<void()>& func) {
+    int ms = wait.total_milliseconds();
+    if (ms < 0) {
+        ms = INT_MAX;
+    }
+    io_service->schedule(ms, func);
+}
+
+void PlanningServer::set_io_service(WIOService* io_service) {
+    set_scheduler(boost::bind(WIOService_schedule, io_service, _1, _2));
+}
+
 void PlanningServer::schedule(const td::TimeDuration& wait,
                               const boost::function<void()>& func) {
-    schedule_action(wait, func);
+    scheduler_(wait, func);
 }
 
 void PlanningServer::process(TaskPtr task) {
 */
 class PlanningServer : public WObject {
 public:
+    /** Function applying some function (second arg) at some time (first arg) */
+    typedef boost::function < void(const td::TimeDuration&,
+                                   const boost::function<void()>&) > Scheduler;
+
     /** Constructor.
-    \deprecated io_service is ignored. Use another constructor
+    \see set_io_service()
     */
     PlanningServer(WIOService* io_service, WObject* p = 0);
 
         default_notify_needed_ = default_notify_needed;
     }
 
+    /** Set function which will be called to apply a task at some time.
+    By default, Wt::Wc::schedule_action() is used.
+    */
+    void set_scheduler(const Scheduler& scheduler);
+
     /** Get IO service.
     \deprecated Return WIOService, used for Wt server, if available, else 0.
     */
     WIOService* io_service();
 
     /** Set IO service.
-    \deprecated Does nothing
+
+    If the number of total milliseconds of the duration exceeds the
+    size of \c int, the duration is decreased to fit into this size (INT_MAX).
+    If sizeof(int) is 4, max duration is about 24.8 days.
+
+    \see set_scheduler
     */
-    void set_io_service(WIOService*)
-    { }
+    void set_io_service(WIOService* io_service);
 
     /** Utility method used to schedule a function.
     This is a method for convenience.
-    \see schedule_action()
+    \see set_scheduler.
     */
     void schedule(const td::TimeDuration& wait,
                   const boost::function<void()>& func);
     Server* server_;
     td::TimeDuration delay_;
     bool default_notify_needed_;
+    Scheduler scheduler_;
 
     void process(TaskPtr task);
 };