CLIENT.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:13k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*************************************************************/
  2. /**                                                         **/
  3. /**                 Microsoft RPC Examples                  **/
  4. /**                 Dictionary Application                  **/
  5. /**          Copyright(c) Microsoft Corp. 1992-1996         **/
  6. /**                                                         **/
  7. /*************************************************************/
  8. /*************************************************************************
  9.  *                                                                       *
  10.  * Remote dictionary example: client side                                *
  11.  *                                                                       *
  12.  * Description:                                                          *
  13.  * This is the driver for the client side remote dictionary              *
  14.  * (splay trees based) demo.  This works as follows:                     *
  15.  *                                                                       *
  16.  *  o in main we bind to the dict interface, and call main_dict;         *
  17.  *                                                                       *
  18.  *  o in dict_main we call VDict_New to initialize the dictionary        *
  19.  *    on the server side, and then call TestLoop.                        *
  20.  *                                                                       *
  21.  *  o in TestLoop we loop responding to user input of the form:          *
  22.  *    <one_letter_opcode> [<optional_arg>],                              *
  23.  *    where <optional_arg> is a an integer constant followed by a        *
  24.  *    string.  Type "?" for a detailed usage message.                    *
  25.  *                                                                       *
  26.  *************************************************************************
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include "replay.h"  // header file generated by MIDL compiler
  33. #include "dict0.h"
  34. #include "util0.h"
  35. #define TAB_STOPS 4
  36. #define RDICT_CURR_RECORD(dict) (((DictState*)dict->state)->curr_record)
  37. char * view = "normal";
  38. void Usage()
  39. {
  40.   printf("Usage : client -sn");
  41.   printf("               -n <server_name>n");
  42.   printf("               -v <view_option>n");
  43.   exit(1);
  44. }
  45. /*************************************************************************/
  46. /***                     Remote Dictionary Print                       ***/
  47. /*************************************************************************/
  48. void
  49. RevPrinTree(
  50.     int lmargin,             /* indentation of the root of the tree     */
  51.     int indent,              /* indentation of subsequent levels        */
  52.     TreeNode *np,            /* pointer to the root node                */
  53.     PrintFun print)          /* short, one line, record print routine   */
  54. {
  55.     static char lspaces[] =
  56. "                                                                                                                                                                                                                                                       ";
  57.     if (np == NULL)
  58.         return;
  59.     RevPrinTree(lmargin+indent, indent, np->left, print);
  60.     if (lmargin > sizeof(lspaces))
  61.         lmargin = sizeof(lspaces);
  62.     lspaces[lmargin] = 0;
  63.     printf(lspaces);
  64.     lspaces[lmargin] = ' ';
  65.     (*print)(np->item);
  66.     RevPrinTree(lmargin+indent, indent, np->right, print);
  67. }
  68. void
  69. LinPrinTree(
  70.     TreeNode *np,           /* pointer to the root node                */
  71.     PrintFun print,         /* short, one line, record print routine   */
  72.     Record * local,         /* local iterator point                    */
  73.     Record * global         /* global iterator point                   */
  74.     )
  75. {
  76.     if (np == NULL)
  77.         return;
  78.     LinPrinTree(np->left, print, local, global);
  79.     if (comp(np->item, local) == 0) {
  80.         if (comp(np->item, global) == 0)
  81.             printf(" ==>> ");
  82.         else
  83.             printf("   >> ");
  84.     }
  85.     else if (comp(np->item, global) == 0)
  86.         printf(" ==   ");
  87.     else
  88.         printf("      ");
  89.     (*print)(np->item);
  90.     LinPrinTree(np->right, print, local, global);
  91. }
  92. void
  93. Clnt_Dict_Print(
  94.     VDict * pvd,
  95.     int indent,
  96.     Record * local,
  97.     Record * global
  98.     )
  99. {
  100.     RDict DictT = {0, 0};
  101.     RDict *prd = &DictT;
  102.     // first: get a new copy a from the server
  103.     VDict_Get_Dict(*pvd, &prd);
  104.     if ( !strcmp(view, "normal") )
  105.         prinTree(0,
  106.                  TAB_STOPS,
  107.                  (TreeNode *) prd->root,
  108.                  printRecord);
  109.     else if ( !strcmp(view, "reverse") || !strcmp(view, "rev") )
  110.         RevPrinTree(0,
  111.                     TAB_STOPS,
  112.                     (TreeNode *) prd->root,
  113.                     printRecord);
  114.     else if ( !strcmp(view, "flat") )
  115.         LinPrinTree((TreeNode *) prd->root,
  116.                     printRecord,
  117.                     local,
  118.                     RDICT_CURR_RECORD(prd));
  119.     RDict_Free_Dict(prd);
  120. }
  121. /*************************************************************************/
  122. /***                     Remote Dictionary Test Loop                   ***/
  123. /*************************************************************************/
  124. void
  125. Usage_Msg()
  126. {
  127.     printf("nUsage: nType a single character, followed by an optional key as follows:nn");
  128.     printf("    i <key> :: Insert <key> into dictionaryn");
  129.     printf("    d <key> :: Delete <key> from dictionaryn");
  130.     printf("    f <key> :: Find <key> in dictionaryn");
  131.     printf("    N       :: next of current item in dictionaryn");
  132.     printf("    P       :: previous of current item in dictionaryn");
  133.     printf("    n       :: Next of local current item in dictionaryn");
  134.     printf("    p       :: Previous of local current item in dictionaryn");
  135.     printf("    h       :: Head (first item) of dictionaryn");
  136.     printf("    t       :: Tail (last item) of dictionaryn");
  137.     printf("    ?       :: Print this messagen");
  138.     printf("    q       :: Quitnn");
  139.     printf("where <key> is <integer> <string>n");
  140. }
  141. void
  142. TestLoop( VDict * pvd )
  143. {
  144.     char currName[80];
  145.     char name[80];
  146.     char op = 0;
  147.     char buffer[80];
  148.     Record r, currRecord;
  149.     Record *pcurrRecord = &currRecord;
  150.     Record *pr = &r;
  151.     Record * pNullRecord = NULL;
  152.     Dict_Status status;
  153.     pcurrRecord->name = currName;
  154.     pr->name = name;
  155.     VDict_Curr_Item(*pvd, &pcurrRecord);
  156.     ItemCopy(pcurrRecord, pr);
  157.     Clnt_Dict_Print(pvd, TAB_STOPS, pcurrRecord, pr);
  158.     Usage_Msg();
  159.     while ( op != 'q' ) {
  160.         printf("nnext op (i d x f n N p P h t ? q): ");
  161.         gets(buffer);
  162.         op = buffer[0];
  163.         if (op == 'i' || op == 'd' || op == 'f' || op == 'I')
  164.             sscanf(buffer+1, "%d %s", &pr->key, pr->name);
  165.         switch (op) {
  166.             case 'h':
  167.                 // get Head of list (first record);
  168.                 status = VDict_Next(*pvd, &pNullRecord);
  169.                 if (pNullRecord != NULL) {
  170.                     ItemCopy(pNullRecord, pcurrRecord);
  171.                     freeRecord(pNullRecord);
  172.                     pNullRecord = NULL;
  173.                 }
  174.                 break;
  175.             case 't':
  176.                 // get Tail of list (last record)
  177.                 status = VDict_Prev(*pvd, &pNullRecord);
  178.                 if (pNullRecord != NULL) {
  179.                     ItemCopy(pNullRecord, pcurrRecord);
  180.                     freeRecord(pNullRecord);
  181.                     pNullRecord = NULL;
  182.                 }
  183.                 break;
  184.             case 'n':
  185.                 // get next record (advance private (local) iterator)
  186.                 status = VDict_Next(*pvd, &pcurrRecord);
  187.                 break;
  188.             case 'p':
  189.                 // get previous record (retreat private (local) iterator)
  190.                 status = VDict_Prev(*pvd, &pcurrRecord);
  191.                 break;
  192.             case 'r':
  193.                 // Reset local iterator to global "current item"
  194.                 status = VDict_Curr_Item(*pvd, &pcurrRecord);
  195.                 break;
  196.             case 'N':
  197.                 // get Next record (advance global iterator)
  198.                 status = VDict_Curr_Next(*pvd, &pr);
  199.                 pr = &r;
  200.                 break;
  201.             case 'P':
  202.                 // get Previous record (retreat global iterator)
  203.                 status = VDict_Curr_Prev(*pvd, &pr);
  204.                 pr = &r;
  205.                 break;
  206.             case 'f':
  207.                 // Find <key>
  208.                 status = VDict_Find(*pvd, &pr);
  209.                 pr = &r;
  210.                 break;
  211.             case 'i':
  212.                 // Insert <key>
  213.                 status = VDict_Insert(*pvd, pr);
  214.                 break;
  215.             case 'I':
  216.                 // Insert (<num'>,"") for all num' s.t. 3 < num' < num
  217.                 status = VDict_I_Dict(*pvd, pr->key);
  218.                 break;
  219.             case 'd':
  220.                 // Delete <key>
  221.                 status = VDict_Delete(*pvd, &pr);
  222.                 if (status != ITEM_NOT_FOUND && status != EMPTY_DICTIONARY) {
  223.                     pr = &r;
  224.                 }
  225.                 break;
  226.             case 'x':
  227.                 // Delete DICT_CURR_ITEM
  228.                 status = VDict_Curr_Delete(*pvd, &pr);
  229.                 if (pr == NULL) {
  230.                     pr = &r;
  231.                 }
  232.                 break;
  233.             case 'X':
  234.                 // Empty the dictionary
  235.                 status = VDict_X_Dict(*pvd);
  236.                 break;
  237.             case '?':
  238.                 Usage_Msg();
  239.                 break;
  240.         }
  241.         if (op != '?' && op != 'q')
  242.             Clnt_Dict_Print(pvd, TAB_STOPS, pcurrRecord, pr);
  243.     }
  244. }
  245. /*************************************************************************/
  246. /***                             Main Loop                             ***/
  247. /*************************************************************************/
  248. void main_dict (short SharedDict)
  249. {
  250.     VDict v_dict = (VDict)0;
  251.     VDict * pvdict;
  252.     pvdict = &v_dict;
  253.     printf ("Getting a new dict...");
  254.     VDict_New( SharedDict, pvdict );
  255.     printf ("Done.n");
  256.     TestLoop(pvdict);
  257. }
  258. int _CRTAPI1
  259. main(int argc, char *argv[])
  260. {
  261.     RPC_STATUS status;
  262.     unsigned char * pszUuid             = NULL;
  263.     unsigned char * pszProtocolSequence = "ncacn_np";
  264.     unsigned char * pszNetworkAddress   = NULL;
  265.     unsigned char * pszEndpoint         = "\pipe\dict";
  266.     unsigned char * pszOptions          = NULL;
  267.     unsigned char * pszStringBinding    = NULL;
  268.     short Shared_Dictionary = 0;  // Share an existing dictionary?
  269.     int i;
  270.     printf ("Microsoft RPC demo Client - Splay (Binary) Tree DataBasen");
  271.     for (i = 1; i < argc; i++) {
  272.         if ((*argv[i] == '-') || (*argv[i] == '/')) {
  273.             switch (tolower(*(argv[i]+1))) {
  274.             case 'n':  // network address
  275.                 pszNetworkAddress = argv[++i];
  276.                 break;
  277.             case 's':
  278.                 Shared_Dictionary = 1;
  279.                 break;
  280.             case 'v':
  281.                 view = argv[++i];
  282.                 break;
  283.             case 'h':
  284.             case '?':
  285.             default:
  286.                 Usage();
  287.             }
  288.         }
  289.         else
  290.             Usage();
  291.     }
  292.     /* The dict_ProtocolStack contains a partially initialized
  293.      * protocol stack for the dict interface.  Fill in the
  294.      * remaining fields:
  295.      */
  296.     status = RpcStringBindingCompose(pszUuid,
  297.                                      pszProtocolSequence,
  298.                                      pszNetworkAddress,
  299.                                      pszEndpoint,
  300.                                      pszOptions,
  301.                                      &pszStringBinding);
  302.     if (status) {
  303.         printf("RpcStringBindingCompose returned 0x%xn", status);
  304.         return(status);
  305.     }
  306.     status = RpcBindingFromStringBinding(pszStringBinding,
  307.                                          &dict_IfHandle);
  308.     if (status) {
  309.         printf("RpcBindingFromStringBinding returned 0x%xn", status);
  310.         return(status);
  311.     }
  312.     status = RpcStringFree(&pszStringBinding);  // remote calls done; unbind
  313.     if (status) {
  314.         printf("RpcStringFree returned 0x%xn", status);
  315.         return(status);
  316.     }
  317.     RpcTryExcept {
  318.        main_dict(Shared_Dictionary);
  319.     }
  320.     RpcExcept(1) {
  321.        printf("RPC runtime raised exception 0x%xn", RpcExceptionCode());
  322.     }
  323.     RpcEndExcept
  324.     status = RpcBindingFree(&dict_IfHandle);  // remote calls done; unbind
  325.     if (status) {
  326.         printf("RpcBindingFree returned 0x%xn", status);
  327.         return(status);
  328.     }
  329.     return(0);
  330. }