Rev 1604 | Rev 1625 | 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/concept_check.hpp> |
||
| 27 | #include <boost/lexical_cast.hpp> |
||
| 28 | |||
| 29 | #include "vm/Manager.h" |
||
| 30 | #include "timer/Manager.h" |
||
| 31 | |||
| 32 | namespace atom { |
||
| 33 | namespace vm { |
||
| 34 | namespace plugin { |
||
| 35 | |||
| 36 | logging::Logger Timer::LOG("vm::plugin::Timer"); |
||
| 37 | |||
| 1604 | runge | 38 | Timer::Timers Timer::timers_; |
| 39 | |||
| 1601 | runge | 40 | Timer::Timer() |
| 41 | { |
||
| 42 | this->name_ = "timer"; |
||
| 43 | |||
| 44 | this->ExportFunction("StartTimer", Timer::Export_StartTimer); |
||
| 45 | this->ExportFunction("ClearTimer", Timer::Export_ClearTimer); |
||
| 46 | } |
||
| 47 | |||
| 48 | Timer::~Timer() |
||
| 49 | { |
||
| 50 | |||
| 51 | } |
||
| 52 | |||
| 1604 | runge | 53 | void Timer::InitializeDone() |
| 1601 | runge | 54 | { |
| 1608 | runge | 55 | Plugin::InitializeDone(); |
| 1604 | runge | 56 | |
| 1608 | runge | 57 | this->ImportFunction("Timer_OnTimeout"); |
| 58 | |||
| 1604 | runge | 59 | timer::Manager::Instance()->ConnectSlots(timer::Manager::SignalOnTimeout::slot_type(&Timer::SlotOnTimeout, this, _1, _2).track(this->tracker_)); |
| 60 | } |
||
| 61 | |||
| 62 | void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat) |
||
| 63 | { |
||
| 64 | if (!repeat) |
||
| 65 | { |
||
| 66 | Timers::iterator it = Timer::timers_.find(timer_id); |
||
| 67 | |||
| 68 | if (it != Timer::timers_.end()) |
||
| 69 | { |
||
| 70 | Timer::timers_.erase(it); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 1601 | runge | 74 | ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList); |
| 75 | arguments->push_back(v8::Integer::New(timer_id)); |
||
| 76 | arguments->push_back(v8::Boolean::New(repeat)); |
||
| 77 | |||
| 1608 | runge | 78 | this->Call( timer_id, "Timer_OnTimeout",arguments); |
| 1601 | runge | 79 | } |
| 80 | |||
| 81 | Value Timer::Export_StartTimer(const v8::Arguments& args) |
||
| 82 | { |
||
| 1604 | runge | 83 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 84 | |||
| 85 | if (args.Length() < 2) |
||
| 86 | { |
||
| 87 | LOG.Error("To few arguments."); |
||
| 88 | } |
||
| 89 | |||
| 90 | timer::TimerId timer_id = timer::Manager::Instance()->Set(args[0]->Uint32Value(), args[1]->BooleanValue()); |
||
| 91 | |||
| 92 | Timer::timers_.insert(timer_id); |
||
| 93 | |||
| 94 | return v8::Integer::New(timer_id); |
||
| 1601 | runge | 95 | } |
| 96 | |||
| 97 | Value Timer::Export_ClearTimer(const v8::Arguments& args) |
||
| 98 | { |
||
| 1604 | runge | 99 | LOG.Debug(std::string(__FUNCTION__) + " called!"); |
| 100 | |||
| 101 | if (args.Length() < 1) |
||
| 102 | { |
||
| 103 | LOG.Error("To few arguments."); |
||
| 104 | } |
||
| 105 | |||
| 106 | Timers::iterator it = Timer::timers_.find(args[0]->IsUint32()); |
||
| 107 | |||
| 108 | if (it != Timer::timers_.end()) |
||
| 109 | { |
||
| 110 | Timer::timers_.erase(it); |
||
| 111 | } |
||
| 112 | |||
| 1601 | runge | 113 | timer::Manager::Instance()->Cancel(args[0]->IsUint32()); |
| 114 | return v8::Undefined(); |
||
| 115 | } |
||
| 116 | |||
| 117 | }; // namespace plugin |
||
| 118 | }; // namespace vm |
||
| 119 | }; // namespace atom |