@@ -13,86 +13,148 @@ using namespace std;
1313using namespace node ;
1414using namespace v8 ;
1515
16- static Handle<Value> pollpri (const Arguments&);
17- static int pollpri_thread (eio_req *);
18- static int pollpri_after (eio_req *);
19- // extern "C" void pollpri_event(struct ev_loop* loop, ev_io* req, int revents);
2016extern " C" void pollpri_event (ev_io* req, int revents);
2117extern " C" void init (Handle<Object>);
2218
23- struct pollpri_request {
24- Persistent<Function> callback;
25- char path[1 ];
26- };
19+ class Pollpri : ObjectWrap {
20+ private:
21+ int fd, epfd;
22+ char *path;
23+ public:
24+ struct pollpri_request {
25+ Pollpri *p;
26+ Persistent<Function> cb;
27+ };
2728
28- class Pollpri : public ObjectWrap {
29- };
29+ static Persistent<FunctionTemplate> ct;
30+
31+ static void Init (Handle<Object> target) {
32+ HandleScope scope;
33+ Local<FunctionTemplate> t = FunctionTemplate::New (New);
34+ ct = Persistent<FunctionTemplate>::New (t);
35+ ct->InstanceTemplate ()->SetInternalFieldCount (1 );
36+ ct->SetClassName (String::NewSymbol (" Pollpri" ));
37+
38+ NODE_SET_PROTOTYPE_METHOD (ct, " pollpri" , pollpri);
39+
40+ target->Set (String::NewSymbol (" Pollpri" ), ct->GetFunction ());
41+ }
3042
31- static Handle<Value> pollpri (const Arguments& args) {
32- HandleScope scope;
33- const char *usage = " usage: pollpri(path, cb)" ;
34- if (args.Length () != 2 ) {
35- return ThrowException (Exception::Error (String::New (usage)));
43+ Pollpri () {
44+ epfd = 0 ;
45+ fd = 0 ;
3646 }
37- String::Utf8Value path (args[0 ]);
38- Local<Function> cb = Local<Function>::Cast (args[1 ]);
3947
40- pollpri_request *pr = (pollpri_request *)
41- malloc (sizeof (struct pollpri_request ) + path.length () + 1 );
42-
43- pr->cb = Persistent<Function>::New (cb);
44- strncpy (pr->path , *path, path.length () + 1 );
48+ ~Pollpri () {
49+ if (epfd) close (epfd);
50+ if (fd) close (fd);
51+ }
4552
46- eio_custom (pollpri_thread, EIO_PRI_DEFAULT, pollpri_after, pr);
47- ev_ref (EV_DEFAULT_UC);
48- printf (" Leaving pollpri\n " );
49- return (Undefined ());
50- }
51-
52- static int pollpri_thread (eio_req *req) {
53- printf (" Entered pollpri_thread\n " );
54- struct pollpri_request * pr = (struct pollpri_request *)req->data ;
55- int epfd = epoll_create (1 );
56- int fd = open (pr->path , O_RDWR | O_NONBLOCK);
57- printf (" open(%s) returned %d: %s\n " , pr->path , fd, strerror (errno));
58- struct epoll_event ev;
59- ev.events = EPOLLPRI;
60- ev.data .fd = fd;
61- int n = epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &ev);
62- printf (" epoll_ctl(%d) returned %d (%d): %s\n " , fd, n, epfd, strerror (errno));
63- ev_io pollpri_watcher;
64- ev_init (&pollpri_watcher, pollpri_event);
65- pollpri_watcher.data = pr;
66- ev_io_set (&pollpri_watcher, epfd, EV_READ | EV_WRITE);
67- ev_io_start (EV_DEFAULT_ &pollpri_watcher);
68- // struct epoll_event events;
69- // n = epoll_wait(epfd, &events, 1, -1);
70- // printf("epoll_wait(%d) returned %d: %s\n", epfd, n, strerror(errno));
71- printf (" Leaving pollpri_thread\n " );
72- return (0 );
73- }
53+ static Handle<Value> New (const Arguments &args) {
54+ printf (" Entered New\n " );
55+ HandleScope scope;
56+ const char *usage = " usage: new Pollpri(path)" ;
57+ if (args.Length () != 1 ) {
58+ return ThrowException (Exception::Error (String::New (usage)));
59+ }
60+ String::Utf8Value path (args[0 ]);
61+ Pollpri *p = new Pollpri ();
62+ p->Wrap (args.This ());
63+ p->path = (char *)malloc (path.length () + 1 );
64+ strncpy (p->path , *path, path.length () + 1 );
65+ printf (" Leaving New\n " );
66+ return (args.This ());
67+ }
7468
75- extern " C" void pollpri_event (ev_io* req, int revents) {
76- printf (" Entered pollpri_event\n " );
77- printf (" Leaving pollpri_event\n " );
78- }
69+ static Handle<Value> pollpri (const Arguments& args) {
70+ printf (" Entered pollpri\n " );
71+ HandleScope scope;
72+ const char *usage = " usage: pollpri(cb)" ;
73+ if (args.Length () != 1 ) {
74+ return ThrowException (Exception::Error (String::New (usage)));
75+ }
76+ Local<Function> cb = Local<Function>::Cast (args[0 ]);
77+
78+ pollpri_request* pr =
79+ (pollpri_request *)malloc (sizeof (struct pollpri_request ));
80+ Pollpri* p = ObjectWrap::Unwrap<Pollpri>(args.This ());
81+
82+ pr->p = p;
83+ pr->cb = Persistent<Function>::New (cb);
84+
85+ eio_custom (pollpri_thread, EIO_PRI_DEFAULT, pollpri_after, pr);
86+ ev_ref (EV_DEFAULT_UC);
87+ printf (" Leaving pollpri\n " );
88+ return (Undefined ());
89+ }
90+
91+ static int pollpri_thread (eio_req *req) {
92+ printf (" Entered pollpri_thread\n " );
93+ struct pollpri_request * pr = (struct pollpri_request *)req->data ;
94+ Pollpri *p = pr->p ;
95+ int epfd = p->epfd ;
96+ int fd = p->fd ;
97+ if (!fd) {
98+ fd = open (p->path , O_RDWR | O_NONBLOCK);
99+ pr->p ->fd = fd;
100+ printf (" open(%s) returned %d: %s\n " , p->path , fd, strerror (errno));
101+ }
102+ if (!epfd) {
103+ epfd = epoll_create (1 );
104+ pr->p ->epfd = epfd;
105+ printf (" epoll_create(1) returned %d: %s\n " , epfd, strerror (errno));
106+ struct epoll_event ev;
107+ ev.events = EPOLLPRI;
108+ ev.data .fd = fd;
109+ int n = epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &ev);
110+ printf (" epoll_ctl(%d) returned %d (%d): %s\n " , fd, n, epfd, strerror (errno));
111+ }
112+ // ev_io pollpri_watcher;
113+ // ev_init(&pollpri_watcher, pollpri_event);
114+ // pollpri_watcher.data = pr;
115+ // ev_io_set(&pollpri_watcher, epfd, EV_READ | EV_WRITE);
116+ // ev_io_start(EV_DEFAULT_ &pollpri_watcher);
117+ struct epoll_event events;
118+ printf (" Calling epoll_wait\n " );
119+ sleep (1 );
120+ int m = epoll_wait (epfd, &events, 1 , -1 );
121+ printf (" epoll_wait(%d) returned %d: %s\n " , epfd, m, strerror (errno));
122+ // close(epfd);
123+ // close(fd);
124+ printf (" Leaving pollpri_thread\n " );
125+ return (0 );
126+ }
127+
128+ static int pollpri_after (eio_req *req) {
129+ printf (" Entered pollpri_after\n " );
130+ HandleScope scope;
131+ ev_unref (EV_DEFAULT_UC);
132+ struct pollpri_request * pr = (struct pollpri_request *)req->data ;
133+ Local<Value> argv[2 ];
134+ argv[0 ] = Local<Value>::New (Null ());
135+ argv[1 ] = String::New (pr->p ->path );
136+ pr->cb ->Call (Context::GetCurrent ()->Global (), 2 , argv);
137+ pr->cb .Dispose ();
138+ // close(pr->p->epfd);
139+ // close(pr->p->fd);
140+ free (pr);
141+ printf (" Leaving pollpri_after\n " );
142+ return (0 );
143+ }
144+
145+ };
79146
80- static int pollpri_after (eio_req *req) {
81- printf (" Entered pollpri_after\n " );
82- HandleScope scope;
83- // ev_unref(EV_DEFAULT_UC);
84- struct pollpri_request * pr = (struct pollpri_request *)req->data ;
85- Local<Value> argv[2 ];
86- argv[0 ] = Local<Value>::New (Null ());
87- argv[1 ] = String::New (pr->path );
88- pr->cb ->Call (Context::GetCurrent ()->Global (), 2 , argv);
89- // pr->cb.Dispose();
90- // free(pr);
91- printf (" Leaving pollpri_after\n " );
92- return (0 );
93- }
147+ Persistent<FunctionTemplate> Pollpri::ct;
94148
95- extern " C" void init (Handle<Object> target) {
96- HandleScope scope;
97- NODE_SET_METHOD (target, " pollpri" , pollpri);
149+ extern " C" {
150+ void init (Handle<Object> target) {
151+ Pollpri::Init (target);
152+ }
153+
154+ void pollpri_event (ev_io* req, int revents) {
155+ printf (" Entered pollpri_event\n " );
156+ printf (" Leaving pollpri_event\n " );
157+ }
158+
159+ NODE_MODULE (pollpri, init);
98160}
0 commit comments