示例#1
0
        /// <summary>
        /// When the timer fires, check for the state of the adapter; if running
        /// raise a new simulated event
        /// </summary>
        /// <param name="state"></param>
        private void RaiseEvent(object state)
        {
            // Ensure that the adapter is in the running state.  If we're
            // shutting down, kill the timer and signal Stopped()
            if (AdapterState.Stopping == AdapterState)
            {
                myTimer.Dispose();
                Stopped();
            }
            if (AdapterState.Running != AdapterState)
            {
                return;
            }

            // Allocate a point event to hold the data for the incoming message.
            // If the event could not be allocated, exit the function
            lock (lockObj)
            {
                // The next event will be raised at now + event period ms, plus a
                // random offset
                int nextEventInterval = config.EventPeriod +
                                        rand.Next(config.EventPeriodRandomOffset);
                myTimer.Change(nextEventInterval, nextEventInterval);

                IntervalEvent <TPayload> currEvent = CreateInsertEvent();
                if (currEvent == null)
                {
                    return;
                }
                currEvent.StartTime = DateTime.Now;
                currEvent.EndTime   = currEvent.StartTime.AddMilliseconds(nextEventInterval);

                // Create a payload object, and fill with values if we have a
                // an initializer defined
                currEvent.Payload = (TPayload)Activator.CreateInstance(typeof(TPayload));
                if (init != null)
                {
                    init.FillValues(currEvent.Payload);
                }

                if (trace.ShouldLog(TraceEventType.Verbose))
                {
                    trace.LogMsg(TraceEventType.Verbose, "INSERT - {0}",
                                 currEvent.FormatEventForDisplay(false));
                }

                // If the event cannot be enqueued, release the memory and signal that
                // the adapter is ready to process more events (via. Ready())
                if (EnqueueOperationResult.Full == Enqueue(ref currEvent))
                {
                    ReleaseEvent(ref currEvent);
                    Ready();
                }
            }
        }
示例#2
0
        /// <summary>
        /// Main worker thread function responsible for dequeueing events and
        /// posting them to the output stream.
        /// </summary>
        private void ConsumeEvents()
        {
            IntervalEvent currentEvent = default(IntervalEvent);

            try
            {
                while (true)
                {
                    if (AdapterState.Stopping == AdapterState)
                    {
                        Stopped();
                        return;
                    }

                    // Dequeue the event. If the dequeue fails, then the adapter state is suspended
                    // or stopping. Assume the former and call Ready() to indicate
                    // readiness to be resumed, and exit the thread.
                    if (DequeueOperationResult.Empty == Dequeue(out currentEvent))
                    {
                        Ready();
                        return;
                    }

                    string writeMsg = String.Empty;

                    if (currentEvent.EventKind == EventKind.Insert)
                    {
                        writeMsg = currentEvent.FormatEventForDisplay(eventType,
                                                                      !config.SingleLine);
                    }
                    else if (currentEvent.EventKind == EventKind.Cti)
                    {
                        writeMsg = String.Format("CTI - {0}",
                                                 currentEvent.StartTime.ToString("hh:mm:ss.fff"));
                    }

                    if (config.Target == TraceTarget.Console)
                    {
                        Console.WriteLine(writeMsg);
                    }
                    else if (config.Target == TraceTarget.Debug)
                    {
                        Debug.Write(writeMsg);
                    }

                    // Every received event needs to be released.
                    ReleaseEvent(ref currentEvent);
                }
            }
            catch (Exception e)
            {
                trace.LogException(e, "Error in console adapter dequeue");
            }
        }