blob: 2e9e1c5bf37e8074f0db3dd649edff027acc541b [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
murgatroid99749666e2015-01-12 18:25:58 -08004 * 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#ifndef NET_GRPC_NODE_CALL_H_
35#define NET_GRPC_NODE_CALL_H_
36
murgatroid99d4d67ad2015-02-09 10:43:21 -080037#include <memory>
murgatroid99016bb502015-02-09 15:55:10 -080038#include <vector>
murgatroid99d4d67ad2015-02-09 10:43:21 -080039
murgatroid99e5061512015-01-12 18:14:35 -080040#include <node.h>
41#include <nan.h>
42#include "grpc/grpc.h"
43
44#include "channel.h"
45
murgatroid99e0123662015-02-13 11:14:03 -080046
murgatroid99e5061512015-01-12 18:14:35 -080047namespace grpc {
48namespace node {
49
murgatroid99d4d67ad2015-02-09 10:43:21 -080050using std::unique_ptr;
murgatroid9917be5892015-02-13 10:19:10 -080051using std::shared_ptr;
murgatroid99d4d67ad2015-02-09 10:43:21 -080052
murgatroid99016bb502015-02-09 15:55:10 -080053v8::Handle<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
54
murgatroid99d4d67ad2015-02-09 10:43:21 -080055class PersistentHolder {
56 public:
murgatroid99016bb502015-02-09 15:55:10 -080057 explicit PersistentHolder(v8::Persistent<v8::Value> persist) :
58 persist(persist) {
murgatroid99d4d67ad2015-02-09 10:43:21 -080059 }
60
61 ~PersistentHolder() {
murgatroid9977659062015-02-11 09:26:25 -080062 NanDisposePersistent(persist);
63 }
murgatroid99d4d67ad2015-02-09 10:43:21 -080064
65 private:
murgatroid99ff43c092015-02-09 11:41:23 -080066 v8::Persistent<v8::Value> persist;
murgatroid99d4d67ad2015-02-09 10:43:21 -080067};
68
murgatroid9917be5892015-02-13 10:19:10 -080069struct Resources {
70 std::vector<unique_ptr<NanUtf8String> > strings;
71 std::vector<unique_ptr<PersistentHolder> > handles;
72};
73
murgatroid99d4d67ad2015-02-09 10:43:21 -080074class Op {
75 public:
murgatroid99ff43c092015-02-09 11:41:23 -080076 virtual v8::Handle<v8::Value> GetNodeValue() const = 0;
murgatroid99d4d67ad2015-02-09 10:43:21 -080077 virtual bool ParseOp(v8::Handle<v8::Value> value, grpc_op *out,
murgatroid9917be5892015-02-13 10:19:10 -080078 shared_ptr<Resources> resources) = 0;
murgatroid99ff43c092015-02-09 11:41:23 -080079 v8::Handle<v8::Value> GetOpType() const;
murgatroid99d4d67ad2015-02-09 10:43:21 -080080
81 protected:
murgatroid99016bb502015-02-09 15:55:10 -080082 virtual std::string GetTypeString() const = 0;
murgatroid99d4d67ad2015-02-09 10:43:21 -080083};
84
murgatroid99e0123662015-02-13 11:14:03 -080085typedef std::vector<unique_ptr<Op>> OpVec;
86
murgatroid99d4d67ad2015-02-09 10:43:21 -080087struct tag {
murgatroid99e0123662015-02-13 11:14:03 -080088 tag(NanCallback *callback, OpVec *ops,
murgatroid9917be5892015-02-13 10:19:10 -080089 shared_ptr<Resources> resources);
murgatroid99d4d67ad2015-02-09 10:43:21 -080090 ~tag();
91 NanCallback *callback;
murgatroid99e0123662015-02-13 11:14:03 -080092 OpVec *ops;
murgatroid9917be5892015-02-13 10:19:10 -080093 shared_ptr<Resources> resources;
murgatroid99d4d67ad2015-02-09 10:43:21 -080094};
95
murgatroid99016bb502015-02-09 15:55:10 -080096v8::Handle<v8::Value> GetTagNodeValue(void *tag);
97
murgatroid991578c6a2015-02-11 16:19:55 -080098NanCallback *GetTagCallback(void *tag);
murgatroid99016bb502015-02-09 15:55:10 -080099
100void DestroyTag(void *tag);
101
murgatroid99e5061512015-01-12 18:14:35 -0800102/* Wrapper class for grpc_call structs. */
103class Call : public ::node::ObjectWrap {
104 public:
105 static void Init(v8::Handle<v8::Object> exports);
106 static bool HasInstance(v8::Handle<v8::Value> val);
107 /* Wrap a grpc_call struct in a javascript object */
108 static v8::Handle<v8::Value> WrapStruct(grpc_call *call);
109
110 private:
111 explicit Call(grpc_call *call);
112 ~Call();
113
114 // Prevent copying
Craig Tillere8e304e2015-01-13 14:41:29 -0800115 Call(const Call &);
116 Call &operator=(const Call &);
murgatroid99e5061512015-01-12 18:14:35 -0800117
118 static NAN_METHOD(New);
murgatroid99d4d67ad2015-02-09 10:43:21 -0800119 static NAN_METHOD(StartBatch);
murgatroid99e5061512015-01-12 18:14:35 -0800120 static NAN_METHOD(Cancel);
murgatroid995f875d22015-03-04 11:28:37 -0800121 static NanCallback *constructor;
murgatroid99e5061512015-01-12 18:14:35 -0800122 // Used for typechecking instances of this javascript class
123 static v8::Persistent<v8::FunctionTemplate> fun_tpl;
124
125 grpc_call *wrapped_call;
126};
127
128} // namespace node
129} // namespace grpc
130
Craig Tiller190d3602015-02-18 09:23:38 -0800131#endif // NET_GRPC_NODE_CALL_H_