Rev 1608 | Rev 1642 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1608 | Rev 1625 | ||
|---|---|---|---|
| Line 35... | Line 35... | ||
| 35 | 35 | ||
| 36 | logging::Logger Timer::LOG("vm::plugin::Timer"); |
36 | logging::Logger Timer::LOG("vm::plugin::Timer"); |
| 37 | 37 | ||
| 38 | Timer::Timers Timer::timers_; |
38 | Timer::Timers Timer::timers_; |
| 39 | 39 | ||
| 40 | Timer::Timer() |
40 | Timer::Timer(boost::asio::io_service& io_service) : Plugin(io_service) |
| 41 | { |
41 | { |
| 42 | this->name_ = "timer"; |
42 | this->name_ = "timer"; |
| 43 | 43 | ||
| 44 | this->ExportFunction("StartTimer", Timer::Export_StartTimer); |
44 | this->ExportFunction("StartTimer", Timer::Export_StartTimer); |
| 45 | this->ExportFunction("ClearTimer", Timer::Export_ClearTimer); |
45 | this->ExportFunction("ClearTimer", Timer::Export_ClearTimer); |
| - | 46 | this->ExportFunction("sleep", Timer::Export_Sleep); |
|
| 46 | } |
47 | } |
| 47 | 48 | ||
| 48 | Timer::~Timer() |
49 | Timer::~Timer() |
| 49 | { |
50 | { |
| 50 | 51 | ||
| Line 59... | Line 60... | ||
| 59 | timer::Manager::Instance()->ConnectSlots(timer::Manager::SignalOnTimeout::slot_type(&Timer::SlotOnTimeout, this, _1, _2).track(this->tracker_)); |
60 | timer::Manager::Instance()->ConnectSlots(timer::Manager::SignalOnTimeout::slot_type(&Timer::SlotOnTimeout, this, _1, _2).track(this->tracker_)); |
| 60 | } |
61 | } |
| 61 | 62 | ||
| 62 | void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat) |
63 | void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat) |
| 63 | { |
64 | { |
| - | 65 | this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat)); |
|
| - | 66 | } |
|
| - | 67 | ||
| - | 68 | void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
|
| - | 69 | { |
|
| - | 70 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
|
| - | 71 | ||
| - | 72 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
|
| - | 73 | ||
| 64 | if (!repeat) |
74 | if (!repeat) |
| 65 | { |
75 | { |
| 66 | Timers::iterator it = Timer::timers_.find(timer_id); |
76 | Timers::iterator it = Timer::timers_.find(timer_id); |
| 67 | 77 | ||
| 68 | if (it != Timer::timers_.end()) |
78 | if (it != Timer::timers_.end()) |
| 69 | { |
79 | { |
| 70 | Timer::timers_.erase(it); |
80 | Timer::timers_.erase(it); |
| 71 | } |
81 | } |
| 72 | } |
82 | } |
| 73 | 83 | ||
| 74 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
84 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 75 | arguments->push_back(v8::Integer::New(timer_id)); |
85 | arguments->push_back(v8::Integer::New(timer_id)); |
| 76 | arguments->push_back(v8::Boolean::New(repeat)); |
86 | arguments->push_back(v8::Boolean::New(repeat)); |
| 77 | 87 | ||
| 78 | this->Call( timer_id, "Timer_OnTimeout",arguments); |
88 | this->Call( timer_id, "Timer_OnTimeout",arguments); |
| 79 | } |
89 | } |
| 80 | 90 | ||
| 81 | Value Timer::Export_StartTimer(const v8::Arguments& args) |
91 | Value Timer::Export_StartTimer(const v8::Arguments& args) |
| 82 | { |
92 | { |
| - | 93 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
|
| - | 94 | ||
| 83 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
95 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 84 | 96 | ||
| 85 | if (args.Length() < 2) |
97 | if (args.Length() < 2) |
| 86 | { |
98 | { |
| 87 | LOG.Error("To few arguments."); |
99 | LOG.Error("To few arguments."); |
| 88 | } |
100 | } |
| 89 | 101 | ||
| 90 | timer::TimerId timer_id = timer::Manager::Instance()->Set(args[0]->Uint32Value(), args[1]->BooleanValue()); |
102 | timer::TimerId timer_id = timer::Manager::Instance()->Set(args[0]->Uint32Value(), args[1]->BooleanValue()); |
| 91 | 103 | ||
| 92 | Timer::timers_.insert(timer_id); |
104 | Timer::timers_.insert(timer_id); |
| 93 | 105 | ||
| 94 | return v8::Integer::New(timer_id); |
106 | return v8::Integer::New(timer_id); |
| 95 | } |
107 | } |
| 96 | 108 | ||
| 97 | Value Timer::Export_ClearTimer(const v8::Arguments& args) |
109 | Value Timer::Export_ClearTimer(const v8::Arguments& args) |
| 98 | { |
110 | { |
| - | 111 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
|
| - | 112 | ||
| - | 113 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
|
| - | 114 | ||
| - | 115 | if (args.Length() < 1) |
|
| - | 116 | { |
|
| - | 117 | LOG.Error("To few arguments."); |
|
| - | 118 | } |
|
| - | 119 | ||
| - | 120 | Timers::iterator it = Timer::timers_.find(args[0]->IsUint32()); |
|
| - | 121 | ||
| - | 122 | if (it != Timer::timers_.end()) |
|
| - | 123 | { |
|
| - | 124 | Timer::timers_.erase(it); |
|
| - | 125 | } |
|
| - | 126 | ||
| - | 127 | timer::Manager::Instance()->Cancel(args[0]->IsUint32()); |
|
| - | 128 | return v8::Undefined(); |
|
| - | 129 | } |
|
| - | 130 | ||
| - | 131 | Value Timer::Export_Sleep(const v8::Arguments& args) |
|
| - | 132 | { |
|
| - | 133 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
|
| - | 134 | ||
| 99 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
135 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 100 | 136 | ||
| 101 | if (args.Length() < 1) |
137 | if (args.Length() < 1) |
| 102 | { |
138 | { |
| 103 | LOG.Error("To few arguments."); |
139 | LOG.Error("To few arguments."); |
| 104 | } |
140 | } |
| 105 | 141 | ||
| 106 |
|
142 | boost::this_thread::sleep(boost::posix_time::milliseconds(args[0]->Uint32Value())); |
| 107 | - | ||
| 108 | if (it != Timer::timers_.end()) |
- | |
| 109 | { |
- | |
| 110 | Timer::timers_.erase(it); |
- | |
| 111 | } |
- | |
| 112 | 143 | ||
| 113 | timer::Manager::Instance()->Cancel(args[0]->IsUint32()); |
- | |
| 114 | return v8::Undefined(); |
144 | return v8::Undefined(); |
| 115 | } |
145 | } |
| 116 | 146 | ||
| 117 | }; // namespace plugin |
147 | }; // namespace plugin |
| 118 | }; // namespace vm |
148 | }; // namespace vm |