home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 376_01 / os2tool.000 / DIRSERV.C < prev    next >
C/C++ Source or Header  |  1992-08-23  |  4KB  |  158 lines

  1. /*
  2. * DIRSERV.C - Testserver for message queues.
  3. *
  4. *
  5. * PROGRAMMER:        Martti Ylikoski
  6. * CREATED:        17.3.1991
  7. */
  8. static char *VERSION = "DIRSERV - Directory Stack Server. Copyright(c) Martti Ylikoski, 1990-1992. Version  1.0" ;
  9. /*
  10. */
  11.  
  12. static char *progname ;
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #define INCL_DOS
  18. #include <os2.h>
  19. //int send_queue(char *queue, char *message, USHORT cbElement, USHORT usRequest) ;
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23. HQUEUE hpushreq, hpopreq ; /* queuehandle */
  24. USHORT ret ;
  25. QUEUERESULT qres ;
  26. PVOID pmesg ;
  27. USHORT cbElement ;
  28. BYTE bElemprty ;
  29. ULONG tsem = 0L ;
  30. USHORT usElementCode ;
  31. int sg, sgpop ;
  32. char *ptmp, queue_name[80], dir[128] ;
  33.  
  34.    progname = argv[0] ;
  35.  
  36.    if (( ret = DosCreateQueue(&hpushreq, QUE_LIFO, "\\queues\\pushreq")) != 0 ||
  37.      (ret = DosCreateQueue(&hpopreq, QUE_FIFO, "\\queues\\popreq")) != 0 )
  38.    {
  39.       fprintf(stderr, "%s: error creating queues...\Exiting...\n", progname ) ;
  40.       return( 1 ) ;
  41.    }
  42.  
  43.    printf("Queues \\queues\\pushreq ja \\queues\\popreq created...\nProcess ready to receive messages...\n") ;
  44.  
  45.    /* read elements from queue and process them until  ... */
  46.    for (;;)
  47.    {
  48.       if (( ret = DosReadQueue(hpopreq, &qres, &cbElement, &pmesg, 0, DCWW_WAIT,
  49.          &bElemprty, NULL)) != 0)
  50.       {
  51.      return( 1 ) ;
  52.       }
  53.  
  54.       if ( qres.usEventCode == 1 )
  55.      return( 0 ) ; /* EXIT-command received */
  56.  
  57.       ptmp = strtok(pmesg, " ") ;
  58.       sgpop = atoi(ptmp) ;
  59.  
  60.       strcpy(queue_name, "\\queues\\sg") ; /* sg prefix - somebody might use PIDs
  61.                           as a name base. We want to minimize
  62.                           risk for a name clash */
  63.       strcat(queue_name, ptmp) ;
  64.  
  65.       DosFreeSeg(SELECTOROF(pmesg)) ;
  66.  
  67.       if ( qres.usEventCode == 2)
  68.       {
  69.      usElementCode = 0 ;
  70.      while (( ret = DosPeekQueue(hpushreq, &qres, &cbElement, &pmesg, &usElementCode,
  71.            DCWW_NOWAIT, &bElemprty, NULL)) == 0)
  72.      {
  73.         sscanf(pmesg, "%d %s", &sg, dir) ;
  74.         if (sg == sgpop)
  75.         {
  76.            QueSend( queue_name, dir, cbElement, 0, NULL, progname) ;
  77.         }
  78.      }
  79.      QueSend( queue_name, ".", 2, 3, NULL, progname) ; /* End of DIRS */
  80.       }
  81.       else
  82.       {
  83.      /* process message... */
  84.      usElementCode = 0 ;
  85.      do
  86.      {
  87.         if (( ret = DosPeekQueue(hpushreq, &qres, &cbElement, &pmesg, &usElementCode,
  88.            DCWW_NOWAIT, &bElemprty, NULL)) != 0)
  89.         {
  90.            strcpy(dir, ". " ) ;
  91.            break ;
  92.         }
  93.  
  94.         sscanf(pmesg, "%d ", &sg) ;
  95.      } while (sg != sgpop) ;
  96.  
  97.      if (ret == 0)
  98.      {
  99.         if (( ret = DosReadQueue(hpushreq, &qres, &cbElement, &pmesg, usElementCode, DCWW_WAIT,
  100.             &bElemprty, NULL)) != 0)
  101.         {
  102.            return( 1 ) ;
  103.         }
  104.  
  105.         ptmp = strtok(pmesg, " ") ;
  106.         sg = atoi(ptmp) ;
  107.         ptmp = strtok(NULL, " ") ;
  108.         strcpy(dir, ptmp) ;
  109.         DosFreeSeg(SELECTOROF(pmesg)) ;
  110.      }
  111.  
  112.      printf("popdir = %s\n", dir) ;
  113.      QueSend( queue_name, dir, cbElement, 0, NULL, progname) ;
  114.       } /* else */
  115.    } /* for(;;) */
  116.  
  117.    DosCloseQueue(hpushreq) ;
  118.    DosCloseQueue(hpopreq) ;
  119.  
  120.    return( 0 ) ;
  121. }
  122. /*
  123. int send_queue(char *queue_name, char *message, USHORT cbElement, USHORT usRequest)
  124. {
  125. HQUEUE hpopresp ;
  126. USHORT ret, qowner ;
  127. SEL mesg, selRecipient ;
  128. PCH ptr ;
  129.  
  130.       if (( ret = DosOpenQueue(&qowner, &hpopresp, queue_name)) != 0)
  131.       {
  132.      fprintf(stderr, "%s: error opening queue %s...\nExiting...\n", progname, queue_name) ;
  133.      return( 1 ) ;
  134.       }
  135.       if (( ret = DosAllocSeg(512, &mesg, SEG_GIVEABLE)) != 0)
  136.       {
  137.      fprintf(stderr, "%s: error in DosAllocSeg...\nExiting...\n", progname) ;
  138.      return( 1 ) ;
  139.       }
  140.  
  141.       if (( ret = DosGiveSeg(mesg, qowner, &selRecipient)) != 0)
  142.       {
  143.      fprintf(stderr, "%s: error in DosGiveSeg...\nExiting...\n", progname) ;
  144.      return( 1 ) ;
  145.       }
  146.       ptr =  MAKEP(mesg, 0) ;
  147.       strcpy(ptr, message) ;
  148.       if (( ret = DosWriteQueue(hpopresp, usRequest, cbElement, ptr, 0)) != 0)
  149.       {
  150.      fprintf(stderr, "%s: error in DosWriteQueue...\nExiting...\n", progname) ;
  151.      return( 1 ) ;
  152.       }
  153.  
  154.       DosCloseQueue(hpopresp) ;
  155.    return( 0 ) ;
  156. }
  157. */
  158.