home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / os2 / rxu11.zip / CHECK4TO.CMD next >
OS/2 REXX Batch file  |  1993-04-08  |  6KB  |  176 lines

  1. /* Rexx */
  2.  
  3. /*******************************************************************
  4.  
  5.   This Rexx program is a generic timeout detecting function which can
  6.   be used to time some activity a Rexx program is performing, and
  7.   perform some timeout-handling action when a specified time limit
  8.   has been exceeded.  The general scheme of use is as such:
  9.  
  10.   handler_string = "Say 'Timeout detected!!'"
  11.   timerid = check4to('Init',handler_string,10000)  // wait 10 seconds
  12.   ...
  13.   Call check4to 'Start', timerid
  14.   {
  15.    ... do whatever you want to time ...
  16.   }
  17.   Call check4to 'Stop', timerid
  18.   ...
  19.   Call check4to 'End', timerid
  20.  
  21.   ==================================================================
  22.  
  23.   The argument list for "check4to" is:
  24.  
  25.     timerid = check4to(func, handler_string, timelimit, arglist)
  26.  
  27.   where:
  28.  
  29.     func         = 'INIT', 'START', 'STOP', 'END'
  30.     handler_code = Rexx program source string to be executed when
  31.                    a timeout is detected
  32.     timelimit    = integer indicating how many miliseconds timer
  33.                    is to wait before executing "handler_string"
  34.     arglist      = argument string for "handler_string" program
  35.     timerid      = token returned when func = 'INIT' and used on
  36.                    subsequent calls (func = 'START','STOP','END')
  37.  
  38.   ==================================================================
  39.  
  40.   This program illustrates one way in which you can use some of the
  41.   functions from the YDBAUTIL package.  Of course, you could modify
  42.   this code to call a normal on-disk Rexx program (i.e. a .CMD file)
  43.   instead of the "handler_string" for instance.  Or you might want
  44.   to keep all the semaphore handles in a blob of allocated memory
  45.   (RxAllocMem) rather than in a Rexx Queue.  In any event, this
  46.   should demonstrate how you can build some quite sophisticated
  47.   programs entirely in Rexx.
  48.  
  49.   ==================================================================
  50.  
  51.   This program demonstrates the use of the following functions in
  52.   the YDBAUTIL function package:
  53.  
  54.   - RxCreateRexxThread()
  55.   - RxCallInstore()
  56.   - RxCreateEventSem()
  57.   - RxPostEventSem()
  58.   - RxResetEventSem()
  59.   - RxWaitEventSem()
  60.   - RxQueryEventSem()
  61.   - RxCloseEventSem()
  62.   - RxPullQueue()
  63.   - RxAddQueue()
  64.  
  65. *******************************************************************/
  66.  
  67. If rxfuncquery('RxCreateRexxThread') Then
  68.   Do
  69.   Call rxfuncadd 'rxydbautilinit','ydbautil','rxydbautilinit'
  70.   Call rxydbautilinit
  71.   End
  72.  
  73. Arg func
  74.  
  75. Select
  76.   When Left(func,3) = 'INI' Then
  77.     Do
  78.     Return Initialize_Timeout_Thread(Arg(2),Arg(3),Arg(4))
  79.     End
  80.   When Left(func,3) = 'STA' Then
  81.     Do
  82.     Call Get_Timeout_Info Arg(2)
  83.     Call rxwaiteventsem hev_enable
  84.     Call rxreseteventsem hev_enable
  85.     Call rxposteventsem hev_start
  86.     End
  87.   When Left(func,3) = 'STO' Then
  88.     Do
  89.     Call Get_Timeout_Info Arg(2)
  90.     Call rxposteventsem hev_free
  91.     End
  92.   When Left(func,3) = 'END' Then
  93.     Do
  94.     Call Get_Timeout_Info Arg(2)
  95.     Call rxposteventsem hev_free
  96.     Call rxwaiteventsem hev_enable
  97.     Call rxreseteventsem hev_enable
  98.     Call rxposteventsem hev_term
  99.     Call rxposteventsem hev_start
  100.     Call rxposteventsem hev_free
  101.     End
  102.   Otherwise
  103.     Nop
  104. End
  105.  
  106. Exit
  107.  
  108. /*                                                                */
  109. /*    Get timeout information from queue                          */
  110. /*                                                                */
  111. Get_Timeout_Info: Procedure Expose hev_enable hev_start hev_free hev_term tid
  112. Arg qname
  113. If \RxQExists(qname) Then
  114.   Exit
  115. hev_enable = rxpullqueue(qname);Call rxaddqueue hev_enable,qname
  116. hev_start  = rxpullqueue(qname);Call rxaddqueue hev_start,qname
  117. hev_free   = rxpullqueue(qname);Call rxaddqueue hev_free,qname
  118. hev_term   = rxpullqueue(qname);Call rxaddqueue hev_term,qname
  119. tid        = rxpullqueue(qname);Call rxaddqueue tid,qname
  120. If hev_enable = '' | hev_start = '' | hev_free = '' | hev_term = '',
  121.  | tid = '' Then
  122.   Exit
  123. Return
  124.  
  125. /*                                                                */
  126. /*    Initialize Timeout Checking Thread                          */
  127. /*                                                                */
  128. Initialize_Timeout_Thread: Procedure
  129.  
  130. Parse Arg handler, timelimit, arglist
  131.  
  132. If handler = '' Then
  133.   Exit
  134. If timelimit = '' Then
  135.   timelimit = 30000    /* 30 seconds, by default */
  136. If \DataType(timelimit,'W') Then
  137.   Exit
  138.  
  139. /* Prepare for and start timeout-checking thread */
  140. tempq = RxQueue('Create')
  141. semrc = rxcreateeventsem('hev_enable')
  142. semrc = rxcreateeventsem('hev_start')
  143. semrc = rxcreateeventsem('hev_free')
  144. semrc = rxcreateeventsem('hev_term')
  145. crlf = '0d0a'x;eof = '1a'x
  146. pstr =       "parse arg hev_enable, hev_start, hev_free, hev_term, timelimit, handler, arglist, qname"crlf
  147. pstr = pstr||"Do Forever"crlf
  148. pstr = pstr||"  Call rxposteventsem hev_enable"crlf
  149. pstr = pstr||"  Call rxwaiteventsem hev_start"crlf
  150. pstr = pstr||"  Call rxreseteventsem hev_start"crlf
  151. pstr = pstr||"  waitrc = rxwaiteventsem(hev_free,timelimit)"crlf
  152. pstr = pstr||"  If waitrc = 640 Then"crlf
  153. pstr = pstr||"    Call RxCallInStore handler, arglist, timelimit"crlf
  154. pstr = pstr||"  terminfo = rxqueryeventsem(hev_term)"crlf
  155. pstr = pstr||"  If Word(terminfo,2) > 0 Then"crlf
  156. pstr = pstr||"    Do"crlf
  157. pstr = pstr||"    Call rxcloseeventsem hev_enable"crlf
  158. pstr = pstr||"    Call rxcloseeventsem hev_start"crlf
  159. pstr = pstr||"    Call rxcloseeventsem hev_free"crlf
  160. pstr = pstr||"    Call rxcloseeventsem hev_term"crlf
  161. pstr = pstr||"    Call rxqueue 'delete',qname"crlf
  162. pstr = pstr||"    Exit"crlf
  163. pstr = pstr||"    End"crlf
  164. pstr = pstr||"  Call rxreseteventsem hev_free"crlf
  165. pstr = pstr||"End"crlf
  166. pstr = pstr||"exit"crlf||eof
  167. tid = rxcreaterexxthread('$'pstr,hev_enable,hev_start,hev_free,hev_term,timelimit,handler,arglist,tempq)
  168.  
  169. Call RxAddQueue hev_enable,tempq
  170. Call RxAddQueue hev_start,tempq
  171. Call RxAddQueue hev_free,tempq
  172. Call RxAddQueue hev_term,tempq
  173. Call RxAddQueue tid,tempq
  174.  
  175. Return tempq
  176.