// // protected override void SetupInitialize(IEngineHub myEngineHub, IEngineContainer engineContainer, int engineID, bool setupGui) { base.SetupInitialize(myEngineHub, engineContainer, engineID, setupGui); ThreadContainer execContainer = (ThreadContainer)engineContainer; execContainer.TryAddEngine(m_Hedger); // add my sub engines to the container. execContainer.TryAddEngine(m_HedgeRuleManager); }
// // protected override void SetupInitialize(IEngineHub myEngineHub, IEngineContainer engineContainer, int engineID, bool setupGui) { if (typeof(UV.Strategies.ExecutionHubs.ExecutionContainers.MultiThreadContainer).IsAssignableFrom(engineContainer.GetType())) { // this is the "first" set up call from the manager container. base.SetupInitialize(myEngineHub, engineContainer, engineID, setupGui); m_MultiThreadContainer = (MultiThreadContainer)engineContainer; m_MultiThreadContainer.TryAddEngine(m_Scratcher); // we need to add our sub engines to the container, just to allow set up and messaging to correctly function } else { // this is the second set up call from the correct container, add correct sub engine mappings ThreadContainer execContainer = (ThreadContainer)engineContainer; execContainer.TryAddEngine(m_Scratcher); if (execContainer.IOrderEngine is CurveTrader) { m_CurveTrader = (CurveTrader)execContainer.IOrderEngine; } m_MultiThreadContainer.TryAddEngineIdToManagingContainer(execContainer, m_Scratcher.EngineID); } }
// // protected override void SetupInitialize(IEngineHub myEngineHub, IEngineContainer engineContainer, int engineID, bool setupGui) { this.m_EngineName = string.Format("HedgeRuleManager:{0}", m_QuoterLeg.m_PriceLeg.InstrumentName); base.SetupInitialize(myEngineHub, engineContainer, engineID, setupGui); m_Hedger = m_QuoterLeg.m_Hedger; m_Log = ((ExecutionHub)myEngineHub).Log; ThreadContainer execContainer = (ThreadContainer)engineContainer; m_HedgeRules.OrderBy(x => x.RuleNumber); // sort our list by the rule number, so they get called in order! foreach (HedgeRule rule in m_HedgeRules) // check that they are all unique, and add them to our container { if (!m_PriorityToRule.ContainsKey(rule.RuleNumber)) { m_PriorityToRule.Add(rule.RuleNumber, rule); execContainer.TryAddEngine((Engine)rule); // add my sub engines to the container. } else { m_Log.NewEntry(LogLevel.Error, "HedgeRuleManager: Failed to find unique hedge rules"); throw new Exception("HedgeRuleManager: Failed to find unique hedge rules"); } } }
}//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; } string containerTypeString = (string)eventArg.DataObjectList[3]; // // Obtain the EngineContainer // Dictionary <int, ThreadContainer> 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, ThreadContainer>(); if (string.IsNullOrEmpty(DefaultHubName)) { DefaultHubName = strategyHubName; } m_ExecutionContainers.Add(strategyHubName, executionContainers); } ThreadContainer container = null; if (!executionContainers.TryGetValue(eventArg.EngineContainerID, out container)) { Type containerType = (typeof(ThreadContainer).Assembly).GetType(containerTypeString); // convert string to exact type. container = (ThreadContainer)Activator.CreateInstance(containerType); // create instance 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) { SetupAndStartContainer(container); // finish initialization, add listener and start } else { } }//ProcessCreateEngine()
// // /// <summary> /// Called by the execution hub thread, not the execution listener! /// </summary> /// <param name="myEngineHub"></param> /// <param name="engineContainer"></param> /// <param name="engineID"></param> /// <param name="setupGui"></param> protected override void SetupInitialize(Lib.Engines.IEngineHub myEngineHub, Lib.Engines.IEngineContainer engineContainer, int engineID, bool setupGui) { base.SetupInitialize(myEngineHub, engineContainer, engineID, setupGui); m_Hub = (Hub)myEngineHub; ExecutionHub executionHub = (ExecutionHub)myEngineHub; m_ExecutionContainer = (ThreadContainer)engineContainer; // this must be a multithreaded container for this execution to work m_Log = m_Hub.Log; // // Set up engines translation with manager container // MultiThreadContainer multiThreadContainer = (MultiThreadContainer)m_ExecutionContainer; multiThreadContainer.TryAddEngineIdToManagingContainer(multiThreadContainer, this.EngineID); // // Create new execution containers to be managed on seperate threads for each curve trader // foreach (CurveTrader curveTrader in m_CurveTraders) { ThreadContainer container = new ThreadContainer(); container.EngineContainerID = System.Threading.Interlocked.Increment(ref m_LastContainerId); // increment our id and save our engine id container.EngineContainerName = string.Format("CurveTrader{0}", container.EngineContainerID); // Add basic name container.RemoteEngineHub = executionHub; // pointer to the local hub, a remote strategy will not control this container.TotalEngineCount = 1; // this is only the number of "super engines" not sub engines! m_ExecutionContainer.TryAddEngine(curveTrader); // add this engine to both containers! container.TryAddEngine(curveTrader); // need to be careful here with "double" launching curveTrader.m_CurveTradeManager = this; // give pointer to manager to each trader...careful with threads! m_CurveTraderPendingLaunch.Add(curveTrader.EngineID); // engine id's we are waiting on to launch properly multiThreadContainer.TryAddEngineIdToManagingContainer(container, curveTrader.EngineID); // now that all engines are added, sent off the container to the hub to be finish processing. EngineEventArgs engineEventArgs = new EngineEventArgs(); engineEventArgs.MsgType = EngineEventArgs.EventType.AddContainer; engineEventArgs.Status = EngineEventArgs.EventStatus.Request; engineEventArgs.EngineContainerID = container.EngineContainerID; engineEventArgs.DataObjectList = new List <object>(); engineEventArgs.DataObjectList.Add("ExecutionHub"); engineEventArgs.DataObjectList.Add(container); m_Hub.HubEventEnqueue(engineEventArgs); } //foreach (CurveLegList curveLegs in m_CurveLegLists) //{ // call hub to create the execution container for each of these sub stratgies, once it is created and done, assign the list // // to the object, the hub will call the set up functions afterwards // ThreadContainer container = new ThreadContainer(); // container.EngineContainerID = System.Threading.Interlocked.Increment(ref m_LastContainerId); // increment our id and save our engine id // container.EngineContainerName = string.Format("CurveTrader{0}", container.EngineContainerID); // Add basic name // container.RemoteEngineHub = executionHub; // pointer to the local hub, a remote strategy will not control this // container.TotalEngineCount = 2; // this is only the number of "super engines" not sub engines! // // Add all Engines to container here // CurveTrader curveTrader = new CurveTrader(); // create new curve trader execution unit // curveTrader.Id = container.EngineContainerID; // use same id as container, (they are 1:1) // curveTrader.EngineID = 0; // I am the first engine, so assign me 0 // m_CurveTraderPendingLaunch.Add(curveTrader.Id); // save id to make sure we have everything before we start // m_CurveTraders.Add(curveTrader); // keep pointer to curve trader, carefuly using it due to threading! // curveTrader.m_CurveLegs = curveLegs; // assign the needed legs to the new unit // curveTrader.m_CurveTradeManager = this; // give curve trader pointer so he can call back up to me. // // TODO: create any other engines here? // // TODO: subscribe to these engines events? when they are launched we want to launch ourselves, etc // if(!container.TryAddEngine((Engine)curveTrader)) // { // m_Log.NewEntry(LogLevel.Error, "CurveTradeManager: Unable to create execution unit!"); // continue; // } // m_ExecutionContainer.TryAddEngine(curveLegs); // // now that all engines are added, sent off the container to the hub to be finish processing. // EngineEventArgs engineEventArgs = new EngineEventArgs(); // engineEventArgs.MsgType = EngineEventArgs.EventType.AddContainer; // engineEventArgs.Status = EngineEventArgs.EventStatus.Request; // engineEventArgs.EngineContainerID = container.EngineContainerID; // engineEventArgs.DataObjectList = new List<object>(); // engineEventArgs.DataObjectList.Add("ExecutionHub"); // engineEventArgs.DataObjectList.Add(container); // m_Hub.HubEventEnqueue(engineEventArgs); //} }