blob: 4074f1509bfd9c8792f772fd97566c1d07d0ede1 [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#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
46namespace grpc {
47namespace node {
48
murgatroid99d4d67ad2015-02-09 10:43:21 -080049using std::unique_ptr;
murgatroid9917be5892015-02-13 10:19:10 -080050using std::shared_ptr;
murgatroid99d4d67ad2015-02-09 10:43:21 -080051
murgatroid99016bb502015-02-09 15:55:10 -080052v8::Handle<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
53
murgatroid99d4d67ad2015-02-09 10:43:21 -080054class PersistentHolder {
55 public:
murgatroid99016bb502015-02-09 15:55:10 -080056 explicit PersistentHolder(v8::Persistent<v8::Value> persist) :
57 persist(persist) {
murgatroid99d4d67ad2015-02-09 10:43:21 -080058 }
59
60 ~PersistentHolder() {
murgatroid9977659062015-02-11 09:26:25 -080061 NanDisposePersistent(persist);
62 }
murgatroid99d4d67ad2015-02-09 10:43:21 -080063
64 private:
murgatroid99ff43c092015-02-09 11:41:23 -080065 v8::Persistent<v8::Value> persist;
murgatroid99d4d67ad2015-02-09 10:43:21 -080066};
67
murgatroid9917be5892015-02-13 10:19:10 -080068struct Resources {
69 std::vector<unique_ptr<NanUtf8String> > strings;
70 std::vector<unique_ptr<PersistentHolder> > handles;
71};
72
murgatroid99d4d67ad2015-02-09 10:43:21 -080073class Op {
74 public:
murgatroid99ff43c092015-02-09 11:41:23 -080075 virtual v8::Handle<v8::Value> GetNodeValue() const = 0;
murgatroid99d4d67ad2015-02-09 10:43:21 -080076 virtual bool ParseOp(v8::Handle<v8::Value> value, grpc_op *out,
murgatroid9917be5892015-02-13 10:19:10 -080077 shared_ptr<Resources> resources) = 0;
murgatroid99ff43c092015-02-09 11:41:23 -080078 v8::Handle<v8::Value> GetOpType() const;
murgatroid99d4d67ad2015-02-09 10:43:21 -080079
80 protected:
murgatroid99016bb502015-02-09 15:55:10 -080081 virtual std::string GetTypeString() const = 0;
murgatroid99d4d67ad2015-02-09 10:43:21 -080082};
83
84struct tag {
85 tag(NanCallback *callback, std::vector<unique_ptr<Op> > *ops,
murgatroid9917be5892015-02-13 10:19:10 -080086 shared_ptr<Resources> resources);
murgatroid99d4d67ad2015-02-09 10:43:21 -080087 ~tag();
88 NanCallback *callback;
89 std::vector<unique_ptr<Op> > *ops;
murgatroid9917be5892015-02-13 10:19:10 -080090 shared_ptr<Resources> resources;
murgatroid99d4d67ad2015-02-09 10:43:21 -080091};
92
murgatroid99016bb502015-02-09 15:55:10 -080093v8::Handle<v8::Value> GetTagNodeValue(void *tag);
94
murgatroid991578c6a2015-02-11 16:19:55 -080095NanCallback *GetTagCallback(void *tag);
murgatroid99016bb502015-02-09 15:55:10 -080096
97void DestroyTag(void *tag);
98
murgatroid99e5061512015-01-12 18:14:35 -080099/* Wrapper class for grpc_call structs. */
100class Call : public ::node::ObjectWrap {
101 public:
102 static void Init(v8::Handle<v8::Object> exports);
103 static bool HasInstance(v8::Handle<v8::Value> val);
104 /* Wrap a grpc_call struct in a javascript object */
105 static v8::Handle<v8::Value> WrapStruct(grpc_call *call);
106
107 private:
108 explicit Call(grpc_call *call);
109 ~Call();
110
111 // Prevent copying
Craig Tillere8e304e2015-01-13 14:41:29 -0800112 Call(const Call &);
113 Call &operator=(const Call &);
murgatroid99e5061512015-01-12 18:14:35 -0800114
115 static NAN_METHOD(New);
murgatroid99d4d67ad2015-02-09 10:43:21 -0800116 static NAN_METHOD(StartBatch);
murgatroid99e5061512015-01-12 18:14:35 -0800117 static NAN_METHOD(Cancel);
murgatroid99e5061512015-01-12 18:14:35 -0800118 static v8::Persistent<v8::Function> constructor;
119 // Used for typechecking instances of this javascript class
120 static v8::Persistent<v8::FunctionTemplate> fun_tpl;
121
122 grpc_call *wrapped_call;
123};
124
125} // namespace node
126} // namespace grpc
127
128#endif // NET_GRPC_NODE_CALL_H_