2013-02-21 12:54 by Haippy, 9237 reading, 0 reviews, Favorites, compilation
Next to the Zookeeper C API Guide II (Monitoring (Wathes), basic constants and structure introduction), this article focuses on Zookeeper C APIs in the various callback functions.
Introduction to various callback functions in Zookeeper C API
Before you introduce the Zookeeper C API, let's start by introducing the prototypes of the various callback functions in the Zookeeper C API:
monitoring functions (watch function) prototypes
typedef void (*WATCHER_FN) (zhandle_t *zh, int type, int state, const char *path,void *watcherctx);
The individual parameters of the monitoring function prototype are explained as follows:
Zh |
Zookeeper handle (handle) |
Type |
The event type. One of the *_event constants. |
State |
Connection status (connection state). The status value is one of the *_state constants. |
Path |
The path of the Znode node that triggered the monitoring event, and if NULL, the event type is Zoo_session_event |
Watcherctx |
Monitor contexts (watcher context). |
Prototypes of other callback functions
There are several callback functions in Zookeeper that are used in asynchronous APIs (functions that typically start with zoo_a*), which are divided into the following types of callback functions that handle the return value type of an asynchronous function: Handling a callback function that returns a void type, handling a callback function that returns a STAT structure, Handles callback functions that return a string, handles callback functions that return data, handles callback functions that return a list of strings (a list of string), and handles callback functions that return a list of strings (a list of string) and a Stat structure, and a callback function that handles returning ACL information , respectively, as follows:
Processing callback function that returns void type typedef void (* void_completion_t) (int rc, const void *data);//Handle returning the STAT structure of the callback function typedef void (* STAT_CO mpletion_t) (int rc, const struct STAT *stat, const void *data); The callback function that handles the return string is typedef void (* string_completion_t) (int RC, const char *value, const void *data); callback function to process return data typedef void (* data_completion_t) (int RC, const char *value, int value_len, const struct STAT *stat, const v OID *data); The callback function that processes the return string list (a list of string) typedef void (* strings_completion_t) (int rc, const struct String_vector *strings, const void *data); The callback function that processes the return string list (a list of string) and the STAT structure at the same time typedef void (* strings_stat_completion_t) (int rc, const struct STRING_VECTOR *strings, const struct STAT *stat, const void *data); callback function for processing and returning ACL information typedef void (* acl_completion_t) (int rc, struct acl_vector *acl, struct Stat *stat, const void *data) ;
Here are some examples of the use of these callback functions:
- Handling callback functions that return void types
typedef void (* void_completion_t) (int rc, const void *data)
This callback function is typically called when an asynchronous API call ends or the Zookeeper client loses a connection.
Rc |
The error code returned by the asynchronous function call, the connection loss/timeout will trigger the prototype function (here refers to the callback function with the function prototype, the same as the same), and the error code is Zconnectionloss--the Zookeeper client and the server-side connection is lost, or Zoperationtimeout-The connection timed out, and data-related events trigger the invocation of the prototype function with the corresponding error code, as shown in the following (0-generation asynchronous function call succeeded)
|
Data |
A pointer passed in by the caller, through which the caller can pass a custom parameter to the callback function , and the developer is responsible for the release of the memory that the pointer points to. |
- Handling callback functions that return a STAT structure
typedef void (* stat_completion_t) (int rc, const struct STAT *stat, const void *data)
This function is typically called when an asynchronous API call ends or the Zookeeper client loses a connection.
Rc |
The error code returned by the asynchronous function call, the connection loss/timeout will trigger the prototype function (here refers to the callback function with the function prototype, the same as the same), and the error code is Zconnectionloss--the Zookeeper client and the server-side connection is lost, or Zoperationtimeout-The connection timed out, and data-related events trigger the invocation of the prototype function with the corresponding error code, as shown in the following (0-generation asynchronous function call succeeded) |
Stat |
Points to the stat information associated with the Znode node, if a value other than 0 is returned (that is, an asynchronous call function fails), the area pointed to by stat is undefined and the developer is not responsible for releasing the memory space that stat points to. |
Data |
A pointer passed in by the caller, through which the caller can pass a custom parameter to the callback function, and the developer is responsible for the release of the memory that the pointer points to. |
- Handling callback functions that return a string
typedef void (* string_completion_t) (int RC, const char *value, const void *data)
This function is typically called when an asynchronous API call ends or the Zookeeper client loses a connection.
Rc |
The error code returned by the asynchronous function call, the connection loss/timeout will trigger the prototype function (here refers to the callback function with the function prototype, the same as the same), and the error code is Zconnectionloss--the Zookeeper client and the server-side connection is lost, or Zoperationtimeout-The connection timed out, and data-related events trigger the invocation of the prototype function with the corresponding error code, as shown in the following (0-generation asynchronous function call succeeded) |
Value |
The returned string |
Data |
A pointer passed in by the caller, through which the caller can pass a custom parameter to the callback function, and the developer is responsible for the release of the memory that the pointer points to. |
- Handling callback functions that return data
typedef void (* data_completion_t) (int RC, const char *value, int value_len, const struct STAT *stat, const void *data)
This function is typically called when an asynchronous API call ends or the Zookeeper client loses a connection.
Rc |
The error code returned by the asynchronous function call, the connection loss/timeout will trigger the prototype function (here refers to the callback function with the function prototype, the same as the same), and the error code is Zconnectionloss--the Zookeeper client and the server-side connection is lost, or Zoperationtimeout-The connection timed out, and data-related events trigger the invocation of the prototype function with the corresponding error code, as shown in the following (0-generation asynchronous function call succeeded) |
Value |
The return value of the asynchronous call, if a non-0 value is returned (that is, an asynchronous call function error), the area pointed to by value is undefined and the developer is not responsible for releasing the memory space pointed to by value . |
Value_len |
Returns the value data byte count (bytes) |
Stat |
Points to the stat information associated with the Znode node, if a value other than 0 is returned (that is, an asynchronous call function fails), the area pointed to by stat is undefined and the developer is not responsible for releasing the memory space that stat points to. |
Data |
A pointer passed in by the caller, through which the caller can pass a custom parameter to the callback function, and the developer is responsible for the release of the memory that the pointer points to. |
- Handling callback functions that return a list of strings (a list of string)
typedef void (* strings_completion_t) (int rc, const struct string_vector *strings, const void *data)
This function is typically called when an asynchronous API call ends or the Zookeeper client loses a connection.
Rc |
The error code returned by the asynchronous function call, the connection loss/timeout will trigger the prototype function (here refers to the callback function with the function prototype, the same as the same), and the error code is Zconnectionloss--the Zookeeper client and the server-side connection is lost, or Zoperationtimeout-The connection timed out, and data-related events trigger the invocation of the prototype function with the corresponding error code, as shown in the following (0-generation asynchronous function call succeeded) |
Strings |
Points to a structure that contains a list of all child node names for a Znode node, and if a value other than 0 is returned (that is, an asynchronous call function fails), the area pointed to by strings is undefined and the developer is not responsible for releasing the memory space that the strings points to. |
Data |
A pointer passed in by the caller, through which the caller can pass a custom parameter to the callback function, and the developer is responsible for the release of the memory that the pointer points to. |
- callback function that processes the return string list (a list of string) and the STAT structure at the same time
typedef void (* strings_stat_completion_t) (int rc, const struct string_vector *strings, const struct STAT *stat, const void *data)
This function is typically called when an asynchronous API call ends or the Zookeeper client loses a connection.
points to the stat information associated with the Znode node, and if a non-0 value is returned (that is, an asynchronous call function fails), the area pointed to by stat is undefined and the developer is not responsible for releasing the memory space that stat points to.
RC |
Async function call returned error code, connection lost/ The timeout will trigger the prototype function (here refers to the callback function with the function prototype, the same as the same), and the error code is Zconnectionloss-- Zookeeper the client-server connection is lost, or zoperationtimeout-- The connection timed out, and data-related events trigger the invocation of the prototype function, with the corresponding error code, as shown later (successful 0 generation asynchronous function call) |
str Ings |
points to the structure that contains a list of all child node names for a znode node, If you return a value other than 0 (that is, an asynchronous call function error), The area pointed to by strings is undefined and the developer is not responsible for releasing strings The memory space pointed to. |
stat | The td>
data |
The pointer passed in by the caller, through which the caller can pass the pointer to the callback function Incoming custom parameters, the developer should be responsible for the release of the memory pointed to by this pointer. |
- Handling callback functions that return ACL information
typedef void (* acl_completion_t) (int rc, struct acl_vector *acl, struct Stat *stat, const void *data)
This function is typically called when an asynchronous API call ends or the Zookeeper client loses a connection.
RC |
Async function call returned error code, connection lost/ The timeout will trigger the prototype function (here refers to the callback function with the function prototype, the same as the same), and the error code is Zconnectionloss-- Zookeeper the client-server connection is lost, or zoperationtimeout-- The connection timed out, and data-related events trigger the invocation of the prototype function, with the corresponding error code, as shown later (successful 0 generation asynchronous function call) |
ACL |
points to a pointer containing ACL information for a znode node, If you return a non-0 value (that is, an asynchronous call function error), the ACL The area pointed to is undefined and the developer is not responsible for releasing ACL points to the memory space. |
stat |
points to the stat information associated with the Znode node, and if a non-0 value is returned (that is, an asynchronous call function fails), the area pointed to by stat is undefined and the developer is not responsible for releasing the memory space that stat points to. |
data |
The pointer passed in by the caller, through which the caller can pass the pointer to the callback function Incoming custom parameters, the developer should be responsible for the release of the memory pointed to by this pointer. |
Now that all the callback functions have been described, the next section will describe the use of Zookeeper C API classifications and basic APIs, see fourth Zookeeper C API Guide IV (C API Overview)
Zookeeper C API Guide III (callback function) (GO)