The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / src / node_stat_watcher.cc @ 6f92da2d

History | View | Annotate | Download (4.14 KB)

1 55048cdf Ryan Dahl
// 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 ff4a9d38 Ben Noordhuis
#include "node_stat_watcher.h"
23 8d2f9e83 Ryan Dahl
24
#include <assert.h>
25 8e5b91c7 Ryan Dahl
#include <string.h>
26 8d2f9e83 Ryan Dahl
#include <stdlib.h>
27
28
namespace node {
29
30 110a9cd8 Ben Noordhuis
using v8::FunctionCallbackInfo;
31
using v8::FunctionTemplate;
32
using v8::Handle;
33
using v8::HandleScope;
34
using v8::Integer;
35
using v8::Local;
36
using v8::Object;
37
using v8::String;
38
using v8::Value;
39 8d2f9e83 Ryan Dahl
40 110a9cd8 Ben Noordhuis
static Cached<String> onchange_sym;
41
static Cached<String> onstop_sym;
42 8d2f9e83 Ryan Dahl
43 f0ce9844 Ben Noordhuis
44 8492c52e Ryan Dahl
void StatWatcher::Initialize(Handle<Object> target) {
45 f65e14ed Trevor Norris
  HandleScope scope(node_isolate);
46 8d2f9e83 Ryan Dahl
47 8492c52e Ryan Dahl
  Local<FunctionTemplate> t = FunctionTemplate::New(StatWatcher::New);
48 110a9cd8 Ben Noordhuis
  t->InstanceTemplate()->SetInternalFieldCount(1);
49
  t->SetClassName(String::NewSymbol("StatWatcher"));
50 8d2f9e83 Ryan Dahl
51 110a9cd8 Ben Noordhuis
  NODE_SET_PROTOTYPE_METHOD(t, "start", StatWatcher::Start);
52
  NODE_SET_PROTOTYPE_METHOD(t, "stop", StatWatcher::Stop);
53 8d2f9e83 Ryan Dahl
54 110a9cd8 Ben Noordhuis
  target->Set(String::NewSymbol("StatWatcher"), t->GetFunction());
55 8d2f9e83 Ryan Dahl
}
56
57
58 b1ffbdc9 Ben Noordhuis
static void Delete(uv_handle_t* handle) {
59
  delete reinterpret_cast<uv_fs_poll_t*>(handle);
60
}
61
62
63 8e29ce9f Fedor Indutny
StatWatcher::StatWatcher() : ObjectWrap(),
64
                             watcher_(new uv_fs_poll_t) {
65 b1ffbdc9 Ben Noordhuis
  uv_fs_poll_init(uv_default_loop(), watcher_);
66
  watcher_->data = static_cast<void*>(this);
67
}
68
69
70
StatWatcher::~StatWatcher() {
71
  Stop();
72
  uv_close(reinterpret_cast<uv_handle_t*>(watcher_), Delete);
73
}
74
75
76 f0ce9844 Ben Noordhuis
void StatWatcher::Callback(uv_fs_poll_t* handle,
77
                           int status,
78 51f128d6 Timothy J Fontaine
                           const uv_stat_t* prev,
79
                           const uv_stat_t* curr) {
80 b1ffbdc9 Ben Noordhuis
  StatWatcher* wrap = static_cast<StatWatcher*>(handle->data);
81
  assert(wrap->watcher_ == handle);
82 f65e14ed Trevor Norris
  HandleScope scope(node_isolate);
83 f0ce9844 Ben Noordhuis
  Local<Value> argv[3];
84
  argv[0] = BuildStatsObject(curr);
85
  argv[1] = BuildStatsObject(prev);
86 f65e14ed Trevor Norris
  argv[2] = Integer::New(status, node_isolate);
87 a26bee8f isaacs
  if (onchange_sym.IsEmpty()) {
88 110a9cd8 Ben Noordhuis
    onchange_sym = String::New("onchange");
89 a26bee8f isaacs
  }
90 110a9cd8 Ben Noordhuis
  MakeCallback(wrap->handle(node_isolate),
91
               onchange_sym,
92
               ARRAY_SIZE(argv),
93
               argv);
94 8d2f9e83 Ryan Dahl
}
95
96
97 110a9cd8 Ben Noordhuis
void StatWatcher::New(const FunctionCallbackInfo<Value>& args) {
98 ef1ffcb7 Ben Noordhuis
  assert(args.IsConstructCall());
99 f65e14ed Trevor Norris
  HandleScope scope(node_isolate);
100 f0ce9844 Ben Noordhuis
  StatWatcher* s = new StatWatcher();
101 0a4ebc3d Trevor Norris
  s->Wrap(args.This());
102 8d2f9e83 Ryan Dahl
}
103
104
105 110a9cd8 Ben Noordhuis
void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
106 ef1ffcb7 Ben Noordhuis
  assert(args.Length() == 3);
107 f65e14ed Trevor Norris
  HandleScope scope(node_isolate);
108 8d2f9e83 Ryan Dahl
109 0a4ebc3d Trevor Norris
  StatWatcher* wrap = ObjectWrap::Unwrap<StatWatcher>(args.This());
110 249c3c16 ssuda
  String::Utf8Value path(args[0]);
111 ef1ffcb7 Ben Noordhuis
  const bool persistent = args[1]->BooleanValue();
112
  const uint32_t interval = args[2]->Uint32Value();
113 8d2f9e83 Ryan Dahl
114 b1ffbdc9 Ben Noordhuis
  if (!persistent) uv_unref(reinterpret_cast<uv_handle_t*>(wrap->watcher_));
115
  uv_fs_poll_start(wrap->watcher_, Callback, *path, interval);
116 f0ce9844 Ben Noordhuis
  wrap->Ref();
117 8d2f9e83 Ryan Dahl
}
118
119
120 110a9cd8 Ben Noordhuis
void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) {
121 f65e14ed Trevor Norris
  HandleScope scope(node_isolate);
122 0a4ebc3d Trevor Norris
  StatWatcher* wrap = ObjectWrap::Unwrap<StatWatcher>(args.This());
123 a26bee8f isaacs
  if (onstop_sym.IsEmpty()) {
124 110a9cd8 Ben Noordhuis
    onstop_sym = String::New("onstop");
125 a26bee8f isaacs
  }
126 110a9cd8 Ben Noordhuis
  MakeCallback(wrap->handle(node_isolate), onstop_sym, 0, NULL);
127 f0ce9844 Ben Noordhuis
  wrap->Stop();
128 8d2f9e83 Ryan Dahl
}
129
130
131 8e29ce9f Fedor Indutny
void StatWatcher::Stop() {
132 b1ffbdc9 Ben Noordhuis
  if (!uv_is_active(reinterpret_cast<uv_handle_t*>(watcher_))) return;
133
  uv_fs_poll_stop(watcher_);
134 f0ce9844 Ben Noordhuis
  Unref();
135 8d2f9e83 Ryan Dahl
}
136
137
138
}  // namespace node