#ifdef AIX_PROD
/* @(#)34    1.5  src/examples/pubsex/greet/greet_multi/client.c, examples.src, os2dce21.dss, 960602a.1 1/11/96 11:00:38 */
/*
 *   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, nationality;
    rpc_ns_import_handle_t   import_context;
    uuid_t                   obj_uuid;

    /* add extra parameter to indicate nationality */
    if (ac != 4)  {
        fprintf(stderr, "Usage: %s address passes nationality\n",av[0]);
        fflush(stdout);
        exit (1);
    }

    server_name = av[1];
    nationality = *av[3];

   if (nationality == 'C')
      uuid_from_string (CANADIAN_OBJECT, &obj_uuid, &st);

   else if (nationality == 'E')
      uuid_from_string (BRITISH_OBJECT, &obj_uuid, &st);

   else if (nationality == 'A')
      uuid_from_string (AMERICAN_OBJECT, &obj_uuid, &st);

   else if (nationality == 'F')
      uuid_from_string (FRENCH_OBJECT, &obj_uuid, &st);

   else if (nationality == 'B')
      uuid_from_string (BELGIAN_OBJECT, &obj_uuid, &st);

   else if (nationality == 'M')
      uuid_from_string (MEXICAN_OBJECT, &obj_uuid, &st);

   else if (nationality == 'S')
      uuid_from_string (SPANISH_OBJECT, &obj_uuid, &st);

   else {
      fprintf(stderr, "Nationality code is incorrect.");
      fflush(stdout);
      exit(1);
      }

   if (st != error_status_ok) {
       dce_error_inq_text (st, error_string, &error_inq_st);
       fprintf (stderr, "Cannot convert string to object uuid: %s\n", error_string);
       fflush(stdout);
       exit(1);
   }

  /* import compatible server bindings from the namespace */
  rpc_ns_binding_import_begin(rpc_c_ns_syntax_dce,
                              "/.:/servers/greet", greet_v1_0_c_ifspec,
                             &obj_uuid, &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);
      fflush(stdout);
      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);
         fflush(stdout);
         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);
         fflush(stdout);
         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);
         fflush(stdout);
         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);
      fflush(stdout);
      exit(1);
  }

    MAX_PASS= atoi(av[2]);


  switch( nationality )
  {
     case 'C':
     case 'A':
     case 'E':

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

     case 'F':
     case 'B':

        for ( i=1; i <= MAX_PASS; i++ )
        {
           greet_rpc(h, "Bonjour Server !", reply);
           printf("Le Greet Server dit: %s\n", reply);
           fflush(stdout);
        }
     break;

     case 'M':
     case 'S':

        for ( i=1; i <= MAX_PASS; i++ )
        {
           greet_rpc(h, "Hola Server !", reply);
           printf("El Greet Server dijo: %s\n", reply);
           fflush(stdout);
        }
     break;

  }
  return(0);
}

