home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / greetz.zip / server.c < prev    next >
Text File  |  1993-11-08  |  6KB  |  174 lines

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include "greet.h"
  4.  
  5. #define MAX_CONCURRENT_CALLS 5
  6.  
  7. /* The server declares nil UUID which it supplies
  8.    in registrations as an object UUID and object type UUID. */
  9.  
  10. extern uuid_t uuid_nil;
  11.  
  12. /* In the first part of the main function, the server calls
  13.    the rpc_network_is_protseq_valid to check that its argument
  14.    specifies a protocol sequence that is supported on its host
  15.    both by the runtime library and the operating system.  */
  16.  
  17. int main (int ac, char *av[])
  18. {
  19.     rpc_binding_vector_p_t  bvec;
  20.     error_status_t          st, error_inq_st;
  21.     idl_boolean             validfamily;
  22.     idl_char                *string_binding;
  23.     char                    *entry_name, error_string[1024];
  24.     int                      i;
  25.  
  26.  
  27.     if (ac != 2) {
  28.         fprintf (stderr,
  29.                 "Number of parameters received for %s is incorrect\n",
  30.                 av[0]);
  31.         exit(1);
  32.     }
  33.  
  34.     entry_name = av[1];
  35.  
  36.     validfamily = rpc_network_is_protseq_valid("ncadg_ip_udp", &st);
  37.     fprintf(stderr,
  38.            "  rpc_network_is_protseq_valid status is %x\n", st);
  39.     if (st != error_status_ok) {
  40.         dce_error_inq_text (st, error_string, &error_inq_st);
  41.         fprintf (stderr, "Cannot check protocol sequence - %s\n",
  42.                  error_string);
  43.         exit(1);
  44.     }
  45.  
  46.     if (!validfamily)  {
  47.         fprintf(stderr, "Protocol sequence is not valid\n");
  48.         exit (1);
  49.     }
  50.  
  51.     /* Calling rpc_server_use_protseq to obtain an endpoint
  52.        on which to listen */
  53.  
  54.     rpc_server_use_protseq("ncadg_ip_udp", MAX_CONCURRENT_CALLS,
  55.                              &st);
  56.     printf("  rpc_server_use_protseq status is %x\n", st);
  57.     if (st != error_status_ok) {
  58.         dce_error_inq_text (st, error_string, &error_inq_st);
  59.         fprintf (stderr, "Cannot use protocol sequence - %s\n",
  60.                  error_string);
  61.         exit(1);
  62.     }
  63.  
  64.  
  65.  
  66.     /* Calling rpc_server_register_if to register its interface with
  67.        the RPC runtime by supplying its interface specifier  */
  68.  
  69.     rpc_server_register_if(greet_v1_0_s_ifspec, &uuid_nil, NULL, &st);
  70.     printf("  rpc_server_register_if status is %x\n", st);
  71.     if (st != rpc_s_ok) {
  72.        fprintf(stderr, "FAULT: %s:%d\n", __FILE__, __LINE__);
  73.         dce_error_inq_text (st, error_string, &error_inq_st);
  74.         fprintf (stderr, "Cannot register interface - %s\n",
  75.                  error_string);
  76.         exit(1);
  77.     }
  78.  
  79.     /* Calling rpc_server_inq_bindings to obtain a vector of
  80.        binding handles that can be used to register the server's
  81.        endpoint. The server then obtains, prints, and frees a
  82.        string binding */
  83.  
  84.      rpc_server_inq_bindings(&bvec, &st);
  85.      printf("  rpc_server_inq_bindings status is %x\n", st);
  86.      if (st != error_status_ok)  {
  87.         dce_error_inq_text (st, error_string, &error_inq_st);
  88.         fprintf (stderr, "Cannot inquire bindings - %s\n",
  89.                  error_string);
  90.         exit(1);
  91.     }
  92.  
  93.     printf("Server %s bindings:\n", entry_name);
  94.  
  95.     for (i = 0; i < bvec->count; i++)  {
  96.          rpc_binding_to_string_binding(bvec->binding_h[i],
  97.               &string_binding, &st);
  98.          printf("  rpc_binding_to_string_binding status is %x\n", st);
  99.          printf("%s\n", (char *)string_binding);
  100.          rpc_string_free(&string_binding, &st);
  101.  
  102.      }
  103.  
  104.      /* The server endpoint is registered in the local Endpoint Map */
  105.  
  106.      rpc_ep_register(greet_v1_0_s_ifspec, bvec,
  107.                     (uuid_vector_p_t) NULL,
  108.                     (unsigned_char_p_t) "greet version 1.0 server",
  109.                     &st);
  110.      printf("  rpc_ep_register status is %x\n", st);
  111.      if (st != error_status_ok) {
  112.           dce_error_inq_text (st, error_string, &error_inq_st);
  113.           fprintf (stderr, "Cannot register end point: %s\n",
  114.                    error_string);
  115.           exit(1);
  116.      }
  117.  
  118.  
  119.  /* export the binding vector the runtime gave us to the namespace */
  120.  
  121.  rpc_ns_binding_export(rpc_c_ns_syntax_dce, "/.:/servers/greet",
  122.                        greet_v1_0_s_ifspec, bvec,
  123.                        (uuid_vector_t *)NULL, &st);
  124.  printf("  rpc_ns_binding_export status is %x\n", st);
  125.  if (st != error_status_ok) {
  126.     dce_error_inq_text(st, error_string, &error_inq_st);
  127.     fprintf(stderr, "Cannot export binding vector: %s\n",
  128.             error_string);
  129.     exit(1);
  130.  }
  131.  
  132.   /*  To begin listening for RPC requests, the server calls
  133.     rpc_server_listen.  This call is placed within the TRY of a
  134.     TRY, CATCH_ALL, ENDTRY sequence, so that if the server receives
  135.     a signal while it is listening, it can unregister its interface
  136.     and its endpoint before it exits.   */
  137.  
  138.   TRY {
  139.       printf("Listening...\n");
  140.       rpc_server_listen(MAX_CONCURRENT_CALLS, &st);
  141.       if (st != error_status_ok){
  142.          dce_error_inq_text(st, error_string, &error_inq_st);
  143.          fprintf(stderr, "Error: %s\n", error_string);
  144.          }
  145.   }
  146.  
  147.  
  148.   CATCH_ALL {
  149.  
  150.          /* unexport binding vector from namespace --
  151.                  not usually done for a persistent server */
  152.  
  153.          printf("Server %s unexporting\n", entry_name);
  154.  
  155.          rpc_ns_binding_unexport(rpc_c_ns_syntax_dce,
  156.                                 "/.:/servers/greet",
  157.                                 greet_v1_0_s_ifspec,
  158.                                 (uuid_vector_t *)NULL, &st);
  159.  
  160.          if (st != error_status_ok) {
  161.             dce_error_inq_text(st, error_string, &error_inq_st);
  162.             fprintf(stderr, "Cannot unexport binding vector - %s\n",
  163.                     error_string);
  164.           /* don't exit here */
  165.           }
  166.  
  167.            printf("Unregistering endpoint \n");
  168.  
  169.            rpc_ep_unregister(greet_v1_0_s_ifspec, bvec,
  170.            (uuid_vector_p_t) NULL, &st);
  171.      }
  172.      ENDTRY;
  173. }
  174.