|
13 | 13 | #include "network_filter.h" |
14 | 14 | #include "interface.h" |
15 | 15 | #include "hypervisor.h" |
| 16 | +#include "worker.h" |
16 | 17 |
|
17 | 18 | namespace NLV { |
18 | 19 |
|
@@ -229,64 +230,45 @@ NAN_METHOD(Hypervisor::New) |
229 | 230 | return info.GetReturnValue().Set(info.This()); |
230 | 231 | } |
231 | 232 |
|
232 | | -int Hypervisor::ConnectWorker::auth_callback(virConnectCredentialPtr cred, |
233 | | - unsigned int ncred, void *data) |
234 | | -{ |
235 | | - Hypervisor *hypervisor = static_cast<ConnectWorker*>(data)->hypervisor_; |
236 | | - |
237 | | - for (unsigned int i = 0; i < ncred; ++i) { |
238 | | - switch (cred[i].type) { |
239 | | - case VIR_CRED_AUTHNAME: |
240 | | - cred[i].result = strdup(hypervisor->username_.c_str()); |
241 | | - if (cred[i].result == NULL) |
242 | | - return -1; |
243 | | - cred[i].resultlen = strlen(cred[i].result); |
244 | | - break; |
245 | | - |
246 | | - case VIR_CRED_PASSPHRASE: |
247 | | - cred[i].result = strdup(hypervisor->password_.c_str()); |
248 | | - if (cred[i].result == NULL) |
249 | | - return -1; |
250 | | - cred[i].resultlen = strlen(cred[i].result); |
251 | | - break; |
252 | | - } |
253 | | - } |
254 | | - |
255 | | - return 0; |
256 | | -} |
257 | | - |
258 | 233 | NAN_METHOD(Hypervisor::Connect) |
259 | 234 | { |
260 | | - Nan::HandleScope scope; |
261 | | - if (info.Length() == 1 && !info[0]->IsFunction()) { |
262 | | - Nan::ThrowTypeError("You must specify a function as first argument"); |
263 | | - return; |
264 | | - } |
265 | | - |
266 | | - Nan::Callback *callback = new Nan::Callback(info[0].As<Function>()); |
267 | | - Hypervisor *hv = Nan::ObjectWrap::Unwrap<Hypervisor>(info.This()); |
268 | | - Nan::AsyncQueueWorker(new ConnectWorker(callback, hv)); |
269 | | - return; |
270 | | -} |
271 | | - |
272 | | -NLV_WORKER_EXECUTE(Hypervisor, Connect) |
273 | | -{ |
274 | | - static int supported_cred_types[] = { |
275 | | - VIR_CRED_AUTHNAME, |
276 | | - VIR_CRED_PASSPHRASE, |
277 | | - }; |
278 | | - |
279 | | - virConnectAuth auth; |
280 | | - auth.credtype = supported_cred_types; |
281 | | - auth.ncredtype = sizeof(supported_cred_types) / sizeof(int); |
282 | | - auth.cb = ConnectWorker::auth_callback; |
283 | | - auth.cbdata = this; |
284 | | - |
285 | | - hypervisor_->handle_ = |
286 | | - virConnectOpenAuth((const char*) hypervisor_->uri_.c_str(), &auth, |
287 | | - hypervisor_->readOnly_ ? VIR_CONNECT_RO : 0); |
288 | | - if (hypervisor_->handle_ == NULL) |
289 | | - SetVirError(virSaveLastError()); |
| 235 | + Hypervisor *hv = Hypervisor::Unwrap(info.This()); |
| 236 | + std::string uri = hv->uri_; |
| 237 | + Worker::RunAsync(info, [=](Worker::SetOnFinishedHandler onFinished ) { |
| 238 | + static int supported_cred_types[] = { |
| 239 | + VIR_CRED_AUTHNAME, |
| 240 | + VIR_CRED_PASSPHRASE, |
| 241 | + }; |
| 242 | + |
| 243 | + virConnectAuth auth; |
| 244 | + auth.credtype = supported_cred_types; |
| 245 | + auth.ncredtype = sizeof(supported_cred_types) / sizeof(int); |
| 246 | + auth.cb = [](virConnectCredentialPtr cred, unsigned int ncred, void *data) { |
| 247 | + Hypervisor *hypervisor = static_cast<Hypervisor*>(data); |
| 248 | + for (unsigned int i = 0; i < ncred; ++i) { |
| 249 | + switch (cred[i].type) { |
| 250 | + case VIR_CRED_AUTHNAME: |
| 251 | + cred[i].result = strdup(hypervisor->username_.c_str()); |
| 252 | + if (cred[i].result == NULL) |
| 253 | + return -1; |
| 254 | + cred[i].resultlen = strlen(cred[i].result); |
| 255 | + break; |
| 256 | + |
| 257 | + case VIR_CRED_PASSPHRASE: |
| 258 | + cred[i].result = strdup(hypervisor->password_.c_str()); |
| 259 | + if (cred[i].result == NULL) |
| 260 | + return -1; |
| 261 | + cred[i].resultlen = strlen(cred[i].result); |
| 262 | + break; |
| 263 | + } |
| 264 | + } |
| 265 | + return 0; |
| 266 | + }; |
| 267 | + auth.cbdata = hv; |
| 268 | + hv->handle_ = virConnectOpenAuth(uri.c_str(), &auth, |
| 269 | + hv->readOnly_ ? VIR_CONNECT_RO : 0); |
| 270 | + return onFinished(PrimitiveReturnHandler(true)); |
| 271 | + }); |
290 | 272 | } |
291 | 273 |
|
292 | 274 | NAN_METHOD(Hypervisor::Disconnect) |
|
0 commit comments