home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / debug / tracebox / querdr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  6.6 KB  |  228 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.   #ifndef WIN32_LEAN_AND_MEAN
  15.     #define WIN32_LEAN_AND_MEAN
  16.   #endif
  17.   #include <windows.h>
  18. #endif
  19.  
  20. #include <iexcept.hpp>
  21. #include <icnrobj.hpp>
  22. #include <ihandle.hpp>
  23. #include <ithread.hpp>
  24. #include "querdr.hpp"
  25. #include "trbrowse.h"
  26.  
  27. #if (IC_MAJOR_VERSION < 320)
  28.   #define IBaseErrorInfo IErrorInfo
  29. #endif
  30.  
  31. #define     BUFFERSIZE      999
  32.  
  33. #ifdef IC_PM
  34. const char     QUEUE_PATH[] = "\\QUEUES\\";
  35. #else
  36. const char     QUEUE_PATH[]   = "\\\\.\\mailslot\\";
  37. const char     QUITMESSAGE[]  = "QuIt";
  38. const unsigned QUITMESSAGELEN = 4;
  39. #endif
  40.  
  41. // Set up the queue for reading.
  42. QueueReader::QueueReader ( const char*           queueName,
  43.                            const IWindowHandle&  targetWindow)
  44.                : target(targetWindow),
  45.                  qHandle (0),
  46.                  queueData( 0 ),
  47.                  fenabled ( true ),
  48.                  finputFilter( ),
  49.                  ffilterLock( ),
  50.                  fqueueLock( ),
  51.                  fRunning( false )
  52. {
  53.   fqueueName = IString(QUEUE_PATH) + IString(queueName);
  54. #ifdef IC_PM
  55.   unsigned long rc = DosCreateQueue(
  56.                        &qHandle,
  57.                        QUE_FIFO | QUE_CONVERT_ADDRESS,
  58.                        fqueueName);
  59.   if (rc!=0)
  60.     ITHROWSYSTEMERROR(rc, "DosCreateQueue",
  61.                       IBaseErrorInfo::accessError,
  62.                       IException::recoverable );
  63. #else
  64.   qHandle = (unsigned long)
  65.     CreateMailslot(
  66.       fqueueName,                      /* identifier                      */
  67.       BUFFERSIZE,                      /* maximum message size            */
  68.       MAILSLOT_WAIT_FOREVER,           /* no time-out for read operations */
  69.       (LPSECURITY_ATTRIBUTES) NULL);   /* no security attributes     */
  70.   if ( qHandle == (unsigned long)INVALID_HANDLE_VALUE )
  71.     ITHROWGUIERROR2("CreateMailSlot",
  72.                    IBaseErrorInfo::accessError,
  73.                    IException::recoverable );
  74.  
  75.   queueData = (char *)GlobalAlloc( GPTR, BUFFERSIZE+1 );
  76. #endif
  77.  
  78.   // Make first entry the queue name.
  79.   if (isEnabled())
  80.     {
  81.     IContainerObject* pobj = new IContainerObject(IString("Queue: ") + fqueueName);
  82.     targetHandle().postEvent(ADD_OBJECT, pobj);
  83.     }
  84. }
  85.  
  86. // Delete the queue.
  87. QueueReader::~QueueReader ( )
  88. {
  89. #ifdef IC_PM
  90.   DosCloseQueue(queueHandle());
  91.   if (queueData)
  92.     DosFreeMem(queueData);
  93. #else
  94.   DWORD written;
  95.   HANDLE queue = CreateFile( fqueueName,
  96.                              GENERIC_WRITE,
  97.                              FILE_SHARE_READ,
  98.                              (LPSECURITY_ATTRIBUTES)NULL,
  99.                              OPEN_EXISTING,
  100.                              FILE_ATTRIBUTE_NORMAL,
  101.                              (HANDLE)NULL );
  102.   if (queue &&
  103.       WriteFile(  queue,
  104.                   QUITMESSAGE,
  105.                   QUITMESSAGELEN,
  106.                   &written,
  107.                   NULL ))
  108.      {
  109.      CloseHandle( queue );
  110.      IResourceLock lock(fqueueLock);
  111.      CloseHandle( (HANDLE)queueHandle() );
  112.      if (queueData)
  113.         GlobalFree((HGLOBAL)queueData );
  114.      }
  115.   else
  116.      {
  117.      ITHROWGUIERROR2("WriteFile",
  118.                     IBaseErrorInfo::accessError,
  119.                     IException::recoverable );
  120.      }
  121. #endif
  122. }
  123.  
  124. // Our Thread function that reads the queue.
  125. void QueueReader::run ( )
  126. {
  127.   IContainerObject* pobj;
  128.   unsigned long   dataLength;
  129.  
  130. #ifdef IC_PM
  131.   unsigned long   rc;
  132.   REQUESTDATA     request;
  133.   BYTE            priority = 0;
  134.  
  135.   request.pid = IThread::current().id();
  136. #endif
  137.  
  138.   // If already started just return.
  139.   if (fRunning)
  140.      return;
  141.   else
  142.      fRunning = true;
  143.  
  144.   {  // Establish scope for fqueueLock.
  145.  
  146.     //Lock the queue.
  147.     IResourceLock lock( fqueueLock );
  148.  
  149.     while(fRunning)
  150.       {
  151.       dataLength = 0;
  152. #ifdef IC_PM
  153.       rc = DosReadQueue (queueHandle(),
  154.                          &request,
  155.                          &dataLength,
  156.                          (void**)&queueData,
  157.                          0,
  158.                          0,
  159.                          &priority,
  160.                          0);
  161.       if(rc!=0)
  162.         ITHROWSYSTEMERROR(rc, "DosReadQueue",
  163.                           IBaseErrorInfo::accessError,
  164.                           IException::recoverable );
  165. #else
  166.       ReadFile( (HANDLE)queueHandle(),
  167.                 queueData,
  168.                 BUFFERSIZE,
  169.                 &dataLength,
  170.                 (LPOVERLAPPED)NULL );
  171.       queueData[dataLength] = '\0';
  172.       if ((dataLength == QUITMESSAGELEN) &&
  173.           (strcmp(queueData, QUITMESSAGE) == 0) )
  174.         fRunning = false;
  175.  
  176. #endif
  177.  
  178.       // Create an object and post a request to the main
  179.       // thread to add it to the container.
  180.       if ( isEnabled() && isIncluded(queueData) && fRunning )
  181.         pobj = new IContainerObject(queueData);
  182.       else
  183.         pobj = 0;
  184.  
  185. #ifdef IC_PM
  186.       DosFreeMem(queueData);
  187.       queueData = 0;
  188. #endif
  189.       Boolean loop = pobj ? true : false;
  190.       while(loop)
  191.          {
  192.          try
  193.            {
  194.            loop = false;
  195.            targetHandle().postEvent(ADD_OBJECT, pobj);
  196.            }
  197.          catch (IException& )
  198.            {
  199.            // If we can't post (message queue full?),
  200.            // wait a bit and try again.
  201.            loop = true;
  202.   #ifdef IC_PM
  203.            DosSleep(100);
  204.   #endif
  205.            }
  206.          }  // while posting
  207.       } // while
  208.   }  // scope of fqueueLock
  209. }
  210.  
  211. /*------------------------------------------------------------------------------
  212. | IBase::Boolean QueueReader::isIncluded                                       |
  213. ------------------------------------------------------------------------------*/
  214. IBase::Boolean QueueReader::isIncluded ( const char* string ) const
  215. {
  216.    IString           inputString(string);
  217.    IResourceLock     lock( ((QueueReader*)this)->filterLock() );
  218.    Boolean           result = true;
  219.    FilterSet::Cursor cursor( ((QueueReader*)this)->inputFilterSet() );
  220.    for (cursor.setToFirst(); (result && cursor.isValid()); cursor.setToNext() )
  221.       {
  222.       if ( inputString.indexOf( cursor.element() ) )
  223.          result = false;
  224.       }
  225.    return ( ((QueueReader*)this)->inputFilterSet().isExcludeList ) ?
  226.            result : !result ;
  227. }
  228.