Learn more about Platform products at http://www.platform.com



Tutorial 1: Request information about hosts in a cluster

This tutorial describes the minimum amount of code required to create an unregistered EGO client that connects to a host cluster.

Using this tutorial, you will ...


Step 1: Preprocessor directives

The first step is to include a reference to the system and API header files. The samples.h header file contains the declaration of methods that are implemented in the samples.

#include <stdlib.h>
#include <stdio.h>
#include "vem.api.h"
#include "samples.h"


Step 2: Implement the principal method

Lines: 4-10: define and initialize a data structure that is used to request a connection with the EGO host cluster. The data structure contains a reference to a configuration file where the master host name and port numbers are stored.

Line 17: the data structure is passed as an argument to the vem_open () method, which opens a connection to the master host. If the connection attempt is successful, a handle is returned; otherwise the method returns NULL. The handle, which is unique to each client, acts as a communication channel to the master host and all subsequent communication occurs through this handle.

Lines 25-33: the vem_name_t structure is initialized with NULL. This structure holds the cluster name, system name, and version. The vem_uname () method is passed the communication handle and, if successful, returns a valid vem_name_t structure (defined as clusterName); otherwise the method returns NULL.

Lines 34 and 35: the cluster info is printed out and the memory allocated to the structure (clusterName) is freed.

Lines 37-42: define and initialize a data structure that holds a list of host names. The hostlist member is initialized with NULL to indicate all hosts should be queried.

1	 int 
2	 sample1() 
3	 {
4	   vem_openreq_t orequest;
5	   vem_handle_t *vhandle = NULL;
6	   /* setup the open request structure with the filename
7	    * that includes the cluster information: master host and port
8	    */
9	   orequest.file = "ego.conf"; // configuration file
10	   orequest.flags=0;
11	 
12	   /* this opens a connection to the vemkd using the master host
13	    * and port specified in the configuration file. Returns NULL
14	  * if unsucessful. All other interactions occur through this
15	    * vem_handle_t returned.
16	    */ 
17	   vhandle = vem_open(&orequest);
18	 
19	   if (vhandle == NULL) {
20	   	 // error opening
21	    	 fprintf(stderr, "Error opening cluster: %s\n",  vem_strerror(vemerrno));
22	    	 return -1;
23	   }
24	 
25	   vem_name_t *clusterName = NULL;
26	   
27	   /* Retrieves the identification of the cluster, name and version */
28	   clusterName = vem_uname(vhandle);
29	   if (clusterName == NULL) {
30	   	 // error connecting
31	    	 fprintf(stderr, "Error connecting to cluster: %s\n",  vem_strerror(vemerrno));
32	    	 return -2;
33	   }
34	   printf(" Connected... %s %s %4.2f\n\n", clusterName->clustername, 
clusterName->sysname, clusterName->version);
35	   vem_free_uname(clusterName); // free memory
36	   
37	   int hin;
38	   vem_hostinforeq_t hinforeq;
39	   hinforeq.resreq = " ";        // resource string, unimplemented
40	   hinforeq.hostlist = NULL;     // all hosts
41	   vem_hostinfo_t *hinfo = NULL; // out parameter, set by vem_getHostInfo
42	   char **attrs = NULL;          // out parameter, set by vem_getHostInfo

Lines 46-49: the vem_getHostInfo () method retrieves information such as hostname, status, and attributes from all hosts in the cluster. If successful, the method returns the number of hosts; otherwise it returns a negative value and an error is flagged.

Lines 52-53: the print_hostInfo () method prints out the host information. Once the host information is printed, memory allocated to the information is freed.

Lines 56-72: the host summary structure is defined and passed to the vem_getHostSummary () method, which retrieves brief information about host availability and utilization. If the method call is successful, a positive integer is returned and the host summary info is printed out and the memory allocated to the host summary structure is freed. The sample1 program then closes the connection to the master host and terminates.

43	  /* Retrieves information about all hosts in the cluster 
44	    * returns the number of hosts, negative if unsuccessful
45	    */
46	   hin = vem_getHostInfo(vhandle, &hinforeq, &hinfo, &attrs);
47	   if (hin < 0) {
48	   	 // error
49	 	 fprintf(stderr, "Error getting hostinfo: %s %d\n",  vem_strerror(vemerrno), hin);
50	   } else {
51	     // print the host names and attributes
52	     print_hostInfo(hin, hinfo, attrs);
53	     vem_free_hostinfo(hinfo, hin, attrs);
54	   }
55	   
56	   vem_hostsummary_t hsummary;
57	 
58	   /* get brief information about the host availabilty and utilization (in 10% 
intervals)
59	    * returns negative on error.
60	    */
61	   int rc = vem_getHostSummary(vhandle, &hsummary);
62	   if (rc < 0) {
63	    	 fprintf(stderr, "Error getting hostsummary: %s\n",  vem_strerror(vemerrno));
64	   } else {
65	     print_hostsummary(&hsummary);
66	     vem_clear_hostsummary(&hsummary);
67	   }
68	 
69	   /* this closes the connection to the vemkd */ 
70	   vem_close(vhandle);
71	   return 0;
72	 } 


Step 3: Send host information to the console

This method prints out the host names and status. Lines 97-107 iterate through the attrs[] array, printing the attribute names, followed by a for loop that prints out the attribute value for each host.

73	 /**
74	  * Prints vem_hostinfo_t type array with hin elements
75	  */
76	 void
77	 print_hostInfo(int hin, vem_hostinfo_t *hinfo, char **attrs)
78	 {
79	   int i=0, j=0;
80	   printf("%-12s\t", "Attribute");
81	   for (i=0; i < hin; i++) {
82	     printf("%-12s\t", hinfo[i].name);
83	   }
84	   printf("\n");
85	   
86	   
87	   printf("%-12s\t", "Status");
88	   for (i=0; i < hin; i++) {
89	     print_host_status(hinfo[i].status);
90	   }
91	   printf("\n");
92	   
93	   j=0;
94	   while(attrs[j] != NULL) {
95	     printf("%-12s\t", attrs[j]);
96	     vem_value_t value;
97	     for (i=0; i < hin; i++) {
98	       value = hinfo[i].attributes[0];
99	       int status = value.value.v_int32;
100	       if(status != HOST_OK) {
101	         value.type = VEM_TYPE_STRING;
102	         value.value.v_string = " ";
103	       } else {
104	         value = hinfo[i].attributes[j];
105	       }
106	       print_vem_value(&value);
107	     }
108	     j++;
109	     printf("\n");
110	   }
111	 }


Step 4: Get the host status

This method formats the host status message for printing to the console.

void          
print_host_status(int status)
{
  switch(status) {
    case HOST_OK:      printf("%-12s\t", "Ok"); break;
    case HOST_UNAVAIL: printf("%-12s\t", "Unavailable"); break;                                          
    case HOST_CLOSE:   printf("%-12s\t", "Closed"); break;
    case HOST_REMOVED: printf("%-12s\t", "Removed"); break;
  }
}


Step 5: Format output according to data type

This method formats each attribute value according to its data type so that it is properly displayed.

void
print_vem_value(vem_value_t *vem_value)
{
  switch(vem_value->type) {
    case VEM_TYPE_NULL:    printf("%-12s\t",                    "NULL"); break;
    case VEM_TYPE_CHAR:    printf("%-12c\t",   vem_value->value.v_char);    break;
    case VEM_TYPE_UCHAR:   printf("%-12c\t",   vem_value->value.v_uchar);   break;          
    case VEM_TYPE_INT16:   printf("%-12d\t",   vem_value->value.v_int16);   break;
    case VEM_TYPE_UINT16:  printf("%-12u\t",   vem_value->value.v_uint16);  break;
    case VEM_TYPE_INT32:   printf("%-12d\t",   vem_value->value.v_int32);   break;
    case VEM_TYPE_UINT32:  printf("%-12u\t",   vem_value->value.v_uint32);  break;         
    case VEM_TYPE_INT64:   printf("%-12lld\t", vem_value->value.v_int64);   break;
    case VEM_TYPE_UINT64:  printf("%-12llu\t", vem_value->value.v_uint64);  break;  
    case VEM_TYPE_FLOAT32: printf("%-12.2f\t",   vem_value->value.v_float32); break;
    case VEM_TYPE_FLOAT64: printf("%-12.2lf\t",  vem_value->value.v_float64); break;
    case VEM_TYPE_BOOL:    printf("%-12d\t",   vem_value->value.v_bool);    break; 
    case VEM_TYPE_TIME:    printf("%-12ld\t",  vem_value->value.v_time);    break; 
    case VEM_TYPE_STRING:  printf("%-12s\t",   vem_value->value.v_string);  break;
    case VEM_TYPE_PTR:     printf("%-12p\t",   vem_value->value.v_ptr);     break;
  }
}


Step 6: Send host summary to the console

This method is passed the host summary structure. The method iterates through the structure's arrays and prints out a list of host status definitions and the number of hosts corresponding to each status definition. The method also prints out the utilization intervals, i.e., 10%, 20%, etc., and the number of hosts that correspond to each interval.

void
print_hostsummary(vem_hostsummary_t *hsummary)
{
	 int i;
	 printf("\nHost Summary:\n");
	 for(i=0; i<hsummary->statusC; i++){
	   printf("%-12s %d\n", hsummary->statusV[i], hsummary->statusSummary[i]);
	 }
	 printf("\nUT Summary:\n");
	 for(i=0; i<hsummary->utC; i++){
	   printf("%-12s %d\n", hsummary->utV[i], hsummary->utSummary[i]);
	 }	  
}


Run the client application

  1. Select Run > Run.

    The Run dialog appears.

  2. In the Configurations list, either select an EGO C Client Application or click New for a new configuration.

    For a new configuration, enter the configuration name.

  3. Enter the project name and C/C++ Application name.
  4. Click Apply and then Run.

Sample Output

[ Top ]


[ Platform Documentation ]


      Date Modified: July 12, 2006
Platform Computing: www.platform.com

Platform Support: support@platform.com
Platform Information Development: doc@platform.com

Copyright © 1994-2006 Platform Computing Corporation. All rights reserved.