home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / debug / prtque / querdr.cpp < prev    next >
Text File  |  1996-10-29  |  4KB  |  150 lines

  1. //************************************************************
  2. // Problem Determination  - Trace Queue Browser
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //************************************************************
  8. #include <ibase.hpp>
  9. #ifdef IC_PM
  10.   #define INCL_DOSQUEUES
  11.   #define INCL_DOSPROCESS
  12.   #include <os2.h>
  13. #else
  14.   #include <windows.h>
  15. #endif
  16.  
  17. #include <iexcept.hpp>
  18. #include <icnrobj.hpp>
  19. #include <ihandle.hpp>
  20. #include <ithread.hpp>
  21. #include "querdr.hpp"
  22. #include "trbrowse.h"
  23.  
  24. #if (IC_MAJOR_VERSION < 320)
  25.   #define IBaseErrorInfo IErrorInfo
  26. #endif
  27.  
  28. #define     BUFFERSIZE      999
  29.  
  30. #ifdef IC_PM
  31. const char     QUEUE_PATH[] = "\\QUEUES\\";
  32. #else
  33. const char     QUEUE_PATH[]   = "\\\\.\\mailslot\\";
  34. #endif
  35.  
  36. // Set up the queue for reading.
  37. QueueReader::QueueReader ( const char*           queueName,
  38.                            const IWindowHandle&  targetWindow)
  39.                : target   (targetWindow),
  40.                  qHandle  ( 0 ),
  41.                  queueData( 0 )
  42. {
  43.   fqueueName = IString(QUEUE_PATH) + IString(queueName);
  44. #ifdef IC_PM
  45.   unsigned long rc = DosCreateQueue(
  46.                        &qHandle,
  47.                        QUE_FIFO | QUE_CONVERT_ADDRESS,
  48.                        fqueueName);
  49.   if (rc!=0)
  50.     ITHROWSYSTEMERROR(rc, "DosCreateQueue",
  51.                       IBaseErrorInfo::accessError,
  52.                       IException::recoverable );
  53. #else
  54.   qHandle = (unsigned long)
  55.     CreateMailslot(
  56.       fqueueName,                      /* identifier                      */
  57.       BUFFERSIZE,                      /* maximum message size            */
  58.       MAILSLOT_WAIT_FOREVER,           /* no time-out for read operations */
  59.       (LPSECURITY_ATTRIBUTES) NULL);   /* no security attributes     */
  60.   if ( qHandle == (unsigned long)INVALID_HANDLE_VALUE )
  61.     ITHROWGUIERROR2("CreateMailSlot",
  62.                    IBaseErrorInfo::accessError,
  63.                    IException::recoverable );
  64.  
  65.   queueData = (char *)GlobalAlloc( GPTR, BUFFERSIZE+1 );
  66. #endif
  67. }
  68.  
  69. // Delete the queue.
  70. QueueReader::~QueueReader ( )
  71. {
  72. #ifdef IC_PM
  73.   DosCloseQueue(queueHandle());
  74.   if (queueData)
  75.     DosFreeMem(queueData);
  76. #else
  77.   CloseHandle( (HANDLE)queueHandle() );
  78.   if (queueData)
  79.      GlobalFree((HGLOBAL)queueData );
  80. #endif
  81. }
  82.  
  83. // Our Thread function that reads the queue.
  84. void QueueReader::run ( )
  85. {
  86.   IContainerObject* pobj;
  87.   unsigned long   dataLength;
  88.  
  89. #ifdef IC_PM
  90.   unsigned long   rc;
  91.   REQUESTDATA     request;
  92.   BYTE            priority = 0;
  93.  
  94.   request.pid = IThread::current().id();
  95. #endif
  96.  
  97.   while(1)
  98.     {
  99.     dataLength = 0;
  100. #ifdef IC_PM
  101.     rc = DosReadQueue (queueHandle(),
  102.                        &request,
  103.                        &dataLength,
  104.                        (void**)&queueData,
  105.                        0,
  106.                        0,
  107.                        &priority,
  108.                        0);
  109.     if(rc!=0)
  110.       ITHROWSYSTEMERROR(rc, "DosReadQueue",
  111.                         IBaseErrorInfo::accessError,
  112.                         IException::recoverable );
  113. #else
  114.     ReadFile( (HANDLE)queueHandle(),
  115.               queueData,
  116.               BUFFERSIZE,
  117.               &dataLength,
  118.               (LPOVERLAPPED)NULL );
  119.     queueData[dataLength] = '\0';
  120. #endif
  121.  
  122.     // Create an object and post a request to the main
  123.     // thread to add it to the container.
  124.     pobj = new IContainerObject(queueData);
  125.  
  126. #ifdef IC_PM
  127.     DosFreeMem(queueData);
  128.     queueData = 0;
  129. #endif
  130.     Boolean loop = true;
  131.     while(loop)
  132.        {
  133.        try
  134.          {
  135.          loop = false;
  136.          targetHandle().postEvent(ADD_OBJECT, pobj);
  137.          }
  138.        catch (IException& )
  139.          {
  140.          // If we can't post (message queue full?),
  141.          // wait a bit and try again.
  142.          loop = true;
  143. #ifdef IC_PM
  144.          DosSleep(100);
  145. #endif
  146.          }
  147.        }  // while posting
  148.     } // while
  149. }
  150.