private int GetFreeThreadIndex()
        {
            string PROC  = "|=> GetFreeThreadIndex() : ";
            int    index = -1;

            try
            {
                lock (_lockUsage)
                {
                    ThreadContainer freeContainer = (from c in _threadContainers.Values
                                                     where c.HaveItems == false
                                                     select c).FirstOrDefault();
                    if (freeContainer != null)
                    {
                        index = freeContainer.ContainerIndex;
                        LogManager.WriteLog(PROC + "Thread " + index.ToString() + " was taken from the free queue.", LogManager.enumLogLevel.Info);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(PROC, ex);
            }
            finally
            {
                // no more free threads
                if (index == -1)
                {
                    index = _rnd.Next(0, _threadCount - 1);
                    LogManager.WriteLog(PROC + "Thread " + index.ToString() + " was taken as random.", LogManager.enumLogLevel.Info);
                }
            }

            return(index);
        }
Ejemplo n.º 2
0
        }//ProcessCreateEngine()

        //
        //
        // *****************************************************************
        // ****              ProcessAddContainerRequest()               ****
        // *****************************************************************
        /// <summary>
        /// A request from a local execution container that would like to add containers
        /// to this hub and have listeners added to them.
        /// </summary>
        /// <param name="eventArg"></param>
        private void ProcessAddContainerRequest(EngineEventArgs eventArg)
        {
            string strategyHubName = (string)eventArg.DataObjectList[0];

            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 = (ThreadContainer)eventArg.DataObjectList[1];

            if (!executionContainers.ContainsKey(eventArg.EngineContainerID))
            {   // double check we haven't added this container before.
                executionContainers.Add(container.EngineContainerID, container);
            }
            else
            {
                Log.NewEntry(LogLevel.Error, "ProcessAddContainerRequest: Duplicate request to add container - {0}", container.EngineContainerName);
            }

            SetupAndStartContainer(container);
        }
Ejemplo n.º 3
0
        //
        // *****************************************
        // ****     ProcessEngineEvent          ****
        // *****************************************
        /// <summary>
        /// A request from outside to change a parameter value.
        /// </summary>
        protected void ProcessEngineEvent(EngineEventArgs eventArgs)
        {
            ThreadContainer 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()
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a unique Thread per CPU core/logical processor.
        /// </summary>
        /// <param name="threadPriority"></param>
        public Task <ThreadContainer[]> CreateCpuCoreThreadContainersAsync(ThreadPriority threadPriority = ThreadPriority.Lowest)
        {
            var cpuCoreThreadContainers = new ThreadContainer[System.CpuCount * Math.Max(System.CoresPerCpu, System.LogicalProcessorsPerCpu)];
            var maximumThreadsPerCpu    = Math.Max(System.CoresPerCpu, System.LogicalProcessorsPerCpu);

            for (int currentCpu = 0; currentCpu < System.CpuCount; currentCpu++)
            {
                for (int currentCore = 0; currentCore < maximumThreadsPerCpu; currentCore++)
                {
                    var threadContainer = new ThreadContainer
                    {
                        CpuCoreNumber             = currentCore,
                        CpuLogicalProcessorNumber = currentCore,
                        CpuNumber               = currentCpu,
                        CoresPerCpu             = System.CoresPerCpu,
                        LogicalProcessorsPerCpu = System.LogicalProcessorsPerCpu,
                        ThreadStatus            = ThreadStatus.Idle,
                        Thread = new Thread(ThreadWorker)
                        {
                            Name         = $"CpuUsageGremlin #{currentCpu}-{currentCore}",
                            Priority     = threadPriority,
                            IsBackground = true
                        }
                    };

                    cpuCoreThreadContainers[currentCpu * maximumThreadsPerCpu + currentCore] = threadContainer;
                }
            }

            return(Task.FromResult(cpuCoreThreadContainers));
        }
Ejemplo n.º 5
0
        }// 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];
            ThreadContainer 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);
            }
        }
Ejemplo n.º 6
0
        private static ThreadContainer parse_ffuuka_json(FoolFuukaParserData ffp_data)
        {
            ThreadContainer tc = null;

            string data = fetch_api(ffp_data);

            JsonObject response = JsonConvert.Import <JsonObject>(data);

            JsonObject threadObject = (JsonObject)response[ffp_data.ThreadID.ToString()];

            JsonObject opPost = (JsonObject)threadObject["op"];

            tc = new ThreadContainer(parse_thread(opPost, ffp_data));

            JsonObject postsObject = (JsonObject)threadObject["posts"];

            foreach (string reply_id in postsObject.Names.Cast <string>())
            {
                JsonObject  replyObject = (JsonObject)postsObject[reply_id];
                GenericPost reply       = parse_reply(replyObject, ffp_data);
                tc.AddReply(reply);
                continue;
            }

            return(tc);
        }
Ejemplo n.º 7
0
 protected override void SetupInitialize(IEngineHub myEngineHub, IEngineContainer engineContainer, int engineID, bool setupGui)
 {
     base.SetupInitialize(myEngineHub, engineContainer, engineID, setupGui);
     m_Hub      = (Hub)myEngineHub;
     this.m_Log = m_Hub.Log;                                              // set up our logging
     this.m_ExecutionContainer = (ThreadContainer)engineContainer;
 }
Ejemplo n.º 8
0
        //
        //
        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);
        }
Ejemplo n.º 9
0
        public static int CreateStream(ITickable world)
        {
            Thread thread    = new Thread(new ParameterizedThreadStart(Tick));
            var    threadId  = thread.ManagedThreadId;
            var    container = new ThreadContainer(thread, world, true);

            IdOfThreads.TryAdd(threadId, container);//добавил в словарь
            thread.Start(container);
            return(threadId);
        }
Ejemplo n.º 10
0
        //
        //
        public override void SetupBegin(IEngineHub myEngineHub, IEngineContainer engineContainer)
        {
            base.SetupBegin(myEngineHub, engineContainer);

            if (myEngineHub is ExecutionHub)
            { // set up ITimer to work
                ThreadContainer execContainer = (ThreadContainer)engineContainer;
                ((ExecutionHub)myEngineHub).SubscribeToTimer((ITimerSubscriber)execContainer.m_ExecutionListener);
                execContainer.m_ExecutionListener.SubscribeToTimer(this); // subscribe to updates
            }
        }
Ejemplo n.º 11
0
 /// <summary>
 /// The actual Stack Worker method that gets executed
 /// </summary>
 private static void StackWorker()
 {
     while (m_WorkerRunning)
     {
         if (m_ThreadStack.Count > 0)
         {
             ThreadContainer tc = m_ThreadStack.Pop();
             tc.Thread.Start(tc.Params);
         }
     }
 }
Ejemplo n.º 12
0
        private OrderEventArgs m_OrderEventArg; // careful, but this is always recycled

        #endregion                              // members


        #region Constructors
        // *****************************************************************
        // ****                     Constructors                        ****
        // *****************************************************************
        //
        //
        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_Log = ((ExecutionHubs.ExecutionHub)myEngineHub).Log;
            }
            else
            {   // this is the second set up call from the correct container, add correct sub engine mappings
                m_ExecutionContainer = (ThreadContainer)engineContainer;
            }
        }
Ejemplo n.º 13
0
 public void AddStaticThread(ThreadContainer tc, bool thumbOnly)
 {
     if (this.watched_threads.ContainsKey(tc.Instance.ID))
     {
         // do nothing
         return;
     }
     else
     {
         this.watched_threads.Add(tc.Instance.ID, new ThreadWorker(this, tc, thumbOnly));
     }
 }
Ejemplo n.º 14
0
        //
        // *************************************************************
        // ****                 Setup Initialize()                  ****
        // *************************************************************
        /// <summary>
        /// Since I depend critically on an OrderBookHub, I will look for them now.
        /// </summary>
        /// <param name="myEngineHub"></param>
        /// <param name="engineContainer"></param>
        /// <param name="engineID"></param>
        /// <param name="setupGui"></param>
        protected override void SetupInitialize(IEngineHub myEngineHub, IEngineContainer engineContainer, int engineID, bool setupGui)
        {
            base.SetupInitialize(myEngineHub, engineContainer, engineID, false);
            EngineGui engineGui = base.SetupGuiTemplates();

            engineGui.LowerHudFullName = typeof(OrderEngineHud).FullName;


            // Collect services that I need.
            m_Hub                = (Hub)myEngineHub;
            this.Log             = m_Hub.Log;
            m_ExecutionContainer = (ThreadContainer)engineContainer;
        }// SetupInitialize()
Ejemplo n.º 15
0
        /// <summary>
        /// This constructor is only used to save dead (from archive) threads
        /// </summary>
        /// <param name="board"></param>
        /// <param name="tc"></param>
        public ThreadWorker(BoardWatcher board, ThreadContainer tc, bool thumbOnly)
        {
            this.ID                 = tc.Instance.ID;
            this.Board              = board;
            this.LastUpdated        = tc.Instance.Time;
            this.AddedAutomatically = false;
            this.IsStatic           = true;
            this.worker             = new BackgroundWorker();
            this.ThumbOnly          = thumbOnly;
            this.ThreadTitle        = tc.Title;

            save_thread_container(tc);
        }
Ejemplo n.º 16
0
        }// CheckSpontaneousEngineEvents()

        //
        //
        // *****************************************************************
        // ****                 SetupAndStartContainer()                ****
        // *****************************************************************
        /// <summary>
        /// Caller would like to complete the container creation process adding an execution
        /// listener and starting the container.
        /// </summary>
        /// <param name="container"></param>
        private void SetupAndStartContainer(ThreadContainer container)
        {
            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();
        }
        protected override void OnSingletonCreated()
        {
            var us         = UpdateSystem.Instance;
            var processors = Math.Max(1, Environment.ProcessorCount - 1);

            for (int i = 0; i < processors; i++)
            {
                var thread = new ThreadContainer(10000);
                var node   = threads.Add(thread);
                thread.SetNode(node);
            }
            var inst = UnityThreading.Instance;

            UnityThreading.CloseThreads.Subscribe(CloseThreads, true);
        }
Ejemplo n.º 18
0
        public override void SingletonCreation()
        {
            var us         = EiUpdateSystem.Instance;
            var processors = Math.Max(1, Environment.ProcessorCount - 1);

            for (int i = 0; i < processors; i++)
            {
                var thread = new ThreadContainer(10000);
                var node   = threads.Add(thread);
                thread.SetNode(node);
            }
            var inst = EiUnityThreading.Instance;

            EiUnityThreading.CloseThreads.SubscribeThreadSafe(CloseThreads);
        }
Ejemplo n.º 19
0
        private void save_thread_container(ThreadContainer tc)
        {
            savePost(tc.Instance);

            if (tc.Instance.File != null)
            {
                Program.dump_files(tc.Instance.File, this.ThumbOnly);
            }

            int count = tc.Replies.Count();

            int with_image = 0;

            for (int i = 0; i < count; i++)
            {
                GenericPost reply = tc.Replies[i];

                savePost(reply);

                if (reply.File != null)
                {
                    ++with_image;
                    Program.dump_files(tc.Replies[i].File, this.ThumbOnly);
                }
            }

            log(new LogEntry()
            {
                Level   = LogEntry.LogLevel.Success,
                Message = string.Format("Static thread {0} was saved successfully.", this.ID),
                Sender  = "ThreadWorker",
                Title   = string.Format("/{0}/ - {1}", this.Board.Board, this.ID)
            });

            log(new LogEntry()
            {
                Level = LogEntry.LogLevel.Info, Message = "Optimizing thread data."
            });

            ThreadStore.GetStorageEngine().OptimizeThread(this.Board.Board, this.ID);

            log(new LogEntry()
            {
                Level = LogEntry.LogLevel.Success, Message = "Optimisation done."
            });
        }
Ejemplo n.º 20
0
        public ThreadContainer GetThreadData(string board, int id)
        {
            APIResponse response = LoadAPI(string.Format("{0}://a.4cdn.org/{1}/thread/{2}.json", Common.HttpPrefix, board, id));

            switch (response.Error)
            {
            case APIResponse.ErrorType.NoError:
                ThreadContainer tc = null;

                JsonObject list = JsonConvert.Import <JsonObject>(response.Data);

                //if (list == null)
                //{
                //    FlushAPI(string.Format("http://a.4cdn.org/{0}/thread/{1}.json", board, id));
                //    return GetThreadData(board, id);
                //}

                if (list.Names.Cast <string>().Contains("posts"))
                {
                    JsonArray data = list["posts"] as JsonArray;

                    tc = new ThreadContainer(ParseThread((JsonObject)data.First(), board));

                    for (int index = 1; index < data.Count; index++)
                    {
                        tc.AddReply(ParseReply((JsonObject)data[index], board));
                    }
                }

                return(tc);

            case APIResponse.ErrorType.NotFound:
                throw new Exception("404");

            case APIResponse.ErrorType.Other:
                throw new Exception(response.Data);

            default:
                return(null);
            }
        }
Ejemplo n.º 21
0
        //
        //
        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);
            }
        }
Ejemplo n.º 22
0
        //
        //
        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");
                }
            }
        }
        /// <summary>
        /// Initializes the threads.
        /// </summary>
        private void InitializeThreads()
        {
            LogManager.WriteLog("|=> Inside InitializeThreads().", LogManager.enumLogLevel.Info);

            try
            {
                for (int i = 0; i < _threadCount; i++)
                {
                    Thread th = new Thread(new ParameterizedThreadStart(this.DoWorker));
                    th.IsBackground = true;
                    th.Name         = "ThreadDispatcher_" + _helperName + "_" + th.ManagedThreadId.ToString();

                    ThreadContainer container = new ThreadContainer();
                    container.WorkerThread   = th;
                    container.WaitHandle     = new ManualResetEvent(false);
                    container.ContainerIndex = i;

                    int index = _closeHandles.Length;
                    Array.Resize <WaitHandle>(ref _closeHandles, index + 1);
                    _closeHandles[index] = container.WaitHandle;

                    _threadContainers.Add(i, container);
                    th.Start(container);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
            }
            finally
            {
                _intialized = 2;
                _evtInitialize.Set();
                LogManager.WriteLog("|=> All the dispatcher threads are initialized.", LogManager.enumLogLevel.Info);
            }
        }
Ejemplo n.º 24
0
 public T Resolve <T>(params IConstructorNamedParameter[] parameters)
 {
     return(ThreadContainer.Resolve <T>(parameters));
 }
        /// <summary>
        /// Adds the thread data.
        /// </summary>
        /// <param name="threadData">The thread data.</param>
        /// <returns></returns>
        public bool AddThreadData(T threadData)
        {
            if (_intialized != 2)
            {
                _evtInitialize.WaitOne();
            }
            string PROC   = "|=> AddThreadData() : ";
            bool   result = false;

            try
            {
                if (!_isListening)
                {
                    LogManager.WriteLog(PROC + "Threads was instructed to close. Unable to add  : " +
                                        threadData.UniqueKey, LogManager.enumLogLevel.Info);
                    return(false);
                }

                // thread index
                int index = 0;
                if (_threadIndexes.ContainsKey(threadData.UniqueKey))
                {
                    index = _threadIndexes[threadData.UniqueKey];
                    if (_enableDetailedLogs)
                    {
                        LogManager.WriteLog(PROC + string.Format("Thread Unique Key [{0}] was found in [{1:D}].",
                                                                 threadData.UniqueKey, index), LogManager.enumLogLevel.Info);
                    }
                }
                else
                {
                    // take any free thread or a randon thread
                    index = GetFreeThreadIndex();

                    // not already exist?
                    if (!_threadIndexes.ContainsKey(threadData.UniqueKey))
                    {
                        _threadIndexes.Add(threadData.UniqueKey, index);
                    }
                }

                // add to thread collection
                if (_threadContainers.ContainsKey(index))
                {
                    ThreadContainer container = _threadContainers[index];
                    lock (container.BufferLock)
                    {
                        threadData.DispatcherThreadName = container.ThreadName;
#if THREAD_NO_QUEUE
                        container.BufferDatas.Add(threadData);
#else
                        container.BufferDatas.Enqueue(threadData);
#endif
                        if (_enableDetailedLogs)
                        {
                            LogManager.WriteLog(PROC + string.Format("[{0}] added to [{1}] container.",
                                                                     threadData.UniqueKey, container.ThreadId), LogManager.enumLogLevel.Info);
                        }
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
            }

            return(result);
        }
        /// <summary>
        /// Does the worker.
        /// </summary>
        private void DoWorker(object state)
        {
            string PROC = "|=> DoWorker() : ";

            LogManager.WriteLog(PROC + "Thread : " + Thread.CurrentThread.ManagedThreadId.ToString(), LogManager.enumLogLevel.Info);
            ThreadContainer container = state as ThreadContainer;
            string          threadMsg = PROC + string.Format("[{0}] : ", container.WorkerThread.Name);

#if THREAD_NO_QUEUE
            List <T> actualDatas = container.ActualDatas;
            List <T> bufferDatas = container.BufferDatas;
#else
            Queue <T> actualDatas = container.ActualDatas;
            Queue <T> bufferDatas = container.BufferDatas;
#endif

            try
            {
                int interval = 100;

                while (_isListening)
                {
                    try
                    {
                        // something to do
                        if (actualDatas.Count > 0)
                        {
                            if (_enableDetailedLogs)
                            {
                                LogManager.WriteLog(threadMsg + string.Format("[{0:D}] actual Items found.", actualDatas.Count), LogManager.enumLogLevel.Info);
                            }
                            while (actualDatas.Count > 0)
                            {
                                if (!_isListening)
                                {
                                    LogManager.WriteLog(threadMsg + "Thread was instructed to close.",
                                                        LogManager.enumLogLevel.Info);
                                    return;
                                }
#if THREAD_NO_QUEUE
                                T threadData = actualDatas[0] as T;
#else
                                T threadData = actualDatas.Dequeue();
#endif

                                LogManager.WriteLog(threadMsg + "Processing Item : " + threadData.UniqueKey, LogManager.enumLogLevel.Info);
                                try
                                {
                                    this.OnProcessThreadData(threadData);
                                }
                                catch (Exception ex)
                                {
                                    ExceptionManager.Publish(ex);
                                }

#if THREAD_NO_QUEUE
                                actualDatas.RemoveAt(0);
#endif
                            }
                        }

                        // if the user added items to buffer datas in the meantime.
                        if (bufferDatas.Count > 0)
                        {
                            if (_enableDetailedLogs)
                            {
                                LogManager.WriteLog(threadMsg + string.Format("[{0:D}] Buffer Items found.", bufferDatas.Count.ToString()), LogManager.enumLogLevel.Info);
                            }

                            while (bufferDatas.Count > 0)
                            {
                                if (!_isListening)
                                {
                                    LogManager.WriteLog(threadMsg + "Thread was instructed to close.",
                                                        LogManager.enumLogLevel.Info);
                                    return;
                                }
                                lock (container.BufferLock)
                                {
#if THREAD_NO_QUEUE
                                    T threadData = bufferDatas[0];
                                    actualDatas.Add(threadData);
                                    bufferDatas.RemoveAt(0);
#else
                                    T threadData = bufferDatas.Dequeue();
                                    if (this.ProcessThreadData != null)
                                    {
                                        actualDatas.Enqueue(threadData);
                                    }
#endif
                                }
                            }
                            if (_enableDetailedLogs)
                            {
                                LogManager.WriteLog(threadMsg + string.Format("[{0:D}] items moved from buffer to actual.", actualDatas.Count), LogManager.enumLogLevel.Info);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ExceptionManager.Publish(ex);
                    }
                    finally
                    {
                        Thread.Sleep(interval);
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
            }
            finally
            {
                LogManager.WriteLog(threadMsg + "Finished Successfully.", LogManager.enumLogLevel.Info);
                container.WaitHandle.Set();
            }
        }
Ejemplo n.º 27
0
        public static AddThreadFromArchiveStatus AddThreadFromArchive(string board, int tid, bool outputConsoleMessages = false, ArchiveInfo info = null)
        {
            if (info == null)
            {
                var archives = ArchivesProvider.GetArchivesForBoard(board, false);

                if (archives.Length == 0)
                {
                    if (outputConsoleMessages)
                    {
                        Console.WriteLine("Cannot find an archive to use for board {0}", board);
                    }
                    return(AddThreadFromArchiveStatus.CannotFindAnArchive);
                }
                else
                {
                    if (outputConsoleMessages)
                    {
                        Console.WriteLine("Found {0} archives for board {1}", archives.Length, board);
                    }

                    foreach (var archive_info in archives)
                    {
                        if (archive_info.Software == ArchiveInfo.ArchiverSoftware.FoolFuuka)
                        {
                            info = archive_info;
                            if (outputConsoleMessages)
                            {
                                Console.WriteLine("Selecting {0} (host: {1}) archive", info.Name, info.Domain);
                            }
                            break;
                        }
                    }
                }
            }

            bool is_4chan_board = Program.ValidBoards.ContainsKey(board);
            bool archive_found  = info != null;

            if (is_4chan_board && archive_found)
            {
                FoolFuukaParserData a = new FoolFuukaParserData(info, board, tid);

                BoardWatcher bw = Program.GetBoardWatcher(board);

                if (outputConsoleMessages)
                {
                    Console.WriteLine("Adding thread {0} from board {1}...", a.ThreadID, a.BOARD);
                }

                ThreadContainer tc = FoolFuukaParser.Parse(a);

                if (tc != null)
                {
                    bw.AddStaticThread(tc, Settings.ThumbnailOnly);
                    if (outputConsoleMessages)
                    {
                        Console.WriteLine("Thread {0} from board {1} added.", a.ThreadID, a.BOARD);
                    }
                    return(AddThreadFromArchiveStatus.Success);
                }
                else
                {
                    if (outputConsoleMessages)
                    {
                        Console.WriteLine("Cannot add this thread. Possible reasons:\n"
                                          + "- The thread ID is invalid\n"
                                          + "- The archive no longer archive this board.\n"
                                          + "- The archive has no JSON API support");
                    }
                    return(AddThreadFromArchiveStatus.Error);
                }
            }
            else if (!is_4chan_board)
            {
                if (outputConsoleMessages)
                {
                    Console.WriteLine("Unsupported board {0}", board);
                }
                return(AddThreadFromArchiveStatus.UnsupportedBoard);
            }
            else if (!archive_found)
            {
                if (outputConsoleMessages)
                {
                    Console.WriteLine("Cannot find an archive with supported software");
                }
                return(AddThreadFromArchiveStatus.UnsupportedSoftware);
            }
            return(AddThreadFromArchiveStatus.Unknown);
        }
Ejemplo n.º 28
0
        }//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()
Ejemplo n.º 29
0
        //
        //
        /// <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);

            //}
        }