#ifdef AIX_PROD
/* @(#)09    1.7  src/examples/pubsex/binop/server.c, examples.src, os2dce21.dss, 960602a.1 2/6/96 13:48:56 */
/*
 *   COMPONENT_NAME: examples.src
 *
 *   FUNCTIONS: main
 *
 *   ORIGINS: 72
 *
 */
#endif /* AIX_PROD */

/* binop server include files, note: Binop uses exception
   handling macros from DCE Threads component.  Note that the
   original exception handling file from DCE was changed to
   exc_handling.h    */
#include <stdio.h>
#include <stdlib.h>
#include <dce/pthread.h>
#include <dce/exc_handling.h>
#include <dce/dce_error.h>
#include "binop.h"
#include <dce/rpc.h>


#define MAX_CONCURRENT_CALLS 5

/* The server declares the manager EPV (defined in the
   manager.c module, and the nil UUID which it supplies
   in registrations as an object UUID and object type UUID. */

extern binop_v1_0_epv_t binop_v1_manager_epv;

#ifdef __BORLANDC__
extern uuid_t _pascal uuid_nil;
#else
extern uuid_t uuid_nil;
#endif

/* In the first part of the main function, the server calls
   the rpc_network_is_protseq_valid to check that its argument
   specifies a protocol sequence that is supported on its host
   both by the runtime library and the operating system.  */

int main (int ac, char *av[])
{
    rpc_binding_vector_p_t  bvec;
    error_status_t          st;
    boolean32               validfamily;
    idl_char                *string_binding;
    int                     i, error_inq_st;
    unsigned char           error_string[132];

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

    validfamily = rpc_network_is_protseq_valid((idl_char *)av[1], &st);
    if (st != error_status_ok) {
        dce_error_inq_text(st, error_string, &error_inq_st);
        fprintf(stderr, "Cannot check protocol sequence - %s\n", error_string);
        exit(1);
    }

    if (!validfamily) {
        fprintf(stderr, "Protocol sequence %s is not valid\n", av[1]);
        exit(1);
    }
    /* Calling rpc_server_use_protseq to obtain an endpoint
       on which to listen */

    rpc_server_use_protseq((idl_char *)av[1], MAX_CONCURRENT_CALLS, &st);
    if (st != error_status_ok) {
        dce_error_inq_text(st, error_string, &error_inq_st);
        fprintf(stderr, "Cannot use protocol sequence - %s\n", error_string);
        exit(1);
    }

    /* Calling rpc_server_register_if to register its interface with
       the RPC runtime by supplying its interface specifier  */

    rpc_server_register_if(binop_v1_0_s_ifspec, &uuid_nil,
              NULL, &st);

    if (st != error_status_ok) {
        dce_error_inq_text(st, error_string, &error_inq_st);
        printf("Cannot register interface - %s\n", error_string);
        exit(1);
    }

    /* Calling rpc_server_inq_bindings to obtain a vector of
       binding handles that can be used to register the server's
       endpoint. The server then obtains, prints, and frees a
       string binding */

     rpc_server_inq_bindings(&bvec, &st);
     if (st != error_status_ok)  {
         dce_error_inq_text(st, error_string, &error_inq_st);
         printf("Cannot inquire bindings - %s\n", error_string);
        exit(1);
    }

    printf("Bindings:\n");

    for (i = 0; i < bvec->count; i++)  {
         rpc_binding_to_string_binding(bvec->binding_h[i], &string_binding, &st);

         printf("%s\n", (char *)string_binding);
         rpc_string_free(&string_binding, &st);

    }

    /* The server endpoint is registered in the local Endpoint Map */

    rpc_ep_register(binop_v1_0_s_ifspec, bvec, (uuid_vector_p_t) NULL,
            (unsigned_char_p_t) "binop version 1.0 server", &st);

    /*  To begin listening for RPC requests, the server calls
       rpc_server_listen.  This call is placed within the TRY of a
       TRY, CATCH_ALL, ENDTRY sequence, so that if the server receives
       a signal while it is listening, it can unregister its interface
       and its endpoint before it exits.   */

    TRY {
          printf("Listening...\n");
          rpc_server_listen(MAX_CONCURRENT_CALLS, &st);
          if (st != error_status_ok)
              dce_error_inq_text(st, error_string, &error_inq_st);
              fprintf(stderr, "Error: %s\n", error_string);
    } CATCH_ALL {

         printf("Unregistering endpoint \n");
         fflush(stdout);
         rpc_ep_unregister(binop_v1_0_s_ifspec, bvec,
             (uuid_vector_p_t) NULL, &st);
    } ENDTRY;

    return (0);
}
