Rev 1741 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1741 | Rev 1746 | ||
|---|---|---|---|
| Line 31... | Line 31... | ||
| 31 | namespace atom { |
31 | namespace atom { |
| 32 | namespace vm { |
32 | namespace vm { |
| 33 | namespace plugin { |
33 | namespace plugin { |
| 34 | 34 | ||
| 35 | logging::Logger Timer::LOG("vm::plugin::Timer"); |
35 | logging::Logger Timer::LOG("vm::plugin::Timer"); |
| 36 | - | ||
| 37 | Timer::Timers Timer::timers_; |
- | |
| 38 | 36 | ||
| 39 | Timer::Timer(boost::asio::io_service& io_service) : Plugin(io_service) |
37 | Timer::Timer(boost::asio::io_service& io_service) : Plugin(io_service) |
| 40 | { |
38 | { |
| 41 | this->name_ = "timer"; |
39 | this->name_ = "timer"; |
| 42 | 40 | ||
| 43 | this->ExportFunction("TimerExport_SetTimer", Timer::Export_SetTimer); |
41 | this->ExportFunction("TimerExport_SetTimer", Timer::Export_SetTimer); |
| 44 | this->ExportFunction("TimerExport_SetAlarm", Timer::Export_SetAlarm); |
42 | this->ExportFunction("TimerExport_SetAlarm", Timer::Export_SetAlarm); |
| 45 | this->ExportFunction("TimerExport_Cancel", Timer::Export_Cancel); |
43 | this->ExportFunction("TimerExport_Cancel", Timer::Export_Cancel); |
| 46 | this->ExportFunction("TimerExport_Sleep", Timer::Export_Sleep); |
44 | this->ExportFunction("TimerExport_Sleep", Timer::Export_Sleep); |
| 47 | } |
45 | } |
| 48 | 46 | ||
| 49 | Timer::~Timer() |
47 | Timer::~Timer() |
| 50 | { |
48 | { |
| 51 | 49 | ||
| 52 | } |
50 | } |
| 53 | 51 | ||
| 54 | void Timer::InitializeDone() |
52 | void Timer::InitializeDone() |
| 55 | { |
53 | { |
| 56 | Plugin::InitializeDone(); |
54 | Plugin::InitializeDone(); |
| 57 | 55 | ||
| Line 69... | Line 67... | ||
| 69 | { |
67 | { |
| 70 | this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat)); |
68 | this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat)); |
| 71 | } |
69 | } |
| 72 | 70 | ||
| 73 | void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
71 | void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
| 74 | { |
72 | { |
| 75 | v8::Locker lock; |
73 | v8::Locker lock; |
| 76 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
74 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 77 | v8::HandleScope handle_scope; |
75 | v8::HandleScope handle_scope; |
| 78 | 76 | ||
| 79 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
77 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 80 | - | ||
| 81 | if (!repeat) |
- | |
| 82 | { |
- | |
| 83 | Timers::iterator it = Timer::timers_.find(timer_id); |
- | |
| 84 | - | ||
| 85 | if (it != Timer::timers_.end()) |
- | |
| 86 | { |
- | |
| 87 | Timer::timers_.erase(it); |
- | |
| 88 | } |
- | |
| 89 | } |
- | |
| 90 | 78 | ||
| 91 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
79 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 92 | arguments->push_back(v8::Integer::New(timer_id)); |
80 | arguments->push_back(v8::Integer::New(timer_id)); |
| 93 | arguments->push_back(v8::Boolean::New(repeat)); |
81 | arguments->push_back(v8::Boolean::New(repeat)); |
| 94 | 82 | ||
| 95 | this->Call( timer_id, "Timer_OnTimeout",arguments); |
83 | this->Call(timer_id, "Timer_OnTimeout",arguments); |
| 96 | } |
84 | } |
| 97 | 85 | ||
| 98 | Value Timer::Export_SetAlarm(const v8::Arguments& args) |
86 | Value Timer::Export_SetAlarm(const v8::Arguments& args) |
| 99 | { |
87 | { |
| 100 | v8::Locker lock; |
88 | v8::Locker lock; |
| 101 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
89 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 102 | v8::HandleScope handle_scope; |
90 | v8::HandleScope handle_scope; |
| 103 | 91 | ||
| 104 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
92 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 105 | 93 | ||
| 106 | if (args.Length() < 1) |
94 | if (args.Length() < 1) |
| Line 110... | Line 98... | ||
| 110 | } |
98 | } |
| 111 | 99 | ||
| 112 | v8::String::AsciiValue time(args[0]); |
100 | v8::String::AsciiValue time(args[0]); |
| 113 | 101 | ||
| 114 | timer::TimerId timer_id = timer::Manager::Instance()->SetAlarm(*time); |
102 | timer::TimerId timer_id = timer::Manager::Instance()->SetAlarm(*time); |
| 115 | - | ||
| 116 | Timer::timers_.insert(timer_id); |
- | |
| 117 | 103 | ||
| 118 | return handle_scope.Close(v8::Integer::New(timer_id)); |
104 | return handle_scope.Close(v8::Integer::New(timer_id)); |
| 119 | } |
105 | } |
| 120 | 106 | ||
| 121 | Value Timer::Export_SetTimer(const v8::Arguments& args) |
107 | Value Timer::Export_SetTimer(const v8::Arguments& args) |
| 122 | { |
108 | { |
| 123 | v8::Locker lock; |
109 | v8::Locker lock; |
| 124 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
110 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 125 | v8::HandleScope handle_scope; |
111 | v8::HandleScope handle_scope; |
| 126 | 112 | ||
| 127 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
113 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 128 | 114 | ||
| 129 | if (args.Length() < 2) |
115 | if (args.Length() < 2) |
| 130 | { |
116 | { |
| 131 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
117 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
| 132 | return handle_scope.Close(v8::Boolean::New(false)); |
118 | return handle_scope.Close(v8::Boolean::New(false)); |
| 133 | } |
119 | } |
| 134 | 120 | ||
| 135 | timer::TimerId timer_id = timer::Manager::Instance()->SetTimer(args[0]->Uint32Value(), args[1]->BooleanValue()); |
121 | timer::TimerId timer_id = timer::Manager::Instance()->SetTimer(args[0]->Uint32Value(), args[1]->BooleanValue()); |
| 136 | - | ||
| 137 | Timer::timers_.insert(timer_id); |
- | |
| 138 | 122 | ||
| 139 | return handle_scope.Close(v8::Integer::New(timer_id)); |
123 | return handle_scope.Close(v8::Integer::New(timer_id)); |
| 140 | } |
124 | } |
| 141 | 125 | ||
| 142 | Value Timer::Export_Cancel(const v8::Arguments& args) |
126 | Value Timer::Export_Cancel(const v8::Arguments& args) |
| Line 151... | Line 135... | ||
| 151 | { |
135 | { |
| 152 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
136 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
| 153 | return handle_scope.Close(v8::Boolean::New(false)); |
137 | return handle_scope.Close(v8::Boolean::New(false)); |
| 154 | } |
138 | } |
| 155 | 139 | ||
| 156 |
|
140 | timer::Manager::Instance()->Cancel(args[0]->Uint32Value()); |
| 157 | 141 | ||
| 158 | if (it != Timer::timers_.end()) |
- | |
| 159 | { |
- | |
| 160 | Timer::timers_.erase(it); |
- | |
| 161 | } |
- | |
| 162 | - | ||
| 163 | timer::Manager::Instance()->Cancel(args[0]->Uint32Value()); |
- | |
| 164 | return handle_scope.Close(v8::Undefined()); |
142 | return handle_scope.Close(v8::Undefined()); |
| 165 | } |
143 | } |
| 166 | 144 | ||
| 167 | Value Timer::Export_Sleep(const v8::Arguments& args) |
145 | Value Timer::Export_Sleep(const v8::Arguments& args) |
| 168 | { |
146 | { |