PreviousNext

Running Programs Automatically When Hostdata Changes

The following example shows how to use the post_processors feature of the well-known hostdata to cause dced to automatically run a program if another hostdata entry changes. In this example, the post_processors file is read, and data is added for the NEWERprinter hostdata entry created in an earlier example. The data is placed in a dced_attr_list_t structure and written back to the post_processors hostdata entry.

dced_binding_handle_t dced_bh;

uuid_t entry_uuid;

sec_attr_t *data_ptr;

error_status_t status;

int i, num_strings, str_size;

sec_attr_enc_str_array_t *attr_array;

unsigned_char_t *string_uuid, temp_string[200];

dced_attr_list_t attr_list;


dced_binding_create(dced_c_service_hostdata,

dced_c_binding_syntax_default,

&dced_bh,

&status);

dced_hostdata_read(dced_bh,

&dced_g_uuid_hostdata_post_proc,

&dced_g_uuid_fileattr,

&data_ptr,

&status);


/* Create New Array and Copy Old Data into it */

num_strings = data_ptr->attr_value.tagged_union.string_array->num_strings + 1;

str_size = sizeof(sec_attr_enc_str_array_t) +

num_strings * sizeof(sec_attr_enc_printstring_p_t);

attr_array = (sec_attr_enc_str_array_t *)malloc(str_size);

attr_array->num_strings = num_strings;

for(i=0; i<(num_strings-1); i++) {

attr_array->strings[i] =

data_ptr->attr_value.tagged_union.string_array->strings[i];

}


dced_inq_id(dced_bh, "NEWERprinter", &entry_uuid, &status);


uuid_to_string(&entry_uuid, &string_uuid, &status);

sprintf(temp_string, "%s %s", string_uuid, "/path/and/program/to/run");

attr_array->strings[num_strings-1] = (dced_string_t)(temp_string);

data_ptr->attr_value.tagged_union.string_array = attr_array;


attr_list.count = 1;

attr_list.list = (sec_attr_t *)malloc(attr_list.count * sizeof(sec_attr_t));

attr_list.list = data_ptr;

dced_hostdata_write(dced_bh,

&dced_g_uuid_hostdata_post_proc,

&attr_list,

&status);


dced_objects_release(dced_bh, 1, (void*)(data_ptr), &status);

dced_binding_free(dced_bh, &status);

The description of this example is as follows:

dced_binding_create( )
This routine creates a dced binding to the hostdata service on a specified host. The binding handle created is used in all subsequent calls to appropriate dced API routines. The dced_c_service_hostdata argument is a constant string that is the well-known name of the hostdata service. When this string is used by itself, it refers to the service on the local host.

dced_hostdata_read( )
This routine reads the hostdata item referred to by the entry UUID. In this example, the global variable dced_g_uuid_hostdata_post_proc represents the UUID for the well-known post_processors file. The second parameter specifies an attribute for the data. Attributes describe how the data is to be interpreted. In this example, we know the data to be read is plain text, so we use the global variable dced_g_uuid_fileattr to specify plain text rather than binary data (dced_g_uuid_binfileattr).

Create a New Array
The next few lines copy the existing array of print strings into a new array that has additional space allocated for the new data.

dced_inq_id( )
This routine acquires the UUID dced that maintains for a known entry name. In this example, we need the UUID for the NEWERprinter hostdata entry so that it can be included in the data stored back in the post_processors file.

uuid_to_string( )
This routine returns the string representation of a UUID. Each line in the post_processors file contains a string UUID and a program name for dced to run if the hostdata entry referred to by the UUID changes. The next few lines create a new string containing the string UUID and a program name, adds the new string to the new array, and reassigns the new array to the old data pointer.

dced_hostdata_write( )
Since hostdata could have more than one attribute associated with each entry, the data must be inserted in an attribute list data structure before the dced_hostdata_write( ) routine is called. In the case of the well-known post_processors hostdata object, the attribute is for a plain text file. The dced_hostdata_write( ) routine replaces the old data with the new data for the hostdata entry represented by the entry UUID.

dced_objects_release( )
Each call to the dced_hostdata_read( ) routine requires a corresponding call to dced_objects_release( ) to release the resources allocated.

dced_binding_free( )
Each call to the dced_binding_create( ) routine requires a corresponding call to dced_binding_free( ) to release the resources allocated.

The post_processors data for this dced now contains an additional string with a UUID and program name. If the hostdata item represented by the UUID for NEWERprinter is changed, dced automatically runs the program.