|
// Copyright (c) 2000, 2002 - 2012 Bharath K A
//
// CREATED: Sunday March 29 11:08:13 CST 2009 by Bharath K A
//
// LAST CHANGED:
//
// AUTHOR: Bharath K A
//
// Copyright 2005 by Bharath K A
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading;
namespace BK.Util
{
internal class GridAsynchHandler : IHttpAsyncHandler
{
private static object lockObj;
private static MultimapBK<string, ResponseDetails> clientDetails;
private static List<WaitHandle> listOfWaitHandles;
private static List<string> listOfClientIds;
private static AutoResetEvent eventToReInit;
private static bool KeepRunning;
private static Thread ProcessingThread;
static GridAsynchHandler()
{
lockObj = new object();
clientDetails = new MultimapBK<string, ResponseDetails>();
eventToReInit = new AutoResetEvent(false);
listOfWaitHandles = new List<WaitHandle>();
listOfClientIds = new List<string>();
listOfWaitHandles.Add(eventToReInit);
listOfClientIds.Add("SELF");
KeepRunning = true;
ProcessingThread = new Thread(new ThreadStart(ProcessUpdates));
ProcessingThread.Start();
}
internal static void RegisterControl(GridControlCometAjax gridToRegister)
{
lock (lockObj)
{
if (null == clientDetails.GetFirstItem(gridToRegister.ClientID))
{
clientDetails.Add(gridToRegister.ClientID, new ResponseDetails(gridToRegister));
listOfClientIds.Add(gridToRegister.ClientID);
listOfWaitHandles.Add(gridToRegister.UpdateEvent);
eventToReInit.Set();
}
// else, It is already registered
}
}
internal static void ProcessUpdates()
{
int WaitNumber = 0;
int TimeoutUpdate = 1;
StringBuilder xmlComposer = new StringBuilder();
while(KeepRunning)
{
xmlComposer.Length = 0;
WaitHandle[] waitHandles = new WaitHandle[listOfWaitHandles.Count];
for (int Counter = 0; Counter < listOfWaitHandles.Count; Counter++)
{
waitHandles[Counter] = listOfWaitHandles[Counter];
}
WaitNumber = AutoResetEvent.WaitAny(waitHandles, GridControlConstants.WAIT_TIMEOUT,false);
if (WaitNumber == AutoResetEvent.WaitTimeout)
{
WaitNumber = TimeoutUpdate;
TimeoutUpdate++;
if (TimeoutUpdate >= listOfWaitHandles.Count)
{
TimeoutUpdate = 1;
}
}
if (WaitNumber == 0)
{
continue;
}
else
{
ResponseDetails respDet = clientDetails.GetFirstItem(listOfClientIds[WaitNumber]);
xmlComposer.AppendFormat(GridControlConstants.RESP_XML_HDR, respDet.GridCtrlAjaxInstance.DynRowCount, respDet.GridCtrlAjaxInstance.DynColCount);
xmlComposer.Append(GridControlConstants.RESP_XML_TBL_DATA_START);
for (int RowItem = 0; RowItem < respDet.GridCtrlAjaxInstance.DynRowCount; RowItem++)
{
for (int ColItem = 0; ColItem < respDet.GridCtrlAjaxInstance.DynColCount; ColItem++)
{
xmlComposer.AppendFormat(GridControlConstants.RESP_XML_BODY_DATA, respDet.GridCtrlAjaxInstance.DynGetContent(RowItem, ColItem));
}
}
xmlComposer.Append(GridControlConstants.RESP_XML_TBL_DATA_END);
xmlComposer.Append(GridControlConstants.RESP_XML_TBL_CLR_START);
for (int RowItem = 0; RowItem < respDet.GridCtrlAjaxInstance.DynRowCount; RowItem++)
{
for (int ColItem = 0; ColItem < respDet.GridCtrlAjaxInstance.DynColCount; ColItem++)
{
xmlComposer.AppendFormat(GridControlConstants.RESP_XML_BODY_COLOR, System.Drawing.ColorTranslator.ToHtml(respDet.GridCtrlAjaxInstance.DynGetColor(RowItem, ColItem)));
}
}
xmlComposer.Append(GridControlConstants.RESP_XML_TBL_CLR_END);
xmlComposer.Append(GridControlConstants.RESP_XML_TAIL);
lock (lockObj)
{
respDet.XmlFeed = xmlComposer.ToString();
respDet.UpdateRequired = true;
}
for (int WorkerCounter = 0; WorkerCounter < GridControlConstants.NUMBER_OF_THREADS; WorkerCounter++)
{
if (respDet.UpdateRequired)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkerThreadProc), respDet);
}
else
{
break;
}
}
}
}
}
public static void WorkerThreadProc(object ThreadParam)
{
ResponseDetails respDetails = (ResponseDetails)ThreadParam;
while (true)
{
GridAsynchResult asyncResult = respDetails.RemoveClient();
if (asyncResult == null)
{
break;
}
else if (asyncResult.Status == ClientStatus.Updated)
{
respDetails.UpdateRequired = false;
respDetails.AddClient(asyncResult);
break;
}
asyncResult.SetCompleted();
}
}
public IAsyncResult BeginProcessRequest(HttpContext Context, AsyncCallback Callback, object ExtraData)
{
string ClientId = Context.ApplicationInstance.Request.QueryString[0];
ResponseDetails respDet = clientDetails.GetFirstItem(ClientId);
GridAsynchResult asyncResultToAdd = new GridAsynchResult(respDet, Context, Callback, ClientStatus.Updated);
respDet.AddClient(asyncResultToAdd);
return asyncResultToAdd;
}
public void EndProcessRequest(IAsyncResult result)
{
try
{
GridAsynchResult asynchResult = (GridAsynchResult)result;
lock (lockObj)
{
asynchResult.ClientContext.Response.ContentType = "text/xml";
asynchResult.ClientContext.Response.Write(asynchResult.RespDetails.XmlFeed);
}
}
catch (Exception)
{
}
}
public void ProcessRequest(HttpContext Context)
{
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.