blob: 43142c7091fe6c30b434a03d413c22f844853771 [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"
murgatroid99f28066b2015-03-04 14:42:31 -080043#include "grpc/support/log.h"
murgatroid99e5061512015-01-12 18:14:35 -080044
45#include "channel.h"
46
murgatroid99e0123662015-02-13 11:14:03 -080047
murgatroid99e5061512015-01-12 18:14:35 -080048namespace grpc {
49namespace node {
50
murgatroid99d4d67ad2015-02-09 10:43:21 -080051using std::unique_ptr;
murgatroid9917be5892015-02-13 10:19:10 -080052using std::shared_ptr;
murgatroid99d4d67ad2015-02-09 10:43:21 -080053
murgatroid99016bb502015-02-09 15:55:10 -080054v8::Handle<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
55
murgatroid99d4d67ad2015-02-09 10:43:21 -080056class PersistentHolder {
57 public:
murgatroid99f28066b2015-03-04 14:42:31 -080058 explicit PersistentHolder(v8::Persistent<v8::Value> *persist) :
murgatroid99016bb502015-02-09 15:55:10 -080059 persist(persist) {
murgatroid99d4d67ad2015-02-09 10:43:21 -080060 }
61
62 ~PersistentHolder() {
murgatroid99f28066b2015-03-04 14:42:31 -080063 NanDisposePersistent(*persist);
64 delete persist;
murgatroid9977659062015-02-11 09:26:25 -080065 }
murgatroid99d4d67ad2015-02-09 10:43:21 -080066
67 private:
murgatroid99f28066b2015-03-04 14:42:31 -080068 v8::Persistent<v8::Value> *persist;
murgatroid99d4d67ad2015-02-09 10:43:21 -080069};
70
murgatroid9917be5892015-02-13 10:19:10 -080071struct Resources {
72 std::vector<unique_ptr<NanUtf8String> > strings;
73 std::vector<unique_ptr<PersistentHolder> > handles;
74};
75
murgatroid99d4d67ad2015-02-09 10:43:21 -080076class Op {
77 public:
murgatroid99ff43c092015-02-09 11:41:23 -080078 virtual v8::Handle<v8::Value> GetNodeValue() const = 0;
murgatroid99d4d67ad2015-02-09 10:43:21 -080079 virtual bool ParseOp(v8::Handle<v8::Value> value, grpc_op *out,
murgatroid9917be5892015-02-13 10:19:10 -080080 shared_ptr<Resources> resources) = 0;
murgatroid99ff43c092015-02-09 11:41:23 -080081 v8::Handle<v8::Value> GetOpType() const;
murgatroid99d4d67ad2015-02-09 10:43:21 -080082
83 protected:
murgatroid99016bb502015-02-09 15:55:10 -080084 virtual std::string GetTypeString() const = 0;
murgatroid99d4d67ad2015-02-09 10:43:21 -080085};
86
murgatroid99e0123662015-02-13 11:14:03 -080087typedef std::vector<unique_ptr<Op>> OpVec;
88
murgatroid99d4d67ad2015-02-09 10:43:21 -080089struct tag {
murgatroid99e0123662015-02-13 11:14:03 -080090 tag(NanCallback *callback, OpVec *ops,
murgatroid9917be5892015-02-13 10:19:10 -080091 shared_ptr<Resources> resources);
murgatroid99d4d67ad2015-02-09 10:43:21 -080092 ~tag();
93 NanCallback *callback;
murgatroid99e0123662015-02-13 11:14:03 -080094 OpVec *ops;
murgatroid9917be5892015-02-13 10:19:10 -080095 shared_ptr<Resources> resources;
murgatroid99d4d67ad2015-02-09 10:43:21 -080096};
97
murgatroid99016bb502015-02-09 15:55:10 -080098v8::Handle<v8::Value> GetTagNodeValue(void *tag);
99
murgatroid991578c6a2015-02-11 16:19:55 -0800100NanCallback *GetTagCallback(void *tag);
murgatroid99016bb502015-02-09 15:55:10 -0800101
102void DestroyTag(void *tag);
103
murgatroid99e5061512015-01-12 18:14:35 -0800104/* Wrapper class for grpc_call structs. */
105class Call : public ::node::ObjectWrap {
106 public:
107 static void Init(v8::Handle<v8::Object> exports);
108 static bool HasInstance(v8::Handle<v8::Value> val);
109 /* Wrap a grpc_call struct in a javascript object */
110 static v8::Handle<v8::Value> WrapStruct(grpc_call *call);
111
112 private:
113 explicit Call(grpc_call *call);
114 ~Call();
115
116 // Prevent copying
Craig Tillere8e304e2015-01-13 14:41:29 -0800117 Call(const Call &);
118 Call &operator=(const Call &);
murgatroid99e5061512015-01-12 18:14:35 -0800119
120 static NAN_METHOD(New);
murgatroid99d4d67ad2015-02-09 10:43:21 -0800121 static NAN_METHOD(StartBatch);
murgatroid99e5061512015-01-12 18:14:35 -0800122 static NAN_METHOD(Cancel);
murgatroid995f875d22015-03-04 11:28:37 -0800123 static NanCallback *constructor;
murgatroid99e5061512015-01-12 18:14:35 -0800124 // Used for typechecking instances of this javascript class
125 static v8::Persistent<v8::FunctionTemplate> fun_tpl;
126
127 grpc_call *wrapped_call;
128};
129
130} // namespace node
131} // namespace grpc
132
Craig Tiller190d3602015-02-18 09:23:38 -0800133#endif // NET_GRPC_NODE_CALL_H_