-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.cpp
More file actions
165 lines (134 loc) · 4.68 KB
/
misc.cpp
File metadata and controls
165 lines (134 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#define BUILDING_NODE_EXTENSION
#include <v8.h>
#include <node.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#define PRINTF
using namespace std;
using namespace node;
using namespace v8;
extern "C" void init(Handle<Object>);
static void pollpri_event(EV_P_ ev_io * req, int revents);
#define QUOTE(name, ...) #name
#define STR(macro) QUOTE(macro)
#define TEST_EV_DEFAULT_NAME STR(EV_DEFAULT_)
class Pollpri: ObjectWrap {
private:
int fd, epfd;
char *path;
public:
ev_io event_watcher, *ew;
static Persistent<FunctionTemplate> ct;
static void Init(Handle<Object> target) {
PRINTF("Entering Init\n");
PRINTF("EV_DEFAULT_ = %s\n", TEST_EV_DEFAULT_NAME);
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
ct = Persistent<FunctionTemplate>::New(t);
ct->InstanceTemplate()->SetInternalFieldCount(1);
ct->SetClassName(String::NewSymbol("Pollpri"));
target->Set(String::NewSymbol("Pollpri"), ct->GetFunction());
PRINTF("Leaving Init\n");
}
Pollpri() {
PRINTF("Entering Pollpri constructor\n");
epfd = 0;
fd = 0;
ev_init(&event_watcher, pollpri_event);
event_watcher.data = this;
ew = &event_watcher;
}
~Pollpri() {
PRINTF("Entering Pollpri destructor\n");
if(epfd) close(epfd);
if(fd) close(fd);
ev_io_stop(EV_DEFAULT_ &event_watcher);
}
static Handle<Value> New(const Arguments& args) {
PRINTF("Entered New\n");
HandleScope scope;
const char *usage = "usage: new Pollpri(path)";
if(!args.IsConstructCall() || args.Length() != 1) {
return ThrowException(Exception::Error(String::New(usage)));
}
String::Utf8Value path(args[0]);
Pollpri *p = new Pollpri();
p->Wrap(args.This());
p->path = (char *)malloc(path.length() + 1);
strncpy(p->path, *path, path.length() + 1);
// Increment reference count to prevent garbage collection
p->Ref();
// Open file to watch
int fd = open(p->path, O_RDWR | O_NONBLOCK);
PRINTF("open(%s) returned %d: %s\n", p->path, fd, strerror(errno));
// Create epoll event
int epfd = epoll_create(1);
PRINTF("epoll_create(1) returned %d: %s\n", epfd, strerror(errno));
struct epoll_event ev;
ev.events = EPOLLPRI;
ev.data.fd = fd;
int n = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
PRINTF("epoll_ctl(%d) returned %d (%d): %s\n", fd, n, epfd, strerror(errno));
// Read initial state
int m = 0;
char buf[64];
m = lseek(fd, 0, SEEK_SET);
PRINTF("seek(%d) %d bytes: %s\n", fd, m, strerror(errno));
m = read(fd, &buf, 63);
buf[m] = 0;
PRINTF("read(%d) %d bytes (%s): %s\n", fd, m, buf, strerror(errno));
// Setup event watcher
ev_io_set(p->ew, epfd, EV_READ);
ev_io_start(EV_DEFAULT_ p->ew);
p->epfd = epfd;
p->fd = fd;
PRINTF("Leaving New\n");
return(args.This());
}
void Event(Pollpri * p, int revents) {
HandleScope scope;
PRINTF("fd = %d, epfd = %d, revents = 0x%0x\n", fd, epfd, revents);
if(revents != EV_READ) {
printf("fd = %d, epfd = %d, revents = 0x%0x\n", fd, epfd, revents);
return;
}
int m = 0;
char buf[64];
m = lseek(fd, 0, SEEK_SET);
PRINTF("seek(%d) %d bytes: %s\n", fd, m, strerror(errno));
m = read(fd, &buf, 63);
buf[m] = 0;
PRINTF("read(%d) %d bytes (%s): %s\n", fd, m, buf, strerror(errno));
Local<Value> emit_v = handle_->Get(String::NewSymbol("emit"));
assert(emit_v->IsFunction());
Local<Function> emit_f = emit_v.As<Function>();
Handle<Value> argv[2];
argv[0] = String::New("edge");
argv[1] = String::New(buf);
TryCatch tc;
emit_f->Call(handle_, 2, argv);
if(tc.HasCaught()) {
FatalException(tc);
}
}
static void pollpri_event(EV_P_ ev_io * req, int revents) {
PRINTF("Entered pollpri_event\n");
Pollpri *p = static_cast<Pollpri*>(req->data);
p->Event(p, revents);
PRINTF("Leaving pollpri_event\n");
}
};
Persistent<FunctionTemplate> Pollpri::ct;
extern "C" {
void init(Handle<Object> target) {
PRINTF("Calling Init\n");
Pollpri::Init(target);
}
NODE_MODULE(pollpri, init)
}