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
for each type of connection: posting, listening (subscribing), or
availing service. For more information about this function, see EvmConnection(5) and EvmConnCreate(3).
EvmEventPost — Posts an
event to EVM. For more information about this function, see EvmEventPost(3).
EvmConnDestroy — Destroys
a specified connection. For more information about this function,
see EvmConnDestroy(3).
You can use the simpler EvmConnCreatePoster macro in place of EvmConnCreate to create a
posting connection. This macro requires fewer arguments than EvmConnCreate. Although it offers fewer connection options,
it is suitable for use by most applications that need to post events
to the local system.
If your program uses transient EVM connections,
you can omit calls to EvmConnCreate or EvmConnDestroy and pass NULL as the connection argument
to EvmEventPost. Connections use significant
processing time to establish, so you must use only transient connections
if they are appropriate for your program. For more information about
connections, see “Choosing a Connection Policy” and EvmEventPost(3).
You can create, post, and destroy an event in
a single call using the EvmEventPostVa function.
For more information about this function, see EvmEventPost(3).
Example 4-4 Posting Events
 |
#include <stdio.h>
#include <evm/evm.h>
int main()
{ EvmEvent_t event;
EvmStatus_t status;
EvmConnection_t conn;
EvmItemValue_t itemval_name;
/* You can create a connection to the EVM daemon using EvmConnCreate() function. The connectionremains in place
until your program exits, or until you explicitly destroy it with EvmConnDestroy(). The first two arguments to
EvmConnCreate() function specify that the connection will be used for posting events. This function waits for
the daemon to acknowledge acceptance of the event before returning, so that you can take action if the attempt fails.
(See EvmConnCreate(3) for other response options.) The NULL value for the third argument indicates that you are making
a connection to the EVM daemon running on the local system. The fourth and fifth values are used for other response types,
and should always be NULL for wait-mode responses. The final argument receives the handle to the connection. You
must supply the returned value in all future calls being made on this connection.*/
status = EvmConnCreate(EvmCONNECTION_POST, EvmRESPONSE_WAIT,NULL,NULL,NULL,&conn);
if (status != EvmERROR_NONE)
{ fprintf(stderr,"Failed to create EVM posting connection\n");
exit(1); } /* Create an event and post it. */
itemval_name.NAME="sys.unix.evm";
EvmEventCreateVa(&event, EvmITEM_NAME, itemval_name,EvmITEM_NONE);
status = EvmEventPost(conn,event);
if (status != EvmERROR_NONE)
{ fprintf(stderr,"Failed to post event\n");
exit(1); }
/* Clean up by destroying the event and the connection. If you expect posting events periodically, it may be better
not to destroy the connection, but to reuse it for all future events. This will save you the overhead of re-establishing
the connection each time you have something to post.*/
EvmEventDestroy(event);
EvmConnDestroy(conn);} |
 |