murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame^] | 1 | #include <node.h> |
| 2 | #include <nan.h> |
| 3 | |
| 4 | #include "grpc/grpc.h" |
| 5 | #include "grpc/support/time.h" |
| 6 | #include "completion_queue_async_worker.h" |
| 7 | #include "event.h" |
| 8 | #include "tag.h" |
| 9 | |
| 10 | namespace grpc { |
| 11 | namespace node { |
| 12 | |
| 13 | using v8::Function; |
| 14 | using v8::Handle; |
| 15 | using v8::Object; |
| 16 | using v8::Persistent; |
| 17 | using v8::Value; |
| 18 | |
| 19 | grpc_completion_queue *CompletionQueueAsyncWorker::queue; |
| 20 | |
| 21 | CompletionQueueAsyncWorker::CompletionQueueAsyncWorker() : |
| 22 | NanAsyncWorker(NULL) { |
| 23 | } |
| 24 | |
| 25 | CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() { |
| 26 | } |
| 27 | |
| 28 | void CompletionQueueAsyncWorker::Execute() { |
| 29 | result = grpc_completion_queue_next(queue, gpr_inf_future); |
| 30 | } |
| 31 | |
| 32 | grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { |
| 33 | return queue; |
| 34 | } |
| 35 | |
| 36 | void CompletionQueueAsyncWorker::Next() { |
| 37 | NanScope(); |
| 38 | CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker(); |
| 39 | NanAsyncQueueWorker(worker); |
| 40 | } |
| 41 | |
| 42 | void CompletionQueueAsyncWorker::Init(Handle<Object> exports) { |
| 43 | NanScope(); |
| 44 | queue = grpc_completion_queue_create(); |
| 45 | } |
| 46 | |
| 47 | void CompletionQueueAsyncWorker::HandleOKCallback() { |
| 48 | NanScope(); |
| 49 | NanCallback event_callback(GetTagHandle(result->tag).As<Function>()); |
| 50 | Handle<Value> argv[] = { |
| 51 | CreateEventObject(result) |
| 52 | }; |
| 53 | |
| 54 | DestroyTag(result->tag); |
| 55 | grpc_event_finish(result); |
| 56 | result = NULL; |
| 57 | |
| 58 | event_callback.Call(1, argv); |
| 59 | } |
| 60 | |
| 61 | } // namespace node |
| 62 | } // namespace grpc |