home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 332.lha / IPC_nov89 / Sources / SimpleClient.c < prev    next >
C/C++ Source or Header  |  1990-01-09  |  6KB  |  182 lines

  1. /************************************************************
  2.  *                                                          *
  3.  *           Simple Client Demo for IPC                     *
  4.  *                                                          *
  5.  *                Pete Goodeve 89:4:01                      *
  6.  *                                                          *
  7.  *  [This module has only been compiled under Lattice;      *
  8.  *   it will need some modification for Manx/Aztec;         *
  9.  *   ... I'd be very grateful if someone would do the       *
  10.  *   conversion...]                                         *
  11.  *                                                          *
  12.  *                                                          *
  13.  *  To exit the client program type an 'end-of-file'        *
  14.  *  (cntrl-'\').                                            *
  15.  *                                                          *
  16.  *  It is a simple "synchronous" client: when it sends a    *
  17.  *  message it waits for the reply before continuing        *
  18.  *  (unlike others that may continue with other activities  *
  19.  *  while the server is processing a message).  The main    *
  20.  *  loop is simple:                                         *
  21.  *                                                          *
  22.  *          Wait for line from keyboard command             *
  23.  *          Send message                                    *
  24.  *          Wait for reply                                  *
  25.  *          Loop to wait for keyboard again.                *
  26.  *                                                          *
  27.  *                                                          *
  28.  ************************************************************/
  29.  
  30. #ifdef LATTICE
  31. #if LATTICE_40 | LATTICE_50
  32. #include "IPC_proto.h"
  33. /* ...else (not recent Lattice) will need library linkage stubs (IPC.o) */
  34. #include <proto/exec.h>
  35. /**  if no proto/exec.h, you also should define
  36. * struct MsgPort * CreatePort(char *, int);
  37. ..**/
  38. #endif
  39. #endif
  40.  
  41. #include "IPC.h"
  42. #include <exec/memory.h>
  43. #include <stdio.h>
  44.  
  45. #define LINESZ 255
  46.  
  47. /*
  48.  *  Define the ID codes recognized by the print format server
  49.  *
  50.  *  (MAKE_ID is defined in IPC.h)
  51.  */
  52.  
  53. /* Message IDs: */
  54. #define CNVA  MAKE_ID('C','N','V','A')
  55.     /* CoNVert to Ascii */
  56.  
  57. /* Item IDs: */
  58.  
  59. #define LINE  MAKE_ID('L','I','N','E')
  60.     /* indicates a complete line of text -- omitting newline */
  61.  
  62. #define TEXT  MAKE_ID('T','E','X','T')
  63.     /* Text block -- may include newlines */
  64.  
  65. #define STRG  MAKE_ID('S','T','R','G')
  66.     /* general non-specific ASCII STRinG */
  67.  
  68.  
  69. struct Library * IPCBase = NULL;
  70.  
  71. struct IPCPort *port=NULL; /* will point to server port */
  72. struct MsgPort *rport=NULL; /* where we get our replies */
  73. struct IPCMessage *imsg=NULL; /* this one message is used repeatedly */
  74.  
  75. void Cleanup();
  76.  
  77.  
  78. /******************************
  79.  *                            *
  80.  *  Main program entry point: *
  81.  *                            *
  82.  ******************************/
  83.  
  84. void main()
  85. {
  86.     struct IPCItem *item, *item0; /* pointers to access message items */
  87.  
  88.  
  89.     char tbuf[LINESZ+1]; /* keyboard input dumped in here */
  90.  
  91.     /****************************************************/
  92.     /** Before anything else, we need the IPC Library: **/
  93.     /****************************************************/
  94.     IPCBase = OpenLibrary("ppipc.library",0);
  95.     if (!IPCBase) {
  96.         puts(
  97.         "Couldn't find ppipc.library!\nHave you installed it in LIBS: ?\n");
  98.         exit(20);
  99.     }
  100.  
  101.     /********************************/
  102.     /** First we set up our ports: **/
  103.     /********************************/
  104.  
  105.     port = GetIPCPort("Demo");
  106.     if (!port) {
  107.         Cleanup();
  108.         exit(21);
  109.     }
  110.  
  111.     rport = CreatePort(NULL,0);
  112.     if (!rport) {
  113.         Cleanup();
  114.         exit(21);
  115.     }
  116.  
  117.  
  118.     /***********************************/
  119.     /** ... then make a message block **/
  120.     /***********************************/
  121.     imsg = CreateIPCMsg(2,0,rport); /* large enough for playing with... */
  122.     /* remember to add more items here if we need them...! */
  123.     if (!imsg) {Cleanup(); exit(22);}
  124.     imsg->ipc_Id = CNVA; /* any message ID will do (except for QUIT) */
  125.     /*.. and note that this simple client doesn't send QUIT */
  126.  
  127.     item0 = &imsg->ipc_Items[0]; /* a convenient permanent reference */
  128.  
  129.  
  130.     /***********************************************************/
  131.     /* The main program loop:                                  */
  132.     /* -- continues until told to quit by end-of-file (ctrl-\) */
  133.     /***********************************************************/
  134.     while (1) {
  135.         item = item0; /* reset item pointer */
  136.  
  137.         /* get keyboard input (terminated by return): */
  138.         /* E-O-F returns a length of zero, causing break from loop */
  139.         if (fgets(tbuf, LINESZ, stdin) == 0) break;
  140.  
  141.         tbuf[strlen(tbuf)-1] = '\0'; /* we assume it ended with a newline
  142.                                         -- so we remove it */
  143.  
  144.     /*******************************/
  145.     /** Now we build the message: **/
  146.     /*******************************/
  147.  
  148.         item->ii_Id = STRG; /* A short string to start out with */
  149.         item->ii_Ptr = (void *)("Got message");
  150.         item->ii_Size = strlen(item->ii_Ptr);
  151.         item++;
  152.         item->ii_Id = LINE; /* now use the line from keyboard */
  153.         item->ii_Ptr = (void *)tbuf;
  154.         item->ii_Size = strlen(item->ii_Ptr);
  155.         item++;
  156.         /* Note that the Client retains ownership */
  157.  
  158.         /* Send the message (if possible) */
  159.         if (!PutIPCMsg(port, imsg)) {
  160.                 puts("No server!\n");
  161.                 continue; /* don't wait for a reply that won't come! */
  162.         }
  163.         /* Wait at the reply port (we assume a reply WILL come!): */
  164.         WaitPort(rport);
  165.         GetMsg(rport);  /* we assume we know what's there! */
  166.     }
  167.     /*** end of main loop ***/
  168.  
  169.     Cleanup();
  170. }
  171.  
  172.  
  173. void Cleanup()
  174. {
  175.     if (port) DropIPCPort(port);
  176.     if (rport) DeletePort(rport);
  177.     if (imsg) DeleteIPCMsg(imsg);
  178.     CloseLibrary(IPCBase);
  179. }
  180.  
  181.  
  182.