VIX API Concepts : Monitoring a Job Object

Monitoring a Job Object

Currently, a client application can use several mechanisms to detect when a job object has been signaled:

A typical asynchronous call looks similar to VixVM_Open(), and there are several common patterns shared by all asynchronous Vix calls. For reference, the following code shows the signature of VixVM_Open().

VixHandle VixVM_Open(VixHandle hostHandle,
                     const char *vmxFilePathName,
                     VixEventProc *callbackProc,
                     void *clientData);

This function creates a Vix job object and returns a VixHandle for this new job object. The caller is responsible for releasing this job object, even if the job object is not used for anything else.

Polling the Job Object for Completion

The job object tracks the status of an asynchronous operation. You can interrogate the completion status of a job object using the function VixJob_CheckCompletion(). This is a non-blocking function that returns a Boolean value representing the completion state of the asynchronous operation. The following example shows the use of VixJob_CheckCompletion() in a polling loop.

  1. Example 2-10.
Bool OpenVMWithPolling(const VixHandle hostHandle,
                       const char *vmName,
                       VixHandle *vmHandle)
{
   VixError err = VIX_OK;
   VixHandle jobHandle = VIX_INVALID_HANDLE;
   Bool completed = FALSE;
   
   if (!vmHandle) {
      return FALSE;
   }
   *vmHandle = VIX_INVALID_HANDLE;
 
   // Start asynchronous operation.
   jobHandle = VixVM_Open(hostHandle,
                          vmName,
                          NULL, // callbackProc,
                          NULL); // clientData
         
   // Poll the job object for completion of asynchronous operation.
   while ( !completed ) {
      sleep(1);
      
      err = VixJob_CheckCompletion(jobHandle, &completed);
      if (VIX_OK != err) {
         Vix_ReleaseHandle(jobHandle);
         return FALSE;
      }
   }
   
   err = Vix_GetProperties(jobHandle,
                           VIX_PROPERTY_JOB_RESULT_HANDLE,
                           vmHandle,
                           VIX_PROPERTY_NONE);
   if (VIX_OK != err) {
      Vix_ReleaseHandle(jobHandle);
      *vmHandle = VIX_INVALID_HANDLE;
      return FALSE;
   }
   
   Vix_ReleaseHandle(jobHandle);
   return TRUE;
   // Caller must release vmHandle.
}

Using the Job Object to Block Calls

The job object allows a client to block until an asynchronous call completes. This achieves the same result as if the asynchronous call were a synchronous call. Here is an example of how this can be done, using the VixJob_Wait() function.

  1. Example 2-11.
VixHandle hostHandle = VIX_INVALID_HANDLE;
VixHandle jobHandle = VIX_INVALID_HANDLE;
VixHandle vmHandle = VIX_INVALID_HANDLE;
 
jobHandle = VixHost_Connect(VIX_API_VERSION,
                            VIX_SERVICEPROVIDER_VMWARE_VI_SERVER,
                            "https://server2.example.com/sdk", // hostName
                            0, // hostPort
                            "root", // username
                            "secretpw", // password
                            0, // options
                            VIX_INVALID_HANDLE, // propertyListHandle
                            NULL, // callbackProc
                            NULL); // clientData
 
// Wait for completion of operation.
err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
if (VIX_OK != err) {
   goto abort;
}
 
// Release handle when done.
Vix_ReleaseHandle(jobHandle);
jobHandle = VIX_INVALID_HANDLE;

Retrieving Results from Job Object Properties

The job object can also be used to retrieve results from an asynchronous operation once the asynchronous operation has completed. You can get these properties by calling Vix_GetProperties() on the job handle. The following example shows how this is done.

  1. Example 2-12.
VixHandle hostHandle = VIX_INVALID_HANDLE;
VixHandle jobHandle = VIX_INVALID_HANDLE;
VixHandle vmHandle = VIX_INVALID_HANDLE;
 
jobHandle = VixHost_Connect(VIX_API_VERSION,
                            VIX_SERVICEPROVIDER_VMWARE_VI_SERVER,
                            "https://server2.example..com/sdk", // hostName
                            0, // hostPort
                            "root", // username
                            "secretpw", // password
                            0, // options
                            VIX_INVALID_HANDLE, // propertyListHandle
                            NULL, // callbackProc
                            NULL); // clientData
 
// Wait for completion of operation.
err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);
if (VIX_OK != err) {
   goto abort;
}
 
// Collect desired result of operation.
err = Vix_GetProperties(jobHandle, 
                        VIX_PROPERTY_JOB_RESULT_HANDLE,
                        &hostHandle,
                        VIX_PROPERTY_NONE);
 
// Release handle when done.
Vix_ReleaseHandle(jobHandle); 
jobHandle = VIX_INVALID_HANDLE;

For convenience, you can also extract properties from the job object with the VixJob_Wait() function. The following example shows how this is done.

  1. Example 2-13.
// Start asynchronous operation.
jobHandle = VixVM_Open(hostHandle,
                       "[standard] WindowsXP/WindowsXP.vmx",
                       NULL, // callbackProc
                       NULL); // clientData
 
// Wait for completion of operation. Collect result handle.
err = VixJob_Wait(jobHandle,
                  VIX_PROPERTY_JOB_RESULT_HANDLE,
                  &vmHandle,
                  VIX_PROPERTY_NONE);
 
// Release handle when done.
Vix_ReleaseHandle(jobHandle); 
jobHandle = VIX_INVALID_HANDLE;

For simplicity, most of the examples in this document use the approach of calling VixJob_Wait() and requesting the desired properties at the same time. Some kinds of applications might increase responsiveness by using callback functions in a multithreaded client.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值