home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / sysembed / sampipc.elh < prev    next >
Encoding:
Text File  |  1995-03-30  |  5.2 KB  |  177 lines

  1. [ 8. Sample IPC Application]
  2.                  EMBEDDED LAN SAMPLE IPC APPLICATION
  3. ═════════════════════════════════════════════════════════════════════════
  4. The Embedded LAN Inter-Process Communication Interface is easily called
  5. in C, just like the Transport Driver Interface.  The following simple IPC
  6. application shows how easy it is to send and receive data on an IPC queue
  7. no matter where it is located.  The Embedded LAN IPC Provider translates
  8. names of queues and message ports such as "\\JAGUAR\JUNGLE" into their
  9. component parts (JAGUAR is the machine name, and JUNGLE in this case is
  10. the queue/message port name), and also allows programs running on the
  11. JAGUAR machine to access its queue or message port as "JUNGLE" without
  12. specifying the full syntax.
  13.  
  14. //
  15. // PROGRAM NAME:  TESTIPC.C.
  16. //
  17. // FUNCTIONAL DESCRIPTION.
  18. //      This program is a test program for the IPC interface.
  19. //
  20. // MODIFICATION HISTORY.
  21. //      S. E. Jones     93/03/18.       Original for Embedded LAN.
  22. //
  23. // NOTICE:  Copyright (C) 1993 General Software, Inc.
  24. //          All rights reserved.
  25. //
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <conio.h>
  30. #include <malloc.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <time.h>
  34. #include <dos.h>
  35. #include "ipc.h"
  36. #include "..\inc\kernel.h"
  37.  
  38. VOID SleepTimerExpireRoutine (HANDLE EventHandle)
  39. {
  40.     SetEvent (EventHandle);
  41. } // SleepTimerExpireRoutine
  42.  
  43. VOID SleepMs (USHORT NumberOfMilliseconds)
  44. {
  45.     HANDLE SleepEvent, SleepTimer;
  46.  
  47.     if (NumberOfMilliseconds == 0) {
  48.         PassTimeSlice ();
  49.         return;
  50.     }
  51.  
  52.     AllocateEvent (&SleepEvent);
  53.     AllocateTimer (&SleepTimer, SleepEvent, SleepTimerExpireRoutine);
  54.  
  55.     StartTimer (SleepTimer, NumberOfMilliseconds);
  56.     WaitEvent (SleepEvent);
  57.  
  58.     DeallocateEvent (SleepEvent);
  59.     DeallocateTimer (SleepTimer);
  60. } // SleepMs
  61.  
  62. VOID main (argc, argv)
  63.     USHORT argc;
  64.     UCHAR *argv [];
  65. {
  66.     IPCSTATUS rc;
  67.     IPCHANDLE Handle, Handle2;
  68.     ULONG InValue=0L;
  69.     ULONG OutValue;
  70.     ULONG Counter=1L;
  71.     ULONG Count=0L;
  72.     ULONG MaxCount = 0xffffffffL;
  73.  
  74.     if (argc == 3) {
  75.         MaxCount = atol (argv [2]);
  76.         if (MaxCount == 0L) {
  77.             printf ("The maxcount parameter must be greater than 0.\n");
  78.             exit (1);
  79.         }
  80.     } else if (argc != 2) {
  81.         printf ("Usage:  TESTIPC queuename\n");
  82.         exit (1);
  83.     }
  84.  
  85.     printf ("Opening queue '%s' for %lu cycles.\n", argv [1], MaxCount);
  86.  
  87.     printf ("           queue operations (open,push,pop,close)\r");
  88.  
  89.     while (TRUE) {
  90.         if (Count == MaxCount) {
  91.             break;
  92.         }
  93.         Count++;
  94.  
  95.         SleepMs (500);
  96.  
  97.         if (kbhit ()) {
  98.             getch ();                   // eat the character.
  99.             break;
  100.         }
  101.  
  102.         printf ("%8ld\r", Counter++);
  103.         rc = IpcCreateQueue (argv [1], &Handle);
  104.         if (rc != IPCSTATUS_SUCCESS) {
  105.             printf ("IpcCreateQueue returned status %u (0x%04x).\n", rc, rc);
  106.             exit (2);
  107.         }
  108.  
  109.         printf ("%8ld\r", Counter++);
  110.         rc = IpcPushQueue (Handle, (PVOID)InValue);
  111.         if (rc != IPCSTATUS_SUCCESS) {
  112.             printf ("IpcPushQueue returned status %u (0x%04x).\n", rc, rc);
  113.             IpcCloseQueue (Handle);
  114.             exit (3);
  115.         }
  116.  
  117.         printf ("%8ld\r", Counter++);
  118.         rc = IpcPopQueue (Handle, &(PVOID)OutValue);
  119.         if (rc != IPCSTATUS_SUCCESS) {
  120.             printf ("IpcPopQueue returned status %u (0x%04x).\n", rc, rc);
  121.             IpcCloseQueue (Handle);
  122.             exit (4);
  123.         }
  124.  
  125.         if (InValue != OutValue) {
  126.             printf ("Value pushed (0x%08lx) is not equal to value popped (0x%08lx).\n",
  127.                     InValue, OutValue);
  128.             IpcCloseQueue (Handle);
  129.             exit (5);
  130.         }
  131.  
  132.         InValue++;
  133.  
  134.         //
  135.         // Open up a second handle and append something to the queue.
  136.         // Then have the first handle instance pop it off.
  137.         //
  138.  
  139.         printf ("%8ld\r", Counter++);
  140.         rc = IpcOpenQueue (argv [1], &Handle2);
  141.         if (rc != IPCSTATUS_SUCCESS) {
  142.             printf ("IpcOpenQueue returned status %u (0x%04x).\n", rc, rc);
  143.             exit (2);
  144.         }
  145.         printf ("%8ld\r", Counter++);
  146.         rc = IpcAppendQueue (Handle2, (PVOID)InValue);
  147.         if (rc != IPCSTATUS_SUCCESS) {
  148.             printf ("IpcAppendQueue returned status %u (0x%04x).\n", rc, rc);
  149.             IpcCloseQueue (Handle);
  150.             exit (3);
  151.         }
  152.         IpcCloseQueue (Handle2);
  153.  
  154.         printf ("%8ld\r", Counter++);
  155.         rc = IpcPopQueue (Handle, &(PVOID)OutValue);
  156.         if (rc != IPCSTATUS_SUCCESS) {
  157.             printf ("IpcPopQueue #2 returned status %u (0x%04x).\n", rc, rc);
  158.             IpcCloseQueue (Handle);
  159.             exit (4);
  160.         }
  161.  
  162.         printf ("%8ld\r", Counter++);
  163.         IpcCloseQueue (Handle);
  164.         if (rc != IPCSTATUS_SUCCESS) {
  165.             printf ("IpcCloseQueue returned status %u (0x%04x).\n", rc, rc);
  166.             exit (5);
  167.         }
  168.     }
  169.  
  170.     //
  171.     // Eat the character and exit the program.
  172.     //
  173.  
  174.     printf ("\nIPC test completed.\n");
  175.     exit (0);                           // exit to DOS with success.
  176. } /* testipc.c */
  177.