gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
call.h
1 /*
2  *
3  * Copyright 2015, 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 
34 #ifndef NET_GRPC_NODE_CALL_H_
35 #define NET_GRPC_NODE_CALL_H_
36 
37 #include <memory>
38 #include <vector>
39 
40 #include <node.h>
41 #include <nan.h>
42 #include "grpc/grpc.h"
43 #include "grpc/support/log.h"
44 
45 #include "channel.h"
46 
47 
48 namespace grpc {
49 namespace node {
50 
51 using std::unique_ptr;
52 using std::shared_ptr;
53 
54 v8::Handle<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array);
55 
57  public:
58  explicit PersistentHolder(v8::Persistent<v8::Value> *persist) :
59  persist(persist) {
60  }
61 
62  ~PersistentHolder() {
63  NanDisposePersistent(*persist);
64  delete persist;
65  }
66 
67  private:
68  v8::Persistent<v8::Value> *persist;
69 };
70 
71 struct Resources {
72  std::vector<unique_ptr<NanUtf8String> > strings;
73  std::vector<unique_ptr<PersistentHolder> > handles;
74 };
75 
76 class Op {
77  public:
78  virtual v8::Handle<v8::Value> GetNodeValue() const = 0;
79  virtual bool ParseOp(v8::Handle<v8::Value> value, grpc_op *out,
80  shared_ptr<Resources> resources) = 0;
81  v8::Handle<v8::Value> GetOpType() const;
82 
83  protected:
84  virtual std::string GetTypeString() const = 0;
85 };
86 
87 typedef std::vector<unique_ptr<Op>> OpVec;
88 
89 struct tag {
90  tag(NanCallback *callback, OpVec *ops,
91  shared_ptr<Resources> resources);
92  ~tag();
93  NanCallback *callback;
94  OpVec *ops;
95  shared_ptr<Resources> resources;
96 };
97 
98 v8::Handle<v8::Value> GetTagNodeValue(void *tag);
99 
100 NanCallback *GetTagCallback(void *tag);
101 
102 void DestroyTag(void *tag);
103 
104 /* Wrapper class for grpc_call structs. */
105 class 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
117  Call(const Call &);
118  Call &operator=(const Call &);
119 
120  static NAN_METHOD(New);
121  static NAN_METHOD(StartBatch);
122  static NAN_METHOD(Cancel);
123  static NanCallback *constructor;
124  // 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 
133 #endif // NET_GRPC_NODE_CALL_H_
Definition: grpc.h:207
Definition: call.h:76
Definition: grpc.h:258
Definition: call.h:56
Definition: call.h:89
Definition: call.h:105
Definition: call.h:71
Definition: call.c:128