PollServiceRequestManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Threading;
  30. using System.Reflection;
  31. using log4net;
  32. using HttpServer;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Monitoring;
  35. namespace OpenSim.Framework.Servers.HttpServer
  36. {
  37. public class PollServiceRequestManager
  38. {
  39. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private readonly BaseHttpServer m_server;
  41. private static Queue m_requests = Queue.Synchronized(new Queue());
  42. private uint m_WorkerThreadCount = 0;
  43. private Thread[] m_workerThreads;
  44. private PollServiceWorkerThread[] m_PollServiceWorkerThreads;
  45. private bool m_running = true;
  46. public PollServiceRequestManager(BaseHttpServer pSrv, uint pWorkerThreadCount, int pTimeout)
  47. {
  48. m_server = pSrv;
  49. m_WorkerThreadCount = pWorkerThreadCount;
  50. m_workerThreads = new Thread[m_WorkerThreadCount];
  51. m_PollServiceWorkerThreads = new PollServiceWorkerThread[m_WorkerThreadCount];
  52. //startup worker threads
  53. for (uint i = 0; i < m_WorkerThreadCount; i++)
  54. {
  55. m_PollServiceWorkerThreads[i] = new PollServiceWorkerThread(m_server, pTimeout);
  56. m_PollServiceWorkerThreads[i].ReQueue += ReQueueEvent;
  57. m_workerThreads[i]
  58. = Watchdog.StartThread(
  59. m_PollServiceWorkerThreads[i].ThreadStart,
  60. String.Format("PollServiceWorkerThread{0}", i),
  61. ThreadPriority.Normal,
  62. false,
  63. true,
  64. null,
  65. int.MaxValue);
  66. }
  67. Watchdog.StartThread(
  68. this.ThreadStart,
  69. "PollServiceWatcherThread",
  70. ThreadPriority.Normal,
  71. false,
  72. true,
  73. null,
  74. 1000 * 60 * 10);
  75. }
  76. internal void ReQueueEvent(PollServiceHttpRequest req)
  77. {
  78. // Do accounting stuff here
  79. Enqueue(req);
  80. }
  81. public void Enqueue(PollServiceHttpRequest req)
  82. {
  83. lock (m_requests)
  84. m_requests.Enqueue(req);
  85. }
  86. public void ThreadStart()
  87. {
  88. while (m_running)
  89. {
  90. Watchdog.UpdateThread();
  91. ProcessQueuedRequests();
  92. Thread.Sleep(1000);
  93. }
  94. }
  95. private void ProcessQueuedRequests()
  96. {
  97. lock (m_requests)
  98. {
  99. if (m_requests.Count == 0)
  100. return;
  101. // m_log.DebugFormat("[POLL SERVICE REQUEST MANAGER]: Processing {0} requests", m_requests.Count);
  102. int reqperthread = (int) (m_requests.Count/m_WorkerThreadCount) + 1;
  103. // For Each WorkerThread
  104. for (int tc = 0; tc < m_WorkerThreadCount && m_requests.Count > 0; tc++)
  105. {
  106. //Loop over number of requests each thread handles.
  107. for (int i = 0; i < reqperthread && m_requests.Count > 0; i++)
  108. {
  109. try
  110. {
  111. m_PollServiceWorkerThreads[tc].Enqueue((PollServiceHttpRequest)m_requests.Dequeue());
  112. }
  113. catch (InvalidOperationException)
  114. {
  115. // The queue is empty, we did our calculations wrong!
  116. return;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. ~PollServiceRequestManager()
  123. {
  124. foreach (object o in m_requests)
  125. {
  126. PollServiceHttpRequest req = (PollServiceHttpRequest) o;
  127. PollServiceWorkerThread.DoHTTPGruntWork(
  128. m_server, req, req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id));
  129. }
  130. m_requests.Clear();
  131. foreach (Thread t in m_workerThreads)
  132. {
  133. t.Abort();
  134. }
  135. m_running = false;
  136. }
  137. }
  138. }