home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR503.W96 / LOCKS.PR_ / LOCKS.PR
Text File  |  1995-06-20  |  4KB  |  194 lines

  1. /***
  2. *
  3. *  Locks.prg
  4. *
  5. *  Sample networking functions to supplant the use of USE,
  6. *  FLOCK(), RLOCK() and APPEND BLANK by adding additional
  7. *  functionality
  8. *
  9. *  Copyright (c) 1993-1995, Computer Associates International Inc.
  10. *  All rights reserved.
  11. *
  12. *  NOTE: Compile with /a /m /n /w options
  13. *
  14. */
  15.  
  16. #include "Common.ch"
  17.  
  18. #define NET_WAIT     0.5   // Seconds to wait between between retries
  19. #define NET_SECS     2     // Number of seconds to continue retry
  20.  
  21.  
  22. /***
  23. *
  24. *  AddRec( [<nWaitSeconds>] ) --> lSuccess
  25. *
  26. *  Attempt to APPEND BLANK with optional retry
  27. *
  28. *  Parameter:
  29. *     nWaitSeconds - Optional time in seconds to retry operation, defaults
  30. *                    to NET_SECS
  31. *
  32. *  Returns:
  33. *     True (.T.) if successful, false (.F.) if not
  34. *
  35. */
  36. FUNCTION AddRec( nWaitSeconds )
  37.  
  38.    LOCAL lForever       // Retry forever?
  39.  
  40.    DEFAULT nWaitSeconds TO NET_SECS
  41.  
  42.    APPEND BLANK
  43.    IF !NETERR()
  44.       RETURN ( .T. )             // NOTE
  45.    ENDIF
  46.  
  47.    lForever := ( nWaitSeconds == 0 )
  48.  
  49.    // Keep trying as long as our time's not up
  50.    DO WHILE ( lForever .OR. ( nWaitSeconds > 0 ) )
  51.  
  52.       APPEND BLANK
  53.  
  54.       IF !NETERR()
  55.          RETURN ( .T. )          // NOTE
  56.       ENDIF
  57.  
  58.       INKEY( NET_WAIT )          // Wait NET_WAIT seconds (defined above)
  59.       nWaitSeconds  -= NET_WAIT
  60.  
  61.    ENDDO
  62.  
  63.    RETURN ( .F. )       // Not locked
  64.  
  65.  
  66.  
  67. /***
  68. *
  69. *  FilLock( [<nWaitSeconds>] ) --> lSuccess
  70. *
  71. *  Attempt to FLOCK() with optional retry
  72. *
  73. *  Parameter:
  74. *     nWaitSeconds - Optional time in seconds to retry operation, defaults
  75. *                    to NET_SECS
  76. *
  77. *  Returns:
  78. *     True if successful, false if not
  79. *
  80. */
  81. FUNCTION FilLock( nSeconds )
  82.  
  83.    LOCAL lForever    // Retry forever?
  84.  
  85.    DEFAULT nSeconds TO NET_SECS
  86.  
  87.    IF FLOCK()
  88.       RETURN ( .T. )       // NOTE
  89.    ENDIF
  90.  
  91.    lForever := ( nSeconds == 0 )
  92.  
  93.    // Keep trying until our time's up
  94.    DO WHILE ( lForever .OR. ( nSeconds > 0 ) )
  95.  
  96.       INKEY( NET_WAIT )    // Wait NET_WAIT seconds
  97.       nSeconds -= NET_WAIT
  98.  
  99.       IF FLOCK()
  100.          RETURN ( .T. )    // NOTE
  101.       ENDIF
  102.  
  103.    ENDDO
  104.  
  105.    RETURN ( .F. )          // Not locked
  106.  
  107.  
  108.  
  109. /***
  110. *
  111. *  NetUse( <cDatabase>, <lOpenMode>, [<nWaitSeconds>] ) --> lSuccess
  112. *
  113. *  Attempt to USE a database file with optional retry
  114. *
  115. *  Parameters:
  116. *     cDatabase    - Database file to open
  117. *     lOpenMode    - Sharing mode: True indicates EXCLUSIVE, false
  118. *                    indicates SHARED
  119. *     nWaitSeconds - Optional time in seconds to retry operation, defaults
  120. *                    to NET_SECS
  121. *
  122. *  Returns:
  123. *     True if successfull, false if not
  124. *
  125. */
  126. FUNCTION NetUse( cDatabase, lOpenMode, nSeconds )
  127.  
  128.    LOCAL lForever    // Retry forever?
  129.  
  130.    DEFAULT nSeconds TO NET_SECS
  131.    lForever := ( nSeconds == 0 )
  132.  
  133.    // Keep trying as long as our time's not up
  134.    DO WHILE ( lForever .OR. ( nSeconds > 0 ) )
  135.  
  136.      // lOpenMode determines the mode files are opened in
  137.      IF lOpenMode
  138.          USE ( cDatabase ) EXCLUSIVE
  139.      ELSE
  140.          USE ( cDatabase ) SHARED
  141.      ENDIF
  142.  
  143.      IF !NETERR()
  144.         RETURN ( .T. )     // NOTE
  145.      ENDIF
  146.  
  147.      INKEY( NET_WAIT )     // Wait
  148.      nSeconds -= NET_WAIT
  149.  
  150.    ENDDO
  151.  
  152.    RETURN ( .F. )          // USE fails
  153.  
  154.  
  155.  
  156. /***
  157. *
  158. *  RecLock( [<nWaitSeconds>] ) --> lSuccess
  159. *
  160. *  Attempt to RLOCK() with optional retry
  161. *
  162. *  Parameter:
  163. *     nWaitSeconds - Optional time in seconds to retry operation, defaults
  164. *                    to NET_SECS
  165. *
  166. *  Returns:
  167. *     True if successful, false if not
  168. *
  169. */
  170. FUNCTION RecLock( nSeconds )
  171.  
  172.    LOCAL lForever          // Retry forever?
  173.  
  174.    DEFAULT nSeconds TO NET_SECS
  175.  
  176.    IF RLOCK()
  177.       RETURN ( .T. )       // NOTE
  178.    ENDIF
  179.  
  180.    lForever := ( nSeconds == 0 )
  181.  
  182.    DO WHILE ( lForever .OR. ( nSeconds > 0 ) )
  183.  
  184.       IF RLOCK()
  185.          RETURN ( .T. )    // NOTE
  186.       ENDIF
  187.  
  188.       INKEY( NET_WAIT )    // Wait 1/2 second
  189.       nSeconds -= NET_WAIT
  190.  
  191.    ENDDO
  192.  
  193.    RETURN ( .F. )          // Not locked
  194.