home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / mach / doc / unpublished / mig_example / server.c.Z / server.c
Encoding:
C/C++ Source or Header  |  1989-11-01  |  3.2 KB  |  105 lines

  1. /***************************************************
  2.  *   Main program for random server
  3.  **************************************************/
  4. #include <stdio.h>
  5. #include <mach.h>
  6. #include <mach_error.h>
  7. #include <mig_errors.h>
  8. #include <mach/message.h>
  9. #include <mach/notify.h>
  10. #include <servers/envmgr.h>
  11.  
  12. extern boolean_t    random_server();
  13.  
  14. /*********************************************************
  15.  * procedure random_run:
  16.  *   Waits for messages to server,
  17.  *   handles them, and replies to sender.
  18.  *********************************************************/
  19. void random_run ()
  20. {
  21.    typedef int space[1024]; /* Maximum message size */
  22.  
  23.    typedef struct DumMsg
  24.         {
  25.            msg_header_t     head;
  26.        msg_type_t        retcodetype;
  27.        kern_return_t    return_code;
  28.            space         body;
  29.         } DumMsg;
  30.  
  31.    kern_return_t    retcode;
  32.    msg_return_t        msgcode;
  33.    boolean_t         ok;
  34.    DumMsg         *pInMsg, *pRepMsg;
  35.  
  36.    pInMsg = (DumMsg *)malloc(sizeof(DumMsg));
  37.    pRepMsg = (DumMsg *)malloc(sizeof(DumMsg));
  38.  
  39.    while (TRUE)
  40.    {
  41.        pInMsg->head.msg_size = sizeof(DumMsg); /* bytes */
  42.        pInMsg->head.msg_local_port = PORT_DEFAULT;
  43.  
  44.     /* wait to receive request from client */
  45.        msgcode = msg_receive(&pInMsg->head,MSG_OPTION_NONE,0);
  46.        if (msgcode != RCV_SUCCESS)
  47.            printf("error %s in Receive, message will be ignored.\n",
  48.         mach_errormsg((kern_return_t)msgcode));
  49.        else 
  50.        {   if (pInMsg->head.msg_type == MSG_TYPE_EMERGENCY)
  51.            {
  52.               if (pInMsg->head.msg_id == NOTIFY_PORT_DELETED)
  53.           {  /* probably the death of a client's reply */}
  54.           else
  55.         printf("Unexpected emergency message received: id is %d\n",
  56.             pInMsg->head.msg_id);
  57.            }
  58.           else  /* normal message */
  59.           {   
  60.         /* call server interface module */
  61.           ok = random_server((msg_header_t *)pInMsg,
  62.                    (msg_header_t *)pRepMsg);
  63.           if (pRepMsg->return_code != MIG_NO_REPLY)
  64.           {  
  65.           /* sending reply message to client */
  66.                   pRepMsg->head.msg_local_port = pInMsg->head.msg_local_port;
  67.                   pRepMsg->head.msg_remote_port = pInMsg->head.msg_remote_port;
  68.                   msgcode = msg_send(&pRepMsg->head,MSG_OPTION_NONE,0);
  69.                   if ((msgcode != SEND_SUCCESS) && 
  70.             (msgcode != SEND_INVALID_PORT))
  71.                                /* Probably remote process death */
  72.                     printf("error %s at Send.\n",
  73.                 mach_errormsg((kern_return_t)msgcode));
  74.           }
  75.       } /* normal message */
  76.        } /* of message handling */
  77.      }  /* of main loop */
  78.  
  79. }
  80.  
  81.  
  82. main()
  83. {
  84.    port_t    ServerPort;
  85.    kern_return_t retcode;
  86.  
  87.     /* add notification port to default port set */
  88.    retcode = port_unrestrict(task_self_,task_notify_);
  89.     /* allocate a service port */
  90.    retcode = port_allocate(task_self(), &ServerPort);
  91.    if (retcode == KERN_SUCCESS)
  92.    {    /* add service port to default port set */
  93.       (void) port_unrestrict(task_self(),ServerPort);
  94.     /* check it in so users can find it */
  95.       retcode  = netname_check_in(NameServerPort,"RandomServerPort",
  96.         PORT_NULL,ServerPort);
  97.    }
  98.    if (retcode != KERN_SUCCESS)
  99.     printf("netname_check_in of RandomServerPort failed with code %s\n",
  100.             mach_errormsg(retcode));
  101.    random_run ();
  102.    printf("(* !!!!! Random server exited - give it up !!!!! *)\n");
  103.    exit(2);  
  104. }
  105.