home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 11 / 11.iso / n / n001 / 2.ddi / EXAMPLES / TUTORIAL / CLIENT / CLIENT.C next >
Encoding:
C/C++ Source or Header  |  1989-12-11  |  6.1 KB  |  216 lines

  1. /*                                                                            *
  2.  *                                                                            *
  3.  *                  Copyright 1987, 1988, 1989 Netwise, Inc.                  *
  4.  *                              All Rights Reserved                           *
  5.  *   This software contains information which is proprietary to and a trade   *
  6.  *   secret of Netwise, Inc. It is not to be used, reproduced, or disclosed   *
  7.  *   except as authorized in your license agreement.                          *
  8.  *                                                                            *
  9.  *                          Restricted Rights Legend                          *
  10.  *   Use, duplication,  or  disclosure  by the  Government  is  subject  to   *
  11.  *   restrictions as set forth in subparagraph (c)(1)(ii) of the Rights  in   *
  12.  *   Technical Data and  Computer Software clause  at 252.227-7013, or  the   *
  13.  *   equivalent Government clause for other agencies.                         *
  14.  *   Contractor: Netwise, Inc., Boulder, CO 80301 USA                         *
  15.  *                                                                            *
  16.  */ 
  17. /*
  18.  * File: tutorial\client\client.c
  19.  *
  20.  * This example runs in the following environments:
  21.  *    NetWare RPC 1.0, NetWare 2.1 or higher, DOS 3.3
  22.  *
  23.  * Client code for the tutorial example. This program calls two remote
  24.  * procedures, opening and closing the connection for each call using
  25.  * the non-persistent style of process binding.
  26.  *
  27.  */ 
  28.  
  29. #include <stdio.h>
  30. #include "database.h"    /* client header file,
  31.                created by RPC compiler */
  32.  
  33. int recerr;    /* external set by remote proc append_record */
  34. extern int _rpcerr_;    /* declare RPC error code */
  35.  
  36. void add_employee_info();
  37. void list_employee_info();
  38.  
  39. /* Server_Name is used to set the process-binding variable.
  40.  * It must be defined as the name the server registers under.
  41.  */
  42. #define Server_Name "example"
  43.  
  44. /* declare variable of type server_name for process binding */
  45. server_name sname;
  46.     
  47. main()
  48. {
  49.     char chr;
  50.  
  51.     /* To access the remote procedures, the process-binding
  52.      * variable sname must be set to the server name.
  53.      * The syntax for the server name is environment specific.
  54.      * If this client were to be moved to another environment,
  55.      * sname might have to be set differently.
  56.      */
  57.     sname = Server_Name;
  58.  
  59.     /* prompt user for function */
  60.     while (TRUE) {
  61.         printf("\nType 'a' to append a new employee record\n");
  62.         printf("     'l' to list an employee record\n");
  63.         printf("     'x' to exit from this program\n");
  64.         scanf(" %c", &chr);
  65.  
  66.         switch (chr) {
  67.         case 'a':
  68.             add_employee_info();
  69.             break;
  70.         case 'l':
  71.             list_employee_info();
  72.             break;
  73.         case 'x':
  74.             /* call remote procedure to shut down
  75.              * server when user exits
  76.              */
  77.             shut_down_server();
  78.             exit(0);
  79.         case EOF :
  80.         case 0   :
  81.             /* Watch out for problem with standard input */
  82.             printf("\nunexpected end of file encountered\n");
  83.             printf("execution aborted");
  84.             shut_down_server();
  85.             exit (1);
  86.         default:
  87.             printf("Enter only 'a', 'l', or 'x'\n");
  88.             break;
  89.         }
  90.     }    
  91. }
  92.  
  93. void
  94. list_employee_info()
  95. {
  96. /* prompt for an employee name, call get_record
  97.  * to get the employee information from the database, and then
  98.  * display it
  99.  */
  100.     int status;
  101.     char name1[FIRSTNAME], name2[LASTNAME];
  102.     struct employee_record *record_ptr;
  103.     char name[NAMESIZE];
  104.  
  105.     printf("\nEnter employee name, first name then last name: ");
  106.     scanf("%s %s", name1, name2);
  107.     strcpy(name, name1);
  108.     strcat(name, " ");
  109.     strcat(name, name2);
  110.  
  111.     /* get employee info from database */
  112.     status = get_record(name, &record_ptr);
  113.  
  114.     /* check RPC error code after remote call */
  115.     if (_rpcerr_) {
  116.         printf("RPC error %d from get_record()\n", _rpcerr_);
  117.         return;
  118.     }
  119.     if (status) {
  120.         printf("Employee %s not found\n", name);
  121.         return;
  122.     }
  123.  
  124.     /* display employee info based on employee type */
  125.     switch (record_ptr->employee_type) {
  126.     case PERMANENT:
  127.         printf("Employee %s, Permanent, No. %d\n",
  128.                 record_ptr->employee_name,
  129.                 record_ptr->employee_info.employee_id);
  130.         break;
  131.     case TEMPORARY:
  132.         printf("Employee %s, Temporary, Agency %s\n",
  133.                 record_ptr->employee_name,
  134.                 record_ptr->employee_info.temp_agency);
  135.         break;
  136.     default:
  137.         printf("Employee %s, Invalid employee_type\n",
  138.                 record_ptr->employee_name);
  139.         break;
  140.     }
  141.     /* free memory allocated by client stub for returned
  142.      * employee structure
  143.      */
  144.     free(record_ptr);
  145.     return;
  146. }
  147.  
  148.     
  149. void
  150. add_employee_info()
  151. {
  152. /* prompt for employee information and then add
  153.  * the employee to the database.
  154.  */
  155.     int loop, status;
  156.     char name1[FIRSTNAME], name2[LASTNAME];
  157.     struct employee_record record;
  158.  
  159.     loop = TRUE;
  160.     printf("\nEnter employee name, first name then last name: ");
  161.     scanf("%s %s", name1, name2);
  162.     strcpy(record.employee_name, name1);
  163.     strcat(record.employee_name, " ");
  164.     strcat(record.employee_name, name2);
  165.     printf("Enter employee type, %c=permanent, %c=temporary: ",
  166.         PERMANENT, TEMPORARY);
  167.     scanf(" %c", &record.employee_type);
  168.  
  169.     /* get additional employee info depending on employee type */
  170.     while (loop) { 
  171.         switch (record.employee_type) {
  172.         case PERMANENT:
  173.             printf("Enter employee number: ");
  174.             scanf("%d", &record.employee_info.employee_id);
  175.             loop = FALSE;
  176.             break;
  177.         case TEMPORARY:
  178.             printf("Enter agency name: ");
  179.             scanf("%s", record.employee_info.temp_agency);
  180.             loop = FALSE;
  181.             break;
  182.         default:
  183.             printf("Enter %c=permanent, %c=temporary\n",
  184.                         PERMANENT, TEMPORARY);
  185.             scanf(" %c", &record.employee_type);    
  186.             break;
  187.         }
  188.     }
  189.  
  190.     /* add record to database and check for errors */
  191.     status = append_record(&record);
  192.  
  193.     /* check RPC error code after remote call */
  194.     if (_rpcerr_) {
  195.         printf("RPC error %d from append_record()\n",_rpcerr_);
  196.         return;
  197.     }
  198.     if (status) {
  199.         printf("Error appending new employee record\n");
  200.         switch (recerr) {
  201.         case OLDNAME:
  202.             printf("Duplicate employee name\n");
  203.             break;
  204.         case OLDNUM:
  205.             printf("Duplicate employee number\n");
  206.             break;
  207.         case DBFULL:
  208.             printf("Data base is full\n");
  209.             break;
  210.         default:
  211.             printf("Unrecognized error\n");
  212.         }            
  213.     }
  214.     return;
  215. }
  216.