Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.8 KiB

  1. //
  2. // MODULE: RecentUse.cpp
  3. //
  4. // PURPOSE: To maintain a "session" this can track whether a give value (either
  5. // a cookie value or an IP address) has been "recently" used
  6. //
  7. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  8. //
  9. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  10. //
  11. // AUTHOR: Joe Mabel
  12. //
  13. // ORIGINAL DATE: 11-4-98
  14. //
  15. // NOTES:
  16. //
  17. // Version Date By Comments
  18. //--------------------------------------------------------------------
  19. // V3.0 11/4/98 JM original
  20. #include "stdafx.h"
  21. #include "RecentUse.h"
  22. //////////////////////////////////////////////////////////////////////
  23. // CRecentUse
  24. //////////////////////////////////////////////////////////////////////
  25. CRecentUse::CRecentUse(DWORD minExpire /* = 15 */)
  26. {
  27. m_minExpire = minExpire;
  28. }
  29. CRecentUse::~CRecentUse()
  30. {
  31. }
  32. // Start tracking a new value
  33. // SIDE EFFECT: if there are more than 10 values being tracked, see if any of them
  34. // are out of date & get rid of them. This strategy is efficient as long as m_Recent
  35. // never gets very big, which is the expectation in APGTS.
  36. // Will fail silently in the very unlikely case that adding to the map throws an exception.
  37. void CRecentUse::Add(CString str)
  38. {
  39. time_t timeNow;
  40. time (&timeNow);
  41. try
  42. {
  43. m_Recent[str] = timeNow;
  44. }
  45. catch (...)
  46. {
  47. }
  48. // SIDE EFFECT
  49. if (m_Recent.size() > 10)
  50. Flush();
  51. }
  52. // If the input string value has been used within the relevant interval, return true
  53. // and update the time of most recent use.
  54. bool CRecentUse::Validate(CString str)
  55. {
  56. bool bRet = false;
  57. TimeMap::iterator it = m_Recent.find(str);
  58. if ( it != m_Recent.end())
  59. bRet = Validate(it);
  60. return bRet;
  61. }
  62. // If the string value it->first has been used within the relevant interval, return true
  63. // and update the time of most recent use (it->second).
  64. // Before calling this, verify that it is a valid iterator, not m_Recent.end()
  65. // SIDE EFFECT: if it->first hasn't been used within the relevant interval, remove *it
  66. // from m_Recent. This side effect means that in the case of a false return, it no longer
  67. // will point to the same value.
  68. bool CRecentUse::Validate(TimeMap::iterator it)
  69. {
  70. bool bRet = false;
  71. time_t timeNow;
  72. time (&timeNow);
  73. if (timeNow - it->second < m_minExpire * 60 /* secs per min */)
  74. {
  75. bRet = true;
  76. it->second = timeNow;
  77. }
  78. else
  79. // SIDE EFFECT: it's not current, no point to keeping it around.
  80. m_Recent.erase(it);
  81. return bRet;
  82. }
  83. // Get rid of all elements of m_Recent that haven't been used within the relevant interval
  84. void CRecentUse::Flush()
  85. {
  86. TimeMap::iterator it = m_Recent.begin();
  87. while (it != m_Recent.end())
  88. {
  89. if (Validate(it))
  90. ++it; // on to the next
  91. else
  92. m_Recent.erase(it);
  93. }
  94. }