using System; using System.Collections; //using System.Diagnostics; using System.Threading; using Wayne.Lib; #if _SINP using Wayne.ForecourtControl.Nfs; #endif namespace Wayne.ForecourtControl.Fusion { #if !(_TEVENTARGS) class AsyncResponseOperation: IDisposable { public int requestId; public string requestType; // TODO: get the response Timeout from as a parameter private int responseTimeout = 60000; // Change to 60 seconds from 30 public EventHandler requestCompleted; public EventHandler> requestCompletedLong; public EventHandler> requestCompletedITankReading; public EventHandler> requestCompletedPumpAccumulatorReading; public long resultLong; public ITankReadingEx resultITankReading; public PumpAccumulatorReading resultPumpAccumulatorReading; public object userToken; public object src; public DateTime requestTime; private System.Threading.Timer responseTimer = null; public event EventHandler OnResponseTimeout; public AsyncResponseOperation(int requestId, string requestType, EventHandler requestCompleted, object userToken, object src) { this.requestId = requestId; this.requestType = requestType; this.requestCompleted = requestCompleted; this.userToken = userToken; this.src = src; requestTime = DateTime.Now; if (responseTimeout > 0) responseTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ResponseTimeout), this, responseTimeout, System.Threading.Timeout.Infinite); } public AsyncResponseOperation(int requestId, string requestType, EventHandler> requestCompleted, object userToken, object src, long resultLong) { this.requestId = requestId; this.requestType = requestType; this.requestCompletedLong = requestCompleted; this.resultLong = resultLong; this.userToken = userToken; this.src = src; requestTime = DateTime.Now; if (responseTimeout > 0) responseTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ResponseTimeout), this, responseTimeout, System.Threading.Timeout.Infinite); } public AsyncResponseOperation(int requestId, string requestType, EventHandler> requestCompleted, object userToken, object src, ITankReadingEx resultITankReading) { this.requestId = requestId; this.requestType = requestType; this.requestCompletedITankReading = requestCompleted; this.resultITankReading = resultITankReading; this.userToken = userToken; this.src = src; requestTime = DateTime.Now; if (responseTimeout > 0) responseTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ResponseTimeout), this, responseTimeout, System.Threading.Timeout.Infinite); } public AsyncResponseOperation(int requestId, string requestType, EventHandler> requestCompleted, object userToken, object src, PumpAccumulatorReading resultPumpAccumulatorReading) { this.requestId = requestId; this.requestType = requestType; this.requestCompletedPumpAccumulatorReading = requestCompleted; this.resultPumpAccumulatorReading = resultPumpAccumulatorReading; this.userToken = userToken; this.src = src; requestTime = DateTime.Now; if (responseTimeout > 0) responseTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ResponseTimeout), this, responseTimeout, System.Threading.Timeout.Infinite); } public void Dispose() { if (responseTimer != null) responseTimer.Dispose(); } private void ResponseTimeout(Object state) { Trace.WriteLine(string.Format("Response Timeout for requestId={0}, requestType={1}", this.requestId, this.requestType)); //FG, MAR-15-2011, bug fix #28041; trigger to GetConfiguration again if there's no pump on FM. EventArgs ea; if (this.requestType == "GetConfiguration") { ea = new EventArgs("GetConfiguration"); } else { ea = new EventArgs(""); } if (OnResponseTimeout != null) OnResponseTimeout.Invoke(this, ea); } } #else class AsyncResponseOperation : IDisposable where TEventArgs : EventArgs { public int requestId; public string requestType; // TODO: get the response Timeout from as a parameter private int responseTimeout = 30000; public EventHandler requestCompleted; public TResult result; public object userToken; public DateTime requestTime; private System.Threading.Timer responseTimer = null; public event EventHandler OnResponseTimeout; public AsyncResponseOperation(int requestId, string requestType, EventHandler _requestCompleted, object userToken, object src, TResult result) { this.requestId = requestId; this.requestType = requestType; this.requestCompleted = _requestCompleted; this.result = result; this.userToken = userToken; this.src = src; requestTime = DateTime.Now; if (responseTimeout > 0) responseTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ResponseTimeout), this, responseTimeout, System.Threading.Timeout.Infinite); } public void Dispose() { if (responseTimer != null) responseTimer.Dispose(); } private void ResponseTimeout(Object state) { Trace.WriteLine(string.Format("Response Timeout for requestId={0}, requestType={1}", this.requestId, this.requestType)); if (OnResponseTimeout != null) OnResponseTimeout.Invoke(this, null); } } #endif #if !(_TEVENTARGS) public class AsyncResponseManager { public Hashtable AsyncResponseOperationList; public event EventHandler OnResponseTimeout; public AsyncResponseManager() { AsyncResponseOperationList = new Hashtable(); } public void OnMessageEnqueuing(object sender, MessageEnqueuedEventArgs e) { // add the request to the 'waiting for response request list' Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("init enqueuing message")); AsyncResponseOperation asyncResponseOperation = null; try { Monitor.Enter(AsyncResponseOperationList); if (e.ResponseRequired) { if (e.RequestCompleted != null) asyncResponseOperation = new AsyncResponseOperation(e.RequestId, e.RequestType, e.RequestCompleted, e.UserToken, e.Scr); else if (e.RequestCompletedLong != null) asyncResponseOperation = new AsyncResponseOperation(e.RequestId, e.RequestType, e.RequestCompletedLong, e.UserToken, e.Scr, e.resultLong); else if (e.RequestCompletedITankReading != null) asyncResponseOperation = new AsyncResponseOperation(e.RequestId, e.RequestType, e.RequestCompletedITankReading, e.UserToken, e.Scr, e.resultITankReading); else if (e.RequestCompletedPumpAccumulatorReading != null) asyncResponseOperation = new AsyncResponseOperation(e.RequestId, e.RequestType, e.RequestCompletedPumpAccumulatorReading, e.UserToken, e.Scr, e.resultPumpAccumulatorReading); else asyncResponseOperation = new AsyncResponseOperation(e.RequestId, e.RequestType, (EventHandler)null, null, null); asyncResponseOperation.OnResponseTimeout += new EventHandler(asyncResponseOperation_OnResponseTimeout); AsyncResponseOperationList.Add(e.RequestId, asyncResponseOperation); } } catch (Exception ex) { Trace.WriteLine("Exception! " + ex.Message + " - " + ex.StackTrace); } finally { Monitor.Exit(AsyncResponseOperationList); } Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("end enqueuing message")); } private void asyncResponseOperation_OnResponseTimeout(object sender, EventArgs e) { Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("init OnResponseTimeout")); if (OnResponseTimeout != null) OnResponseTimeout.Invoke(this, e); Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("end OnResponseTimeout")); } public void SendResponse(int requestId, bool result) { SendResponse(requestId, result, null); } public void SendResponse(int requestId, bool result, Object objresult) { AsyncResponseOperation asyncResponseOperation = null; try { Trace.WriteLine(string.Format("init SendResponse requestId={0}", requestId)); Monitor.Enter(AsyncResponseOperationList); if (AsyncResponseOperationList.ContainsKey(requestId)) { asyncResponseOperation = (AsyncResponseOperation)(this.AsyncResponseOperationList[requestId]); this.RemoveRequest(requestId); } Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("SendResponse got operation requestId={0}", requestId)); } catch (Exception ex) { Trace.WriteLine("Exception! " + ex.Message + " - " + ex.StackTrace); } finally { Monitor.Exit(AsyncResponseOperationList); } try { Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("SendResponse requestId={0}", requestId)); if (asyncResponseOperation != null) { if (asyncResponseOperation.requestCompleted != null) { Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("SendResponse requestId={0} requestCompleted != null", requestId)); asyncResponseOperation.requestCompleted.BeginInvoke(asyncResponseOperation.src, new AsyncCompletedEventArgs(result, asyncResponseOperation.userToken), null, null); } else if (asyncResponseOperation.requestCompletedLong != null) { Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("SendResponse requestId={0} requestCompletedLong != null", requestId)); //asyncResponseOperation.resultLong = (long)objresult; asyncResponseOperation.requestCompletedLong.BeginInvoke(asyncResponseOperation.src, new AsyncCompletedEventArgs(result, asyncResponseOperation.resultLong, asyncResponseOperation.userToken), null, null); } else if (asyncResponseOperation.requestCompletedITankReading != null) { Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("SendResponse requestId={0} requestCompletedITankReading != null", requestId)); asyncResponseOperation.resultITankReading = (ITankReadingEx)objresult; asyncResponseOperation.requestCompletedITankReading.BeginInvoke(asyncResponseOperation.src, new AsyncCompletedEventArgs(result, asyncResponseOperation.resultITankReading, asyncResponseOperation.userToken), null, null); } else if (asyncResponseOperation.requestCompletedPumpAccumulatorReading != null) { Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("SendResponse requestId={0} requestCompletedPumpAccumulatorReading != null", requestId)); asyncResponseOperation.resultPumpAccumulatorReading = (PumpAccumulatorReading)objresult; asyncResponseOperation.requestCompletedPumpAccumulatorReading.BeginInvoke(asyncResponseOperation.src, new AsyncCompletedEventArgs(result, asyncResponseOperation.resultPumpAccumulatorReading, asyncResponseOperation.userToken), null, null); } } Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("end SendResponse requestId={0}", requestId)); } catch (Exception ex) { Trace.WriteLine("Exception! " + ex.Message + " - " + ex.StackTrace); } } private void RemoveRequest(int requestId) { try { if (this.AsyncResponseOperationList.ContainsKey(requestId)) { AsyncResponseOperation asyncResponseOperation = (AsyncResponseOperation)(this.AsyncResponseOperationList[requestId]); asyncResponseOperation.Dispose(); AsyncResponseOperationList.Remove(requestId); } } catch (Exception ex) { Trace.WriteLine("Exception! " + ex.Message + " - " + ex.StackTrace); } } } #else public class AsyncResponseManager where TEventArgs : EventArgs { public Hashtable AsyncResponseOperationList; public event EventHandler OnResponseTimeout; public AsyncResponseManager() { AsyncResponseOperationList = new Hashtable(); } public void ifsfmessages_OnMessageEnqueuing(object sender, MessageEnqueuedEventArgs e) { // add the request to the 'waiting for response request list' Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("init enqueuing message")); AsyncResponseOperation asyncResponseOperation = null; try { Monitor.Enter(AsyncResponseOperationList); if (e.ResponseRequired) { if (e.RequestCompleted != null) asyncResponseOperation = new AsyncResponseOperation(e.RequestId, e.RequestType, e.RequestCompleted, e.UserToken, e.result); else asyncResponseOperation = new AsyncResponseOperation(e.RequestId, e.RequestType, (EventHandler)null, null); asyncResponseOperation.OnResponseTimeout += new EventHandler(asyncResponseOperation_OnResponseTimeout); AsyncResponseOperationList.Add(e.RequestId, asyncResponseOperation); } } catch (Exception ex) { Trace.WriteLine("Exception! " + ex.Message + " - " + ex.StackTrace); } finally { Monitor.Exit(AsyncResponseOperationList); } Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("end enqueuing message")); } private void asyncResponseOperation_OnResponseTimeout(object sender, EventArgs e) { Trace.Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("init OnResponseTimeout")); if (OnResponseTimeout != null) OnResponseTimeout.Invoke(this, null); Trace.Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("end OnResponseTimeout")); } public void SendResponse(int requestId, bool result) { SendResponse(requestId, result, null); } public void SendResponse(int requestId, bool result, Object objresult) { AsyncResponseOperation asyncResponseOperation = null; try { Trace.WriteLine(string.Format("init SendResponse requestId={0}", requestId)); Monitor.Enter(AsyncResponseOperationList); if (AsyncResponseOperationList.ContainsKey(requestId)) { asyncResponseOperation = (AsyncResponseOperation)(this.AsyncResponseOperationList[requestId]); this.RemoveRequest(requestId); } Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("SendResponse got operation requestId={0}", requestId)); } catch (Exception ex) { Trace.WriteLine("Exception! " + ex.Message + " - " + ex.StackTrace); } finally { Monitor.Exit(AsyncResponseOperationList); } try { Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("SendResponse requestId={0}", requestId)); if (asyncResponseOperation != null) { if (asyncResponseOperation.requestCompleted != null) asyncResponseOperation.requestCompleted.BeginInvoke(asyncResponseOperation.src, new TEventArgs(result, asyncResponseOperation.userToken), null, null); asyncResponseOperation.requestCompleted.BeginInvoke(this, new AsyncCompletedEventArgs(result, t, asyncResponseOperation.userToken), null, null); } Trace.WriteLineIf(Trace.CheckTraceLevel(3), string.Format("end SendResponse requestId={0}", requestId)); } catch (Exception ex) { Trace.WriteLine("Exception! " + ex.Message + " - " + ex.StackTrace); } } private void RemoveRequest(int requestId) { try { if (this.AsyncResponseOperationList.ContainsKey(requestId)) { AsyncResponseOperation asyncResponseOperation = (AsyncResponseOperation)(this.AsyncResponseOperationList[requestId]); asyncResponseOperation.Dispose(); AsyncResponseOperationList.Remove(requestId); } } catch (Exception ex) { Trace.WriteLine("Exception! " + ex.Message + " - " + ex.StackTrace); } } } #endif }