home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Collection - Online Library - January 1996 / CKITOS2196.ISO / diskette / gg244090.dsk / unc.dsk / CHAPTER.09 / SERVER.C < prev   
C/C++ Source or Header  |  1993-10-28  |  5KB  |  153 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /* Module:      server.c                                                   */
  4. /*                                                                         */
  5. /* Description: DCE RPC setup for the MessageBox application.              */
  6. /*              Catches ^C for cleaning up binding information and         */
  7. /*              exporting the mbox structure to a local file.              */
  8. /*                                                                         */
  9. /***************************************************************************/
  10.  
  11. # include <stdlib.h>
  12. # include <pthread.h>
  13. # include <dce/rpc.h>
  14.  
  15. # include "mbox.h"
  16. # include "common.h"
  17.  
  18. # define MAX_CONC_CALLS_PROTSEQ         2
  19. # define MAX_CONC_CALLS_TOTAL           4
  20. # define IF_HANDLE                      MessageBox_v2_0_s_ifspec
  21.  
  22. # ifdef IBMOS2
  23. # define FNAME                          "message.box"
  24. # else
  25. # define FNAME                          "/tmp/message.box"
  26. # endif
  27.  
  28. int mbox_import( const char * );
  29. int mbox_export( const char * );
  30.  
  31. int main( int argc, char *argv[] )
  32. {
  33.         unsigned_char_t         *server_name;
  34.         rpc_binding_vector_t    *bind_vector_p;
  35.         unsigned32              status;
  36.         sigset_t                sigset;
  37.         pthread_t               this_thread;
  38.  
  39. # ifdef IBMOS2
  40.         pthread_inst_exception_handler();
  41. # endif
  42.         this_thread     = pthread_self();
  43.  
  44.         /* Register the authentification level which will be used */
  45.         printf("Registering authentication level with RPC runtime...\n");
  46.         rpc_server_register_auth_info(
  47.                 PRINCIPAL_NAME,
  48.                 rpc_c_authn_default,
  49.                 NULL,
  50.                 NULL,
  51.                 &status
  52.         );
  53.         ERRCHK( status );
  54.  
  55.         /* Register interface/epv associations with rpc runtime. */
  56.         printf("Registering server interface with RPC runtime...\n");
  57.         rpc_server_register_if ( IF_HANDLE, NULL, NULL, &status );
  58.         ERRCHK( status );
  59.  
  60.         /* Inform rpc runtime of a protocol sequence to use. */
  61.         switch ( (( argc > 1 ) ? *argv[1] | ' ' : 'a') ) {
  62.         case 't':
  63.            rpc_server_use_protseq ("ncacn_ip_tcp", MAX_CONC_CALLS_PROTSEQ, &status );
  64.            break;
  65.         case 'u':
  66.            rpc_server_use_protseq ("ncadg_ip_udp", MAX_CONC_CALLS_PROTSEQ, &status );
  67.            break;
  68.         case 'a':
  69.         default:
  70.            rpc_server_use_all_protseqs ( MAX_CONC_CALLS_PROTSEQ, &status );
  71.         }
  72.         ERRCHK( status );
  73.  
  74.         /* Ask the runtime which binding handle will be used. */
  75.         rpc_server_inq_bindings( &bind_vector_p, &status );
  76.         ERRCHK( status );
  77.  
  78.         /* Register binding information with endpoint map */
  79.         printf("Registering server endpoints with endpoint mapper (RPCD)...\n");
  80.         rpc_ep_register(
  81.                 IF_HANDLE,
  82.                 bind_vector_p,
  83.                 NULL,
  84.                 "Message Box server, version 2.0",
  85.                 &status
  86.         );
  87.         ERRCHK( status );
  88.  
  89.         /* Export binding info to the namespace. */
  90.         printf("Exporting server bindings into CDS namespace...\n");
  91.         rpc_ns_binding_export (
  92.                 rpc_c_ns_syntax_default,
  93.                 ENTRY_NAME,
  94.                 IF_HANDLE,
  95.                 bind_vector_p,
  96.                 NULL,
  97.                 &status
  98.         );
  99.         ERRCHK( status );
  100.  
  101.         /* baggage to handle ctrl-C */
  102.         sigemptyset(&sigset);
  103.         sigaddset(&sigset, SIGINT);
  104.         sigaddset(&sigset, SIGTERM);
  105.         if (pthread_signal_to_cancel_np(&sigset, &this_thread) != 0) {
  106.                 printf("pthread_signal_to_cancel_np failed\n");
  107.                 exit(1);
  108.         }
  109.  
  110.         /* Import the mbox structure from file FNAME */
  111.         mbox_import(FNAME);
  112.  
  113.         TRY {
  114.                 /* Listen for service requests. */
  115.                 printf( "Server %s listening...\n", ENTRY_NAME );
  116.                 rpc_server_listen ( MAX_CONC_CALLS_TOTAL, &status );
  117.                 ERRCHK( status );
  118.         }
  119.         FINALLY {
  120.                 /* Export the mbox structure to file FNAME */
  121.                 mbox_export(FNAME);
  122.  
  123.                 /* Unexport the binding information from the namespace. */
  124.                 printf("Unexporting server bindings from CDS namespace...\n");
  125.                 rpc_ns_binding_unexport (
  126.                         rpc_c_ns_syntax_default,
  127.                         ENTRY_NAME,
  128.                         IF_HANDLE,
  129.                         NULL,
  130.                         &status
  131.                 );
  132.                 ERRCHK( status );
  133.  
  134.                 /* Unregister interface from RPC runtime */
  135.                 printf("Unregistering server interface with RPC runtime...\n");
  136.                 rpc_server_unregister_if ( IF_HANDLE, NULL, &status );
  137.                 ERRCHK( status );
  138.  
  139.                 /* Unregister interface from EPV */
  140.                 printf("Unregistering server endpoints with endpoint mapper (RPCD)...\n");
  141.                 rpc_ep_unregister ( IF_HANDLE, bind_vector_p, NULL, &status );
  142.                 ERRCHK( status );
  143.  
  144. # ifdef IBMOS2
  145.                 pthread_dinst_exception_handler();
  146. # endif
  147.  
  148.                 exit ( 0 );
  149.         }
  150.         ENDTRY;
  151. }
  152.  
  153.