VIX API Concepts : Callback Functions

Callback Functions

All asynchronous Vix functions, such as VixVM_Open(), include a parameter for a callback procedure and a parameter that is passed to the callback procedure. These parameters are optional, so a caller can pass NULL for either. The prototype of this callback procedure parameter is:

typedef void (VixEventProc)(VixHandle handle,
                            VixEventType eventType,
                            VixHandle moreEventInfo,
                            void *clientData);

Using a Callback Function

If the caller provides a callback procedure, that procedure is registered with the job object and is invoked when the job object is signaled. For example, if a caller passes a callback procedure to VixVM_Open(), that callback procedure is invoked when the virtual machine has been opened. This situation could happen either before or after VixVM_Open() returns. It also can happen on any thread.

This mechanism allows the Vix function to complete asynchronously, so the application should not call VixJob_Wait() when using a callback function.

When a callback procedure is invoked, it is passed the clientData parameter that was passed in the original call to the asynchronous function. This allows a callback procedure to associate some context with an outstanding asynchronous call.

  1. Example 2-14.
void myCallback(VixHandle jobHandle,
                VixEventType eventType,
                VixHandle moreEventInfo,
                void *clientData)
{
   VixError err;
   VixError asyncErr;
   VixHandle vmHandle = VIX_INVALID_HANDLE;
 
   /*
    * Ignore progress callbacks. Check only for final signal.
    */
   if (VIX_EVENTTYPE_JOB_COMPLETED != eventType) {
      return;
   }
    
   err = Vix_GetProperties(jobHandle,
                           VIX_PROPERTY_JOB_RESULT_HANDLE,
                           &vmHandle,
                           VIX_PROPERTY_JOB_RESULT_ERROR_CODE,
                           &asyncErr,
                           VIX_PROPERTY_NONE);
    
   if (VIX_OK != asyncErr) {
      /*
       * The open failed.
       */
   }
}
  
int main()
{
   VixError err = VIX_OK;
   VixHandle hostHandle = VIX_INVALID_HANDLE;
   VixHandle jobHandle = VIX_INVALID_HANDLE;
   VixHandle vmHandle = VIX_INVALID_HANDLE;
   char *contextData = "Hello, Vix";
 
   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
 
   // Block for host connection to complete.
   err = VixJob_Wait(jobHandle, 
                     VIX_PROPERTY_JOB_RESULT_HANDLE,
                     &hostHandle,
                     VIX_PROPERTY_NONE);
   Vix_ReleaseHandle(jobHandle);
   jobHandle = VIX_INVALID_HANDLE;
   if (VIX_OK != err) {
      goto abort;
   }
 
   // Use callback function to capture completion of virtual machine open.
   jobHandle = VixVM_Open(hostHandle,
                          "[standard] WindowsXP/WindowsXP.vmx",
                          myCallback,
                          contextData);
   /*
    * Do something, like pump a message pump. 
    * Later, myCallback will be invoked on another thread.
    */ 
    
abort:
   Vix_ReleaseHandle(jobHandle);
   jobHandle = VIX_INVALID_HANDLE;
}

Callback Events

Note that a callback might be called several times, for several different reasons. For example, it might be called for periodic progress updates. The eventType parameter indicates why the callback is being called. The supported event types are:

  •  VIX_EVENTTYPE_JOB_COMPLETED -- This event indicates that the asynchronous action has completed, whether successfully or not.
  •  VIX_EVENTTYPE_JOB_PROGRESS -- This event may be passed several times to report progress on an asynchronous action.
  •  VIX_EVENTTYPE_FIND_ITEM -- This event is used by VixHost_FindItems().
  •  VIX_EVENTTYPE_HOST_INITIALIZED -- This event is used by VixHost_Connect().
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值