HP-UX Event ManagerProgrammer's Guide
Example 4-3 Adding and Retrieving Variables
#include <stdio.h>
#include <evm/evm.h>
int main()
{EvmEvent_t event;
EvmStatus_t status;
/* When using a primitive function to add a variable to the event, you must first place the value in a union
of type
EvmVarValue_t. The members of the union have the same names as the EVM variable types.*/
EvmVarValue_t varval_1, varval_2;
EvmVarStruct_t varinfo;
EvmString_t progname;
EvmUint16_t exit_code;
EvmItemValue_t itemval_name;
itemval_name.NAME="sys.unix.evm";
EvmEventCreateVa(&event,EvmITEM_NAME,itemval_name,EvmITEM_NONE);
/* Set and retrieve some values using basic set/get functions: */
varval_1.STRING = "my_app";
varval_2.UINT16 = 17;
/* Use EvmVarSet() to add the variables to the event, giving them meaningful names.
The final two arguments are set to 0 unless you are adding an opaque variable or supplying an I18N message
ID for a string variable. */
EvmVarSet(event,"progname",EvmTYPE_STRING,varval_1,0,0);
EvmVarSet(event,"exit_code",EvmTYPE_UINT16,varval_2,0,0);
/* You can retrieve the value of any variable by passing its name to EvmVarGet() function, which copies the
value into an EvmVarStruct_t structure. The structure also contains the name, type, and size of the variable,
so you can write generic code to handle any type of variable. Retrieving a variable does not remove the
variable from the event. You can retrieve it as many times as you want. The returned information always takeup
space in heap memory, so you must use EvmVarRelease() to clean up after using the value.*/
status = EvmVarGet(event,"progname",&varinfo);
if (status == EvmERROR_NONE)
{ fprintf(stdout,"Program name:%s\n",varinfo.value.STRING);
EvmVarRelease(&varinfo); }
status = EvmVarGet(event,"exit_code",&varinfo);
if (status == EvmERROR_NONE)
{ fprintf(stdout,"Exit code: %d\n",varinfo.value.UINT16);
EvmVarRelease(&varinfo); }
/* Set and retrieve the same values using convenience functions: */
/* The EvmVarSetString() and EvmVarSetUint16() functions, along with other functions in the EvmVarSetXxx()
family,
are simpler alternatives to the EvmVarSet() function. They require fewer arguments, and there is no need to
set up a value structure. If you want to supply an I18N message-id with a string variable, use the
EvmVarSetStringI18N() function */
EvmVarSetString(event,"progname","my_app");
EvmVarSetUint16(event,"exit_code",17);
/* The EvmVarGetString() and EvmVarGetUint16() functions, along with other functionsin the EvmVarGetXxx() family,
are simpler alternatives to the EvmVarGet() function. They require fewer arguments, and there is no need to set
up a
variable information structure or to follow the retrieval operation with a call to EvmVarRelease().*/
status = EvmVarGetString(event,"progname",&progname,NULL);
if (status == EvmERROR_NONE)
fprintf(stdout,"Program name: %s\n",progname);
/* If you use a convenience function to retrieve a string or opaque variable,the function allocates heap space
for
the value and returns a pointer to the heap. You must use free() to release the heap space after you have
finished
with the value.*/
free(progname);
status = EvmVarGetUint16(event,"exit_code",&exit_code);
if (status == EvmERROR_NONE)
fprintf(stdout,"Exit code: %d\n",exit_code);
/* After you have finished with the event, free the storage space used by the event. */
EvmEventDestroy(event);}
Posting Events
When an event is posted, it is distributed to the subscribers by the EVM daemon. Before you can
post the event, you must create a posting connection to the Event Manager daemon.
Example 4-4 shows how to create the connection, post the event, and disconnect the connection.
Example 4-4 introduces the following functions:
• EvmConnCreate — Establishes a connection between an application program and EVM,
and defines how I/O activity is to be handled. A separate connection must be established
Posting Events 41