} //ProcessConfirmMessage() // // #endregion// Process Socket Events #region Process Incoming Local Events // ***************************************************************** // **** ProcessEngineEvent **** // ***************************************************************** /// <summary> /// These come from engine hub confirmations. /// </summary> /// <param name="eventArg"></param> private void ProcessEngineEvent(EngineEventArgs eventArg) { EngineEventArgs.EventStatus eventStatus = eventArg.Status; EngineEventArgs.EventType eventType = eventArg.MsgType; string hubName = eventArg.EngineHubName; // Process requests if (eventStatus == EngineEventArgs.EventStatus.Request) { // Requests are targeting a specific engine hub. foreach (KeyValuePair <int, ForeignServer> server in m_ServersConnected) { if (server.Value.Services.ContainsKey(hubName)) { Message msg = GetMessage(MessageType.EngineEvent, MessageState.Confirmed, eventArg); SendMessage(server.Key, msg); } } } else { // These are broadcasted foreach (KeyValuePair <int, ForeignServer> server in m_ServersConnected) { // if the hubName is in hubs that this server is subscribed to, send along message. Message msg = GetMessage(MessageType.EngineEvent, MessageState.Confirmed, eventArg); SendMessage(server.Key, msg); } } }//ProcessEngineEvent()
private void button2_Click(object sender, EventArgs e) { // Change data object o = status; status++; // store data string s = o.ToString(); EngineEventArgs.EventStatus result = ConvertType.ChangeType <EngineEventArgs.EventStatus>(s); }
// // // **** Process Event **** // public virtual void ProcessEvent(EventArgs e) { if (e.GetType() != typeof(EngineEventArgs)) { return; } EngineEventArgs eArgs = (EngineEventArgs)e; EngineEventArgs.EventType eventType = eArgs.MsgType; EngineEventArgs.EventStatus eventStatus = eArgs.Status; if (eventStatus != EngineEventArgs.EventStatus.Confirm) { return; } switch (eventType) { case EngineEventArgs.EventType.ParameterChange: if (eArgs.DataIntA != null) { for (int i = 0; i < eArgs.DataIntA.Length; ++i) { int id = eArgs.DataIntA[i]; object newValue = eArgs.DataObjectList[i]; // last argument was 0, fixed. m_ParamControl[id].SetValue(newValue); } m_IsUpdateRequired = true; } break; case EngineEventArgs.EventType.ParameterValue: if (eArgs.DataIntA != null) { for (int i = 0; i < eArgs.DataIntA.Length; ++i) { int id = eArgs.DataIntA[i]; object newValue = eArgs.DataObjectList[i]; // last argument was 0, fixed. m_ParamControl[id].SetValue(newValue); } m_IsUpdateRequired = true; } break; default: break; } }
// ***************************************************************** // **** Public Methods **** // ***************************************************************** // // // // // **** Process EngineEvent **** // public bool ProcessEngineEvent(EngineEventArgs eArgs) { EngineEventArgs.EventType eventType = eArgs.MsgType; EngineEventArgs.EventStatus eventStatus = eArgs.Status; if (eventStatus != EngineEventArgs.EventStatus.Confirm) { return(false); } switch (eventType) { case EngineEventArgs.EventType.ParameterChange: if (eArgs.DataIntA != null) { for (int i = 0; i < eArgs.DataIntA.Length; ++i) { ParamControlBase control; if (m_ParamControl.TryGetValue(eArgs.DataIntA[i], out control)) { control.SetValue(eArgs.DataObjectList[i]); m_IsRegenerationRequired = true; } } } break; case EngineEventArgs.EventType.ParameterValue: if (eArgs.DataIntA != null) { for (int i = 0; i < eArgs.DataIntA.Length; ++i) { ParamControlBase control; if (m_ParamControl.TryGetValue(eArgs.DataIntA[i], out control)) { control.SetValue(eArgs.DataObjectList[i]); m_IsRegenerationRequired = true; } } } break; default: break; } return(m_IsRegenerationRequired); }// ProcessEngineEvent()
}// InitializeEngine() // // // **** Process Event **** // public void ProcessEvent(EventArgs e) { // Initialize and validate if (!m_IsGraphsInitialized) { // Graphs not initialized yet. Just store these events. m_InitialEvents.Add(e); return; } else if (m_InitialEvents.Count > 0) { // The graphs have just been initialized, so // lets extract all the previously stored initial events. List <EventArgs> events = new List <EventArgs>(); events.AddRange(m_InitialEvents); // add already acrued events. m_InitialEvents.Clear(); // clearing this signals we are ready for normal operation. events.Add(e); // add the latest event also. foreach (EventArgs anEvent in events) { ProcessEvent(anEvent); } } if (e.GetType() != typeof(EngineEventArgs)) { return; } // // Process Engine Events // EngineEventArgs eArgs = (EngineEventArgs)e; EngineEventArgs.EventType eventType = eArgs.MsgType; EngineEventArgs.EventStatus eventStatus = eArgs.Status; if (eventStatus != EngineEventArgs.EventStatus.Confirm) { return; // we only process confirmations. } if (eArgs.DataObjectList != null) { foreach (object o in eArgs.DataObjectList) { // ***************************************** // **** Curve Definitiion List **** // ***************************************** Type dataType = o.GetType(); //if (dataType == typeof(List<CurveDefinition>)) if (dataType == typeof(CurveDefinitionList)) { // Given a list of curve definitions, update pre-defined curves, // or create new curves. These must be created by the windows thread. // These are usually only received at the start of the run when all parameters are broadcasted. List <CurveDefinition> list = ((CurveDefinitionList)o).CurveDefinitions; Dictionary <int, EngineEventArgs> requestedGraphRequests = new Dictionary <int, EngineEventArgs>(); // place to put CurveDefs for not yet created graphs. foreach (CurveDefinition cDef in list) { ZedGraphControl zg1; lock (GraphListLock) { if (!m_GraphList.TryGetValue(cDef.GraphID, out zg1)) { // Since we can't find this GraphID, request it to be created below. m_ZedGraphIDRequested.Add(cDef.GraphID); zg1 = null; // this signals that graph doesn't exit. } } if (zg1 == null && !requestedGraphRequests.ContainsKey(cDef.GraphID)) { // We couldn't find GraphID, so request it to be created it now! // Also make sure we only request it once thru this loop, just to save time. AddGraph(this, eArgs); // Asynch call will call ProcessEvent() again. EngineEventArgs newEvent = new EngineEventArgs(); // Event we will restack until after graph exists. newEvent.DataObjectList = new List <object>(); newEvent.Status = EngineEventArgs.EventStatus.Confirm; requestedGraphRequests.Add(cDef.GraphID, newEvent);// Mark this ID as having just been requested. } if (requestedGraphRequests.ContainsKey(cDef.GraphID)) { // This is a curve for a graph we just requested... push onto wait list. requestedGraphRequests[cDef.GraphID].DataObjectList.Add(cDef); continue; // skip } UpdateCurveDefinition(zg1, cDef); }//next curveDefn // Push any unfulfilled requests back onto queue. foreach (EngineEventArgs eeArgs in requestedGraphRequests.Values) { m_InitialEvents.Add(eeArgs); } } // ***************************************** // **** ZGraphPoints **** // ***************************************** else if (dataType == typeof(ZGraphPoints)) { // This is a list of ZGraphPoint objects. This message is from the AllParameter broadcast // at the beginning of the run. A new ZGraphControl will request all parameter values // only when first created. Therefore, if we are an old ZGraphControl, it probably is not // for us. if (m_LoadedHistory) { return; } m_LoadedHistory = true; List <ZGraphPoint> pointList = ((ZGraphPoints)o).Points; foreach (ZGraphPoint zpt in pointList) { ZedGraphControl zg1; if (!m_GraphList.TryGetValue(zpt.GraphID, out zg1)) { // No currently existing graph with this id. continue; } CurveItem curve = null; if (zg1.GraphPane != null) { curve = zg1.GraphPane.CurveList[zpt.CurveName]; if (curve == null) { // a new curve showed up - create a new curve, and plot this point anyway. Color newColor = m_DefaultColors[zg1.GraphPane.CurveList.Count % m_DefaultColors.Length]; curve = zg1.GraphPane.AddCurve(zpt.CurveName, new PointPairList(), newColor, SymbolType.None); } } // Add/replace points in the plot. IPointListEdit ip = null; if (curve != null) { ip = curve.Points as IPointListEdit; } if (ip != null) { if (zpt.IsReplaceAtX) { // This is a request to replace point with new one. int foundIndex = -1; for (int i = 0; i < ip.Count; ++i) // search for X-value of point to replace { if (ip[i].X == zpt.X) // very unsafe { foundIndex = i; break; // stop looking } } if (foundIndex > -1) { ip[foundIndex].Y = zpt.Y; } else { ip.Add(zpt.X, zpt.Y); // sort? } } else { ip.Add(zpt.X, zpt.Y); // simple serial addition of new data point. } } }//next zpt } // ***************************************** // **** List<ZGraphPoint> **** // ***************************************** /* * else if (dataType == typeof(List<ZGraphPoint>)) * { // This is a list of ZGraphPoints. This message is from the AllParameter broadcast * // at the beginning of the run. A new ZGraphControl will request all parameter values * // only when first created. Therefore, if we are an old ZGraphControl, it probably is not * // for us. * List<ZGraphPoint> pointList = (List<ZGraphPoint>) o; * foreach (ZGraphPoint zpt in pointList) * { * ZedGraphControl zg1; * if (!m_GraphList.TryGetValue(zpt.GraphID, out zg1)) * { // No currently existing graph with this id. * continue; * } * CurveItem curve = null; * if (zg1.GraphPane != null) * { * curve = zg1.GraphPane.CurveList[zpt.CurveName]; * if (curve == null) * { // a new curve showed up - create a new curve, and plot this point anyway. * Color newColor = m_DefaultColors[zg1.GraphPane.CurveList.Count % m_DefaultColors.Length]; * curve = zg1.GraphPane.AddCurve(zpt.CurveName, new PointPairList(), newColor, SymbolType.None); * } * } * // Add/replace points in the plot. * IPointListEdit ip = null; * if (curve != null) * ip = curve.Points as IPointListEdit; * if (ip != null) * { * if (zpt.IsReplaceAtX) * { // This is a request to replace point with new one. * int foundIndex = -1; * for (int i = 0; i < ip.Count; ++i) // search for X-value of point to replace * if (ip[i].X == zpt.X) // very unsafe * { * foundIndex = i; * break; // stop looking * } * if (foundIndex > -1) * ip[foundIndex].Y = zpt.Y; * else * { * ip.Add(zpt.X, zpt.Y); * // sort? * } * * } * else * ip.Add(zpt.X, zpt.Y); // simple serial addition of new data point. * } * }//next zpt * } */ // ***************************************** // **** Curve Definition **** // ***************************************** else if (dataType == typeof(CurveDefinition)) { CurveDefinition zNewCurve = (CurveDefinition)o; ZedGraphControl zg1; if (!m_GraphList.TryGetValue(zNewCurve.GraphID, out zg1)) { // New graph ID! continue; // TODO: Create new graph on the fly. } UpdateCurveDefinition(zg1, zNewCurve); } // ***************************************** // **** ZGraphPoint **** // ***************************************** else if (dataType == typeof(ZGraphText)) { ZGraphText zpt = (ZGraphText)o; ZedGraphControl zg1; if (!m_GraphList.TryGetValue(zpt.GraphID, out zg1)) { // No currently existing graph with this id. continue; } TextObj text = new TextObj(zpt.Text, zpt.X, zpt.Y); text.FontSpec.Angle = zpt.FontAngle; text.Location.AlignH = zpt.FontAlignH; text.Location.AlignV = zpt.FontAlignV; text.FontSpec.Size = zpt.FontSize; text.FontSpec.Border.IsVisible = zpt.FontBorderIsVisible; text.FontSpec.Fill.IsVisible = zpt.FontFillIsVisible; zg1.GraphPane.GraphObjList.Add(text); } // ***************************************** // **** ZGraphPoint **** // ***************************************** else if (dataType == typeof(ZGraphPoint)) { ZGraphPoint zpt = (ZGraphPoint)o; ZedGraphControl zg1; if (!m_GraphList.TryGetValue(zpt.GraphID, out zg1)) { // No currently existing graph with this id. continue; } CurveItem curve = null; if (zg1.GraphPane != null) { curve = zg1.GraphPane.CurveList[zpt.CurveName]; if (curve == null) { // a new curve showed up - create a new curve, and plot this point anyway. Color newColor = m_DefaultColors[zg1.GraphPane.CurveList.Count % m_DefaultColors.Length]; curve = zg1.GraphPane.AddCurve(zpt.CurveName, new PointPairList(), newColor, SymbolType.None); } } // Add/replace points in the plot. IPointListEdit ip = null; if (curve != null) { ip = curve.Points as IPointListEdit; } if (ip != null) { if (zpt.IsReplaceAtX) { // This is a request to replace point with new one. int foundIndex = -1; for (int i = 0; i < ip.Count; ++i) // search for X-value of point to replace { if (ip[i].X == zpt.X) // very unsafe { foundIndex = i; break; // stop looking } } if (foundIndex > -1) { ip[foundIndex].Y = zpt.Y; } else { ip.Add(zpt.X, zpt.Y); // sort? } } else { ip.Add(zpt.X, zpt.Y); // simple serial addition of new data point. } } }// ZPoint } m_IsUpdateRequired = true; //Regenerate(this, EventArgs.Empty); } //if e.DataObjectList is empty } // ProcessEvent()
// ***************************************************************** // **** Control Management **** // ***************************************************************** //public HudPanel GetHudPanel() //{ // return null; //} //public IEngineControl GetControl() //{ // return null; //} // // **** GetControl() **** // // <summary> // This implements part of IEngine. It automatically creates a copy of the // generic Pop Up control panel for displaying parameters of this engine. // </summary> // <returns></returns> /* * public IEngineControl GetControl() * { * if (m_EnginePopUpControl != null) * { * Type t = m_EnginePopUpControl.GetType(); * object p = null; * p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, m_EnginePopUpControl, new object[] { this.EngineID, m_PInfo }); * return (IEngineControl)p; * } * else * return null; * }// GetControl(). * // * // * // **** GetHudPanel() **** * // * /// <summary> * /// This implements part of IEngine. Automatically creates a copy of the heads up panel * /// to display critical parameters. * /// </summary> * /// <returns></returns> * public HudPanel GetHudPanel() * { * if (m_HudPanel != null) * { * Type t = m_HudPanel.GetType(); * object p = null; * p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, m_HudPanel, new object[] { this.EngineID, m_PInfo }); * return (HudPanel)p; * } * else * return null; * }// GetControl(). * // * // This overload allows outsiders to reset and change EnginePanels. * public HudPanel GetHudPanel(HudPanel newPanel) * { * m_HudPanel = newPanel; * return newPanel; * } */ // // // // #endregion//Control Management #region Engine Event Processing // ***************************************************************** // **** Public Methods **** // ***************************************************************** // // // // **** ProcessEvent() **** // /// <summary> /// This method is the way that EngineEventArgs are passed to an engine to process. /// The action taken depends on the type of engine. /// The implementation is required by IEngine; engines can override this implementation. /// Current implementation: /// 1. This implementation is most common, for a Strategy engine that receives from outsiders /// (normally user requests for parameter changes) to change its internal parameters. /// A. It tries to make the requested changes and respond to the caller the results. /// 2. If an engine overrides this with additional functionality, this method can still be /// called to process all EngineEventArgs events, it does nothing and returns when passed /// any other type of event. /// </summary> /// <param name="e">Request passed in via an EngineEventArg.</param> public virtual void ProcessEvent(EventArgs e) { if (e.GetType() != typeof(EngineEventArgs)) { return; // here is processing of EngineEventArgs only! } EngineEventArgs eArgs = (EngineEventArgs)e; EngineEventArgs.EventType requestType = eArgs.MsgType; EngineEventArgs.EventStatus requestStatus = eArgs.Status; // Validate this message if (requestStatus != EngineEventArgs.EventStatus.Request) { return; // I ignore all but requests. } // // Process the request. // switch (requestType) { // // **** Parameter Value Request **** // case EngineEventArgs.EventType.ParameterValue: // This is a request to broadcast the values of a single, multiple or all parameters. // eArgs.DataIntA contains parameter IDs for each parameter requested, or // eArgs.DataIntA = null, if all are requested. eArgs.DataObjectList = new List <object>(); if (GetParameterValue(ref eArgs.DataIntA, ref eArgs.DataObjectList)) { eArgs.Status = EngineEventArgs.EventStatus.Confirm; // found parameters } else { eArgs.Status = EngineEventArgs.EventStatus.Failed; // did not find parameters } break; // // **** Parameter Change Request **** // case EngineEventArgs.EventType.ParameterChange: // This is a request to change the value of one or more parameters. if (eArgs.DataIntA == null) { // Here, cannot blindly change all parameter values, this should NOT be null. eArgs.Status = EngineEventArgs.EventStatus.Failed; } else { // eArgs.DataIntA contains list of parameter IDs that user wants changed. // eArgs.DataObjectList contains new values desired. bool logEnabled = false; if (m_Log != null) { logEnabled = m_Log.BeginEntry(Hubs.LogLevel.Major, "ProcessEvent: {2} {0} #{1} parameter change request: ", m_EngineName, m_EngineID, (m_Parent != null ? string.Format("{0} #{1}", m_Parent.EngineContainerName, m_Parent.EngineContainerID) : string.Empty)); } for (int i = 0; i < eArgs.DataIntA.Length; ++i) //ith parameter in the list inside the event. { int pID = eArgs.DataIntA[i]; // propertyID of parameter ParameterInfo pInfo = m_PInfo[pID]; PropertyInfo property = this.GetType().GetProperty(pInfo.Name); if (logEnabled) { m_Log.AppendEntry("[{0}, requested value={1},", pInfo.Name, eArgs.DataObjectList[i]); } if (property.CanWrite) { // If writable, try to set value of this parameter in *this* object. try { property.SetValue(this, eArgs.DataObjectList[i], null); } catch (Exception) { } } // Regardless of result, get the current value object o = property.GetValue(this, null); eArgs.DataObjectList[i] = o; if (logEnabled) { m_Log.AppendEntry(" final value={0}] ", eArgs.DataObjectList[i]); } } if (logEnabled) { m_Log.EndEntry(); } // Set status to confirm signifying that the values in the message // are the correct, confirmed values from the engine. eArgs.Status = EngineEventArgs.EventStatus.Confirm; this.IsUpdateRequired = true; // signal that we need updating! } break; default: eArgs.Status = EngineEventArgs.EventStatus.Failed; break; } }//end ProcessEngineEvent()