public override FindEventResultList GetEventSearchResults(string SearchToken, int MinResults, bool MinResultsSpecified, int MaxResults, bool MaxResultsSpecified, string WaitTime)
        {
            EventsSearchSession session = SearchSessionManager.Instance.GetSession(SearchToken) as EventsSearchSession;

            if (session != null)
            {
                List <FindEventResult> data = (List <FindEventResult>)session.Data;

                FindEventResultList list = new FindEventResultList();

                Random rnd = new Random();
                int    cnt = Math.Min(rnd.Next(1, 4), data.Count);

                rnd = new Random();
                int sleep = rnd.Next(1, 3);
                System.Threading.Thread.Sleep(400 * sleep + 450);

                list.Result = new FindEventResult[cnt];
                for (int i = 0; i < cnt; i++)
                {
                    list.Result[i] = data[0];
                    data.RemoveAt(0);
                }
                session.ResultsSent = session.ResultsSent + cnt;
                list.SearchState    = data.Count > 0 ? SearchState.Searching : SearchState.Completed;

                if (data.Count == 0)
                {
                    SearchSessionManager.Instance.Sessions.Remove(session);
                }
                return(list);
            }
            else
            {
                CommonUtils.ReturnFault(new string[] { "Sender", "InvalidArgVal", "InvalidToken" });
                return(null);
            }
        }
        public override string FindEvents(System.DateTime StartPoint,
                                          System.DateTime EndPoint,
                                          bool EndPointSpecified,
                                          SearchScope Scope,
                                          EventFilter SearchFilter,
                                          bool IncludeStartState, int MaxMatches, bool MaxMatchesSpecified, string KeepAliveTime)
        {
            string token = SearchSessionManager.Instance.GetNextToken();

            int    keepAliveTime = 0;
            string strKeepAlive  = KeepAliveTime.Substring(2, KeepAliveTime.Length - 3);

            keepAliveTime = int.Parse(strKeepAlive);
            if (KeepAliveTime.EndsWith("M"))
            {
                keepAliveTime = keepAliveTime * 60;
            }

            EventsSearchSession session = new EventsSearchSession()
            {
                KeepAlive   = keepAliveTime,
                Token       = token,
                Started     = System.DateTime.Now,
                LastRequest = System.DateTime.Now,
                StartPoint  = StartPoint,
                EndPoint    = EndPointSpecified ? new Nullable <System.DateTime>(EndPoint) : null
            };

            if (MaxMatchesSpecified)
            {
                session.MaxMatches = MaxMatches;
            }

            if (Scope != null && Scope.IncludedRecordings != null & Scope.IncludedRecordings.Length > 0)
            {
                session.RecordingToken = Scope.IncludedRecordings[0];
            }

            List <FindEventResult> resultsList = null;

            bool ascending = true;

            if (session.EndPoint.HasValue)
            {
                if (session.EndPoint.Value < session.StartPoint.Value)
                {
                    ascending = false;
                }
            }

            // to get different events from one search operation to another
            //SearchStorage.Instance.ClearEvents();
            //SearchStorage.Instance.ChangeEventsGeneration();

            IEnumerable <FindEventResult> filtered =
                SearchStorage.Instance.Events.Where(
                    E =>
                    E.RecordingToken == session.RecordingToken);


            if (ascending)
            {
                resultsList =
                    filtered.Where(E => (E.Time >= session.StartPoint &&
                                         E.Time <= session.EndPoint)).OrderBy(E => E.Time).ToList();
            }
            else
            {
                resultsList =
                    filtered.Where(E => (E.Time <= session.StartPoint &&
                                         E.Time >= session.EndPoint)).OrderByDescending(E => E.Time).ToList();
            }

            if (session.MaxMatches.HasValue)
            {
                int cnt = Math.Min(session.MaxMatches.Value, resultsList.Count);
                List <FindEventResult> corrected = new List <FindEventResult>();
                for (int i = 0; i < cnt; i++)
                {
                    corrected.Add(resultsList[i]);
                }
                session.Data = corrected;
            }
            else
            {
                session.Data = resultsList;
            }

            SearchSessionManager.Instance.Sessions.Add(session);
            return(token);
        }