public static IEnumerable<CycleCompletedArgs> GetCycles(this RemoteViewClient client, Action<RemoteViewClient> attachAction)
        {
            using (var resultQueue = new BlockingCollection<object>(new ConcurrentQueue<object>()))
            using (var otherQueue = new BlockingCollection<object>(new ConcurrentQueue<object>()))
            {
                var resultListener = new EventViewResultListener();
                resultListener.CycleCompleted += (sender, e) => resultQueue.Add(e);

                resultListener.CycleExecutionFailed += (s, e) => otherQueue.Add(e);
                resultListener.ProcessTerminated += (s, e) => otherQueue.Add(e);
                resultListener.ViewDefinitionCompilationFailed += (s, e) => otherQueue.Add(e);

                client.SetResultListener(resultListener);

                attachAction(client);

                TimeSpan timeout = TimeSpan.FromMinutes(1);

                try
                {
                    while (true)
                    {
                        object next;
                        var index = BlockingCollection<object>.TryTakeFromAny(new[] { resultQueue, otherQueue }, out next, timeout);
                        if (index == 0)
                        {
                            yield return (CycleCompletedArgs)next;
                        }
                        else
                        {
                            var detailMessage = string.Format("for {0} after {1}\n state {2} is completed {3}", client.GetViewDefinition().Name, timeout, client.GetState(), client.IsCompleted);
                            switch (index)
                            {
                                case 0:
                                    throw new ArgumentException("index");
                                case 1:
                                    throw new Exception(string.Format("Error occured whilst getting results {0}\n{1}", next, detailMessage));
                                default:

                                    throw new TimeoutException("No results received " + detailMessage);
                            }
                        }
                    }
                }
                finally
                {
                    client.RemoveResultListener();
                }
            }
        }
        public static void WithViewCycle(Action<ViewDefinitionCompiledArgs, IViewCycle, RemoteViewClient> action, string viewName = "Demo Equity Option Test View")
        {
            using (var executedMre = new ManualResetEventSlim(false))
            using (var remoteViewClient = Context.ViewProcessor.CreateClient())
            {
                ViewDefinitionCompiledArgs compiled = null;
                CycleCompletedArgs cycle = null;
                JavaException error = null;
                var listener = new EventViewResultListener();
                listener.ProcessCompleted += delegate { executedMre.Set(); };
                listener.CycleCompleted += delegate(object sender, CycleCompletedArgs e)
                                               {
                                                   cycle = e;
                                                   executedMre.Set();
                                               };
                listener.ViewDefinitionCompiled += delegate(object sender, ViewDefinitionCompiledArgs e) { compiled = e; };
                listener.ViewDefinitionCompilationFailed += delegate(object sender, ViewDefinitionCompilationFailedArgs e)
                                                                {
                                                                    error = e.Exception;
                                                                    executedMre.Set();
                                                                };

                remoteViewClient.SetResultListener(listener);
                remoteViewClient.SetViewCycleAccessSupported(true);
                remoteViewClient.AttachToViewProcess(viewName, ExecutionOptions.SingleCycle);
                Assert.Null(remoteViewClient.CreateLatestCycleReference());

                executedMre.Wait(TimeSpan.FromMinutes(1));
                Assert.Null(error);
                Assert.NotNull(compiled);
                Assert.NotNull(cycle);

                using (var engineResourceReference = remoteViewClient.CreateLatestCycleReference())
                {
                    action(compiled, engineResourceReference.Value, remoteViewClient);
                }
            }
        }
        public static Tuple<IEnumerable<ViewDefinitionCompiledArgs>, IEnumerable<CycleCompletedArgs>> RunToCompletion(IViewExecutionOptions options)
        {
            using (var remoteViewClient = Context.ViewProcessor.CreateClient())
            {
                var cycles = new ConcurrentQueue<CycleCompletedArgs>();
                var compiles = new ConcurrentQueue<ViewDefinitionCompiledArgs>();

                using (var manualResetEvent = new ManualResetEvent(false))
                {
                    var listener = new EventViewResultListener();
                    listener.ViewDefinitionCompiled += (sender, e) => compiles.Enqueue(e);
                    listener.CycleCompleted += (sender, e) => cycles.Enqueue(e);
                    listener.ProcessCompleted += (sender, e) => manualResetEvent.Set();
                    remoteViewClient.SetResultListener(listener);
                    remoteViewClient.AttachToViewProcess(ViewName, options);
                    manualResetEvent.WaitOne();
                }

                Assert.InRange(compiles.Count, cycles.Any() ? 1 : 0, cycles.Count + 1);
                Assert.True(remoteViewClient.IsCompleted);
                return new Tuple<IEnumerable<ViewDefinitionCompiledArgs>, IEnumerable<CycleCompletedArgs>>(compiles, cycles);
            }
        }
 private static WeakReference[] UseClient(string vdName)
 {
     var firstClient = Context.ViewProcessor.CreateClient(); //NOTE: no using
     {
         var finished = new ManualResetEventSlim(false);
         var eventViewResultListener = new EventViewResultListener();
         eventViewResultListener.ProcessCompleted += delegate { finished.Set(); };
         firstClient.SetResultListener(eventViewResultListener); //Make sure the active connection is made
         firstClient.AttachToViewProcess(vdName, ExecutionOptions.SingleCycle);
         finished.Wait(TimeSpan.FromMinutes(1));
         return new[] { new WeakReference(firstClient) };
     }
 }
        public void ViewResultsMatchDefinition(ViewDefinition viewDefinition)
        {
            using (var remoteViewClient = Context.ViewProcessor.CreateClient())
            {
                ICompiledViewDefinition compiledViewDefinition = null;
                IViewComputationResultModel viewComputationResultModel = null;
                var resultsReady = new ManualResetEvent(false);

                var listener = new EventViewResultListener();
                listener.ViewDefinitionCompiled += delegate(object sender, ViewDefinitionCompiledArgs e)
                                                       {
                                                           compiledViewDefinition = e.CompiledViewDefinition;
                                                       };
                listener.CycleCompleted += delegate(object sender, CycleCompletedArgs e)
                                               {
                                                   viewComputationResultModel = e.FullResult;
                                                   resultsReady.Set();
                                               };
                remoteViewClient.SetResultListener(listener);

                remoteViewClient.AttachToViewProcess(viewDefinition.UniqueID, ExecutionOptions.RealTime);

                if (!resultsReady.WaitOne(TimeSpan.FromMinutes(2)))
                    throw new TimeoutException("Failed to get results for " + viewDefinition.Name + " client " + remoteViewClient.GetUniqueId());

                Assert.NotNull(compiledViewDefinition);

                foreach (var viewResultEntry in viewComputationResultModel.AllResults)
                {
                    Assert.NotNull(viewResultEntry.ComputedValue.Value);
                    AssertDefinitionContains(viewDefinition, viewResultEntry);
                }

                var countActualValues = viewComputationResultModel.AllResults.Count();

                var countMaxExpectedValues = CountMaxExpectedValues(compiledViewDefinition);

                Console.Out.WriteLine("{0} {1} {2}", viewDefinition.Name, countActualValues, countMaxExpectedValues);
                Assert.InRange(countActualValues, 1, countMaxExpectedValues);
            }
        }
 public void HeartbeatWorks()
 {
     const string vdName = "Equity Option Test View 1";
     using (var firstClient = Context.ViewProcessor.CreateClient())
     {
         var finished = new ManualResetEventSlim(false);
         var eventViewResultListener = new EventViewResultListener();
         eventViewResultListener.ProcessCompleted += delegate { finished.Set(); };
         firstClient.SetResultListener(eventViewResultListener); //Make sure the active connection is made
         firstClient.AttachToViewProcess(vdName, ExecutionOptions.SingleCycle);
         finished.Wait(TimeSpan.FromMinutes(1));
         Thread.Sleep(TimeSpan.FromMinutes(1));
         Assert.NotNull(firstClient.GetUniqueId());
     }
 }
        public void CanReAttach()
        {
            var timeout = TimeSpan.FromMinutes(0.5);

            using (var remoteViewClient = Context.ViewProcessor.CreateClient())
            {
                var compilationResult = new BlockingCollection<ICompiledViewDefinition>();
                var compilationError = new BlockingCollection<JavaException>();

                var eventViewResultListener = new EventViewResultListener();
                eventViewResultListener.ViewDefinitionCompiled +=
                    (sender, e) => compilationResult.Add(e.CompiledViewDefinition);
                eventViewResultListener.ViewDefinitionCompilationFailed +=
                    (sender, e) => compilationError.Add(e.Exception);

                remoteViewClient.SetResultListener(eventViewResultListener);
                remoteViewClient.AttachToViewProcess("Equity Option Test View 1", ExecutionOptions.RealTime);

                ICompiledViewDefinition result;

                Assert.True(compilationResult.TryTake(out result, timeout));
                Assert.Equal("Equity Option Test View 1", result.ViewDefinition.Name);
                remoteViewClient.DetachFromViewProcess();
                remoteViewClient.AttachToViewProcess("Equity Option Test View 2", ExecutionOptions.RealTime);
                Assert.True(compilationResult.TryTake(out result, timeout));
                Assert.Equal("Equity Option Test View 2", result.ViewDefinition.Name);
            }
        }
        public void CanGetCompilationResults(ViewDefinition definition)
        {
            using (var remoteViewClient = Context.ViewProcessor.CreateClient())
            {
                var compilationResult = new BlockingCollection<object>();

                var eventViewResultListener = new EventViewResultListener();
                eventViewResultListener.ViewDefinitionCompiled +=
                    (sender, e) => compilationResult.Add(e.CompiledViewDefinition);
                eventViewResultListener.ViewDefinitionCompilationFailed +=
                    (sender, e) => compilationResult.Add(e.Exception);

                remoteViewClient.SetResultListener(eventViewResultListener);
                remoteViewClient.AttachToViewProcess(definition.UniqueID, ExecutionOptions.GetCompileOnly());

                var result = compilationResult.Take();
                Assert.IsNotType(typeof(Exception), result);
                Debug.WriteLine(definition.UniqueID);
                Assert.IsAssignableFrom(typeof(ICompiledViewDefinition), result);

                var viewDefin = (ICompiledViewDefinition)result;
                ValueAssertions.AssertSensibleValue(viewDefin);
            }
        }
        public void NumberOfResultsIsConsistent(ViewDefinition defn)
        {
            const int cyclesCount = 5;

            using (var remoteViewClient = Context.ViewProcessor.CreateClient())
            using (var mre = new ManualResetEvent(false))
            {
                var cycles = new BlockingCollection<IEngineResourceReference<IViewCycle>>();

                var listener = new EventViewResultListener();
                listener.ProcessCompleted += delegate { mre.Set(); };
                listener.ViewDefinitionCompilationFailed += delegate { mre.Set(); };
                listener.CycleExecutionFailed += delegate { mre.Set(); };

                listener.CycleCompleted += (sender, e) =>
                                               {
                                                   cycles.Add(remoteViewClient.CreateCycleReference(e.FullResult.ViewCycleId));
                                                   remoteViewClient.TriggerCycle();
                                               };

                remoteViewClient.SetResultListener(listener);
                remoteViewClient.SetViewCycleAccessSupported(true);

                var sequence = ArbitraryViewCycleExecutionSequence.Create(Enumerable.Range(0, cyclesCount).Select(i => DateTimeOffset.Now + TimeSpan.FromHours(i)));
                var options = new ExecutionOptions(sequence, ViewExecutionFlags.TriggersEnabled | ViewExecutionFlags.AwaitMarketData, null, new ViewCycleExecutionOptions(default(DateTimeOffset), ExecutionOptions.GetDefaultMarketDataSpec()));

                remoteViewClient.AttachToViewProcess(defn.UniqueID, options);

                TimeSpan timeout = TimeSpan.FromMinutes(5);
                if (! mre.WaitOne(timeout))
                {
                    throw new TimeoutException(string.Format("Failed to get result in {0}", timeout));
                }
                Assert.Equal(cyclesCount, cycles.Count);

                var specs = cycles.Select(GetAllSpecs).ToList();

                var inconsistent = specs.Zip(specs.Skip(1), Tuple.Create).SelectMany(
                    t =>
                        {
                            var diff = new HashSet<Tuple<string, ValueSpecification>>(t.Item1);
                            diff.SymmetricExceptWith(t.Item2);
                            return diff;
                        }).Distinct();
                if (inconsistent.Any())
                {
                    var counts = string.Join(",", specs.Select(c => c.Count.ToString()));
                    var inconsistentStrings = specs.Select(s => string.Join(",", s.Where(x => inconsistent.Contains(x)).Select(x => x.ToString())));
                    string inconsistentString = string.Join(Environment.NewLine, inconsistentStrings);
                    throw new Exception(string.Format("Inconsistent number of results for {0} {1}: {2}", defn.Name, counts, inconsistentString));
                }
            }
        }
        public void CanHoldLotsOfCycles()
        {
            using (var remoteViewClient = Context.ViewProcessor.CreateClient())
            {
                const int cyclesCount = 1000;

                var cycles = new BlockingCollection<IEngineResourceReference<IViewCycle>>();

                var listener = new EventViewResultListener();
                listener.ProcessCompleted += delegate { cycles.Add(null); };
                listener.ViewDefinitionCompilationFailed += delegate { cycles.Add(null); };
                listener.CycleExecutionFailed += delegate { cycles.Add(null); };

                listener.CycleCompleted += (sender, e) =>
                                               {
                                                   remoteViewClient.Pause();
                                                   //Use parallel to fill the thread pool
                                                   Parallel.For(0, cyclesCount, _ => cycles.Add(remoteViewClient.CreateCycleReference(e.FullResult.ViewCycleId)));
                                               };

                remoteViewClient.SetResultListener(listener);
                remoteViewClient.SetViewCycleAccessSupported(true);
                var options = ExecutionOptions.RealTime;
                remoteViewClient.AttachToViewProcess("Demo Equity Option Test View", options);

                var cyclesList = new List<IEngineResourceReference<IViewCycle>>();

                TimeSpan timeout = TimeSpan.FromMinutes(2);
                for (int i = 0; i < cyclesCount; i++)
                {
                    IEngineResourceReference<IViewCycle> cycle;
                    if (!cycles.TryTake(out cycle, timeout))
                    {
                        throw new TimeoutException(string.Format("Failed to get result {0} in {1}", i, timeout));
                    }
                    if (cycle == null)
                    {
                        throw new Exception("Some error occured");
                    }
                    cyclesList.Add(cycle);
                }
                remoteViewClient.DetachFromViewProcess(); //Stop gathering more and more referencess

                Thread.Sleep(TimeSpan.FromSeconds(20)); //Make sure we have to heartbeat a few times
                foreach (var engineResourceReference in cyclesList)
                {
                    Assert.NotNull(engineResourceReference.Value.UniqueId);
                }
            }
        }
        private void RefreshMyDataImpl(string viewName, string liveDataSource, CancellationToken cancellationToken)
        {
            Invoke(delegate { resultsTableView.DataContext = null; }, cancellationToken);

            var clientLock = new object();

            lock (clientLock)
            {
                var client = OGContext.ViewProcessor.CreateClient();
                RoutedEventHandler pausedHandler = delegate
                {
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        client.Pause();
                    }
                };
                RoutedEventHandler unpausedHandler = delegate
                {
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        client.Resume();
                    }
                };
                //Must do this before we can throw any exceptions
                ThreadPool.RegisterWaitForSingleObject(cancellationToken.WaitHandle, delegate
                {
                    lock (clientLock)
                    {
                        client.Dispose();
                        pauseToggle.Checked -= pausedHandler;
                        pauseToggle.Unchecked -= unpausedHandler;
                    }
                }, null, int.MaxValue, true);

                //Now we can start hooking things together
                pauseToggle.Checked += pausedHandler;
                pauseToggle.Unchecked += unpausedHandler;

                ComputationResultsTables resultsTable = null;
                int count = 0;

                var eventViewResultListener = new EventViewResultListener();
                eventViewResultListener.ViewDefinitionCompiled +=
                    delegate(object sender, ViewDefinitionCompiledArgs args)
                        {
                            resultsTable = new ComputationResultsTables(_remoteSecuritySource, args.CompiledViewDefinition);
                            Invoke(delegate
                                       {
                                           resultsTableView.DataContext = resultsTable;
                                           SetStatus(string.Format("Waiting for first cycle..."));
                                       }, cancellationToken);
                        };
                eventViewResultListener.CycleCompleted += delegate(object sender, CycleCompletedArgs e)
                                                              {
                                                                  resultsTable.Update(e);
                                                                  SetStatus(GetMessage(e.FullResult ?? (IViewResultModel)e.DeltaResult, ref count));
                                                              };

                eventViewResultListener.ViewDefinitionCompilationFailed +=
                    delegate(object sender, ViewDefinitionCompilationFailedArgs args)
                    {
                        Invoke(delegate
                                   {
                                       SetStatus(string.Format("Failed to compile {0} @ {1}", args.Exception, args.ValuationTime), true);
                                       resultsTableView.DataContext = null;
                                   }, cancellationToken);
                    };
                eventViewResultListener.CycleExecutionFailed +=
                    delegate(object sender, CycleExecutionFailedArgs args)
                    {
                        Invoke(delegate
                        {
                            SetStatus(string.Format("Failed to execute {0} @ {1}", args.Exception, args.ExecutionOptions.ValuationTime), true);
                        }, cancellationToken);
                    };

                eventViewResultListener.ProcessTerminated +=
                    delegate {
                        Invoke(() => SetStatus(string.Format("Process terminated @ {0}", DateTimeOffset.Now), true), cancellationToken);
                    };

                SetStatus(string.Format("Waiting for compilation..."));
                client.SetResultMode(ViewResultMode.FullThenDelta);
                client.SetResultListener(eventViewResultListener);
                client.AttachToViewProcess(viewName, ExecutionOptions.GetRealTime(liveDataSource));
            }
        }
        public void RemoveChangesResults()
        {
            var valueRequirement = new ValueRequirement("Market_Value", new ComputationTargetSpecification(ComputationTargetType.Primitive, BloombergUid));

            var defn = GetViewDefinition();
            using (var remoteClient = Context.ViewProcessor.CreateClient())
            {
                var liveDataOverrideInjector = remoteClient.LiveDataOverrideInjector;
                const double newValue = 1234.5678;

                ManualResetEvent mre = new ManualResetEvent(false);
                IViewComputationResultModel results = null;
                var listener = new EventViewResultListener();
                listener.CycleCompleted += delegate(object sender, CycleCompletedArgs e)
                {
                    results = e.FullResult;
                    mre.Set();
                };
                remoteClient.SetResultListener(listener);
                remoteClient.AttachToViewProcess(defn.UniqueID, ExecutionOptions.RealTime);
                liveDataOverrideInjector.AddValue(valueRequirement, newValue);
                liveDataOverrideInjector.RemoveValue(valueRequirement);

                mre.WaitOne();
                mre.Reset();

                var result = results.AllResults.Where(r => valueRequirement.IsSatisfiedBy(r.ComputedValue.Specification)).First();
                Assert.NotEqual(newValue, (double)result.ComputedValue.Value);
            }
        }