Rev 1740 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1601 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 5 | * This program is free software; you can redistribute it and/or modify |
||
| 6 | * it under the terms of the GNU General Public License as published by |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 15 | * You should have received a copy of the GNU General Public License along |
||
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "Timer.h" |
||
| 22 | |||
| 1604 | runge | 23 | #include <v8-debug.h> |
| 1601 | runge | 24 | #include <stdio.h> |
| 25 | |||
| 26 | #include <boost/lexical_cast.hpp> |
||
| 27 | |||
| 28 | #include "vm/Manager.h" |
||
| 29 | #include "timer/Manager.h" |
||
| 30 | |||
| 31 | namespace atom { |
||
| 32 | namespace vm { |
||
| 33 | namespace plugin { |
||
| 34 | |||
| 35 | logging::Logger Timer::LOG("vm::plugin::Timer"); |
||
| 36 | |||
| 1604 | runge | 37 | Timer::Timers Timer::timers_; |
| 38 | |||
| 1625 | runge | 39 | Timer::Timer(boost::asio::io_service& io_service) : Plugin(io_service) |
| 1601 | runge | 40 | { |
| 41 | this->name_ = "timer"; |
||
| 42 | |||
| 1646 | runge | 43 | this->ExportFunction("TimerExport_SetTimer", Timer::Export_SetTimer); |
| 44 | this->ExportFunction("TimerExport_SetAlarm", Timer::Export_SetAlarm); |
||
| 45 | this->ExportFunction("TimerExport_Cancel", Timer::Export_Cancel); |
||
| 46 | this->ExportFunction("TimerExport_Sleep", Timer::Export_Sleep); |
||
| 1601 | runge | 47 | } |
| 48 | |||
| 49 | Timer::~Timer() |
||
| 50 | { |
||
| 51 | |||
| 52 | } |
||
| 53 | |||
| 1604 | runge | 54 | void Timer::InitializeDone() |
| 1601 | runge | 55 | { |
| 1608 | runge | 56 | Plugin::InitializeDone(); |
| 1604 | runge | 57 | |
| 1608 | runge | 58 | this->ImportFunction("Timer_OnTimeout"); |
| 59 | |||
| 1604 | runge | 60 | timer::Manager::Instance()->ConnectSlots(timer::Manager::SignalOnTimeout::slot_type(&Timer::SlotOnTimeout, this, _1, _2).track(this->tracker_)); |
| 61 | } |
||
| 62 | |||
| 1647 | runge | 63 | void Timer::CallOutput(unsigned int request_id, std::string output) |
| 64 | { |
||
| 65 | LOG.Info(output); |
||
| 66 | } |
||
| 67 | |||
| 1604 | runge | 68 | void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat) |
| 69 | { |
||
| 1625 | runge | 70 | this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat)); |
| 71 | } |
||
| 72 | |||
| 73 | void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
||
| 74 | { |
||
| 1740 | runge | 75 | v8::Locker lock; |
| 1625 | runge | 76 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 1741 | runge | 77 | v8::HandleScope handle_scope; |
| 1625 | runge | 78 | |
| 1642 | runge | 79 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1625 | runge | 80 | |
| 1604 | runge | 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 | |||
| 1601 | runge | 91 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 92 | arguments->push_back(v8::Integer::New(timer_id)); |
||
| 93 | arguments->push_back(v8::Boolean::New(repeat)); |
||
| 94 | |||
| 1608 | runge | 95 | this->Call( timer_id, "Timer_OnTimeout",arguments); |
| 1601 | runge | 96 | } |
| 97 | |||
| 1646 | runge | 98 | Value Timer::Export_SetAlarm(const v8::Arguments& args) |
| 1601 | runge | 99 | { |
| 1740 | runge | 100 | v8::Locker lock; |
| 1625 | runge | 101 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 1741 | runge | 102 | v8::HandleScope handle_scope; |
| 1625 | runge | 103 | |
| 1642 | runge | 104 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1604 | runge | 105 | |
| 1646 | runge | 106 | if (args.Length() < 1) |
| 107 | { |
||
| 108 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
||
| 1741 | runge | 109 | return handle_scope.Close(v8::Boolean::New(false)); |
| 1646 | runge | 110 | } |
| 111 | |||
| 112 | v8::String::AsciiValue time(args[0]); |
||
| 113 | |||
| 114 | timer::TimerId timer_id = timer::Manager::Instance()->SetAlarm(*time); |
||
| 115 | |||
| 116 | Timer::timers_.insert(timer_id); |
||
| 117 | |||
| 1741 | runge | 118 | return handle_scope.Close(v8::Integer::New(timer_id)); |
| 1646 | runge | 119 | } |
| 120 | |||
| 121 | Value Timer::Export_SetTimer(const v8::Arguments& args) |
||
| 122 | { |
||
| 1740 | runge | 123 | v8::Locker lock; |
| 1646 | runge | 124 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 1741 | runge | 125 | v8::HandleScope handle_scope; |
| 1646 | runge | 126 | |
| 127 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
||
| 128 | |||
| 1604 | runge | 129 | if (args.Length() < 2) |
| 130 | { |
||
| 1644 | runge | 131 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
| 1741 | runge | 132 | return handle_scope.Close(v8::Boolean::New(false)); |
| 1604 | runge | 133 | } |
| 134 | |||
| 1646 | runge | 135 | timer::TimerId timer_id = timer::Manager::Instance()->SetTimer(args[0]->Uint32Value(), args[1]->BooleanValue()); |
| 1604 | runge | 136 | |
| 137 | Timer::timers_.insert(timer_id); |
||
| 138 | |||
| 1741 | runge | 139 | return handle_scope.Close(v8::Integer::New(timer_id)); |
| 1601 | runge | 140 | } |
| 141 | |||
| 1646 | runge | 142 | Value Timer::Export_Cancel(const v8::Arguments& args) |
| 1601 | runge | 143 | { |
| 1740 | runge | 144 | v8::Locker lock; |
| 1625 | runge | 145 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 1741 | runge | 146 | v8::HandleScope handle_scope; |
| 1625 | runge | 147 | |
| 1642 | runge | 148 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1604 | runge | 149 | |
| 150 | if (args.Length() < 1) |
||
| 151 | { |
||
| 1644 | runge | 152 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
| 1741 | runge | 153 | return handle_scope.Close(v8::Boolean::New(false)); |
| 1604 | runge | 154 | } |
| 155 | |||
| 1651 | runge | 156 | Timers::iterator it = Timer::timers_.find(args[0]->Uint32Value()); |
| 1604 | runge | 157 | |
| 158 | if (it != Timer::timers_.end()) |
||
| 159 | { |
||
| 160 | Timer::timers_.erase(it); |
||
| 161 | } |
||
| 162 | |||
| 1651 | runge | 163 | timer::Manager::Instance()->Cancel(args[0]->Uint32Value()); |
| 1741 | runge | 164 | return handle_scope.Close(v8::Undefined()); |
| 1601 | runge | 165 | } |
| 166 | |||
| 1625 | runge | 167 | Value Timer::Export_Sleep(const v8::Arguments& args) |
| 168 | { |
||
| 1740 | runge | 169 | v8::Locker lock; |
| 1625 | runge | 170 | v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext()); |
| 1741 | runge | 171 | v8::HandleScope handle_scope; |
| 1625 | runge | 172 | |
| 1642 | runge | 173 | //LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 1625 | runge | 174 | |
| 175 | if (args.Length() < 1) |
||
| 176 | { |
||
| 1644 | runge | 177 | LOG.Error(std::string(__FUNCTION__) + ": To few arguments."); |
| 1741 | runge | 178 | return handle_scope.Close(v8::Boolean::New(false)); |
| 1625 | runge | 179 | } |
| 180 | |||
| 181 | boost::this_thread::sleep(boost::posix_time::milliseconds(args[0]->Uint32Value())); |
||
| 182 | |||
| 1741 | runge | 183 | return handle_scope.Close(v8::Undefined()); |
| 1625 | runge | 184 | } |
| 185 | |||
| 1601 | runge | 186 | }; // namespace plugin |
| 187 | }; // namespace vm |
||
| 1625 | runge | 188 | }; // namespace atom |