}//ProcessCreateEngine() // // ***************************************** // **** ProcessEngineEvent **** // ***************************************** /// <summary> /// A request from outside to change a parameter value. /// </summary> protected void ProcessEngineEvent(EngineEventArgs eventArgs) { ExecutionContainer strategy = null; int strategyID = eventArgs.EngineContainerID; // parameter change requested for this strategy. if (strategyID < 0) { // This request is for all strategies Log.NewEntry(LogLevel.Error, "ProcessEngineEvent: Negative EngineContainerId not allowed in {0}.", eventArgs); eventArgs.EngineHubName = this.ServiceName; eventArgs.Status = EngineEventArgs.EventStatus.Failed; OnEngineChanged(eventArgs); } else if (m_ExecutionContainers[DefaultHubName].TryGetValue(strategyID, out strategy)) { // Found the strategy, pass it the request now. // He is on another thread, so give him a thread safe copy. // He will be allowed to modify this object to compose his response. //EngineEventArgs copyEventArgs = eventArgs.Copy(); strategy.ProcessEvent(eventArgs.Copy()); } else { // Unknown strategy Log.NewEntry(LogLevel.Error, "ProcessEngineEvent: Unknown EngineContainerId {0}", eventArgs); eventArgs.EngineHubName = this.ServiceName; eventArgs.Status = EngineEventArgs.EventStatus.Failed; OnEngineChanged(eventArgs); } }// ProcessParameterChangeRequest()
}// ProcessParameterChangeRequest() // // // ***************************************** // **** ProcessSyntheticOrderRequest **** // ***************************************** /// <summary> /// A request for submission of a synthetic order. /// </summary> /// <param name="engineEventArg"></param> private void ProcessSyntheticOrderRequest(EngineEventArgs engineEventArg) { SyntheticOrder syntheticOrder = (SyntheticOrder)engineEventArg.DataObjectList[0]; ExecutionContainer strategy = null; int strategyID = engineEventArg.EngineContainerID; if (strategyID < 0) { // This request is for all strategies Log.NewEntry(LogLevel.Error, "ProcessEngineEvent: Negative EngineContainerId not allowed in {0}.", syntheticOrder); engineEventArg.EngineHubName = this.ServiceName; engineEventArg.Status = EngineEventArgs.EventStatus.Failed; OnEngineChanged(engineEventArg); } else if (m_ExecutionContainers[DefaultHubName].TryGetValue(strategyID, out strategy)) { // Found the strategy, pass it the request now. // He is on another thread, so give him a thread safe copy. strategy.ProcessEvent(engineEventArg.Copy()); } else { // Unknown strategy Log.NewEntry(LogLevel.Error, "ProcessEngineEvent: Unknown EngineContainerId {0}", syntheticOrder); engineEventArg.EngineHubName = this.ServiceName; engineEventArg.Status = EngineEventArgs.EventStatus.Failed; OnEngineChanged(engineEventArg); } }
}//ProcessServiceStateRequests() // // // ***************************************************************** // **** ProcessCreateStrategyRequest() **** // ***************************************************************** /// <summary> /// Process request to create new Engine. All the operations here /// are performed by the Hub thread. Once the Execution Strategy is /// ready to be launched, it is passed to its own thread, and then /// never again touched by the hub thread. /// </summary> /// <param name="eventArg">contains data</param> private void ProcessCreateStrategyRequest(EngineEventArgs eventArg) { // // Validate data in eventArg // if (eventArg.Status != EngineEventArgs.EventStatus.Request) { // I only respond to a request. return; } if (eventArg.DataObjectList == null || eventArg.DataObjectList.Count < 2) { Log.NewEntry(LogLevel.Warning, "ProcessCreateNewContainer: Failed to extract data."); return; } string xmlString = (string)eventArg.DataObjectList[0]; string strategyHubName = (string)eventArg.DataObjectList[1]; int engineCount = 0; if (!int.TryParse((string)eventArg.DataObjectList[2], out engineCount)) { engineCount = -1; } // // Obtain the EngineContainer // Dictionary <int, ExecutionContainer> executionContainers = null; if (!m_ExecutionContainers.TryGetValue(strategyHubName, out executionContainers)) { // This is first container for this particular hub. Create a place for it. executionContainers = new Dictionary <int, ExecutionContainer>(); if (string.IsNullOrEmpty(DefaultHubName)) { DefaultHubName = strategyHubName; } m_ExecutionContainers.Add(strategyHubName, executionContainers); } ExecutionContainer container = null; if (!executionContainers.TryGetValue(eventArg.EngineContainerID, out container)) { container = new ExecutionContainer(); container.EngineContainerID = eventArg.EngineContainerID; executionContainers.Add(container.EngineContainerID, container); if (engineCount >= 0) { container.TotalEngineCount = engineCount; } container.EngineContainerName = "need ContainerName"; // Locate the Strategy server this request came from IService iService; if (AppServices.GetInstance().TryGetService(strategyHubName, out iService) && iService is IEngineHub) { container.RemoteEngineHub = (IEngineHub)iService; } // TODO: Continue initializing the container } // // Create the Engine. // bool isSuccess = true; byte[] byteArray = Encoding.ASCII.GetBytes(xmlString); using (System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray)) { try { StringifiableReader stringReader = new StringifiableReader(stream); List <IStringifiable> objects = stringReader.ReadToEnd(); foreach (IStringifiable iObject in objects) { if (iObject is Engine) { if (container.TryAddEngine((Engine)iObject)) { //Engine engine = (Engine)iObject; //int engineID = engine.EngineID; // These are created via attributes (so they coincide with values set by StrategyHub). //((Engine)iObject).SetupInitialize(this, container, engineID); } else { isSuccess = false; Log.NewEntry(LogLevel.Warning, "ProcessCreateEngine: Failed to add {0} to container {1}.", iObject, container); } } } } catch (Exception ex) { isSuccess = false; Log.NewEntry(LogLevel.Warning, "ProcessCreateEngine: {0}", ex.Message); } }//using if (isSuccess == false) { return; } // Finalize if (engineCount == container.EngineList.Count && container.IOrderEngine != null) { // container.SetupInitialize(this); // The new engine created call back is now called on listener threads, // after the execution strategy is ready to run. He must call his // container function to broadcast his readiness! // Connect the appropriate listener and START! ExecutionListener listener = CreateListener(string.Format("Listener{0}", container.EngineContainerID)); container.AddExecutionListener(listener); container.Start(); } else { } }//ProcessCreateEngine()