murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame^] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 34 | #include <node.h> |
| 35 | #include <nan.h> |
| 36 | |
| 37 | #include "grpc/grpc.h" |
murgatroid99 | 7765906 | 2015-02-11 09:26:25 -0800 | [diff] [blame] | 38 | #include "grpc/support/log.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 39 | #include "grpc/support/time.h" |
| 40 | #include "completion_queue_async_worker.h" |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 41 | #include "call.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 42 | |
| 43 | namespace grpc { |
| 44 | namespace node { |
| 45 | |
| 46 | using v8::Function; |
| 47 | using v8::Handle; |
| 48 | using v8::Object; |
| 49 | using v8::Persistent; |
| 50 | using v8::Value; |
| 51 | |
| 52 | grpc_completion_queue *CompletionQueueAsyncWorker::queue; |
| 53 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 54 | CompletionQueueAsyncWorker::CompletionQueueAsyncWorker() |
| 55 | : NanAsyncWorker(NULL) {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 56 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 57 | CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() {} |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 58 | |
| 59 | void CompletionQueueAsyncWorker::Execute() { |
| 60 | result = grpc_completion_queue_next(queue, gpr_inf_future); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 61 | if (result->data.op_complete != GRPC_OP_OK) { |
| 62 | SetErrorMessage("The batch encountered an error"); |
| 63 | } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Craig Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 66 | grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { return queue; } |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 67 | |
| 68 | void CompletionQueueAsyncWorker::Next() { |
| 69 | NanScope(); |
| 70 | CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker(); |
| 71 | NanAsyncQueueWorker(worker); |
| 72 | } |
| 73 | |
| 74 | void CompletionQueueAsyncWorker::Init(Handle<Object> exports) { |
| 75 | NanScope(); |
| 76 | queue = grpc_completion_queue_create(); |
| 77 | } |
| 78 | |
| 79 | void CompletionQueueAsyncWorker::HandleOKCallback() { |
| 80 | NanScope(); |
murgatroid99 | 1578c6a | 2015-02-11 16:19:55 -0800 | [diff] [blame] | 81 | NanCallback *callback = GetTagCallback(result->tag); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 82 | Handle<Value> argv[] = {NanNull(), GetTagNodeValue(result->tag)}; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 83 | |
murgatroid99 | 1578c6a | 2015-02-11 16:19:55 -0800 | [diff] [blame] | 84 | callback->Call(2, argv); |
murgatroid99 | 7765906 | 2015-02-11 09:26:25 -0800 | [diff] [blame] | 85 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 86 | DestroyTag(result->tag); |
| 87 | grpc_event_finish(result); |
| 88 | result = NULL; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void CompletionQueueAsyncWorker::HandleErrorCallback() { |
| 92 | NanScope(); |
murgatroid99 | 1578c6a | 2015-02-11 16:19:55 -0800 | [diff] [blame] | 93 | NanCallback *callback = GetTagCallback(result->tag); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 94 | Handle<Value> argv[] = {NanError(ErrorMessage())}; |
| 95 | |
murgatroid99 | 1578c6a | 2015-02-11 16:19:55 -0800 | [diff] [blame] | 96 | callback->Call(1, argv); |
murgatroid99 | 7765906 | 2015-02-11 09:26:25 -0800 | [diff] [blame] | 97 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 98 | DestroyTag(result->tag); |
| 99 | grpc_event_finish(result); |
| 100 | result = NULL; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace node |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame^] | 104 | } // namespace grpc |