PollServiceRequestManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 HttpServer;
  31. namespace OpenSim.Framework.Servers.HttpServer
  32. {
  33. public class PollServiceRequestManager
  34. {
  35. private readonly BaseHttpServer m_server;
  36. private static Queue m_requests = Queue.Synchronized(new Queue());
  37. private uint m_WorkerThreadCount = 0;
  38. private Thread[] m_workerThreads;
  39. private PollServiceWorkerThread[] m_PollServiceWorkerThreads;
  40. private Thread m_watcherThread;
  41. private bool m_running = true;
  42. public PollServiceRequestManager(BaseHttpServer pSrv, uint pWorkerThreadCount, int pTimeout)
  43. {
  44. m_server = pSrv;
  45. m_WorkerThreadCount = pWorkerThreadCount;
  46. m_workerThreads = new Thread[m_WorkerThreadCount];
  47. m_PollServiceWorkerThreads = new PollServiceWorkerThread[m_WorkerThreadCount];
  48. //startup worker threads
  49. for (uint i=0;i<m_WorkerThreadCount;i++)
  50. {
  51. m_PollServiceWorkerThreads[i] = new PollServiceWorkerThread(m_server, pTimeout);
  52. m_PollServiceWorkerThreads[i].ReQueue += ReQueueEvent;
  53. m_workerThreads[i] = new Thread(m_PollServiceWorkerThreads[i].ThreadStart);
  54. m_workerThreads[i].Name = String.Format("PollServiceWorkerThread{0}",i);
  55. //Can't add to thread Tracker here Referencing OpenSim.Framework creates circular reference
  56. m_workerThreads[i].Start();
  57. }
  58. //start watcher threads
  59. m_watcherThread = new Thread(ThreadStart);
  60. m_watcherThread.Name = "PollServiceWatcherThread";
  61. m_watcherThread.Start();
  62. }
  63. internal void ReQueueEvent(PollServiceHttpRequest req)
  64. {
  65. // Do accounting stuff here
  66. Enqueue(req);
  67. }
  68. public void Enqueue(PollServiceHttpRequest req)
  69. {
  70. lock (m_requests)
  71. m_requests.Enqueue(req);
  72. }
  73. public void ThreadStart(object o)
  74. {
  75. while (m_running)
  76. {
  77. ProcessQueuedRequests();
  78. Thread.Sleep(1000);
  79. }
  80. }
  81. private void ProcessQueuedRequests()
  82. {
  83. lock (m_requests)
  84. {
  85. if (m_requests.Count == 0)
  86. return;
  87. int reqperthread = (int) (m_requests.Count/m_WorkerThreadCount) + 1;
  88. // For Each WorkerThread
  89. for (int tc = 0; tc < m_WorkerThreadCount && m_requests.Count > 0; tc++)
  90. {
  91. //Loop over number of requests each thread handles.
  92. for (int i=0;i<reqperthread && m_requests.Count > 0;i++)
  93. {
  94. try
  95. {
  96. m_PollServiceWorkerThreads[tc].Enqueue((PollServiceHttpRequest)m_requests.Dequeue());
  97. }
  98. catch (InvalidOperationException)
  99. {
  100. // The queue is empty, we did our calculations wrong!
  101. return;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. ~PollServiceRequestManager()
  108. {
  109. foreach (object o in m_requests)
  110. {
  111. PollServiceHttpRequest req = (PollServiceHttpRequest) o;
  112. m_server.DoHTTPGruntWork(req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id), new OSHttpResponse(new HttpResponse(req.HttpContext, req.Request), req.HttpContext));
  113. }
  114. m_requests.Clear();
  115. foreach (Thread t in m_workerThreads)
  116. {
  117. t.Abort();
  118. }
  119. m_running = false;
  120. }
  121. }
  122. }