#ifdef AIX_PROD
/* @(#)20    1.5  src/examples/pubsex/greet/greet_cds/client.c, examples.src, os2dce21.dss, 960602a.1 1/11/96 10:55:12 */
/*
 *   COMPONENT_NAME: examples.src
 *
 *   FUNCTIONS: main
 *
 *   ORIGINS: 72
 *
 */
#endif /* AIX_PROD */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "greet.h"
#include <dce/dce_error.h>


int main(int ac, char*av[])
{
   handle_t                 h;
   error_status_t           st;
   idl_char                 *string_binding;
   int                      i, MAX_PASS, error_inq_st;
   char                     reply[STR_SZ], error_string[1024],
                            *server_name;
   rpc_ns_import_handle_t   import_context;

   if (ac != 3)  {
       fprintf(stderr, "Usage: %s address passes\n", av[0]);
       exit (1);
   }

   server_name = av[1];
   printf("Server name: %s\n",server_name);

   /* import compatible server bindings from the namespace */

   rpc_ns_binding_import_begin(rpc_c_ns_syntax_dce,
                               "/.:/servers/greet", greet_v1_0_c_ifspec,
                              (uuid_t *)NULL, &import_context, &st);

   if (st != error_status_ok) {
       dce_error_inq_text(st, error_string, &error_inq_st);
       fprintf(stderr, "Cannot begin importing binding - %s\n", error_string);
       exit(1);
      }

   /* sift through bindings and choose the first one over udp */

   while (1) {
       rpc_ns_binding_import_next(import_context, &h, &st);
       if (st == rpc_s_no_more_bindings) {
          dce_error_inq_text(st, error_string, &error_inq_st);
          fprintf(stderr, "Cannot find binding over udp: %s\n", error_string);
          exit(1);
       }

        rpc_binding_to_string_binding(h, &string_binding, &st);
        if (st != error_status_ok) {
           dce_error_inq_text(st, error_string, &error_inq_st);
           fprintf(stderr, "Cannot convert binding to string binding: %s\n",
           error_string);
           exit(1);
        }

        /* out of curiosity, print the binding */

        if (strstr(string_binding, "ncadg_ip_udp") != 0) {
            fprintf(stdout, "Client bound to server %s at %s\n",
            server_name, string_binding);
            rpc_string_free(&string_binding, &st);
            break;
         }

   }
   /* end the binding import lookup loop */

   rpc_ns_binding_import_done(&import_context, &st);
   if (st != error_status_ok) {
       dce_error_inq_text(st, error_string, &error_inq_st);
       fprintf(stderr, "Cannot end binding import: %s\n", error_string);
       exit(1);
   }

   fprintf(stdout, "\n");

   MAX_PASS= atoi(av [2]);

   for (i=1; i <= MAX_PASS; i++) {
       greet_rpc(h, "Hello Server !", reply);
       printf("The Greet Server said: %s\n", reply);
       fflush(stdout);
   }
   return(0);
}
