diff options
author | murgatroid99 <mlumish@google.com> | 2015-07-28 15:18:57 -0700 |
---|---|---|
committer | murgatroid99 <mlumish@google.com> | 2015-07-28 15:18:57 -0700 |
commit | c7f4d4fb84566992bc89d2ffceefdec1a2e83040 (patch) | |
tree | 64e813e8127ec9efb3cce41a1d8ccda7bdfd1037 /src/node/ext/channel.cc | |
parent | 8e06c2e62e0be2606598dd1f2a6582b585364520 (diff) |
Wrap connectivity API, expose it to client as waitForReady
Diffstat (limited to 'src/node/ext/channel.cc')
-rw-r--r-- | src/node/ext/channel.cc | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/node/ext/channel.cc b/src/node/ext/channel.cc index c43b55f115..fa99c10d22 100644 --- a/src/node/ext/channel.cc +++ b/src/node/ext/channel.cc @@ -33,12 +33,17 @@ #include <vector> +#include "grpc/support/log.h" + #include <node.h> #include <nan.h> #include "grpc/grpc.h" #include "grpc/grpc_security.h" +#include "call.h" #include "channel.h" +#include "completion_queue_async_worker.h" #include "credentials.h" +#include "timeval.h" namespace grpc { namespace node { @@ -51,11 +56,31 @@ using v8::Handle; using v8::HandleScope; using v8::Integer; using v8::Local; +using v8::Number; using v8::Object; using v8::Persistent; using v8::String; using v8::Value; +class ConnectivityStateOp : public Op { + public: + Handle<Value> GetNodeValue() const { + return NanNew<Number>(new_state); + } + + bool ParseOp(Handle<Value> value, grpc_op *out, + shared_ptr<Resources> resources) { + return true; + } + + grpc_connectivity_state new_state; + + protected: + std::string GetTypeString() const { + return "new_state"; + } +}; + NanCallback *Channel::constructor; Persistent<FunctionTemplate> Channel::fun_tpl; @@ -78,6 +103,12 @@ void Channel::Init(Handle<Object> exports) { NanNew<FunctionTemplate>(Close)->GetFunction()); NanSetPrototypeTemplate(tpl, "getTarget", NanNew<FunctionTemplate>(GetTarget)->GetFunction()); + NanSetPrototypeTemplate( + tpl, "getConnectivityState", + NanNew<FunctionTemplate>(GetConnectivityState)->GetFunction()); + NanSetPrototypeTemplate( + tpl, "watchConnectivityState", + NanNew<FunctionTemplate>(WatchConnectivityState)->GetFunction()); NanAssignPersistent(fun_tpl, tpl); Handle<Function> ctr = tpl->GetFunction(); constructor = new NanCallback(ctr); @@ -196,5 +227,54 @@ NAN_METHOD(Channel::GetTarget) { NanReturnValue(NanNew(grpc_channel_get_target(channel->wrapped_channel))); } +NAN_METHOD(Channel::GetConnectivityState) { + NanScope(); + if (!HasInstance(args.This())) { + return NanThrowTypeError( + "getConnectivityState can only be called on Channel objects"); + } + Channel *channel = ObjectWrap::Unwrap<Channel>(args.This()); + int try_to_connect = (int)args[0]->Equals(NanTrue()); + NanReturnValue(grpc_channel_check_connectivity_state(channel->wrapped_channel, + try_to_connect)); +} + +NAN_METHOD(Channel::WatchConnectivityState) { + NanScope(); + if (!HasInstance(args.This())) { + return NanThrowTypeError( + "watchConnectivityState can only be called on Channel objects"); + } + if (!args[0]->IsUint32()) { + return NanThrowTypeError( + "watchConnectivityState's first argument must be a channel state"); + } + if (!(args[1]->IsNumber() || args[1]->IsDate())) { + return NanThrowTypeError( + "watchConnectivityState's second argument must be a date or a number"); + } + if (!args[2]->IsFunction()) { + return NanThrowTypeError( + "watchConnectivityState's third argument must be a callback"); + } + grpc_connectivity_state last_state = + static_cast<grpc_connectivity_state>(args[0]->Uint32Value()); + double deadline = args[1]->NumberValue(); + Handle<Function> callback_func = args[2].As<Function>(); + NanCallback *callback = new NanCallback(callback_func); + Channel *channel = ObjectWrap::Unwrap<Channel>(args.This()); + ConnectivityStateOp *op = new ConnectivityStateOp(); + unique_ptr<OpVec> ops(new OpVec()); + ops->push_back(unique_ptr<Op>(op)); + grpc_channel_watch_connectivity_state( + channel->wrapped_channel, last_state, &op->new_state, + MillisecondsToTimespec(deadline), CompletionQueueAsyncWorker::GetQueue(), + new struct tag(callback, + ops.release(), + shared_ptr<Resources>(nullptr))); + CompletionQueueAsyncWorker::Next(); + NanReturnUndefined(); +} + } // namespace node } // namespace grpc |