Added most of the plugin implementation

This commit is contained in:
murgatroid99 2015-09-24 10:54:55 -07:00
parent 2a29cd291f
commit 0f10c60783
2 changed files with 54 additions and 1 deletions

View File

@ -242,5 +242,34 @@ NAN_METHOD(Credentials::CreateInsecure) {
info.GetReturnValue().Set(WrapStruct(NULL));
}
NAN_METHOD(Credentials::CreateFromPlugin) {
if (!info[0]->IsFunction()) {
return Nan::ThrowTypeError(
"createFromPlugin's argument must be a function");
}
grpc_metadata_credentials_plugin plugin;
plugin_state *state = new plugin_state;
state->callback = new Nan::Callback(info[0].As<Function>());
plugin.get_metadata = plugin_get_metadata;
plugin.destroy = plugin_destroy_state;
plugin.state = reinterpret_cast<void*>(state);
grpc_credentials *creds = grpc_metadata_credentials_create_from_plugin(plugin,
NULL);
if (creds == NULL) {
info.GetReturnValue().SetNull();
} else {
info.GetReturnValue().Set(WrapStruct(creds()));
}
}
void plugin_get_metadata(void *state, const char *service_url,
grpc_credentials_plugin_metadata_cb cb,
void *user_data) {
uv_async_t *async = new uv_async_t;
uv_async_init(uv_default_loop(),
async,
PluginCallback);
}
} // namespace node
} // namespace grpc

View File

@ -83,13 +83,37 @@ typedef struct plugin_state {
Nan::Callback *callback;
} plugin_state;
typedef struct plugin_callback_data {
plugin_state *state;
const char *service_url;
grpc_credentials_plugin_metadata_cb cb;
void *user_data;
} plugin_callback_data;
void plugin_get_metadata(void *state, const char *service_url,
grpc_credentials_plugin_metadata_cb cb, void *user_data);
grpc_credentials_plugin_metadata_cb cb,
void *user_data);
void plugin_destroy_state(void *state);
static NAN_METHOD(PluginCallback);
NAN_INLINE NAUV_WORK_CB(SendPluginCallback) {
Nan::HandleScope scope;
plugin_callback_data *data = reinterpret_cast<plugin_callback_data>(
async->data);
v8::Local<v8::Function> plugin_callback = Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(PluginCallback).ToLocalChecked());
// Attach cb and user_data to plugin_callback so that it can access them later
const int argc = 2;
v8::Local<v8::Value> argv = {Nan::New(data->service_url).ToLocalChecked(),
plugin_callback};
NanCallback *callback = static_cast<NanCallback*>(async->data);
callback->Call(argc, argv);
uv_unref((uv_handle_t *)async);
delete async;
}
} // namespace node
} // namespace grpc