murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 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 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 34 | #ifndef NET_GRPC_NODE_CALL_H_ |
| 35 | #define NET_GRPC_NODE_CALL_H_ |
| 36 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 37 | #include <memory> |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 38 | #include <vector> |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 39 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 40 | #include <node.h> |
| 41 | #include <nan.h> |
| 42 | #include "grpc/grpc.h" |
| 43 | |
| 44 | #include "channel.h" |
| 45 | |
murgatroid99 | e012366 | 2015-02-13 11:14:03 -0800 | [diff] [blame] | 46 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 47 | namespace grpc { |
| 48 | namespace node { |
| 49 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 50 | using std::unique_ptr; |
murgatroid99 | 17be589 | 2015-02-13 10:19:10 -0800 | [diff] [blame] | 51 | using std::shared_ptr; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 52 | |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 53 | v8::Handle<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array); |
| 54 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 55 | class PersistentHolder { |
| 56 | public: |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 57 | explicit PersistentHolder(v8::Persistent<v8::Value> persist) : |
| 58 | persist(persist) { |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ~PersistentHolder() { |
murgatroid99 | 7765906 | 2015-02-11 09:26:25 -0800 | [diff] [blame] | 62 | NanDisposePersistent(persist); |
| 63 | } |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 64 | |
| 65 | private: |
murgatroid99 | ff43c09 | 2015-02-09 11:41:23 -0800 | [diff] [blame] | 66 | v8::Persistent<v8::Value> persist; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
murgatroid99 | 17be589 | 2015-02-13 10:19:10 -0800 | [diff] [blame] | 69 | struct Resources { |
| 70 | std::vector<unique_ptr<NanUtf8String> > strings; |
| 71 | std::vector<unique_ptr<PersistentHolder> > handles; |
| 72 | }; |
| 73 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 74 | class Op { |
| 75 | public: |
murgatroid99 | ff43c09 | 2015-02-09 11:41:23 -0800 | [diff] [blame] | 76 | virtual v8::Handle<v8::Value> GetNodeValue() const = 0; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 77 | virtual bool ParseOp(v8::Handle<v8::Value> value, grpc_op *out, |
murgatroid99 | 17be589 | 2015-02-13 10:19:10 -0800 | [diff] [blame] | 78 | shared_ptr<Resources> resources) = 0; |
murgatroid99 | ff43c09 | 2015-02-09 11:41:23 -0800 | [diff] [blame] | 79 | v8::Handle<v8::Value> GetOpType() const; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 80 | |
| 81 | protected: |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 82 | virtual std::string GetTypeString() const = 0; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
murgatroid99 | e012366 | 2015-02-13 11:14:03 -0800 | [diff] [blame] | 85 | typedef std::vector<unique_ptr<Op>> OpVec; |
| 86 | |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 87 | struct tag { |
murgatroid99 | e012366 | 2015-02-13 11:14:03 -0800 | [diff] [blame] | 88 | tag(NanCallback *callback, OpVec *ops, |
murgatroid99 | 17be589 | 2015-02-13 10:19:10 -0800 | [diff] [blame] | 89 | shared_ptr<Resources> resources); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 90 | ~tag(); |
| 91 | NanCallback *callback; |
murgatroid99 | e012366 | 2015-02-13 11:14:03 -0800 | [diff] [blame] | 92 | OpVec *ops; |
murgatroid99 | 17be589 | 2015-02-13 10:19:10 -0800 | [diff] [blame] | 93 | shared_ptr<Resources> resources; |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 96 | v8::Handle<v8::Value> GetTagNodeValue(void *tag); |
| 97 | |
murgatroid99 | 1578c6a | 2015-02-11 16:19:55 -0800 | [diff] [blame] | 98 | NanCallback *GetTagCallback(void *tag); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 99 | |
| 100 | void DestroyTag(void *tag); |
| 101 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 102 | /* Wrapper class for grpc_call structs. */ |
| 103 | class 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 Tiller | e8e304e | 2015-01-13 14:41:29 -0800 | [diff] [blame] | 115 | Call(const Call &); |
| 116 | Call &operator=(const Call &); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 117 | |
| 118 | static NAN_METHOD(New); |
murgatroid99 | d4d67ad | 2015-02-09 10:43:21 -0800 | [diff] [blame] | 119 | static NAN_METHOD(StartBatch); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 120 | static NAN_METHOD(Cancel); |
murgatroid99 | 5f875d2 | 2015-03-04 11:28:37 -0800 | [diff] [blame^] | 121 | static NanCallback *constructor; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 122 | // 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 Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 131 | #endif // NET_GRPC_NODE_CALL_H_ |