blob: dbacdf034edd6c9b03d5e050466291d24cf654bf [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
3 * Copyright 2014, Google Inc.
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
murgatroid99e5061512015-01-12 18:14:35 -080034#include <node.h>
35#include <nan.h>
36
37#include "grpc/grpc.h"
murgatroid9977659062015-02-11 09:26:25 -080038#include "grpc/support/log.h"
murgatroid99e5061512015-01-12 18:14:35 -080039#include "grpc/support/time.h"
40#include "completion_queue_async_worker.h"
murgatroid99016bb502015-02-09 15:55:10 -080041#include "call.h"
murgatroid99e5061512015-01-12 18:14:35 -080042
43namespace grpc {
44namespace node {
45
46using v8::Function;
47using v8::Handle;
48using v8::Object;
49using v8::Persistent;
50using v8::Value;
51
52grpc_completion_queue *CompletionQueueAsyncWorker::queue;
53
Craig Tillere8e304e2015-01-13 14:41:29 -080054CompletionQueueAsyncWorker::CompletionQueueAsyncWorker()
55 : NanAsyncWorker(NULL) {}
murgatroid99e5061512015-01-12 18:14:35 -080056
Craig Tillere8e304e2015-01-13 14:41:29 -080057CompletionQueueAsyncWorker::~CompletionQueueAsyncWorker() {}
murgatroid99e5061512015-01-12 18:14:35 -080058
59void CompletionQueueAsyncWorker::Execute() {
60 result = grpc_completion_queue_next(queue, gpr_inf_future);
murgatroid9977659062015-02-11 09:26:25 -080061 gpr_log(GPR_DEBUG, "Handling response on call %p", result->call);
murgatroid99d4d67ad2015-02-09 10:43:21 -080062 if (result->data.op_complete != GRPC_OP_OK) {
63 SetErrorMessage("The batch encountered an error");
64 }
murgatroid99e5061512015-01-12 18:14:35 -080065}
66
Craig Tillere8e304e2015-01-13 14:41:29 -080067grpc_completion_queue *CompletionQueueAsyncWorker::GetQueue() { return queue; }
murgatroid99e5061512015-01-12 18:14:35 -080068
69void CompletionQueueAsyncWorker::Next() {
70 NanScope();
71 CompletionQueueAsyncWorker *worker = new CompletionQueueAsyncWorker();
72 NanAsyncQueueWorker(worker);
73}
74
75void CompletionQueueAsyncWorker::Init(Handle<Object> exports) {
76 NanScope();
77 queue = grpc_completion_queue_create();
78}
79
80void CompletionQueueAsyncWorker::HandleOKCallback() {
81 NanScope();
murgatroid9977659062015-02-11 09:26:25 -080082 gpr_log(GPR_DEBUG, "Handling response on call %p", result->call);
murgatroid99d4d67ad2015-02-09 10:43:21 -080083 NanCallback callback = GetTagCallback(result->tag);
murgatroid99016bb502015-02-09 15:55:10 -080084 Handle<Value> argv[] = {NanNull(), GetTagNodeValue(result->tag)};
murgatroid99e5061512015-01-12 18:14:35 -080085
murgatroid9977659062015-02-11 09:26:25 -080086 callback.Call(2, argv);
87
murgatroid99e5061512015-01-12 18:14:35 -080088 DestroyTag(result->tag);
89 grpc_event_finish(result);
90 result = NULL;
murgatroid99d4d67ad2015-02-09 10:43:21 -080091}
92
93void CompletionQueueAsyncWorker::HandleErrorCallback() {
94 NanScope();
95 NanCallback callback = GetTagCallback(result->tag);
96 Handle<Value> argv[] = {NanError(ErrorMessage())};
97
murgatroid9977659062015-02-11 09:26:25 -080098 callback.Call(1, argv);
99
murgatroid99d4d67ad2015-02-09 10:43:21 -0800100 DestroyTag(result->tag);
101 grpc_event_finish(result);
102 result = NULL;
murgatroid99e5061512015-01-12 18:14:35 -0800103}
104
105} // namespace node
106} // namespace grpc