Skip to content
Newer
Older
100644 169 lines (122 sloc) 4.49 KB
f35a396 Revert "remove node_io_watcher"
Igor Zinkovsky authored
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #include <node_io_watcher.h>
23
24 #include <node.h>
25 #include <v8.h>
26
27 #include <assert.h>
28
29 namespace node {
30
31 using namespace v8;
32
33 Persistent<FunctionTemplate> IOWatcher::constructor_template;
34 Persistent<String> callback_symbol;
35
36
37 void IOWatcher::Initialize(Handle<Object> target) {
38 HandleScope scope;
39
40 Local<FunctionTemplate> t = FunctionTemplate::New(IOWatcher::New);
41 constructor_template = Persistent<FunctionTemplate>::New(t);
42 constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
43 constructor_template->SetClassName(String::NewSymbol("IOWatcher"));
44
45 NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", IOWatcher::Start);
46 NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", IOWatcher::Stop);
47 NODE_SET_PROTOTYPE_METHOD(constructor_template, "set", IOWatcher::Set);
48
49 target->Set(String::NewSymbol("IOWatcher"), constructor_template->GetFunction());
50
51 callback_symbol = NODE_PSYMBOL("callback");
52 }
53
54
55 void IOWatcher::Callback(EV_P_ ev_io *w, int revents) {
56 IOWatcher *io = static_cast<IOWatcher*>(w->data);
57 assert(w == &io->watcher_);
58 HandleScope scope;
59
60 Local<Value> callback_v = io->handle_->Get(callback_symbol);
61 if (!callback_v->IsFunction()) {
62 io->Stop();
63 return;
64 }
65
66 Local<Function> callback = Local<Function>::Cast(callback_v);
67
68 TryCatch try_catch;
69
70 Local<Value> argv[2];
71 argv[0] = Local<Value>::New(revents & EV_READ ? True() : False());
72 argv[1] = Local<Value>::New(revents & EV_WRITE ? True() : False());
73
74 callback->Call(io->handle_, 2, argv);
75
76 if (try_catch.HasCaught()) {
77 FatalException(try_catch);
78 }
79 }
80
81
82 //
83 // var io = new process.IOWatcher();
84 // process.callback = function (readable, writable) { ... };
85 // io.set(fd, true, false);
86 // io.start();
87 //
88 Handle<Value> IOWatcher::New(const Arguments& args) {
89 if (!args.IsConstructCall()) {
90 return FromConstructorTemplate(constructor_template, args);
91 }
92
93 HandleScope scope;
94 IOWatcher *s = new IOWatcher();
95 s->Wrap(args.This());
96 return args.This();
97 }
98
99
100 Handle<Value> IOWatcher::Start(const Arguments& args) {
101 HandleScope scope;
102 IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
103 io->Start();
104 return Undefined();
105 }
106
107
108 Handle<Value> IOWatcher::Stop(const Arguments& args) {
109 HandleScope scope;
110 IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
111 io->Stop();
112 return Undefined();
113 }
114
115
116 void IOWatcher::Start() {
117 if (!ev_is_active(&watcher_)) {
118 ev_io_start(EV_DEFAULT_UC_ &watcher_);
119 Ref();
120 }
121 }
122
123
124 void IOWatcher::Stop() {
125 if (ev_is_active(&watcher_)) {
126 ev_io_stop(EV_DEFAULT_UC_ &watcher_);
127 Unref();
128 }
129 }
130
131
132 Handle<Value> IOWatcher::Set(const Arguments& args) {
133 HandleScope scope;
134
135 IOWatcher *io = ObjectWrap::Unwrap<IOWatcher>(args.Holder());
136
137 if (!args[0]->IsInt32()) {
138 return ThrowException(Exception::TypeError(
139 String::New("First arg should be a file descriptor.")));
140 }
141
142 int fd = args[0]->Int32Value();
143
144 if (!args[1]->IsBoolean()) {
145 return ThrowException(Exception::TypeError(
146 String::New("Second arg should boolean (readable).")));
147 }
148
149 int events = 0;
150
151 if (args[1]->IsTrue()) events |= EV_READ;
152
153 if (!args[2]->IsBoolean()) {
154 return ThrowException(Exception::TypeError(
155 String::New("Third arg should boolean (writable).")));
156 }
157
158 if (args[2]->IsTrue()) events |= EV_WRITE;
159
160 assert(!io->watcher_.active);
161 ev_io_set(&io->watcher_, fd, events);
162
163 return Undefined();
164 }
165
166
167
168 } // namespace node
Something went wrong with that request. Please try again.