Example #1
0
		public async Task OpenAsync()
		{
			tcs = new System.Threading.CancellationTokenSource();
			m_stream = await OpenStreamAsync();
			StartParser();
			MultiPartMessageCache.Clear();
		}
        public async Task CleanupTestLoadBalancers()
        {
            ILoadBalancerService provider = CreateProvider();
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(60)));
            string queueName = CreateRandomLoadBalancerName();

            LoadBalancer[] allLoadBalancers = ListAllLoadBalancers(provider, null, cancellationTokenSource.Token).Where(loadBalancer => loadBalancer.Name.StartsWith(TestLoadBalancerPrefix, StringComparison.OrdinalIgnoreCase)).ToArray();
            int blockSize = 10;
            for (int i = 0; i < allLoadBalancers.Length; i += blockSize)
            {
                await provider.RemoveLoadBalancerRangeAsync(
                    allLoadBalancers
                        .Skip(i)
                        .Take(blockSize)
                        .Select(loadBalancer =>
                            {
                                Console.WriteLine("Deleting load balancer: {0}", loadBalancer.Name);
                                return loadBalancer.Id;
                            })
                        .ToArray(), // included to ensure the Console.WriteLine is only executed once per load balancer
                    AsyncCompletionOption.RequestCompleted,
                    cancellationTokenSource.Token,
                    null);
            }
        }
Example #3
0
		private void EhLoaded(object sender, RoutedEventArgs e)
		{
			this._printerStatusCancellationTokenSource = new System.Threading.CancellationTokenSource();
			this._printerStatusCancellationToken = _printerStatusCancellationTokenSource.Token;

			System.Threading.Tasks.Task.Factory.StartNew(UpdatePrinterStatusGuiElements, _printerStatusCancellationToken);
		}
Example #4
0
        public Renard(string portName)
        {
            this.serialPort = new SerialPort(portName, 57600);
            this.renardData = new byte[24];

            this.cancelSource = new System.Threading.CancellationTokenSource();
            this.firstChange = new System.Diagnostics.Stopwatch();

            this.senderTask = new Task(x =>
                {
                    while (!this.cancelSource.IsCancellationRequested)
                    {
                        bool sentChanges = false;

                        lock (lockObject)
                        {
                            if (this.dataChanges > 0)
                            {
                                this.firstChange.Stop();
                                //log.Info("Sending {0} changes to Renard. Oldest {1:N2}ms",
                                //    this.dataChanges, this.firstChange.Elapsed.TotalMilliseconds);
                                this.dataChanges = 0;
                                sentChanges = true;

                                SendSerialData(this.renardData);
                            }
                        }

                        if(!sentChanges)
                            System.Threading.Thread.Sleep(10);
                    }
                }, this.cancelSource.Token, TaskCreationOptions.LongRunning);

            Executor.Current.Register(this);
        }
Example #5
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // Check the task cost
            var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;
            if (cost == BackgroundWorkCostValue.High)
            {
                return;
            }

            // Get the cancel token
            var cancel = new System.Threading.CancellationTokenSource();
            taskInstance.Canceled += (s, e) =>
            {
                cancel.Cancel();
                cancel.Dispose();
            };

            // Get deferral
            var deferral = taskInstance.GetDeferral();
            try
            {
                // Update Tile with the new xml
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(TileUpdaterTask.w10TileXml);

                TileNotification tileNotification = new TileNotification(xmldoc);
                TileUpdater tileUpdator = TileUpdateManager.CreateTileUpdaterForApplication();
                tileUpdator.Update(tileNotification);
            }
            finally
            {
                deferral.Complete();
            }
        }
        public async void Execute(string token, string content)
        {
            Uri uri = new Uri(API_ADDRESS + path);

            var rootFilter = new HttpBaseProtocolFilter();

            rootFilter.CacheControl.ReadBehavior = Windows.Web.Http.Filters.HttpCacheReadBehavior.MostRecent;
            rootFilter.CacheControl.WriteBehavior = Windows.Web.Http.Filters.HttpCacheWriteBehavior.NoCache;

            HttpClient client = new HttpClient(rootFilter);
            //client.DefaultRequestHeaders.Add("timestamp", DateTime.Now.ToString());
            if(token != null)
                client.DefaultRequestHeaders.Add("x-access-token", token);

            System.Threading.CancellationTokenSource source = new System.Threading.CancellationTokenSource(2000);

            HttpResponseMessage response = null;
            if (requestType == GET)
            {
                try
                {
                    response = await client.GetAsync(uri).AsTask(source.Token);
                }
                catch (TaskCanceledException)
                {
                    response = null;
                }

            }else if (requestType == POST)
            {
                HttpRequestMessage msg = new HttpRequestMessage(new HttpMethod("POST"), uri);
                if (content != null)
                {
                    msg.Content = new HttpStringContent(content);
                    msg.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json");
                }

                try
                {
                    response = await client.SendRequestAsync(msg).AsTask(source.Token);
                }
                catch (TaskCanceledException)
                {
                    response = null;
                }
            }

            if (response == null)
            {
                if (listener != null)
                    listener.onTaskCompleted(null, requestCode);
            }
            else
            {
                string answer = await response.Content.ReadAsStringAsync();

                if(listener != null)
                    listener.onTaskCompleted(answer, requestCode);
            }
        }
 public async Task TestGetHome()
 {
     IQueueingService provider = CreateProvider();
     CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(10)));
     HomeDocument document = await provider.GetHomeAsync(cancellationTokenSource.Token);
     Assert.IsNotNull(document);
     Console.WriteLine(JsonConvert.SerializeObject(document, Formatting.Indented));
 }
 public static void ClassCleanup()
 {
     IDatabaseService provider = UserDatabaseTests.CreateProvider();
     using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
     {
         provider.RemoveDatabaseInstanceAsync(_instance.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null).Wait();
     }
 }
 internal CancellationCallbackInfo(Action<object> callback, object stateForCallback, SynchronizationContext targetSyncContext, ExecutionContext targetExecutionContext, System.Threading.CancellationTokenSource cancellationTokenSource)
 {
     this.Callback = callback;
     this.StateForCallback = stateForCallback;
     this.TargetSyncContext = targetSyncContext;
     this.TargetExecutionContext = targetExecutionContext;
     this.CancellationTokenSource = cancellationTokenSource;
 }
Example #10
0
        void Launcher_BuildUpdated(Client.Launcher.BuildUpdatedEventArgs e)
        {
            HashSet<Settings.IDatFile> dats = new HashSet<Settings.IDatFile>();
            List<Settings.IAccount> accounts = new List<Settings.IAccount>();

            //only accounts with a unique dat file need to be updated

            foreach (ushort uid in Settings.Accounts.GetKeys())
            {
                var a = Settings.Accounts[uid];
                if (a.HasValue && (a.Value.DatFile == null || dats.Add(a.Value.DatFile)))
                {
                    accounts.Add(a.Value);
                }
            }

            if (accounts.Count > 0)
            {
                System.Threading.CancellationTokenSource cancel = new System.Threading.CancellationTokenSource(3000);

                try
                {
                    this.BeginInvoke(new MethodInvoker(
                        delegate
                        {
                            BeforeShowDialog();
                            try
                            {
                                activeWindows++;
                                using (formUpdating f = new formUpdating(accounts, true, true))
                                {
                                    f.Shown += delegate
                                    {
                                        cancel.Cancel();
                                    };
                                    f.ShowDialog(this);
                                }
                            }
                            finally
                            {
                                activeWindows--;
                            }
                        }));
                }
                catch { }

                try
                {
                    cancel.Token.WaitHandle.WaitOne();
                }
                catch (Exception ex)
                {
                    ex = ex;
                }

                e.Update(accounts);
            }
        }
 public bool SymbolProcessorTaskWorkDiconnectCancellationTest()
 {
     if (null == _cts)
         _cts = new System.Threading.CancellationTokenSource();
     Disconnect();
     base.OnNewDataReceived = SymbolProcessorTaskWorkCancellationTest;
     dataQueue.CompleteAdding();
     SymbolProcessorTaskWork();
     return _dataReceived;
 }
	    protected ZXingSurfaceView(IntPtr javaReference, JniHandleOwnership transfer) 
            : base(javaReference, transfer) 
        {
            lastPreviewAnalysis = DateTime.Now.AddMilliseconds(options.InitialDelayBeforeAnalyzingFrames);

            this.surface_holder = Holder;
            this.surface_holder.AddCallback(this);
            this.surface_holder.SetType(SurfaceType.PushBuffers);

            this.tokenSource = new System.Threading.CancellationTokenSource();
	    }
Example #13
0
        public async Task shouldCancelDuringBuild()
        {
            var cancellationTestee = new PipelineManager();
            var canceller = new System.Threading.CancellationTokenSource();
            var progressReporter = new PipelineProgress(canceller.Cancel, 50, false);

            var result = await cancellationTestee.BuildPipelineAsync(this.scheme, canceller.Token, progressReporter, new TestSubject());

            var refinedResults = result.Select(r => r.Outcome).ToArray();

            Assert.IsTrue(refinedResults.Contains(StepBuildResults.Cancelled) && refinedResults.Contains(StepBuildResults.Completed), "Token may have expirec before the build took place.\nResults are:\n" + refinedResults.PrintContentsToString(Environment.NewLine));
        }
Example #14
0
		/// <summary>
		/// Opens the device connection.
		/// </summary>
		/// <returns></returns>
		public async Task OpenAsync()
		{
			lock (m_lockObject)
			{
				if (IsOpen) return;
				IsOpen = true;
			}
			m_cts = new System.Threading.CancellationTokenSource();
			m_stream = await OpenStreamAsync();
			StartParser();
			MultiPartMessageCache.Clear();
		}
Example #15
0
 public static HubblUser LoadUser()
 {
     var source = new System.Threading.CancellationTokenSource (10000);
     var token = source.Token;
     try {
         var file = FileSystem.Current.LocalStorage.GetFileAsync ("user", token).Result;
         var sUser = file.ReadAllTextAsync ().Result;
         var user = JsonConvert.DeserializeObject<HubblUser>(sUser);
         user.IsHub = false;
         return user;
     } catch (Exception) {
         return null;
     }
 }
Example #16
0
		public async Task CloseAsync()
		{
			if (tcs != null)
			{
				closeTask = new TaskCompletionSource<bool>();
				if (tcs != null)
					tcs.Cancel();
				tcs = null;
			}
			await closeTask.Task;
			await CloseStreamAsync(m_stream);
			MultiPartMessageCache.Clear();
			m_stream = null;
		}
		public ZXingSurfaceView (Activity activity, MobileBarcodeScanningOptions options, Action<ZXing.Result> callback)
			: base (activity)
		{
			this.activity = activity;
			this.callback = callback;
			this.options = options;

			lastPreviewAnalysis = DateTime.Now.AddMilliseconds(options.InitialDelayBeforeAnalyzingFrames);

			this.surface_holder = Holder;
			this.surface_holder.AddCallback (this);
			this.surface_holder.SetType (SurfaceType.PushBuffers);
			
			this.tokenSource = new System.Threading.CancellationTokenSource();
		}
        public void CleanupTestQueues()
        {
            IQueueingService provider = CreateProvider();
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(60)));
            QueueName queueName = CreateRandomQueueName();

            IEnumerable<Task<IEnumerable<CloudQueue>>> allQueueTasks = ListAllQueuesAsync(provider, null, false, cancellationTokenSource.Token);
            CloudQueue[] allQueues = allQueueTasks.SelectMany(task => task.Result).ToArray();
            Task[] deleteTasks = Array.ConvertAll(allQueues, queue =>
            {
                Console.WriteLine("Deleting queue: {0}", queue.Name);
                return provider.DeleteQueueAsync(queue.Name, cancellationTokenSource.Token);
            });
            Task.WaitAll(deleteTasks);
        }
Example #19
0
		public async Task StartAsync()
		{
			if (tcs != null)
				return;
			if (m_device == null)
				throw new ObjectDisposedException("Device");
			tcs = new System.Threading.CancellationTokenSource();
			socket = new StreamSocket();
			await socket.ConnectAsync(
#if NETFX_CORE
								m_device.ConnectionHostName,
								m_device.ConnectionServiceName);
#else
								m_device.HostName, "1");
			//socket = await Windows.Networking.Proximity.PeerFinder.ConnectAsync(m_device.HostName;
#endif
			if (tcs.IsCancellationRequested) //Stop was called while opening device
			{
				socket.Dispose();
				socket = null;
				throw new TaskCanceledException();
			} var token = tcs.Token;
			var _ = Task.Run(async () =>
			{
				var stream = socket.InputStream.AsStreamForRead();
				byte[] buffer = new byte[1024];
				while (!token.IsCancellationRequested)
				{
					int readCount = 0;
					try
					{
						readCount = await stream.ReadAsync(buffer, 0, 1024, token).ConfigureAwait(false);
					}
					catch { }
					if (token.IsCancellationRequested)
						break;
					if (readCount > 0)
					{
						OnData(buffer.Take(readCount).ToArray());
					}
					await Task.Delay(10, token);
				}
				if(socket != null)
					socket.Dispose();
				if (closeTask != null)
					closeTask.SetResult(true);
			});
		}
        public async Task TestCreateUser()
        {
            IDatabaseService provider = UserDatabaseTests.CreateProvider();
            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                string host = null;
                UserName userName = UserDatabaseTests.CreateRandomUserName(host);
                string password = UserDatabaseTests.CreateRandomPassword();
                UserConfiguration userConfiguration = new UserConfiguration(userName, password);
                await provider.CreateUserAsync(_instance.Id, userConfiguration, cancellationTokenSource.Token);

                await provider.ListDatabaseUsersAsync(_instance.Id, null, null, cancellationTokenSource.Token);

                await provider.RemoveUserAsync(_instance.Id, userName, cancellationTokenSource.Token);
            }
        }
        public override int Run()
        {
            Dictionary<string, object> argumentsToPass = new Dictionary<string, object>();
            Console.WriteLine("{0} with arguments:", this.plan.Name);
            foreach (ActionPropertyInfo s in this.expectedArguments)
            {
                Console.WriteLine("{0}: {1}", s.Name, this.GetArgument(s.Name));
                argumentsToPass.Add(s.Name, this.GetArgument(s.Name));
            }

            foreach (ActionPropertyInfo s in this.optionalArguments)
            {
                var argValue = this.GetArgument(s.Name);
                Console.WriteLine("{0}(optional): {1} - default value '{2}'", s.Name, argValue, s.Value);

                if (argValue != null)
                {
                    argumentsToPass.Add(s.Name, this.GetArgument(s.Name));
                }
            }

            try
            {
                this.actionExecutionEngine.OnProgressChanged += this.ActionExecutionEngine_OnProgressChanged;

                var cts = new System.Threading.CancellationTokenSource();

                Task t = actionExecutionEngine.ExecuteAsync(this.plan, argumentsToPass, cts.Token);
                t.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Exception inner = ex.InnerException;
                while (inner != null)
                {
                    Console.WriteLine(inner.Message);
                    inner = inner.InnerException;
                }

                this.retVal = -1;
            }

            return this.retVal;
        }
        public void MusicPlayer_PlaysMusic()
        {
            System.Diagnostics.Trace.WriteLine("Started");
            MusicPlayer p = new MusicPlayer();
            var ts = new System.Threading.CancellationTokenSource();
            p.MuteUnMute();
            Task.Factory.StartNew(p.PlayMusic, null, ts.Token);

            System.Threading.Thread.Sleep(200);
            string track1 = p.CurrentlyPlaying;
            System.Diagnostics.Trace.WriteLine("Playing 1:" + track1.ToString());
            p.PlayNext();

            Assert.IsFalse(string.IsNullOrWhiteSpace(track1));

            ts.Cancel(true);
            System.Diagnostics.Trace.WriteLine("Stopped");
        }
Example #23
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.update_ui("Start Clicked");

            cTokenSource = new System.Threading.CancellationTokenSource();
            var cToken = cTokenSource.Token;

            task = System.Threading.Tasks.Task<int>.Factory
                .StartNew( () => this.GenerateNumbers(cToken), cToken)
                .ContinueWith( t =>
                                   {
                                       this.ts_updateui("TASK: ContinueWith: DONE");
                                       return 1;
                                   })
                ;

            cToken.Register(FormTask.cancelNotification);
        }
        public MainWindow()
        {
            Trace.Listeners.Add(new TextWriterTraceListener("LanguageBooster.log", "myListener"));
            Trace.Listeners.Add(new MyListener());

            Trace.AutoFlush = true;
            System.Diagnostics.Trace.WriteLine("Started");

            Instance = this;
            InitializeComponent();
            enterNameForm.Name = "EnterNameFormInstance";

            this.source = new System.Threading.CancellationTokenSource();
            Task.Factory.StartNew(player.PlayMusic, this.Dispatcher, source.Token);

            this.Cursor = Cursors.None;

            System.Diagnostics.Trace.WriteLine("Loading data.");
            DataManager.LoadData();
            System.Diagnostics.Trace.WriteLine("Loading data. DONE");
        }
        public static void ClassInitialize(TestContext testContext)
        {
            IDatabaseService provider = UserDatabaseTests.CreateProvider();
            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                ReadOnlyCollection<DatabaseFlavor> flavors = provider.ListFlavorsAsync(cancellationTokenSource.Token).Result;
                if (flavors.Count == 0)
                    Assert.Inconclusive("The service did not report any flavors.");

                DatabaseFlavor smallestFlavor = flavors.Where(i => i.Memory.HasValue).OrderBy(i => i.Memory).First();
                string instanceName = UserDatabaseTests.CreateRandomDatabaseInstanceName();
                DatabaseInstanceConfiguration configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName);
                _instance = provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null).Result;

                _databaseName = UserDatabaseTests.CreateRandomDatabaseName();
                DatabaseConfiguration databaseConfiguration = new DatabaseConfiguration(_databaseName, null, null);
                provider.CreateDatabaseAsync(_instance.Id, databaseConfiguration, cancellationTokenSource.Token).Wait();

                _databaseName2 = UserDatabaseTests.CreateRandomDatabaseName();
                DatabaseConfiguration databaseConfiguration2 = new DatabaseConfiguration(_databaseName2, null, null);
                provider.CreateDatabaseAsync(_instance.Id, databaseConfiguration2, cancellationTokenSource.Token).Wait();
            }
        }
Example #26
0
        public Task Start()
        {
            this.cancelSource = new System.Threading.CancellationTokenSource();

            this.task = new Task(a =>
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();
                for (int currentPos = 0; currentPos < this.timeline.Count; currentPos++)
                {
                    double elapsed = this.timeline.Keys[currentPos];

                    while (watch.Elapsed.TotalSeconds < elapsed)
                    {
                        System.Threading.Thread.Sleep(1);
                        if (this.cancelSource.Token.IsCancellationRequested)
                            return;
                    }

                    var codes = this.timeline.Values[currentPos];

                    // Invoke
                    var handler = TimelineTrigger;
                    foreach (var code in codes)
                    {
                        Console.WriteLine(string.Format("Invoking codes {1} at {0:N2} s   (pos {2})", elapsed, code, currentPos + 1));
                        if (handler != null)
                            handler(this, new TimelineEventArgs(elapsed, code, currentPos + 1));
                    }

                }
            }, this.cancelSource.Token, TaskCreationOptions.LongRunning);

            task.Start();

            return task;
        }
Example #27
0
        public async Task TestQueueMessages()
        {
            int clientCount = 3;
            int serverCount = 2;

            Assert.IsTrue(clientCount > 0);
            Assert.IsTrue(serverCount > 0);

            QueueName requestQueueName = CreateRandomQueueName();

            QueueName[] responseQueueNames = Enumerable.Range(0, clientCount).Select(i => CreateRandomQueueName()).ToArray();

            IQueueingService        provider = CreateProvider();
            CancellationTokenSource testCancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(300)));

            Stopwatch initializationTimer = Stopwatch.StartNew();

            Console.WriteLine("Creating request queue...");
            await provider.CreateQueueAsync(requestQueueName, testCancellationTokenSource.Token);

            Console.WriteLine("Creating {0} response queues...", responseQueueNames.Length);
            await Task.Factory.ContinueWhenAll(responseQueueNames.Select(queueName => (Task)provider.CreateQueueAsync(queueName, testCancellationTokenSource.Token)).ToArray(), TaskExtrasExtensions.PropagateExceptions);

            TimeSpan initializationTime = initializationTimer.Elapsed;

            Console.WriteLine("Initialization time: {0} sec", initializationTime.TotalSeconds);

            TimeSpan testDuration = TimeSpan.FromSeconds(10);
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(testDuration);

            Stopwatch processingTimer = Stopwatch.StartNew();

            List <Task <int> > clientTasks = new List <Task <int> >();
            List <Task <int> > serverTasks = new List <Task <int> >();

            for (int i = 0; i < clientCount; i++)
            {
                clientTasks.Add(PublishMessages(requestQueueName, responseQueueNames[i], cancellationTokenSource.Token));
            }
            for (int i = 0; i < serverCount; i++)
            {
                serverTasks.Add(SubscribeMessages(requestQueueName, cancellationTokenSource.Token));
            }

            // wait for all client and server tasks to finish processing
            await Task.Factory.ContinueWhenAll(clientTasks.Concat(serverTasks).Cast <Task>().ToArray(), TaskExtrasExtensions.PropagateExceptions);

            int clientTotal = 0;
            int serverTotal = 0;

            for (int i = 0; i < clientTasks.Count; i++)
            {
                Console.WriteLine("Client {0}: {1} messages", i, clientTasks[i].Result);
                clientTotal += clientTasks[i].Result;
            }
            for (int i = 0; i < serverTasks.Count; i++)
            {
                Console.WriteLine("Server {0}: {1} messages", i, serverTasks[i].Result);
                serverTotal += serverTasks[i].Result;
            }

            double clientRate = clientTotal / testDuration.TotalSeconds;
            double serverRate = serverTotal / testDuration.TotalSeconds;

            Console.WriteLine("Total client messages: {0} ({1} messages/sec, {2} messages/sec/client)", clientTotal, clientRate, clientRate / clientCount);
            Console.WriteLine("Total server messages: {0} ({1} messages/sec, {2} messages/sec/server)", serverTotal, serverRate, serverRate / serverCount);

            Console.WriteLine("Deleting request queue...");
            await provider.DeleteQueueAsync(requestQueueName, testCancellationTokenSource.Token);

            Console.WriteLine("Deleting {0} response queues...", responseQueueNames.Length);
            await Task.Factory.ContinueWhenAll(responseQueueNames.Select(queueName => provider.DeleteQueueAsync(queueName, testCancellationTokenSource.Token)).ToArray(), TaskExtrasExtensions.PropagateExceptions);

            if (clientTotal == 0)
            {
                Assert.Inconclusive("No messages were fully processed by the test.");
            }
        }
Example #28
0
        public async Task ExtendedUnloadTest()
        {
            var store = await StoreBuilder.New().EnableScoping().CreateAsync();

            var initialSchema = await store.Schemas.New <InitialDomainDefinition>().CreateAsync();

            var initial = await store.DomainModels.New().CreateAsync("D1");

            // Create a category in the initial domain
            Category a = null;

            try
            {
                using (var tx = store.BeginSession())
                {
                    a       = new Category(initial);
                    a.Name  = "Classe A";
                    a.Value = 1;
                    tx.AcceptChanges();
                }
            }
            catch (SessionException)
            {
                throw new Exception("Inconclusive");
            }

            // Add a constraint
            store.GetSchemaEntity <Category>().AddImplicitConstraint <Category>(
                ca =>
                ca.Value > 0,
                "Value ==0");

            Random rnd = new Random(DateTime.Now.Millisecond);

            System.Threading.CancellationTokenSource cancel = new System.Threading.CancellationTokenSource();

            // Run 2 thread in parallel
            Task.Factory.StartNew(() =>
            {
                while (!cancel.Token.IsCancellationRequested)
                {
                    using (var s = store.BeginSession(new SessionConfiguration {
                        Readonly = true, IsolationLevel = SessionIsolationLevel.Serializable
                    }))
                    {
                        //  s.AcquireLock(LockType.Shared, a.Id.CreateAttributeIdentity("Value"));
                        var x = store.GetElement <Category>(((IModelElement)a).Id);
                        var v = x.Value;
                        Assert.Equal(false, v != 1 && v != 9);
                    }

                    Sleep(11);
                }
            }, cancel.Token);

            Task.Factory.StartNew(() =>
            {
                while (!cancel.Token.IsCancellationRequested)
                {
                    using (var s = store.BeginSession(new SessionConfiguration {
                        Readonly = true, IsolationLevel = SessionIsolationLevel.Serializable
                    }))
                    {
                        // s.AcquireLock(LockType.Shared, a.Id.CreateAttributeIdentity("Value"));
                        var x = store.GetElement <Category>(((IModelElement)a).Id);
                        var v = x.Value;
                        Assert.Equal(false, v != 1 && v != 9);
                    }
                    Sleep(7);
                }
            }, cancel.Token);

            // Load a schema extension
            await initialSchema.LoadSchemaExtension(new ExtensionsDomainDefinition());

            // Iterate to make hot load and unload of the extension
            for (int i = 1; i < 300; i++)
            {
                Sleep(10);

                var xDomain = await initial.CreateScopeAsync("Ex1");

                store.GetSchemaEntity <CategoryEx>().AddImplicitConstraint <CategoryEx>(ca => ca.Value < 10, "Value == 10");
                using (var tx = store.BeginSession())
                {
                    //tx.AcquireLock(LockType.Exclusive, a.Id.CreateAttributeIdentity("Value"));
                    var c = store.GetElement <CategoryEx>(((IModelElement)a).Id);
                    //  c.Text2 = "Classe C";
                    c.XValue = 2;
                    c.Value  = 9;
                    tx.AcceptChanges();
                }

                var xx = store.GetElement <Category>(((IModelElement)a).Id).Value;
                Assert.Equal(9, xx);

                Sleep(12);
                store.DomainModels.Unload(xDomain);
            }

            cancel.Cancel(false);
        }
Example #29
0
 public async Task TestGetNodeHealth()
 {
     IQueueingService        provider = CreateProvider();
     CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(10)));
     await provider.GetNodeHealthAsync(cancellationTokenSource.Token);
 }
Example #30
0
        Run()
        {
            Log.Detail("Running build");

            // TODO: should the rank collections be sorted, so that modules with fewest dependencies are first?

            var graph           = Graph.Instance;
            var metaDataType    = graph.BuildModeMetaData.GetType();
            var useEvaluation   = CheckIfModulesNeedRebuilding(metaDataType);
            var explainRebuild  = CommandLineProcessor.Evaluate(new Options.ExplainBuildReason());
            var immediateOutput = CommandLineProcessor.Evaluate(new Options.ImmediateOutput());

            ExecutePreBuild(metaDataType);

            // necessary if built with debug symbols
            IOWrapper.CreateDirectoryIfNotExists(graph.BuildRoot);

            var threadCount = CommandLineProcessor.Evaluate(new Options.MultiThreaded());

            if (0 == threadCount)
            {
                threadCount = System.Environment.ProcessorCount;
            }

            System.Exception abortException = null;
            if (threadCount > 1)
            {
                using (var cancellationSource = new System.Threading.CancellationTokenSource())
                {
                    var cancellationToken = cancellationSource.Token;

                    // LongRunning is absolutely necessary in order to achieve paralleism
                    var creationOpts     = System.Threading.Tasks.TaskCreationOptions.LongRunning;
                    var continuationOpts = System.Threading.Tasks.TaskContinuationOptions.LongRunning;

                    var scheduler = new LimitedConcurrencyLevelTaskScheduler(threadCount);

                    var factory = new System.Threading.Tasks.TaskFactory(
                        cancellationToken,
                        creationOpts,
                        continuationOpts,
                        scheduler);

                    var tasks = new Array <System.Threading.Tasks.Task>();
                    foreach (var rank in graph.Reverse())
                    {
                        foreach (var module in rank)
                        {
                            var context = new ExecutionContext(useEvaluation, explainRebuild, immediateOutput);
                            var task    = factory.StartNew(() =>
                            {
                                if (cancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }
                                var depTasks = new Array <System.Threading.Tasks.Task>();
                                foreach (var dep in module.Dependents)
                                {
                                    if (null == dep.ExecutionTask)
                                    {
                                        continue;
                                    }
                                    depTasks.Add(dep.ExecutionTask);
                                }
                                foreach (var dep in module.Requirements)
                                {
                                    if (null == dep.ExecutionTask)
                                    {
                                        continue;
                                    }
                                    depTasks.Add(dep.ExecutionTask);
                                }
                                System.Threading.Tasks.Task.WaitAll(depTasks.ToArray());
                                if (cancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                try
                                {
                                    (module as IModuleExecution).Execute(context);
                                }
                                catch (Exception ex)
                                {
                                    abortException = ex;
                                    cancellationSource.Cancel();
                                }
                                finally
                                {
                                    if (context.OutputStringBuilder != null && context.OutputStringBuilder.Length > 0)
                                    {
                                        Log.Info(context.OutputStringBuilder.ToString());
                                    }
                                    if (context.ErrorStringBuilder != null && context.ErrorStringBuilder.Length > 0)
                                    {
                                        Log.Info(context.ErrorStringBuilder.ToString());
                                    }
                                }
                            });
                            tasks.Add(task);
                            module.ExecutionTask = task;
                        }
                    }
                    try
                    {
                        System.Threading.Tasks.Task.WaitAll(tasks.ToArray());
                    }
                    catch (System.AggregateException exception)
                    {
                        if (!(exception.InnerException is System.Threading.Tasks.TaskCanceledException))
                        {
                            throw new Exception(exception, "Error during threaded build");
                        }
                    }
                }
            }
            else
            {
                foreach (var rank in graph.Reverse())
                {
                    if (null != abortException)
                    {
                        break;
                    }
                    foreach (IModuleExecution module in rank)
                    {
                        var context = new ExecutionContext(useEvaluation, explainRebuild, immediateOutput);
                        try
                        {
                            module.Execute(context);
                        }
                        catch (Exception ex)
                        {
                            abortException = ex;
                            break;
                        }
                        finally
                        {
                            if (context.OutputStringBuilder != null && context.OutputStringBuilder.Length > 0)
                            {
                                Log.Info(context.OutputStringBuilder.ToString());
                            }
                            if (context.ErrorStringBuilder != null && context.ErrorStringBuilder.Length > 0)
                            {
                                Log.Info(context.ErrorStringBuilder.ToString());
                            }
                        }
                    }
                }
            }

            if (null != abortException)
            {
                throw new Exception(abortException, "Error during {0}threaded build", (threadCount > 1) ? string.Empty : "non-");
            }

            ExecutePostBuild(metaDataType);
        }
Example #31
0
 public Listener()
 {
     _HttpListener     = new System.Net.HttpListener();
     _CancelGetContext = new System.Threading.CancellationTokenSource();
 }
Example #32
0
        protected internal async void Listen()  // прослушивание входящих подключений
        {
            System.Net.Sockets.TcpListener tcpListener = System.Net.Sockets.TcpListener.Create(Form1.myForm.LocalHost.intPortChat);
            try
            {
                tcpListener.Start();
                while (true)
                {
                    using (var tcpClient = await tcpListener.AcceptTcpClientAsync())
                    {
                        string id = Guid.NewGuid().ToString();   // id connection
                        // Form1.myForm._richTextBoxEchoAdd( DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ": " + "Client has connected " + id);

                        using (var networkStream = tcpClient.GetStream())
                        {
                            var buffer    = new byte[4096];
                            var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);

                            var message = encryptDecrypt.DecryptRijndael(Encoding.UTF8.GetString(buffer, 0, byteCount), salt);
                            try { cts.Cancel(); } catch { }
                            try
                            {
                                if (Form1.myForm.RemoteHost.HostName == null)
                                {
                                    try { Form1.myForm.RemoteHost.HostName = System.Net.Dns.GetHostEntry(message.Split('(')[0].Trim()); } catch { }
                                    try { Form1.myForm.RemoteHost.ipAddress = System.Net.IPAddress.Parse((message.Split('(')[1]).Split(')')[0].Trim()); } catch { }
                                    try { Form1.myForm.RemoteHost.intPortGetFile = Convert.ToInt32(message.Split(':')[2].Trim()); } catch { }
                                    Form1.myForm.RemoteHost.SetInfo();
                                }
                                else if (Form1.myForm.RemoteHost.HostName != null)
                                {
                                    string currentIP = (message.Split('(')[1]).Split(')')[0].Trim();
                                    if (currentIP != Form1.myForm.RemoteHost.ipAddress.ToString())
                                    {
                                        try { Form1.myForm.RemoteHost.HostName = System.Net.Dns.GetHostEntry(message.Split('(')[0].Trim()); } catch { }
                                        try { Form1.myForm.RemoteHost.ipAddress = System.Net.IPAddress.Parse((message.Split('(')[1]).Split(')')[0].Trim()); } catch { }
                                        try { Form1.myForm.RemoteHost.intPortGetFile = Convert.ToInt32(message.Split(':')[2].Trim()); } catch { }
                                        Form1.myForm.RemoteHost.SetInfo();
                                    }
                                }

                                // Form1.myForm._richTextBoxEchoAdd(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ": " + message);

                                buffer = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael(message, salt));

                                //byte[] msg = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael("Hi!", salt));
                                await networkStream.WriteAsync(buffer, 0, buffer.Length);

                                buffer    = new byte[4096];
                                byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);

                                message = encryptDecrypt.DecryptRijndael(Encoding.UTF8.GetString(buffer, 0, byteCount), salt);
                                //Write all info into log
                                //Form1.myForm._richTextBoxEchoAdd(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ": " + Form1.myForm.RemoteHost.UserName + ": " + message);

                                doAction         = new DoAction();
                                doAction.message = Form1.myForm.RemoteHost.UserName + " " + message;
                                doAction.CheckGotMessage();
                                if (doAction.ActionSelected == "NAME")
                                {
                                    buffer = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael(doAction.answer, salt));
                                    await networkStream.WriteAsync(buffer, 0, buffer.Length);
                                }
                                else if (doAction.ActionSelected == "TAKE")
                                {
                                    buffer = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael(doAction.answer, salt));
                                    // await networkStream.WriteAsync(buffer, 0, buffer.Length);
                                }
                                else if (doAction.ActionSelected == "UPDATESERVER")
                                {
                                    Random rnd           = new Random();
                                    string sActionFolder = rnd.Next().ToString();
                                    System.IO.DirectoryInfo UpdateFolderFullPath = new System.IO.DirectoryInfo(System.Windows.Forms.Application.StartupPath + "\\" + sActionFolder);

                                    try { UpdateFolderFullPath.Create(); } catch { }
                                    Form1.myForm.strFolderUpdates = UpdateFolderFullPath.ToString();
                                    try
                                    {
                                        using (Microsoft.Win32.RegistryKey EvUserKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(myRegKey))
                                        {
                                            EvUserKey.SetValue("UPDATESERVERFOLDER", encryptDecrypt.EncryptStringToBase64Text(Form1.myForm.strFolderUpdates, btsMess1, btsMess2), Microsoft.Win32.RegistryValueKind.String);
                                        }
                                    }
                                    catch { }

                                    string sPathCmd = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "myCltSvr.cmd");
                                    doAction.UpdateServerMakecmd(sPathCmd, sActionFolder);

                                    buffer = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael(Form1.myForm.LocalHost.UserName + ": Ready to get a file on the port: " + Form1.myForm.LocalHost.intPortGetFile, salt));
                                    await networkStream.WriteAsync(buffer, 0, buffer.Length);

                                    SendGetFile getFile = new SendGetFile();
                                    getFile.GetFile(System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, sActionFolder), Form1.myForm.LocalHost.intPortGetFile);

                                    doAction.RunProcess(sPathCmd);
                                }
                                else if (doAction.ActionSelected == "GET") //Prepare to get file by Server
                                {
                                    SendGetFile getFile = new SendGetFile();
                                    buffer = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael(Form1.myForm.LocalHost.UserName + ": Ready to get a file on the port: " + Form1.myForm.LocalHost.intPortGetFile, salt));
                                    await networkStream.WriteAsync(buffer, 0, buffer.Length);  //Добавить cancelationtoken

                                    getFile.GetFile(Form1.myForm.strFolderGet, Form1.myForm.LocalHost.intPortGetFile);
                                }
                                else if (doAction.ActionSelected.Length > 0)
                                {
                                    cts = new System.Threading.CancellationTokenSource();
                                    bool messageIsCommand = false;
                                    foreach (string str in actionWord) //
                                    {
                                        if (doAction.ActionSelected == str)
                                        {
                                            messageIsCommand = true;
                                            break;
                                        }
                                    }
                                    if (messageIsCommand != true && Form1.myForm.controlled != "uncontrolcheck")
                                    {
                                        System.Threading.Tasks.Task.Run(() => MakeDelayForHide(10000), cts.Token); //отмена задачи если будет   cts.Cancel
                                    }

                                    buffer = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael(doAction.message, salt));
                                    await networkStream.WriteAsync(buffer, 0, buffer.Length);
                                }
                                else if (doAction.ActionSelected == "")
                                {
                                    buffer = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael(Form1.myForm.RemoteHost.UserName + ": said  -= " + message.ToUpper(), salt));
                                    await networkStream.WriteAsync(buffer, 0, buffer.Length);
                                }
                                else
                                {
                                    buffer = System.Text.Encoding.UTF8.GetBytes(encryptDecrypt.EncryptRijndael(Form1.myForm.RemoteHost.UserName + ": said nothing", salt));
                                    await networkStream.WriteAsync(buffer, 0, buffer.Length);
                                }
                                await networkStream.WriteAsync(buffer, 0, buffer.Length);
                            }
                            catch// (Exception expt)
                            {
                                //   Form1.myForm._AppendTextToFile(Form1.myForm.FileLog, DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ": Listen().tcpClient.GetStream(): " + expt.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception expt)
            { Form1.myForm._AppendTextToFile(Form1.myForm.FileLog, DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ": Listen(): " + expt.Message); }
            finally { tcpListener.Stop(); }
        }
Example #33
0
        private void OnStart(object sender, EventArgs e)
        {
            var locations = this._Index.Where(l => l.Selected == true).ToList();

            if (locations.Count == 0)
            {
                this.LogError("No files selected.");
                return;
            }

            this.progressBar1.Minimum = 0;
            this.progressBar1.Maximum = locations.Count;
            this.progressBar1.Value   = 0;

            if (this.saveFolderBrowserDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var basePath = this.saveFolderBrowserDialog.SelectedPath;

            if (Directory.GetFiles(basePath).Length > 0 ||
                Directory.GetDirectories(basePath).Length > 0)
            {
                if (MessageBox.Show(
                        this,
                        "Folder is not empty, continue anyway?",
                        "Question",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }
            }

            var paths = new Dictionary <KeyValuePair <string, bool>, string>();

            foreach (var kv in locations.Select(l => new KeyValuePair <string, bool>(l.File, l.IsPackage)).Distinct())
            {
                var    inputName = FixFilename(kv.Key, kv.Value);
                string inputPath = null;

                if (this._PackagePath != null)
                {
                    inputPath = Path.Combine(this._PackagePath, inputName);
                    if (File.Exists(inputPath) == false)
                    {
                        inputPath = null;
                    }
                }

                if (inputPath == null)
                {
                    this.openContainerFileDialog.Title  = "Open " + inputName;
                    this.openContainerFileDialog.Filter = inputName + "|" + inputName;

                    if (this.openContainerFileDialog.ShowDialog() != DialogResult.OK)
                    {
                        this.LogError("Could not find \"{0}\"!", inputName);
                    }
                    else
                    {
                        inputPath = this.openContainerFileDialog.FileName;
                    }
                }

                paths.Add(kv, inputPath);
            }

            this.ToggleControls(true);

            var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

            DateTime startTime = DateTime.Now;

            var pcbPath = Path.Combine(Path.GetDirectoryName(_ConverterPath), "packed_codebooks.bin");

            var validating = this.validateCheckBox.Checked == true;
            var converting = this.convertCheckBox.Checked == true;
            var revorbing  = this.revorbCheckBox.Checked == true;

            this._ExtractCancellationToken = new System.Threading.CancellationTokenSource();
            var token = this._ExtractCancellationToken.Token;
            var task  = Task.Factory.StartNew(
                () =>
            {
                int succeeded, failed;

                var streams   = new Dictionary <KeyValuePair <string, bool>, Stream>();
                var dupeNames = new Dictionary <string, int>();
                try
                {
                    int current;
                    succeeded = failed = current = 0;

                    foreach (var location in locations)
                    {
                        if (token.IsCancellationRequested == true)
                        {
                            this.LogWarning("Extraction cancelled.");
                            break;
                        }

                        current++;
                        this.SetProgress(current);

                        var source = new KeyValuePair <string, bool>(location.File, location.IsPackage);
                        if (paths.ContainsKey(source) == false)
                        {
                            failed++;
                            continue;
                        }

                        Stream input;
                        if (streams.ContainsKey(source) == false)
                        {
                            if (source.Value == false)
                            {
                                input           = File.OpenRead(paths[source]);
                                streams[source] = input;
                            }
                            else
                            {
                                input = File.OpenRead(paths[source]);
                                input = ReadPackage(input);
                            }
                        }
                        else
                        {
                            input = streams[source];
                        }

                        if (validating == true)
                        {
                            input.Seek(location.Offset, SeekOrigin.Begin);
                            var bytes = input.ReadBytes(location.Size);
                            var hash  = WwiseIndex.FileHash.Compute(bytes);

                            if (hash != location.Hash)
                            {
                                failed++;

                                this.LogError(
                                    "Hash mismatch for \"{0}.{1}\"! ({2} vs {3})",
                                    location.File,
                                    location.Name,
                                    location.Hash,
                                    hash);

                                continue;
                            }
                        }

                        var name = location.Name;
                        if (name.EndsWith("_wav") == true)
                        {
                            name = name.Substring(0, name.Length - 4);
                        }


                        var outputPath = Path.Combine(basePath, location.File, location.Path + "." + name);

                        int dupeCounter;
                        if (dupeNames.TryGetValue(outputPath, out dupeCounter) == false)
                        {
                            dupeCounter = 1;
                        }

                        if (dupeCounter > 1)
                        {
                            outputPath += string.Format(" [#{0}]", dupeCounter);
                        }

                        dupeCounter++;
                        dupeNames[outputPath] = dupeCounter;

                        var oggPath  = outputPath + ".ogg";
                        var riffPath = outputPath + ".riff";

                        Directory.CreateDirectory(Path.GetDirectoryName(riffPath));

                        using (var output = File.Create(riffPath))
                        {
                            input.Seek(location.Offset, SeekOrigin.Begin);
                            output.WriteFromStream(input, location.Size);
                        }

                        if (converting == true)
                        {
                            var ogger = new System.Diagnostics.Process
                            {
                                StartInfo =
                                {
                                    UseShellExecute        = false,
                                    CreateNoWindow         = true,
                                    RedirectStandardOutput = true,
                                    FileName  = this._ConverterPath,
                                    Arguments = string.Format(
                                        "-o \"{0}\" --pcb \"{2}\" \"{1}\"",
                                        oggPath,
                                        riffPath,
                                        pcbPath)
                                }
                            };

                            ogger.Start();
                            ogger.WaitForExit();

                            if (ogger.ExitCode != 0)
                            {
                                string stdout = ogger.StandardOutput.ReadToEnd();

                                this.LogError("Failed to convert \"{0}.{1}\"!",
                                              location.File,
                                              location.Name);
                                this.LogMessage(stdout);
                                File.Delete(oggPath);
                                failed++;
                                continue;
                            }

                            File.Delete(riffPath);

                            if (revorbing == true)
                            {
                                var revorber = new System.Diagnostics.Process
                                {
                                    StartInfo =
                                    {
                                        UseShellExecute        = false,
                                        CreateNoWindow         = true,
                                        RedirectStandardOutput = true,
                                        FileName  = this._RevorbPath,
                                        Arguments = string.Format(
                                            "\"{0}\"",
                                            oggPath)
                                    }
                                };

                                revorber.Start();
                                revorber.WaitForExit();

                                if (revorber.ExitCode != 0)
                                {
                                    string stdout = revorber.StandardOutput.ReadToEnd();

                                    this.LogError("Failed to revorb \"{0}.{1}\"!",
                                                  location.File,
                                                  location.Name);
                                    this.LogMessage(stdout);
                                }
                            }
                        }

                        succeeded++;
                    }
                }
                finally
                {
                    foreach (var stream in streams.Values)
                    {
                        if (stream != null)
                        {
                            stream.Close();
                        }
                    }
                }

                this.LogSuccess("Done, {0} succeeded, {1} failed, {2} total.", succeeded, failed, succeeded + failed);
            },
                this._ExtractCancellationToken.Token);

            task.ContinueWith(
                t =>
            {
                var elapsed = DateTime.Now.Subtract(startTime);
                this.LogSuccess("Extracted in {0}m {1}s {2}ms",
                                elapsed.Minutes,
                                elapsed.Seconds,
                                elapsed.Milliseconds);
                this.ToggleControls(false);
            },
                System.Threading.CancellationToken.None,
                TaskContinuationOptions.OnlyOnRanToCompletion,
                uiScheduler);

            task.ContinueWith(
                t =>
            {
                this.LogError("Failed to extract!");
                if (t.Exception != null)
                {
                    this.LogError(t.Exception.InnerException != null
                                          ? t.Exception.InnerException.ToString()
                                          : t.Exception.ToString());
                }
                this.ToggleControls(false);
            },
                System.Threading.CancellationToken.None,
                TaskContinuationOptions.OnlyOnFaulted,
                uiScheduler);
        }
Example #34
0
        static void Main(string[] args)
        {
            var log = new ConsoleLogService();

            log.Info("Initialisation");

            var options = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                return;
            }

            if (options.SourceConnectionString == null || options.TargetConnectionString == null || options.RecordBatchSize == 0)
            {
                var configService    = new AzureBlobConfigurationService(new MyConfigurationManager(), log);
                var persistentConfig = configService.Get <MigrateFromAvmsConfiguration>();

                if (options.SourceConnectionString == null)
                {
                    options.SourceConnectionString = persistentConfig.SourceConnectionString;
                }
                if (options.TargetConnectionString == null)
                {
                    options.TargetConnectionString = persistentConfig.TargetConnectionString;
                }
                if (options.RecordBatchSize == 0)
                {
                    options.RecordBatchSize = persistentConfig.RecordBatchSize;
                }
                if (options.MaxNumberOfChangesToDetailPerTable == null)
                {
                    options.MaxNumberOfChangesToDetailPerTable = persistentConfig.MaxNumberOfChangesToDetailPerTable;
                }
                if (options.AnonymiseData == false)
                {
                    options.AnonymiseData = persistentConfig.AnonymiseData;
                }
            }

            if (options.Verbose)
            {
                Console.WriteLine($@"
SourceConnectionString             = {options.SourceConnectionString}
TargetConnectionString             = {options.TargetConnectionString}
RecordBatchSize                    = {options.RecordBatchSize}
MaxNumberOfChangesToDetailPerTable = {options.MaxNumberOfChangesToDetailPerTable}
AnonymiseData                      = {options.AnonymiseData}
SingleFullUpdate                   = {options.SingleFullUpdate}
");
            }

            var sourceDatabase = new GetOpenConnectionFromConnectionString(options.SourceConnectionString);
            var targetDatabase = new GetOpenConnectionFromConnectionString(options.TargetConnectionString);

            var genericSyncRepository = new GenericSyncRespository(log, sourceDatabase, targetDatabase);
            var avmsSyncRepository    = new AvmsSyncRespository(log, sourceDatabase, targetDatabase);

            Func <ITableSpec, IMutateTarget> createMutateTarget;

            if (options.DummyRun)
            {
                createMutateTarget = tableSpec => new DummyMutateTarget(log, tableSpec);
            }
            else
            {
                createMutateTarget = tableSpec => new MutateTarget(log, genericSyncRepository, (int)Math.Max(5000 * tableSpec.BatchSizeMultiplier, 1), tableSpec);
            }

            var tables = new AvmsToAvmsPlusTables(log, options, avmsSyncRepository, !options.ExcludeVacancy).All;

            if (options.SingleTable != null)
            {
                tables = tables.Where(t => t.Name == options.SingleTable);
            }

            var controller = new Controller(options, log, genericSyncRepository, createMutateTarget, tables);

            if (options.Reset)
            {
                controller.Reset();
            }

            var cancellationTokenSource = new System.Threading.CancellationTokenSource();

            bool threaded = true;

            if (options.SingleFullUpdate)
            {
                controller.DoFullScanForAll(threaded);
            }
            else
            {
                controller.DoAll(new System.Threading.CancellationTokenSource(), threaded);
            }
        }
Example #35
0
        /// <summary>
        /// 从Url下载,并保存到指定目录
        /// </summary>
        /// <param name="serviceProvider">.NetCore 下的服务器提供程序,如果 .NET Framework 则保留 null</param>
        /// <param name="url">需要下载文件的Url</param>
        /// <param name="filePathName">保存文件的路径,如果下载文件包含文件名,按照文件名储存,否则将分配Ticks随机文件名</param>
        /// <param name="timeOut">超时时间</param>
        /// <returns></returns>
        public static string Download(IServiceProvider serviceProvider, string url, string filePathName, int timeOut = Config.TIME_OUT)
        {
            var dir = Path.GetDirectoryName(filePathName) ?? "/";

            Directory.CreateDirectory(dir);

#if NET451
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method  = "GET";
            request.Timeout = timeOut;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (Stream responseStream = response.GetResponseStream())
            {
                string responseFileName = null;
                //如:content-disposition: inline; filename="WeChatSampleBuilder-2.0.0.zip"; filename*=utf-8''WeChatSampleBuilder-2.0.0.zip
                var contentDescriptionHeader = response.GetResponseHeader("Content-Disposition");

                if (!string.IsNullOrEmpty(contentDescriptionHeader))
                {
                    var fileName = Regex.Match(contentDescriptionHeader, @"(?<=filename="")([\s\S]+)(?="")", RegexOptions.IgnoreCase).Value;

                    responseFileName = Path.Combine(dir, fileName);
                }

                var fullName = responseFileName ?? Path.Combine(dir, GetRandomFileName());

                using (var fs = File.Open(fullName, FileMode.OpenOrCreate))
                {
                    byte[] bArr = new byte[1024];
                    int    size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    while (size > 0)
                    {
                        fs.Write(bArr, 0, size);
                        fs.Flush();
                        size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    }
                }

                return(fullName);
            }
#else
            System.Net.Http.HttpClient httpClient = serviceProvider.GetRequiredService <SenparcHttpClient>().Client;
            using (var cts = new System.Threading.CancellationTokenSource(timeOut))
            {
                try
                {
                    using (var responseMessage = httpClient.GetAsync(url, cancellationToken: cts.Token).Result)
                    {
                        if (responseMessage.StatusCode == HttpStatusCode.OK)
                        {
                            string responseFileName = null;
                            //ContentDisposition可能会为Null
                            if (responseMessage.Content.Headers.ContentDisposition != null &&
                                responseMessage.Content.Headers.ContentDisposition.FileName != null &&
                                responseMessage.Content.Headers.ContentDisposition.FileName != "\"\"")
                            {
                                responseFileName = Path.Combine(dir, responseMessage.Content.Headers.ContentDisposition.FileName.Trim('"'));
                            }

                            var fullName = responseFileName ?? Path.Combine(dir, GetRandomFileName());
                            using (var fs = File.Open(fullName, FileMode.Create))
                            {
                                using (var responseStream = responseMessage.Content.ReadAsStreamAsync().Result)
                                {
                                    responseStream.CopyTo(fs);
                                    fs.Flush();
                                }
                            }
                            return(fullName);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                catch { throw; }
            }
#endif
        }
Example #36
0
        public void Initialize()
        {
            try
            {
                //初始化连接
                if (_readConnection == null)
                {
                    _readConnection = _factory.Connection(_mqConfig);
                }

                //初始化IMode
                if (_readChannel == null)
                {
                    _readChannel = _readConnection.CreateModel();
                    _readChannel.BasicQos(0, _prefetchCount, false);
                    try
                    {
                        // 申明交换
                        _readChannel.ExchangeDeclare(_mqConfig.ExchangeName, _mqConfig.ExchangeType, _mqConfig.ExchangeDurable);
                    }
                    catch (Exception ex)
                    {
                        if (ExcptionCallback != null)
                        {
                            ExcptionCallback(this, ex, string.Format("mq队列声明失败!"));
                        }
                    }
                    // 声明队列
                    _readChannel.QueueDeclare(_mqConfig.QueueName, true, false, false, null);

                    // 绑定交换
                    _readChannel.QueueBind(_mqConfig.QueueName, _mqConfig.ExchangeName, _mqConfig.BindKey);
                }



                //初始化Consumer
                if (_consumer == null)
                {
                    _consumer = new QueueingBasicConsumer(_readChannel);

                    _readChannel.BasicConsume(_mqConfig.QueueName, _noAck, _consumer);
                }

                if (_mainCancelSource != null)
                {
                    return;
                }

                _mainCancelSource = new System.Threading.CancellationTokenSource();
                System.Threading.Tasks.Task.Factory.StartNew(ReceiveThread, _mainCancelSource.Token);
            }
            catch (Exception ex)
            {
                if (ExcptionCallback != null)
                {
                    ExcptionCallback(this, ex, string.Format("mq队列初始化失败!"));
                }
                ClearChannel();
            }
        }
 public void setCancellationToken(System.Threading.CancellationTokenSource cancellationTokenSource)
 {
     _cancellationToken = cancellationTokenSource;
 }
 public Authenticator()
 {
     CancelTokenSource = new System.Threading.CancellationTokenSource();
 }
 protected void TriggerHostShutdown(System.Threading.CancellationTokenSource hostShutdownSource)
 {
 }
Example #40
0
        private async Task ProcessAsync(IEnumerable <FileUploadRequest> pendings)
        {
            CancellationTokenSource = new System.Threading.CancellationTokenSource();



            var client = FileUploaderService.Instance.Receiver.GetHttpClient();

            var tasks = pendings.Select(async pending =>
            {
                if (pending.Cancelled)
                {
                    pending.ResponseCode = 555;
                    if (System.IO.File.Exists(pending.FilePath))
                    {
                        System.IO.File.Delete(pending.FilePath);
                    }
                    return;
                }

                if (!System.IO.File.Exists(pending.FilePath))
                {
                    pending.ResponseCode = 555;
                    pending.Cancelled    = true;
                    return;
                }

                try
                {
                    System.Threading.CancellationTokenSource fileCancelSource = new System.Threading.CancellationTokenSource();
                    CancellationTokenSource.Token.Register(() =>
                    {
                        if (pending.Cancelled)
                        {
                            pending.ResponseCode = 555;
                            fileCancelSource.Cancel();
                        }
                    });

                    var msg = new HttpRequestMessage(new HttpMethod(pending.Method.ToUpper()), pending.Url);

                    using (var input = System.IO.File.OpenRead(pending.FilePath))
                    {
                        var body = new StreamContent(input);
                        body.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(pending.ContentType);

                        var fileInfo = new System.IO.FileInfo(pending.FilePath);

                        body.Headers.ContentLength = fileInfo.Length;

                        if (pending.Headers != null)
                        {
                            foreach (var h in pending.Headers)
                            {
                                body.Headers.TryAddWithoutValidation(h.Key, h.Value);
                            }
                        }

                        msg.Content = new ProgressableStreamContent(body, (n, t) =>
                        {
                            try
                            {
                                pending.TotalBytes = (int)t;
                                pending.TotalSent  = (int)n;
                                FileUploaderService.Instance.ReportProgress(pending);
                            }
                            catch (Exception ex)
                            {
                                FileUploaderService.Instance.ReportFatalError(ex);
                            }
                        });

                        System.Diagnostics.Debug.WriteLine($"Starting upload for {pending.Identifier}");

                        using (var response = await client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead, fileCancelSource.Token))
                        {
                            using (var s = await response.Content.ReadAsStreamAsync())
                            {
                                pending.ResponseContentType = response.Content.Headers.ContentType.ToString();

                                pending.ResponseCode = (int)response.StatusCode;

                                using (var outs = System.IO.File.OpenWrite(pending.ResponseFilePath))
                                {
                                    await s.CopyToAsync(outs);
                                }
                            }
                        }

                        System.Diagnostics.Debug.WriteLine($"Finished upload for {pending.Identifier}");
                    }

                    FileUploaderService.Instance.Storage.Save();
                }
                catch (TaskCanceledException)
                {
                }
                catch (Exception ex)
                {
                    FileUploaderService.Instance.ReportFatalError(ex);
                }
            });

            await Task.WhenAll(tasks);

            // also save last status...
            FileUploaderService.Instance.Storage.Save();
            await FileUploaderService.Instance.ReportPendingStatus();
        }
Example #41
0
        public void StartupApp(object sender, StartupEventArgs e)
        {
            //Determine if the permissionChange is succesfull after launcher update
            bool isGoodUpdate = false;
            bool isLogging    = false;

            Logger.Instance.Write("Application starting up...");

            foreach (string a in e.Args)
            {
                if (a.Equals("--log", StringComparison.OrdinalIgnoreCase))
                {
                    Logger.Instance.StartLogConsole();
                    isLogging = true;
                }
                if (a.StartsWith("--patch-result="))
                {
                    string code = a.Substring("--patch-result=".Length);
                    Logger.Instance.Write($"Startup Parameter 'patch-result' found - contents: {code}");
                    //If the code !=0 -> there is something wrong with the patching of the launcher
                    if (code != "0")
                    {
                        MessageBox.Show(string.Format("Failed to update the launcher (code {0}).\n\nPlease close any applications related to Renegade-X and try again.", code), "Patch failed", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else//Otherwise -> change folderpermissions and afterwards launch the launcher
                    {
                        try {
                            SetFullControlPermissionsToEveryone(GameInstallation.GetRootPath());
                            isGoodUpdate = true; //Set isGoodUpdate to true to indicate correct permissionChange
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                else if (a.StartsWith("--firstInstall")) //Init the first install
                {
                    Logger.Instance.Write("Startup parameters 'firstInstall' found - Starting RenX Installer");
                    Installer x = new Installer();
                    x.Show();
                    x.FirstInstall();
                    return;
                }
                else if (a.StartsWith("--UpdateGame="))//Manually opdate the game to a given URL.
                {
                    // Close any other instances of the RenX-Launcher
                    if (InstanceHandler.IsAnotherInstanceRunning())
                    {
                        InstanceHandler.KillDuplicateInstance();
                    }

                    var    targetDir      = GameInstallation.GetRootPath();
                    var    applicationDir = System.IO.Path.Combine(GameInstallation.GetRootPath(), "patch");
                    String patchUrl       = a.Substring("--UpdateGame=".Length);
                    var    patchVersion   = VersionCheck.GetLatestGameVersionName();

                    var progress = new Progress <DirectoryPatcherProgressReport>();
                    var cancellationTokenSource = new System.Threading.CancellationTokenSource();

                    RxPatcher.Instance.AddNewUpdateServer(patchUrl, "");
                    System.Threading.Tasks.Task task = RxPatcher.Instance.ApplyPatchFromWebDownloadTask(RXPatchLib.RxPatcher.Instance.GetNextUpdateServerEntry(), targetDir, applicationDir, progress, cancellationTokenSource.Token, null); // no verificaiton on instructions.json, as we're bypassing standard version checking

                    var window = new Views.ApplyUpdateWindow(task, RxPatcher.Instance, progress, patchVersion, cancellationTokenSource, Views.ApplyUpdateWindow.UpdateWindowType.Update);

                    window.ShowDialog();

                    VersionCheck.UpdateGameVersion();
                    return;
                }
            }

            if (LauncherTwo.Properties.Settings.Default.UpgradeRequired)
            {
                LauncherTwo.Properties.Settings.Default.Upgrade();
                LauncherTwo.Properties.Settings.Default.UpgradeRequired = false;
                LauncherTwo.Properties.Settings.Default.Save();
            }

            /* Commented out untill I found a better way to intergrate it in the installation
             * if (!GameInstallation.IsRootPathPlausible())
             * {
             *  var result = MessageBox.Show("The game path seems to be incorrect. Please ensure that the launcher is placed in the correct location. If you proceed, files in the following location might be affected:\n\n" + GameInstallation.GetRootPath() + "\n\nAre you sure want to proceed?", "Invalid game path", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
             *  if (result != MessageBoxResult.Yes)
             *  {
             *      Shutdown();
             *      return;
             *  }
             * }
             */
            //If no args are present, or a permissionChange update was executed -> normally start the launcher
            if (e.Args.Length == 0 || isGoodUpdate || isLogging)
            {
                if (InstanceHandler.IsAnotherInstanceRunning() && !isGoodUpdate)
                {
                    MessageBox.Show("Error:\nUnable to start Renegade-X Launcher: Another instance is already running!",
                                    "Renegade-X Launcher", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    Current.Shutdown();
                }

                Logger.Instance.Write("Initial application startup complete, Creating new MainWindow");
                new MainWindow().Show();
            }

            /*else
             * {
             *  Application.Current.Shutdown();
             * }*/
        }
Example #42
0
        public Peer(WebSocket socket)
        {
            _Socket = socket;

            _CancelSource = new System.Threading.CancellationTokenSource();
        }
Example #43
0
		//BarcodeReader reader;

		public ZxingSurfaceView (ZxingActivity activity, MobileBarcodeScanningOptions options)
			: base (activity)
		{
			this.activity = activity;

			screenResolution = new Size(this.activity.WindowManager.DefaultDisplay.Width, this.activity.WindowManager.DefaultDisplay.Height);

			this.options = options;


			this.reader = this.options.BuildMultiFormatReader();

			this.surface_holder = Holder;
			this.surface_holder.AddCallback (this);
			this.surface_holder.SetType (SurfaceType.PushBuffers);



			this.tokenSource = new System.Threading.CancellationTokenSource();
		}
Example #44
0
        void ILayer.Render(Graphics g, MapViewport map)
        {
            // We don't need to regenerate the tiles
            if (map.Envelope.Equals(_lastViewport) && _numPendingDownloads == 0)
            {
                g.DrawImage(_bitmap, Point.Empty);
                return;
            }

            // Create a backbuffer
            lock (_renderLock)
            {
                if (_bitmap == null || _bitmap.Size != map.Size)
                {
                    _bitmap = new Bitmap(map.Size.Width, map.Size.Height, PixelFormat.Format32bppArgb);
                    using (var tmpGraphics = Graphics.FromImage(_bitmap))
                        tmpGraphics.Clear(Color.Transparent);
                }
            }
            // Save the last viewport
            _lastViewport = map.Envelope;

            // Cancel old rendercycle
            ((ITileAsyncLayer)this).Cancel();

            var mapViewport = map.Envelope;
            var mapSize     = map.Size;

            var mapColumnWidth  = _cellSize.Width + _cellBuffer.Width;
            var mapColumnHeight = _cellSize.Height + _cellBuffer.Width;
            var columns         = (int)Math.Ceiling((double)mapSize.Width / mapColumnWidth);
            var rows            = (int)Math.Ceiling((double)mapSize.Height / mapColumnHeight);

            var renderMapSize = new Size(columns * _cellSize.Width + _cellBuffer.Width,
                                         rows * _cellSize.Height + _cellBuffer.Height);
            var horizontalFactor = (double)renderMapSize.Width / mapSize.Width;
            var verticalFactor   = (double)renderMapSize.Height / mapSize.Height;

            var diffX = 0.5d * (horizontalFactor * mapViewport.Width - mapViewport.Width);
            var diffY = 0.5d * (verticalFactor * mapViewport.Height - mapViewport.Height);

            var totalRenderMapViewport = mapViewport.Grow(diffX, diffY);
            var columnWidth            = totalRenderMapViewport.Width / columns;
            var rowHeight = totalRenderMapViewport.Height / rows;

            var rmdx = (int)((mapSize.Width - renderMapSize.Width) * 0.5f);
            var rmdy = (int)((mapSize.Height - renderMapSize.Height) * 0.5f);

            var tileSize = Size.Add(_cellSize, Size.Add(_cellBuffer, _cellBuffer));

            var miny = totalRenderMapViewport.MinY;
            var pty  = rmdy + renderMapSize.Height - tileSize.Height;

            for (var i = 0; i < rows; i++)
            {
                var minx = totalRenderMapViewport.MinX;
                var ptx  = rmdx;
                for (var j = 0; j < columns; j++)
                {
                    var tmpMap = new Map(_cellSize);

                    tmpMap.Layers.Add(_baseLayer);
                    tmpMap.DisposeLayersOnDispose = false;
                    tmpMap.ZoomToBox(new Envelope(minx, minx + columnWidth, miny, miny + rowHeight));

                    var cancelToken = new System.Threading.CancellationTokenSource();
                    var token       = cancelToken.Token;
                    var pt          = new Point(ptx, pty);
                    var t           = new System.Threading.Tasks.Task(delegate
                    {
                        if (token.IsCancellationRequested)
                        {
                            token.ThrowIfCancellationRequested();
                        }

                        var res = RenderCellOnThread(token, pt, tmpMap);
                        if (res)
                        {
                            System.Threading.Interlocked.Decrement(ref _numPendingDownloads);
                            var e = DownloadProgressChanged;
                            if (e != null)
                            {
                                e(_numPendingDownloads);
                            }
                        }
                    }, token);
                    var dt = new RenderTask {
                        CancellationToken = cancelToken, Task = t
                    };
                    lock (_currentTasks)
                    {
                        _currentTasks.Add(dt);
                        _numPendingDownloads++;
                    }
                    t.Start();
                    minx += columnWidth;
                    ptx  += _cellSize.Width;
                }
                miny += rowHeight;
                pty  -= _cellSize.Height;
            }
        }
Example #45
0
        public PcapStreamReader(System.IO.Stream pcapStream, int packetQueueSize, StreamReadCompletedCallback streamReadCompletedCallback, bool startBackgroundWorkers, long streamMaxLength, int readTimeoutMilliseconds)
        {
            this.pcapStream              = pcapStream;
            this.streamLength            = streamMaxLength;
            this.readBytesEstimate       = 0;
            this.readTimeoutMilliseconds = readTimeoutMilliseconds;

            this.packetQueueMaxSize          = packetQueueSize;
            this.streamReadCompletedCallback = streamReadCompletedCallback;


            //TODO: Figure out if it is a libpcap or pcapNG stream...
            this.pcapParser = PcapParserFactory.CreatePcapParser(this);// new PcapParser(pcapStream, this.AbortReadingPcapStream);

            this.packetQueueBC = new System.Collections.Concurrent.BlockingCollection <PcapFrame>(this.packetQueueMaxSize);
            //this.packetQueue = new System.Collections.Concurrent.ConcurrentQueue<PcapFrame>();
            //this.packetQueueHasRoomEvent = new System.Threading.AutoResetEvent(true);
            this.enqueuedByteCount = 0;
            this.dequeuedByteCount = 0;

            this.backgroundStreamReaderCanceller = new System.Threading.CancellationTokenSource();
            System.Threading.CancellationToken cancellationToken = backgroundStreamReaderCanceller.Token;
            this.readAction = new Action(() => {
                DateTime firstFrameTimestamp = DateTime.MinValue;
                DateTime lastFrameTimestamp  = DateTime.MinValue;
                int framesCount = 0;
                try {
                    //int sleepMilliSecs = 20;

                    while (!cancellationToken.IsCancellationRequested && !this.EndOfStream())
                    {
                        PcapFrame packet = this.pcapParser.ReadPcapPacketBlocking();
                        //PcapFrame packet = await this.pcapParser.ReadPcapPacketAsync(cancellationToken);
                        if (firstFrameTimestamp == DateTime.MinValue)
                        {
                            firstFrameTimestamp = packet.Timestamp;
                        }
                        lastFrameTimestamp = packet.Timestamp;
                        framesCount++;
                        this.enqueuedByteCount += packet.Data.Length;

                        while (!this.packetQueueBC.TryAdd(packet, 1000, cancellationToken))
                        {
                            if (cancellationToken.IsCancellationRequested || this.EndOfStream())
                            {
                                break;
                            }
                        }
                        //this.packetQueue.Enqueue(packet);
                    }
                }
                catch (System.IO.EndOfStreamException) {
                    //Do nothing, just stop reading
                    this.pcapStream = null;
                }
                catch (System.IO.IOException) {
                    //probably a socket timout
                    if (!(this.pcapStream is System.IO.FileStream) && this.pcapStream != null)
                    {
                        if (this.pcapStream.CanWrite)
                        {
                            this.pcapStream.Flush();
                        }
                        this.pcapStream.Dispose();
                    }
                    //this.pcapStream = null;
                }
                catch (OperationCanceledException) {
                    if (!(this.pcapStream is System.IO.FileStream) && this.pcapStream != null)
                    {
                        if (this.pcapStream.CanWrite)
                        {
                            this.pcapStream.Flush();
                        }
                        this.pcapStream.Dispose();
                    }
                }

#if !DEBUG
                catch (Exception ex) {
                    this.pcapStream = null;
                    //this.backgroundStreamReaderCanceller.Cancel();
                    //e.Cancel = true;
                    //e.Result = ex.Message;
                    this.AbortFileRead();
                }
#endif
                //do a callback with this.filename as well as first and last timestamp
                if (this.streamReadCompletedCallback != null && firstFrameTimestamp != DateTime.MinValue && lastFrameTimestamp != DateTime.MinValue)
                {
                    this.streamReadCompletedCallback(framesCount, firstFrameTimestamp, lastFrameTimestamp);
                }
            });

            if (startBackgroundWorkers)
            {
                this.StartBackgroundWorkers();
            }
        }
Example #46
0
        Run()
        {
            Log.Detail("Running build");

            // TODO: should the rank collections be sorted, so that modules with fewest dependencies are first?

            var graph = Graph.Instance;
            var metaDataType = graph.BuildModeMetaData.GetType();
            var useEvaluation = CheckIfModulesNeedRebuilding(metaDataType);
            var explainRebuild = CommandLineProcessor.Evaluate(new Options.ExplainBuildReason());
            var immediateOutput = CommandLineProcessor.Evaluate(new Options.ImmediateOutput());

            ExecutePreBuild(metaDataType);

            if (!System.IO.Directory.Exists(graph.BuildRoot))
            {
                System.IO.Directory.CreateDirectory(graph.BuildRoot);
            }

            var threadCount = CommandLineProcessor.Evaluate(new Options.MultiThreaded());
            if (0 == threadCount)
            {
                threadCount = System.Environment.ProcessorCount;
            }

            System.Exception abortException = null;
            if (threadCount > 1)
            {
                var cancellationSource = new System.Threading.CancellationTokenSource();
                var cancellationToken = cancellationSource.Token;

                // LongRunning is absolutely necessary in order to achieve paralleism
                var creationOpts = System.Threading.Tasks.TaskCreationOptions.LongRunning;
                var continuationOpts = System.Threading.Tasks.TaskContinuationOptions.LongRunning;

                var scheduler = new LimitedConcurrencyLevelTaskScheduler(threadCount);

                var factory = new System.Threading.Tasks.TaskFactory(
                        cancellationToken,
                        creationOpts,
                        continuationOpts,
                        scheduler);

                var tasks = new Array<System.Threading.Tasks.Task>();
                foreach (var rank in graph.Reverse())
                {
                    foreach (var module in rank)
                    {
                        var context = new ExecutionContext(useEvaluation, explainRebuild, immediateOutput);
                        var task = factory.StartNew(() =>
                            {
                                if (cancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }
                                var depTasks = new Array<System.Threading.Tasks.Task>();
                                foreach (var dep in module.Dependents)
                                {
                                    if (null == dep.ExecutionTask)
                                    {
                                        continue;
                                    }
                                    depTasks.Add(dep.ExecutionTask);
                                }
                                foreach (var dep in module.Requirements)
                                {
                                    if (null == dep.ExecutionTask)
                                    {
                                        continue;
                                    }
                                    depTasks.Add(dep.ExecutionTask);
                                }
                                System.Threading.Tasks.Task.WaitAll(depTasks.ToArray());
                                if (cancellationToken.IsCancellationRequested)
                                {
                                    return;
                                }

                                try
                                {
                                    (module as IModuleExecution).Execute(context);
                                }
                                catch (Exception ex)
                                {
                                    abortException = ex;
                                    cancellationSource.Cancel();
                                }
                                finally
                                {
                                    if (context.OutputStringBuilder != null && context.OutputStringBuilder.Length > 0)
                                    {
                                        Log.Info(context.OutputStringBuilder.ToString());
                                    }
                                    if (context.ErrorStringBuilder != null && context.ErrorStringBuilder.Length > 0)
                                    {
                                        Log.Info(context.ErrorStringBuilder.ToString());
                                    }
                                }
                            });
                        tasks.Add(task);
                        module.ExecutionTask = task;
                    }
                }
                try
                {
                    System.Threading.Tasks.Task.WaitAll(tasks.ToArray());
                }
                catch (System.AggregateException exception)
                {
                    if (!(exception.InnerException is System.Threading.Tasks.TaskCanceledException))
                    {
                        throw new Exception(exception, "Error during threaded build");
                    }
                }
            }
            else
            {
                foreach (var rank in graph.Reverse())
                {
                    if (null != abortException)
                    {
                        break;
                    }
                    foreach (IModuleExecution module in rank)
                    {
                        var context = new ExecutionContext(useEvaluation, explainRebuild, immediateOutput);
                        try
                        {
                            module.Execute(context);
                        }
                        catch (Exception ex)
                        {
                            abortException = ex;
                            break;
                        }
                        finally
                        {
                            if (context.OutputStringBuilder != null && context.OutputStringBuilder.Length > 0)
                            {
                                Log.Info(context.OutputStringBuilder.ToString());
                            }
                            if (context.ErrorStringBuilder != null && context.ErrorStringBuilder.Length > 0)
                            {
                                Log.Info(context.ErrorStringBuilder.ToString());
                            }
                        }
                    }
                }
            }

            if (null != abortException)
            {
                throw new Exception(abortException, "Error during {0}threaded build", (threadCount > 1) ? string.Empty : "non-");
            }

            ExecutePostBuild(metaDataType);
        }
Example #47
0
        private async void Fade(int fromId, int toId)
        {
            //System.Diagnostics.Debug.WriteLine("Fade requested {0} to {1}", fromId, toId);
            if (fromId == toId)
            {
                return;
            }
            if (BaseLayers.Count < fromId || BaseLayers.Count < toId || toId < 0 || fromId < 0)
            {
                return;
            }
            if (tcs != null)
            {
                cancelSource.Cancel();
                await tcs.Task;
            }
            var from = BaseLayers[fromId];
            var to   = BaseLayers[toId];

            if (from.LoadStatus != Esri.ArcGISRuntime.LoadStatus.Loaded || to.LoadStatus != Esri.ArcGISRuntime.LoadStatus.Loaded)
            {
                from.IsVisible = false;
                to.IsVisible   = true;
                return;
            }
            DispatcherTimer t = new DispatcherTimer()
            {
                Interval = TimeSpan.FromMilliseconds(10)
            };
            var step     = t.Interval.TotalMilliseconds / TimeSpan.FromSeconds(.5).TotalMilliseconds;
            var localTcs = tcs = new System.Threading.Tasks.TaskCompletionSource <object>();

            cancelSource = new System.Threading.CancellationTokenSource();
            var token = cancelSource.Token;

            if (toId > fromId)             //Layer on top got turned on - Fade it in
            {
                to.Opacity   = 0;
                to.IsVisible = true;
                t.Tick      += (s, e2) =>
                {
                    to.Opacity = Math.Min(1, to.Opacity + step);

                    if (token.IsCancellationRequested || to.Opacity == 1)
                    {
                        t.Stop();
                        to.Opacity     = 1;
                        from.IsVisible = false;
                        localTcs.SetResult(null);
                    }
                    //System.Diagnostics.Debug.WriteLine("Fading {0} in @ {1}, viz={2}", toId, to.Opacity, to.IsVisible);
                };
            }
            else             //Layer below got turned on - Fade layer on top out
            {
                to.Opacity   = 1;
                to.IsVisible = true;
                t.Tick      += (s, e2) =>
                {
                    from.Opacity = Math.Max(0, from.Opacity - step);
                    if (token.IsCancellationRequested || from.Opacity == 0)
                    {
                        t.Stop();
                        from.Opacity   = 0;
                        from.IsVisible = false;
                        localTcs.SetResult(null);
                    }
                    //System.Diagnostics.Debug.WriteLine("Fading {0} out @ {1}, viz={2}", fromId, from.Opacity, from.IsVisible);
                };
            }
            t.Start();
        }
Example #48
0
 /// <summary>
 /// Required method for Designer support - do not modify
 /// the contents of this method with the code editor.
 /// </summary>
 private void InitializeComponent()
 {
     this.components = new System.ComponentModel.Container();
     DevExpress.XtraBars.Docking2010.Views.Tabbed.DockingContainer dockingContainer1 = new DevExpress.XtraBars.Docking2010.Views.Tabbed.DockingContainer();
     System.Threading.CancellationTokenSource       cancellationTokenSource1         = new System.Threading.CancellationTokenSource();
     System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DataProviderUC));
     this.documentGroup1       = new DevExpress.XtraBars.Docking2010.Views.Tabbed.DocumentGroup(this.components);
     this.document1            = new DevExpress.XtraBars.Docking2010.Views.Tabbed.Document(this.components);
     this.documentManager1     = new DevExpress.XtraBars.Docking2010.DocumentManager(this.components);
     this.tabbedView1          = new DevExpress.XtraBars.Docking2010.Views.Tabbed.TabbedView(this.components);
     this.dockManager1         = new DevExpress.XtraBars.Docking.DockManager(this.components);
     this.dockPanel1           = new DevExpress.XtraBars.Docking.DockPanel();
     this.dockPanel1_Container = new DevExpress.XtraBars.Docking.ControlContainer();
     this.ucLogs1              = new Analogy.UCLogs();
     this.dockPanel2           = new DevExpress.XtraBars.Docking.DockPanel();
     this.dockPanel2_Container = new DevExpress.XtraBars.Docking.ControlContainer();
     this.document2            = new DevExpress.XtraBars.Docking2010.Views.Tabbed.Document(this.components);
     ((System.ComponentModel.ISupportInitialize)(this.documentGroup1)).BeginInit();
     ((System.ComponentModel.ISupportInitialize)(this.document1)).BeginInit();
     ((System.ComponentModel.ISupportInitialize)(this.documentManager1)).BeginInit();
     ((System.ComponentModel.ISupportInitialize)(this.tabbedView1)).BeginInit();
     ((System.ComponentModel.ISupportInitialize)(this.dockManager1)).BeginInit();
     this.dockPanel1.SuspendLayout();
     this.dockPanel1_Container.SuspendLayout();
     this.dockPanel2.SuspendLayout();
     ((System.ComponentModel.ISupportInitialize)(this.document2)).BeginInit();
     this.SuspendLayout();
     //
     // documentGroup1
     //
     this.documentGroup1.Items.AddRange(new DevExpress.XtraBars.Docking2010.Views.Tabbed.Document[] {
         this.document1,
         this.document2
     });
     //
     // document1
     //
     this.document1.Caption               = "dockPanel1";
     this.document1.ControlName           = "dockPanel1";
     this.document1.FloatLocation         = new System.Drawing.Point(388, 428);
     this.document1.FloatSize             = new System.Drawing.Size(200, 200);
     this.document1.Properties.AllowClose = DevExpress.Utils.DefaultBoolean.True;
     this.document1.Properties.AllowFloat = DevExpress.Utils.DefaultBoolean.True;
     this.document1.Properties.AllowFloatOnDoubleClick = DevExpress.Utils.DefaultBoolean.True;
     //
     // documentManager1
     //
     this.documentManager1.ContainerControl = this;
     this.documentManager1.View             = this.tabbedView1;
     this.documentManager1.ViewCollection.AddRange(new DevExpress.XtraBars.Docking2010.Views.BaseView[] {
         this.tabbedView1
     });
     //
     // tabbedView1
     //
     this.tabbedView1.DocumentGroups.AddRange(new DevExpress.XtraBars.Docking2010.Views.Tabbed.DocumentGroup[] {
         this.documentGroup1
     });
     this.tabbedView1.Documents.AddRange(new DevExpress.XtraBars.Docking2010.Views.BaseDocument[] {
         this.document1,
         this.document2
     });
     dockingContainer1.Element = this.documentGroup1;
     this.tabbedView1.RootContainer.Nodes.AddRange(new DevExpress.XtraBars.Docking2010.Views.Tabbed.DockingContainer[] {
         dockingContainer1
     });
     //
     // dockManager1
     //
     this.dockManager1.Form = this;
     this.dockManager1.RootPanels.AddRange(new DevExpress.XtraBars.Docking.DockPanel[] {
         this.dockPanel1,
         this.dockPanel2
     });
     this.dockManager1.TopZIndexControls.AddRange(new string[] {
         "DevExpress.XtraBars.BarDockControl",
         "DevExpress.XtraBars.StandaloneBarDockControl",
         "System.Windows.Forms.StatusBar",
         "System.Windows.Forms.MenuStrip",
         "System.Windows.Forms.StatusStrip",
         "DevExpress.XtraBars.Ribbon.RibbonStatusBar",
         "DevExpress.XtraBars.Ribbon.RibbonControl",
         "DevExpress.XtraBars.Navigation.OfficeNavigationBar",
         "DevExpress.XtraBars.Navigation.TileNavPane",
         "DevExpress.XtraBars.TabFormControl",
         "DevExpress.XtraBars.FluentDesignSystem.FluentDesignFormControl",
         "DevExpress.XtraBars.ToolbarForm.ToolbarFormControl"
     });
     //
     // dockPanel1
     //
     this.dockPanel1.Controls.Add(this.dockPanel1_Container);
     this.dockPanel1.DockedAsTabbedDocument = true;
     this.dockPanel1.FloatLocation          = new System.Drawing.Point(388, 428);
     this.dockPanel1.ID           = new System.Guid("c976b390-5c1f-49ec-974c-0c64c88fb284");
     this.dockPanel1.Name         = "dockPanel1";
     this.dockPanel1.OriginalSize = new System.Drawing.Size(200, 200);
     this.dockPanel1.Text         = "dockPanel1";
     //
     // dockPanel1_Container
     //
     this.dockPanel1_Container.Controls.Add(this.ucLogs1);
     this.dockPanel1_Container.Location = new System.Drawing.Point(0, 0);
     this.dockPanel1_Container.Name     = "dockPanel1_Container";
     this.dockPanel1_Container.Size     = new System.Drawing.Size(1328, 843);
     this.dockPanel1_Container.TabIndex = 0;
     //
     // ucLogs1
     //
     this.ucLogs1.CancellationTokenSource = cancellationTokenSource1;
     this.ucLogs1.CurrentColumnsFields    = ((System.Collections.Generic.List <System.ValueTuple <string, string> >)(resources.GetObject("ucLogs1.CurrentColumnsFields")));
     this.ucLogs1.DataProvider            = null;
     this.ucLogs1.Dock = System.Windows.Forms.DockStyle.Fill;
     this.ucLogs1.DoNotAddToRecentHistory        = false;
     this.ucLogs1.ExcludeFilterCriteriaUIOptions = ((System.Collections.Generic.List <Analogy.DataTypes.FilterCriteriaUIOption>)(resources.GetObject("ucLogs1.ExcludeFilterCriteriaUIOptions")));
     this.ucLogs1.FileDataProvider               = null;
     this.ucLogs1.ForceNoFileCaching             = false;
     this.ucLogs1.IncludeFilterCriteriaUIOptions = ((System.Collections.Generic.List <Analogy.DataTypes.FilterCriteriaUIOption>)(resources.GetObject("ucLogs1.IncludeFilterCriteriaUIOptions")));
     this.ucLogs1.Location     = new System.Drawing.Point(0, 0);
     this.ucLogs1.Margin       = new System.Windows.Forms.Padding(3, 2, 3, 2);
     this.ucLogs1.Name         = "ucLogs1";
     this.ucLogs1.RealTimeMode = true;
     this.ucLogs1.Size         = new System.Drawing.Size(1328, 843);
     this.ucLogs1.TabIndex     = 0;
     //
     // dockPanel2
     //
     this.dockPanel2.Controls.Add(this.dockPanel2_Container);
     this.dockPanel2.DockedAsTabbedDocument = true;
     this.dockPanel2.FloatLocation          = new System.Drawing.Point(211, 148);
     this.dockPanel2.ID               = new System.Guid("1f34e499-086e-424e-9295-178006e0ee1c");
     this.dockPanel2.Name             = "dockPanel2";
     this.dockPanel2.OriginalSize     = new System.Drawing.Size(200, 200);
     this.dockPanel2.SavedIndex       = 1;
     this.dockPanel2.SavedMdiDocument = true;
     this.dockPanel2.Text             = "dockPanel2";
     //
     // dockPanel2_Container
     //
     this.dockPanel2_Container.Location = new System.Drawing.Point(0, 0);
     this.dockPanel2_Container.Name     = "dockPanel2_Container";
     this.dockPanel2_Container.Size     = new System.Drawing.Size(1328, 843);
     this.dockPanel2_Container.TabIndex = 0;
     //
     // document2
     //
     this.document2.Caption               = "dockPanel2";
     this.document2.ControlName           = "dockPanel2";
     this.document2.FloatLocation         = new System.Drawing.Point(211, 148);
     this.document2.FloatSize             = new System.Drawing.Size(200, 200);
     this.document2.Properties.AllowClose = DevExpress.Utils.DefaultBoolean.True;
     this.document2.Properties.AllowFloat = DevExpress.Utils.DefaultBoolean.True;
     this.document2.Properties.AllowFloatOnDoubleClick = DevExpress.Utils.DefaultBoolean.True;
     //
     // DataProviderUC
     //
     this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 16F);
     this.AutoScaleMode       = System.Windows.Forms.AutoScaleMode.Font;
     this.Name  = "DataProviderUC";
     this.Size  = new System.Drawing.Size(1334, 876);
     this.Load += new System.EventHandler(this.DataProviderUC_Load);
     ((System.ComponentModel.ISupportInitialize)(this.documentGroup1)).EndInit();
     ((System.ComponentModel.ISupportInitialize)(this.document1)).EndInit();
     ((System.ComponentModel.ISupportInitialize)(this.documentManager1)).EndInit();
     ((System.ComponentModel.ISupportInitialize)(this.tabbedView1)).EndInit();
     ((System.ComponentModel.ISupportInitialize)(this.dockManager1)).EndInit();
     this.dockPanel1.ResumeLayout(false);
     this.dockPanel1_Container.ResumeLayout(false);
     this.dockPanel2.ResumeLayout(false);
     ((System.ComponentModel.ISupportInitialize)(this.document2)).EndInit();
     this.ResumeLayout(false);
 }
Example #49
0
        CheckIfModulesNeedRebuilding(
            System.Type metaType)
        {
            var graph = Graph.Instance;
            var modulesNeedEvaluating = new Array <Module>();

            // not all build modes need to determine if modules are up-to-date
            var evaluationRequiredAttr =
                metaType.GetCustomAttributes(typeof(EvaluationRequiredAttribute), false) as EvaluationRequiredAttribute[];

            if (0 == evaluationRequiredAttr.Length)
            {
                // query if any individual modules override this
                foreach (var rank in graph.Reverse())
                {
                    foreach (Module module in rank)
                    {
                        var moduleEvaluationRequiredAttr = module.GetType().GetCustomAttributes(typeof(EvaluationRequiredAttribute), true) as EvaluationRequiredAttribute[];
                        if (moduleEvaluationRequiredAttr.Length > 0 && moduleEvaluationRequiredAttr[0].Enabled)
                        {
                            modulesNeedEvaluating.Add(module);
                        }
                    }
                }

                if (0 == modulesNeedEvaluating.Count)
                {
                    Log.DebugMessage("No Bam.Core.EvaluationRequired attribute on build mode metadata, assume rebuilds necessary");
                    return(false);
                }
            }

            if ((evaluationRequiredAttr.Length > 0) && !evaluationRequiredAttr[0].Enabled && 0 == modulesNeedEvaluating.Count)
            {
                Log.DebugMessage("Module evaluation disabled");
                return(false);
            }

            using (var cancellationSource = new System.Threading.CancellationTokenSource())
            {
                var cancellationToken = cancellationSource.Token;

                // LongRunning is absolutely necessary in order to achieve paralleism
                var creationOpts     = System.Threading.Tasks.TaskCreationOptions.LongRunning;
                var continuationOpts = System.Threading.Tasks.TaskContinuationOptions.LongRunning;

                var threadCount = 1;
                var scheduler   = new LimitedConcurrencyLevelTaskScheduler(threadCount);

                var factory = new System.Threading.Tasks.TaskFactory(
                    cancellationToken,
                    creationOpts,
                    continuationOpts,
                    scheduler);

                graph.MetaData = factory;

                if (0 == modulesNeedEvaluating.Count)
                {
                    Log.DebugMessage("Module evaluation enabled for build mode {0}", graph.Mode);
                    foreach (var rank in graph.Reverse())
                    {
                        foreach (Module module in rank)
                        {
                            module.Evaluate();
                        }
                    }
                }
                else
                {
                    Log.DebugMessage("Module evaluation disabled for build mode {0}, but enabled for individual modules:", graph.Mode);
                    foreach (var module in modulesNeedEvaluating)
                    {
                        Log.DebugMessage("\tEvaluation for module {0}", module.GetType().ToString());
                        module.Evaluate();
                    }
                }
            }

            return(true);
        }
Example #50
0
        //[Test]
        // TODO : fix that
        public void WorkflowCompleteBufferedRecordsTest()
        {
            int maxBuffered = 10;
            var token       = new System.Threading.CancellationTokenSource();
            var serdes      = new StringSerDes();
            var config      = new StreamConfig <StringSerDes, StringSerDes>();

            config.ApplicationId = "test-group";
            config.MaxTaskIdleMs = (long)TimeSpan.FromSeconds(100).TotalMilliseconds;
            config.BufferedRecordsPerPartition = maxBuffered;
            config.PollMs = 10;

            var builder = new StreamBuilder();

            var stream1 = builder.Stream <string, string>("topic1");
            var stream2 = builder.Stream <string, string>("topic2");

            stream1
            .Join(stream2, (v1, v2) => $"{v1}-{v2}", JoinWindowOptions.Of(TimeSpan.FromSeconds(10)))
            .To("output");

            var topo = builder.Build();

            var supplier = new SyncKafkaSupplier();
            var producer = supplier.GetProducer(config.ToProducerConfig());
            var consumer = supplier.GetConsumer(config.ToConsumerConfig("test-consum"), null);

            consumer.Subscribe("output");
            var thread = StreamThread.Create(
                "thread-0", "c0",
                topo.Builder, new StreamMetricsRegistry(), config,
                supplier, supplier.GetAdmin(config.ToAdminConfig("admin")),
                0) as StreamThread;

            thread.Start(token.Token);

            for (int i = 0; i < maxBuffered + 1; ++i)
            {
                producer.Produce("topic1", new Confluent.Kafka.Message <byte[], byte[]>
                {
                    Key   = serdes.Serialize("key", new SerializationContext()),
                    Value = serdes.Serialize($"coucou{i}", new SerializationContext())
                });
            }

            // CONSUME PAUSE AFTER maxBuffered + 1 messages
            System.Threading.Thread.Sleep(50);

            // Add one message more with consumer in stream thread in pause
            producer.Produce("topic1", new Confluent.Kafka.Message <byte[], byte[]>
            {
                Key   = serdes.Serialize("key", new SerializationContext()),
                Value = serdes.Serialize($"coucou{maxBuffered + 1}", new SerializationContext())
            });

            Assert.AreEqual(1, thread.ActiveTasks.Count());
            var task = thread.ActiveTasks.ToArray()[0];

            Assert.IsNotNull(task.Grouper);
            Assert.IsFalse(task.Grouper.AllPartitionsBuffered);
            Assert.AreEqual(maxBuffered + 1, task.Grouper.NumBuffered());
            Assert.AreEqual(maxBuffered + 1, task.Grouper.NumBuffered(new TopicPartition("topic1", 0)));
            Assert.AreEqual(0, task.Grouper.NumBuffered(new TopicPartition("topic2", 0)));

            producer.Produce("topic2", new Confluent.Kafka.Message <byte[], byte[]>
            {
                Key   = serdes.Serialize("key", new SerializationContext()),
                Value = serdes.Serialize($"test", new SerializationContext())
            });

            List <ConsumeResult <byte[], byte[]> > records = new List <ConsumeResult <byte[], byte[]> >();

            do
            {
                records.AddRange(consumer.ConsumeRecords(TimeSpan.FromMilliseconds(100)).ToList());
            } while (records.Count() <= 12);

            Assert.AreEqual(maxBuffered + 2, records.Count());
            for (int i = 0; i < maxBuffered + 2; ++i)
            {
                var message = records.ToArray()[i];
                Assert.AreEqual("key", serdes.Deserialize(message.Message.Key, new SerializationContext()));
                Assert.IsTrue(serdes.Deserialize(message.Message.Value, new SerializationContext())
                              .Contains($"coucou{i}-"));
            }

            token.Cancel();
            thread.Dispose();
        }
Example #51
0
        static public int RunBotSession(
            byte[] kalmitElmApp,
            Func <byte[], byte[]> getFileFromHashSHA256,
            string processStoreDirectory,
            Action <string> logEntry,
            Action <LogEntry.ProcessBotEventReport> logProcessBotEventReport,
            string botConfiguration,
            string sessionId,
            string botSource,
            BotSourceModel.LiteralNodeObject botCode)
        {
            var botId = Kalmit.CommonConversion.StringBase16FromByteArray(Kalmit.CommonConversion.HashSHA256(kalmitElmApp));

            var botSessionClock = System.Diagnostics.Stopwatch.StartNew();

            /*
             * Implementat store and process based on Kalmit Web Host
             * from https://github.com/Viir/Kalmit/blob/640078f59bea3fa2ba1af43372933cff304b8c94/implement/PersistentProcess/PersistentProcess.WebHost/Startup.cs
             * */

            var process = new Kalmit.PersistentProcess.PersistentProcessWithHistoryOnFileFromElm019Code(
                new EmptyProcessStore(),
                kalmitElmApp,
                kalmitProcessLogEntry => logEntry("kalmitProcessLogEntry: " + kalmitProcessLogEntry),
                new Kalmit.ElmAppInterfaceConfig {
                RootModuleFilePath = "src/Main.elm", RootModuleName = "Main"
            });

            var processStore = new ProcessStoreInFileDirectory(
                processStoreDirectory,
                () =>
            {
                var time          = DateTimeOffset.UtcNow;
                var directoryName = time.ToString("yyyy-MM-dd");
                return(System.IO.Path.Combine(directoryName, directoryName + "T" + time.ToString("HH") + ".composition.jsonl"));
            });

            (DateTimeOffset time, string statusDescriptionText, InterfaceToBot.BotResponse.DecodeEventSuccessStructure response)? lastBotStep = null;

            var botSessionTaskCancellationToken = new System.Threading.CancellationTokenSource();
            var activeBotTasks = new ConcurrentDictionary <InterfaceToBot.StartTask, System.Threading.Tasks.Task>();

            bool pauseBot = false;

            (string text, DateTimeOffset time)lastConsoleUpdate = (null, DateTimeOffset.MinValue);

            void updatePauseContinue()
            {
                if (DotNetConsole.KeyAvailable)
                {
                    var inputKey = DotNetConsole.ReadKey();

                    if (inputKey.Key == ConsoleKey.Enter)
                    {
                        pauseBot = false;
                        displayStatusInConsole();
                    }
                }

                if (Windows.IsKeyDown(Windows.VK_CONTROL) && Windows.IsKeyDown(Windows.VK_MENU))
                {
                    pauseBot = true;
                    displayStatusInConsole();
                }
            }

            void cleanUpBotTasksAndPropagateExceptions()
            {
                foreach (var(request, engineTask) in activeBotTasks.ToList())
                {
                    if (engineTask.Exception != null)
                    {
                        throw new Exception("Bot task '" + request.taskId?.TaskIdFromString + "' has failed with exception", engineTask.Exception);
                    }

                    if (engineTask.IsCompleted)
                    {
                        activeBotTasks.TryRemove(request, out var _);
                    }
                }
            }

            var displayLock = new object();

            void displayStatusInConsole()
            {
                lock (displayLock)
                {
                    cleanUpBotTasksAndPropagateExceptions();

                    var textToDisplay = string.Join("\n", textLinesToDisplayInConsole());

                    var time = DateTimeOffset.UtcNow;

                    if (lastConsoleUpdate.text == textToDisplay && time < lastConsoleUpdate.time + TimeSpan.FromSeconds(1))
                    {
                        return;
                    }

                    if (botSessionTaskCancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    DotNetConsole.Clear();
                    DotNetConsole.WriteLine(textToDisplay);

                    lastConsoleUpdate = (textToDisplay, time);
                }
            }

            IEnumerable <string> textLinesToDisplayInConsole()
            {
                //  TODO: Add display bot configuration.

                yield return
                    ("Bot " + UserInterface.BotIdDisplayText(botId) +
                     " in session '" + sessionId + "'" +
                     (pauseBot ?
                      " is paused. Press the enter key to continue." :
                      " is running. Press CTRL + ALT keys to pause the bot."));

                if (!lastBotStep.HasValue)
                {
                    yield break;
                }

                var lastBotStepAgeInSeconds = (int)((DateTimeOffset.UtcNow - lastBotStep.Value.time).TotalSeconds);

                var activeBotTasksSnapshot = activeBotTasks.ToList();

                var activeBotTasksDescription =
                    TruncateWithEllipsis(string.Join(", ", activeBotTasksSnapshot.Select(task => task.Key.taskId.TaskIdFromString)), 60);

                yield return
                    ("Last bot event was " + lastBotStepAgeInSeconds + " seconds ago at " + lastBotStep.Value.time.ToString("HH-mm-ss.fff") + ". " +
                     "There are " + activeBotTasksSnapshot.Count + " tasks in progress (" + activeBotTasksDescription + ").");

                yield return("Status message from bot:\n");

                yield return(lastBotStep.Value.statusDescriptionText);

                yield return("");
            }

            string TruncateWithEllipsis(string originalString, int lengthLimit)
            {
                if (lengthLimit < originalString?.Length)
                {
                    return(originalString.Substring(0, lengthLimit) + "...");
                }

                return(originalString);
            }

            long lastRequestToReactorTimeInSeconds = 0;

            async System.Threading.Tasks.Task requestToReactor(RequestToReactorUseBotStruct useBot)
            {
                lastRequestToReactorTimeInSeconds = (long)botSessionClock.Elapsed.TotalSeconds;

                var toReactorStruct = new RequestToReactorStruct {
                    UseBot = useBot
                };

                var serializedToReactorStruct = Newtonsoft.Json.JsonConvert.SerializeObject(toReactorStruct);

                var reactorClient = new System.Net.Http.HttpClient();

                reactorClient.DefaultRequestHeaders.UserAgent.Add(
                    new System.Net.Http.Headers.ProductInfoHeaderValue(new System.Net.Http.Headers.ProductHeaderValue("windows-console", BotEngine.AppVersionId)));

                var content = new System.Net.Http.ByteArrayContent(System.Text.Encoding.UTF8.GetBytes(serializedToReactorStruct));

                var response = await reactorClient.PostAsync("https://reactor.botengine.org/api/", content);

                var responseString = await response.Content.ReadAsStringAsync();
            }

            System.Threading.Tasks.Task fireAndForgetReportToReactor(RequestToReactorUseBotStruct report)
            {
                lastRequestToReactorTimeInSeconds = (long)botSessionClock.Elapsed.TotalSeconds;

                return(System.Threading.Tasks.Task.Run(() =>
                {
                    try
                    {
                        requestToReactor(report).Wait();
                    }
                    catch { }
                }));
            }

            var botSourceIsPublic     = BotSourceIsPublic(botSource);
            var botPropertiesFromCode = BotSourceModel.BotCode.ReadPropertiesFromBotCode(botCode);

            fireAndForgetReportToReactor(new RequestToReactorUseBotStruct
            {
                StartSession = new RequestToReactorUseBotStruct.StartSessionStruct
                {
                    botId                 = botId,
                    sessionId             = sessionId,
                    botSource             = botSourceIsPublic ? botSource : null,
                    botPropertiesFromCode = botSourceIsPublic ? botPropertiesFromCode : null,
                }
            });

            var queuedBotEvents = new ConcurrentQueue <InterfaceToBot.BotEvent>();

            var createVolatileHostAttempts = 0;

            var volatileHosts = new ConcurrentDictionary <string, Kalmit.CSharpScriptContext>();

            InterfaceToBot.Result <InterfaceToBot.TaskResult.RunInVolatileHostError, InterfaceToBot.TaskResult.RunInVolatileHostComplete> ExecuteRequestToRunInVolatileHost(
                InterfaceToBot.Task.RunInVolatileHostStructure runInVolatileHost)
            {
                if (!volatileHosts.TryGetValue(runInVolatileHost.hostId.VolatileHostIdFromString, out var volatileHost))
                {
                    return(new InterfaceToBot.Result <InterfaceToBot.TaskResult.RunInVolatileHostError, InterfaceToBot.TaskResult.RunInVolatileHostComplete>
                    {
                        Err = new InterfaceToBot.TaskResult.RunInVolatileHostError
                        {
                            hostNotFound = new object(),
                        }
                    });
                }

                var stopwatch = System.Diagnostics.Stopwatch.StartNew();

                var fromHostResult = volatileHost.RunScript(runInVolatileHost.script);

                stopwatch.Stop();

                return(new InterfaceToBot.Result <InterfaceToBot.TaskResult.RunInVolatileHostError, InterfaceToBot.TaskResult.RunInVolatileHostComplete>
                {
                    Ok = new InterfaceToBot.TaskResult.RunInVolatileHostComplete
                    {
                        exceptionToString = fromHostResult.Exception?.ToString(),
                        returnValueToString = fromHostResult.ReturnValue?.ToString(),
                        durationInMilliseconds = stopwatch.ElapsedMilliseconds,
                    }
                });
            }

            void processBotEvent(InterfaceToBot.BotEvent botEvent)
            {
                var       eventTime                    = DateTimeOffset.UtcNow;
                Exception processEventException        = null;
                string    serializedEvent              = null;
                string    serializedResponse           = null;
                string    compositionRecordHash        = null;
                long?     processingTimeInMilliseconds = null;

                try
                {
                    serializedEvent = SerializeToJsonForBot(botEvent);

                    var processingTimeStopwatch = System.Diagnostics.Stopwatch.StartNew();

                    var processEventResult = process.ProcessEvents(
                        new[]
                    {
                        serializedEvent
                    });

                    processingTimeStopwatch.Stop();

                    processingTimeInMilliseconds = processingTimeStopwatch.ElapsedMilliseconds;

                    compositionRecordHash = Kalmit.CommonConversion.StringBase16FromByteArray(processEventResult.Item2.serializedCompositionRecordHash);

                    processStore.AppendSerializedCompositionRecord(processEventResult.Item2.serializedCompositionRecord);

                    serializedResponse = processEventResult.responses.Single();

                    var botResponse = Newtonsoft.Json.JsonConvert.DeserializeObject <InterfaceToBot.BotResponse>(serializedResponse);

                    if (botResponse.DecodeEventSuccess == null)
                    {
                        throw new Exception("Bot reported decode error: " + botResponse.DecodeEventError);
                    }

                    var statusDescriptionText =
                        botResponse.DecodeEventSuccess?.ContinueSession?.statusDescriptionText ??
                        botResponse.DecodeEventSuccess?.FinishSession?.statusDescriptionText;

                    lastBotStep = (eventTime, statusDescriptionText, botResponse.DecodeEventSuccess);

                    foreach (var startTask in botResponse.DecodeEventSuccess?.ContinueSession?.startTasks ?? Array.Empty <InterfaceToBot.StartTask>())
                    {
                        var engineTask = System.Threading.Tasks.Task.Run(() => startTaskAndProcessEvent(startTask), botSessionTaskCancellationToken.Token);

                        activeBotTasks[startTask] = engineTask;
                    }
                }
                catch (Exception exception)
                {
                    processEventException = exception;
                }

                logProcessBotEventReport(new LogEntry.ProcessBotEventReport
                {
                    time = eventTime,
                    processingTimeInMilliseconds = processingTimeInMilliseconds,
                    exception             = processEventException,
                    serializedResponse    = serializedResponse,
                    compositionRecordHash = compositionRecordHash,
                });

                if (processEventException != null)
                {
                    throw new Exception("Failed to process bot event.", processEventException);
                }

                displayStatusInConsole();
            }

            //  TODO: Get the bot requests from the `init` function.

            processBotEvent(new InterfaceToBot.BotEvent {
                SetBotConfiguration = botConfiguration ?? ""
            });

            while (true)
            {
                displayStatusInConsole();

                updatePauseContinue();

                var millisecondsToNextNotification =
                    (lastBotStep?.response?.ContinueSession?.notifyWhenArrivedAtTime?.timeInMilliseconds - botSessionClock.ElapsedMilliseconds) ?? 1000;

                System.Threading.Thread.Sleep((int)Math.Min(1000, Math.Max(10, millisecondsToNextNotification)));

                var lastRequestToReactorAgeInSeconds = (long)botSessionClock.Elapsed.TotalSeconds - lastRequestToReactorTimeInSeconds;

                if (10 <= lastRequestToReactorAgeInSeconds)
                {
                    fireAndForgetReportToReactor(new RequestToReactorUseBotStruct
                    {
                        ContinueSession = new RequestToReactorUseBotStruct.ContinueSessionStruct
                        {
                            sessionId = sessionId, statusDescriptionText = lastBotStep?.statusDescriptionText
                        }
                    });
                }

                if (pauseBot)
                {
                    continue;
                }

                var botStepTime = DateTimeOffset.UtcNow;

                var lastBotStepAgeMilli =
                    botStepTime.ToUnixTimeMilliseconds() - lastBotStep?.time.ToUnixTimeMilliseconds();

                if (lastBotStep?.response?.FinishSession != null)
                {
                    logEntry("Bot has finished.");
                    botSessionTaskCancellationToken.Cancel();

                    fireAndForgetReportToReactor(new RequestToReactorUseBotStruct
                    {
                        FinishSession = new RequestToReactorUseBotStruct.ContinueSessionStruct
                        {
                            sessionId = sessionId, statusDescriptionText = lastBotStep?.statusDescriptionText
                        }
                    }).Wait(TimeSpan.FromSeconds(3));

                    return(0);
                }

                if (lastBotStep?.response?.ContinueSession?.notifyWhenArrivedAtTime?.timeInMilliseconds <= botSessionClock.ElapsedMilliseconds ||
                    !(lastBotStepAgeMilli < 10_000))
                {
                    processBotEvent(new InterfaceToBot.BotEvent
                    {
                        ArrivedAtTime = new InterfaceToBot.TimeStructure {
                            timeInMilliseconds = botSessionClock.ElapsedMilliseconds
                        },
                    });
                }

                if (queuedBotEvents.TryDequeue(out var botEvent))
                {
                    processBotEvent(botEvent);
                }
            }

            void startTaskAndProcessEvent(InterfaceToBot.StartTask startTask)
            {
                var taskResult = performTask(startTask.task);

                queuedBotEvents.Enqueue(
                    new InterfaceToBot.BotEvent
                {
                    CompletedTask = new InterfaceToBot.CompletedTaskStructure
                    {
                        taskId     = startTask.taskId,
                        taskResult = taskResult,
                    },
                });
            }

            InterfaceToBot.TaskResult performTask(InterfaceToBot.Task task)
            {
                if (task?.CreateVolatileHost != null)
                {
                    var volatileHostId = System.Threading.Interlocked.Increment(ref createVolatileHostAttempts).ToString();

                    volatileHosts[volatileHostId] = new Kalmit.CSharpScriptContext(getFileFromHashSHA256);

                    return(new InterfaceToBot.TaskResult
                    {
                        CreateVolatileHostResponse = new InterfaceToBot.Result <object, InterfaceToBot.TaskResult.CreateVolatileHostComplete>
                        {
                            Ok = new InterfaceToBot.TaskResult.CreateVolatileHostComplete
                            {
                                hostId = new InterfaceToBot.VolatileHostId {
                                    VolatileHostIdFromString = volatileHostId
                                },
                            },
                        },
                    });
                }

                if (task?.ReleaseVolatileHost != null)
                {
                    volatileHosts.TryRemove(task?.ReleaseVolatileHost.hostId.VolatileHostIdFromString, out var volatileHost);

                    return(new InterfaceToBot.TaskResult {
                        CompleteWithoutResult = new object()
                    });
                }

                if (task?.RunInVolatileHost != null)
                {
                    var result = ExecuteRequestToRunInVolatileHost(task?.RunInVolatileHost);

                    return(new InterfaceToBot.TaskResult
                    {
                        RunInVolatileHostResponse = result,
                    });
                }

                return(null);
            }
        }
 /// <summary>
 /// Required method for Designer support - do not modify
 /// the contents of this method with the code editor.
 /// </summary>
 private void InitializeComponent()
 {
     System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(_BaseHardware));
     System.Threading.CancellationTokenSource       cancellationTokenSource1 = new System.Threading.CancellationTokenSource();
     System.Threading.CancellationTokenSource       cancellationTokenSource2 = new System.Threading.CancellationTokenSource();
     System.Threading.CancellationTokenSource       cancellationTokenSource3 = new System.Threading.CancellationTokenSource();
     System.Threading.CancellationTokenSource       cancellationTokenSource4 = new System.Threading.CancellationTokenSource();
     System.Threading.CancellationTokenSource       cancellationTokenSource5 = new System.Threading.CancellationTokenSource();
     System.Threading.CancellationTokenSource       cancellationTokenSource6 = new System.Threading.CancellationTokenSource();
     this.pic_Divisor_001                      = new System.Windows.Forms.PictureBox();
     this.ComputerSystem                       = new DiscoveryLight.UI.DeviceControls.DeviceDataControls._ComputerSystemDataControl();
     this.OperatingSystem                      = new DiscoveryLight.UI.DeviceControls.DeviceDataControls._OperatingSystemDataControl();
     this.ComputerSystemProduct                = new DiscoveryLight.UI.DeviceControls.DeviceDataControls._ComputerSystemProductDataControl();
     this.pic_Divisor_002                      = new System.Windows.Forms.PictureBox();
     this.pic_Divisor_003                      = new System.Windows.Forms.PictureBox();
     this._BaseFreeRamDevicePerformance        = new DiscoveryLight.UI.DeviceControls.DevicePerformanceControls._BaseFreeRamDevicePerformance();
     this._BaseFreeStorageDevicePerformance    = new DiscoveryLight.UI.DeviceControls.DevicePerformanceControls._BaseFreeStorageDevicePerformance();
     this._BaseProcessorUsageDevicePerformance = new DiscoveryLight.UI.DeviceControls.DevicePerformanceControls._BaseProcessorUsageDevicePerformance();
     ((System.ComponentModel.ISupportInitialize)(this.pic_Divisor_001)).BeginInit();
     ((System.ComponentModel.ISupportInitialize)(this.pic_Divisor_002)).BeginInit();
     ((System.ComponentModel.ISupportInitialize)(this.pic_Divisor_003)).BeginInit();
     this.SuspendLayout();
     //
     // pic_Divisor_001
     //
     resources.ApplyResources(this.pic_Divisor_001, "pic_Divisor_001");
     this.pic_Divisor_001.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
     this.pic_Divisor_001.Name      = "pic_Divisor_001";
     this.pic_Divisor_001.TabStop   = false;
     //
     // ComputerSystem
     //
     resources.ApplyResources(this.ComputerSystem, "ComputerSystem");
     this.ComputerSystem.ClassName        = "_ComputerSystem";
     this.ComputerSystem.ClassType        = typeof(DiscoveryLight.UI.DeviceControls.DeviceDataControls._ComputerSystemDataControl);
     this.ComputerSystem.CurrentDevice    = null;
     this.ComputerSystem.CurrentSubDevice = null;
     this.ComputerSystem.Name             = "ComputerSystem";
     this.ComputerSystem.Period           = System.TimeSpan.Parse("00:00:00.5000000");
     this.ComputerSystem.TokenSource      = cancellationTokenSource1;
     //
     // OperatingSystem
     //
     resources.ApplyResources(this.OperatingSystem, "OperatingSystem");
     this.OperatingSystem.ClassName        = "_OperatingSystem";
     this.OperatingSystem.ClassType        = typeof(DiscoveryLight.UI.DeviceControls.DeviceDataControls._OperatingSystemDataControl);
     this.OperatingSystem.CurrentDevice    = null;
     this.OperatingSystem.CurrentSubDevice = null;
     this.OperatingSystem.Name             = "OperatingSystem";
     this.OperatingSystem.Period           = System.TimeSpan.Parse("00:00:00.5000000");
     this.OperatingSystem.TokenSource      = cancellationTokenSource2;
     //
     // ComputerSystemProduct
     //
     resources.ApplyResources(this.ComputerSystemProduct, "ComputerSystemProduct");
     this.ComputerSystemProduct.ClassName        = "_ComputerSystemProduct";
     this.ComputerSystemProduct.ClassType        = typeof(DiscoveryLight.UI.DeviceControls.DeviceDataControls._ComputerSystemProductDataControl);
     this.ComputerSystemProduct.CurrentDevice    = null;
     this.ComputerSystemProduct.CurrentSubDevice = null;
     this.ComputerSystemProduct.Name             = "ComputerSystemProduct";
     this.ComputerSystemProduct.Period           = System.TimeSpan.Parse("00:00:00.5000000");
     this.ComputerSystemProduct.TokenSource      = cancellationTokenSource3;
     //
     // pic_Divisor_002
     //
     resources.ApplyResources(this.pic_Divisor_002, "pic_Divisor_002");
     this.pic_Divisor_002.BackColor = System.Drawing.SystemColors.ActiveCaption;
     this.pic_Divisor_002.Name      = "pic_Divisor_002";
     this.pic_Divisor_002.TabStop   = false;
     //
     // pic_Divisor_003
     //
     resources.ApplyResources(this.pic_Divisor_003, "pic_Divisor_003");
     this.pic_Divisor_003.BackColor = System.Drawing.SystemColors.ActiveCaption;
     this.pic_Divisor_003.Name      = "pic_Divisor_003";
     this.pic_Divisor_003.TabStop   = false;
     //
     // _BaseFreeRamDevicePerformance
     //
     resources.ApplyResources(this._BaseFreeRamDevicePerformance, "_BaseFreeRamDevicePerformance");
     this._BaseFreeRamDevicePerformance.ClassName        = "BaseFreeRamDevicePerformance";
     this._BaseFreeRamDevicePerformance.ClassType        = typeof(DiscoveryLight.UI.DeviceControls.DevicePerformanceControls._BaseFreeRamDevicePerformance);
     this._BaseFreeRamDevicePerformance.CurrentDevice    = null;
     this._BaseFreeRamDevicePerformance.CurrentSubDevice = null;
     this._BaseFreeRamDevicePerformance.Name             = "_BaseFreeRamDevicePerformance";
     this._BaseFreeRamDevicePerformance.Period           = System.TimeSpan.Parse("00:00:00.5000000");
     this._BaseFreeRamDevicePerformance.TokenSource      = cancellationTokenSource4;
     //
     // _BaseFreeStorageDevicePerformance
     //
     resources.ApplyResources(this._BaseFreeStorageDevicePerformance, "_BaseFreeStorageDevicePerformance");
     this._BaseFreeStorageDevicePerformance.ClassName        = "BaseFreeStorageDevicePerformance";
     this._BaseFreeStorageDevicePerformance.ClassType        = typeof(DiscoveryLight.UI.DeviceControls.DevicePerformanceControls._BaseFreeStorageDevicePerformance);
     this._BaseFreeStorageDevicePerformance.CurrentDevice    = null;
     this._BaseFreeStorageDevicePerformance.CurrentSubDevice = null;
     this._BaseFreeStorageDevicePerformance.Name             = "_BaseFreeStorageDevicePerformance";
     this._BaseFreeStorageDevicePerformance.Period           = System.TimeSpan.Parse("00:00:00.5000000");
     this._BaseFreeStorageDevicePerformance.TokenSource      = cancellationTokenSource5;
     //
     // _BaseProcessorUsageDevicePerformance
     //
     resources.ApplyResources(this._BaseProcessorUsageDevicePerformance, "_BaseProcessorUsageDevicePerformance");
     this._BaseProcessorUsageDevicePerformance.ClassName        = "_BaseProcessorUsageDevicePerformance";
     this._BaseProcessorUsageDevicePerformance.ClassType        = typeof(DiscoveryLight.UI.DeviceControls.DevicePerformanceControls._BaseProcessorUsageDevicePerformance);
     this._BaseProcessorUsageDevicePerformance.CurrentDevice    = null;
     this._BaseProcessorUsageDevicePerformance.CurrentSubDevice = null;
     this._BaseProcessorUsageDevicePerformance.Name             = "_BaseProcessorUsageDevicePerformance";
     this._BaseProcessorUsageDevicePerformance.Period           = System.TimeSpan.Parse("00:00:00.5000000");
     this._BaseProcessorUsageDevicePerformance.TokenSource      = cancellationTokenSource6;
     //
     // _BaseHardware
     //
     resources.ApplyResources(this, "$this");
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.Controls.Add(this._BaseProcessorUsageDevicePerformance);
     this.Controls.Add(this._BaseFreeStorageDevicePerformance);
     this.Controls.Add(this._BaseFreeRamDevicePerformance);
     this.Controls.Add(this.pic_Divisor_003);
     this.Controls.Add(this.pic_Divisor_002);
     this.Controls.Add(this.ComputerSystemProduct);
     this.Controls.Add(this.OperatingSystem);
     this.Controls.Add(this.ComputerSystem);
     this.Controls.Add(this.pic_Divisor_001);
     this.Name = "_BaseHardware";
     ((System.ComponentModel.ISupportInitialize)(this.pic_Divisor_001)).EndInit();
     ((System.ComponentModel.ISupportInitialize)(this.pic_Divisor_002)).EndInit();
     ((System.ComponentModel.ISupportInitialize)(this.pic_Divisor_003)).EndInit();
     this.ResumeLayout(false);
 }
Example #53
0
        public void Connect(string hostName, int port)
        {
            //How to secure socket connections with TLS/SSL:
            //http://msdn.microsoft.com/en-us/library/windows/apps/jj150597.aspx

            //Networking in Windows 8 Apps - Using StreamSocket for TCP Communication
            //http://blogs.msdn.com/b/metulev/archive/2012/10/22/networking-in-windows-8-apps-using-streamsocket-for-tcp-communication.aspx

            Socket = new StreamSocket();
            Socket.Control.KeepAlive = true;
            Socket.Control.NoDelay   = true;

            var host = new HostName(hostName);

            System.Threading.CancellationTokenSource tokenSource = new System.Threading.CancellationTokenSource();

            // https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj710176.aspx#content
            try
            {
                if (ConnectTimeout > TimeSpan.Zero)
                {
                    tokenSource.CancelAfter(ConnectTimeout);
                }

                var task = Socket.ConnectAsync(host, port.ToString(), SocketProtectionLevel.PlainSocket).AsTask(tokenSource.Token);
                task.ConfigureAwait(false);
                task.Wait();
                Connected = task.IsCompleted;
            }
            catch (AggregateException ex)
            {
                //https://msdn.microsoft.com/en-us/library/dd537614(v=vs.110).aspx?f=255&MSPPError=-2147217396

                Connected = false;
                if (ex.InnerException != null)
                //throw ex.InnerException;
                {
                    if (ex.Message.Contains("No such host is known") || ex.Message.Contains("unreachable host"))
                    {
                        throw new Exception("Socket Exception: " + ex.Message);
                    }
                    else
                    {
                        throw ex.InnerException;
                    }
                }
                else
                {
                    throw ex;
                }
            }
            finally {
                // https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource
                tokenSource.Dispose();
            }

            if (!Connected)
            {
                throw new TimeoutException("Connection timed out!");
            }
        }
Example #54
0
        /// <summary>
        ///
        /// This method will build a trigger that will transfer tokens from a holding account to a receiver.
        ///
        /// NOTE: UNDER CONSTRUCTION
        ///
        /// </summary>
        public static ISuccessTrigger BuildTokenTransferTrigger(this WonkaBizSource poSource, string psRecvAddress, long pnTransferAmt, string psWeb3Url = "", System.Threading.CancellationTokenSource poTokenSrc = null)
        {
            Nethereum.Web3.Web3 web3 = WonkaExtensions.GetWeb3(poSource.Password, psWeb3Url);

            var TransferTrigger = new WonkaEthEIP20TransferTrigger(web3, poSource.ContractAddress, psRecvAddress, pnTransferAmt, poTokenSrc);

            return(TransferTrigger);
        }
Example #55
0
		void RunDialog (string message)
		{
			if (disposed)
				return;
			
			string title;
			
			if (message == null) {
				title = GettextCatalog.GetString ("Waiting for debugger");
			} else {
				message = message.Trim ();
				int i = message.IndexOfAny (new char[] { '\n', '\r' });
				if (i > 0) {
					title = message.Substring (0, i).Trim ();
					message = message.Substring (i).Trim ();
				} else {
					title = message;
					message = null;
				}
			}

			var gm = new GenericMessage (title, message, cts.Token);
			gm.Buttons.Add (AlertButton.Cancel);
			gm.DefaultButton = 0;
			MessageService.GenericAlert (gm);
			cts = null;
			
			if (!disposed && UserCancelled != null) {
				UserCancelled (null, null);
			}
		}
Example #56
0
        public async Task TestCreateBackup()
        {
            IDatabaseService provider = CreateProvider();

            using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TestTimeout(TimeSpan.FromSeconds(600))))
            {
                ReadOnlyCollection <DatabaseFlavor> flavors = await provider.ListFlavorsAsync(cancellationTokenSource.Token);

                if (flavors.Count == 0)
                {
                    Assert.Inconclusive("The service did not report any flavors.");
                }

                DatabaseFlavor smallestFlavor = flavors.Where(i => i.Memory.HasValue).OrderBy(i => i.Memory).First();
                string         instanceName   = CreateRandomDatabaseInstanceName();
                DatabaseInstanceConfiguration configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName);
                DatabaseInstance instance = await provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                ReadOnlyCollection <Backup> initialBackups = await provider.ListBackupsForInstanceAsync(instance.Id, cancellationTokenSource.Token);

                Assert.IsNotNull(initialBackups);
                Assert.AreEqual(0, initialBackups.Count);

                string backupName        = CreateRandomBackupName();
                string backupDescription = "My backup";
                BackupConfiguration backupConfiguration = new BackupConfiguration(instance.Id, backupName, backupDescription);
                Backup backup = await provider.CreateBackupAsync(backupConfiguration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                Assert.AreEqual(BackupStatus.Completed, backup.Status);

                Backup backupCopy = await provider.GetBackupAsync(backup.Id, cancellationTokenSource.Token);

                Assert.AreEqual(backup.Id, backupCopy.Id);

                ReadOnlyCollection <Backup> allBackups = await provider.ListBackupsAsync(cancellationTokenSource.Token);

                ReadOnlyCollection <Backup> instanceBackups = await provider.ListBackupsForInstanceAsync(instance.Id, cancellationTokenSource.Token);

                Assert.IsTrue(allBackups.Count >= instanceBackups.Count);
                Assert.AreEqual(1, instanceBackups.Count);
                Assert.AreEqual(backupName, instanceBackups[0].Name);
                Assert.AreEqual(backupDescription, instanceBackups[0].Description);

                await provider.EnableRootUserAsync(instance.Id, cancellationTokenSource.Token);

                Assert.AreEqual(true, await provider.CheckRootEnabledStatusAsync(instance.Id, cancellationTokenSource.Token));

                await provider.RemoveDatabaseInstanceAsync(instance.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                ReadOnlyCollection <Backup> instanceBackupsAfterRemove = await provider.ListBackupsForInstanceAsync(instance.Id, cancellationTokenSource.Token);

                Assert.AreEqual(instanceBackups.Count, instanceBackupsAfterRemove.Count);

                configuration = new DatabaseInstanceConfiguration(smallestFlavor.Href, new DatabaseVolumeConfiguration(1), instanceName, new RestorePoint(backup.Id));
                instance      = await provider.CreateDatabaseInstanceAsync(configuration, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);

                Assert.AreEqual(false, await provider.CheckRootEnabledStatusAsync(instance.Id, cancellationTokenSource.Token));

                await provider.RemoveBackupAsync(backup.Id, cancellationTokenSource.Token);

                /* Cleanup
                 */
                await provider.RemoveDatabaseInstanceAsync(instance.Id, AsyncCompletionOption.RequestCompleted, cancellationTokenSource.Token, null);
            }
        }
Example #57
0
        CheckIfModulesNeedRebuilding(
            System.Type metaType)
        {
            // not all build modes need to determine if modules are up-to-date
            var evaluationRequiredAttr =
                metaType.GetCustomAttributes(typeof(EvaluationRequiredAttribute), false) as EvaluationRequiredAttribute[];
            if (0 == evaluationRequiredAttr.Length)
            {
                Log.DebugMessage("No Bam.Core.EvaluationRequired attribute on build mode metadata, assume rebuilds necessary");
                return false;
            }

            if (!evaluationRequiredAttr[0].Enabled)
            {
                Log.DebugMessage("Module evaluation disabled");
                return false;
            }

            Log.DebugMessage("Module evaluation enabled");

            var cancellationSource = new System.Threading.CancellationTokenSource();
            var cancellationToken = cancellationSource.Token;

            // LongRunning is absolutely necessary in order to achieve paralleism
            var creationOpts = System.Threading.Tasks.TaskCreationOptions.LongRunning;
            var continuationOpts = System.Threading.Tasks.TaskContinuationOptions.LongRunning;

            var threadCount = 1;
            var scheduler = new LimitedConcurrencyLevelTaskScheduler(threadCount);

            var factory = new System.Threading.Tasks.TaskFactory(
                    cancellationToken,
                    creationOpts,
                    continuationOpts,
                    scheduler);

            var graph = Graph.Instance;
            graph.MetaData = factory;

            foreach (var rank in graph.Reverse())
            {
                foreach (Module module in rank)
                {
                    module.Evaluate();
                }
            }

            return true;
        }
Example #58
0
 public static void CancelAfter(this System.Threading.CancellationTokenSource cancelSource, int millisecondsDelay);
Example #59
0
            protected override void Start()
            {
                try
                {
                    try
                    {
                        if (Status != StatusType.Queued)
                        {
                            throw new Exception("Stream has not been queued.");
                        }

                        base.Start();

                        _FileInfo = _DriveService.GetFile(FileId);

                        if (_FileInfo == null)
                        {
                            throw new Exception("File '" + FileId + "' no longer exists.");
                        }

                        if (String.IsNullOrEmpty(_FileInfo.FilePath))
                        {
                            throw new Exception("FileInfo.FilePath is blank.");
                        }
                    }
                    catch (Exception exception)
                    {
                        Log.Error(exception);
                    }

                    try
                    {
                        if (_FileInfo.IsGoogleDoc)
                        {
                            try
                            {
                                API.DriveService.WriteGoogleDoc(_FileInfo);

                                DriveService_ProgressChanged(DownloadStatus.Completed, 0, null);

                                return;
                            }
                            catch (Exception exception)
                            {
                                Log.Error(exception);
                            }
                        }
                        else if (String.IsNullOrEmpty(_FileInfo.DownloadUrl))
                        {
                            try
                            {
                                DriveService_ProgressChanged(DownloadStatus.Completed, 0, null);

                                return;
                            }
                            catch (Exception exception)
                            {
                                Log.Error(exception);
                            }
                        }
                        else if (_FileInfo.FileSize == 0)
                        {
                            try
                            {
                                API.DriveService.CreateFile(FileInfo);

                                DriveService_ProgressChanged(DownloadStatus.Completed, 0, null);

                                return;
                            }
                            catch (Exception exception)
                            {
                                Log.Error(exception);
                            }
                        }
                        else
                        {
                            FileInfoStatus fileInfoStatus = API.DriveService.GetFileInfoStatus(_FileInfo);

                            if (fileInfoStatus == FileInfoStatus.ModifiedOnDisk ||
                                fileInfoStatus == FileInfoStatus.OnDisk)
                            {
                                if (CheckIfAlreadyDownloaded)
                                {
                                    try
                                    {
                                        DriveService_ProgressChanged(DownloadStatus.Completed, 0, null);

                                        return;
                                    }
                                    catch (Exception exception)
                                    {
                                        Log.Error(exception);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Log.Error(exception);
                    }

                    try
                    {
                        API.DriveService.DeleteFile(_FileInfo.FilePath);

                        _DownloadFilePath = _FileInfo.FilePath + ".download";

                        Lock(_DownloadFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
                    }
                    catch (Exception exception)
                    {
                        Log.Error(exception);
                    }

                    try
                    {
                        _FileSize      = API.DriveService.GetLong(_FileInfo.FileSize);
                        _LastWriteTime = API.DriveService.GetFileLastWriteTime(_DownloadFilePath);
                    }
                    catch (Exception exception)
                    {
                        Log.Error(exception);
                    }

                    try
                    {
                        using (API.DriveService.Connection connection = API.DriveService.Connection.Create())
                        {
                            var request = new MediaDownloader(connection.Service);

                            request.ProgressChanged += DriveService_ProgressChanged;

                            if (ChunkSize <= 0)
                            {
                                request.ChunkSize = API.DriveService.Settings.DownloadFileChunkSize;
                            }
                            else if (ChunkSize > MediaDownloader.MaximumChunkSize)
                            {
                                request.ChunkSize = MediaDownloader.MaximumChunkSize;
                            }
                            else
                            {
                                request.ChunkSize = ChunkSize;
                            }

                            _CancellationTokenSource = new System.Threading.CancellationTokenSource();

                            System.Threading.Tasks.Task <IDownloadProgress> task = request.DownloadAsync(_FileInfo.DownloadUrl,
                                                                                                         _FileStream,
                                                                                                         _CancellationTokenSource.Token);
                        }
                    }
                    catch (Exception exception)
                    {
                        Log.Error(exception);
                    }
                }
                catch (Exception exception)
                {
                    try
                    {
                        _Status           = StatusType.Failed;
                        _ExceptionMessage = exception.Message;

                        DriveService_ProgressChanged(DownloadStatus.Failed, 0, exception);
                    }
                    catch
                    {
                        Debugger.Break();
                    }

                    Log.Error(exception);
                }
            }
        public void StreamThreadNormalWorkflowWithRebalancing()
        {
            List <ThreadState> allStates = new List <ThreadState>();
            var expectedStates           = new List <ThreadState>
            {
                ThreadState.CREATED,
                ThreadState.STARTING,
                ThreadState.PARTITIONS_ASSIGNED,
                ThreadState.RUNNING,
                ThreadState.PARTITIONS_REVOKED,
                ThreadState.PARTITIONS_ASSIGNED,
                ThreadState.RUNNING,
                ThreadState.PENDING_SHUTDOWN,
                ThreadState.DEAD
            };

            var source = new System.Threading.CancellationTokenSource();
            var config = new StreamConfig <StringSerDes, StringSerDes>();

            config.ApplicationId = "test";
            config.Guarantee     = ProcessingGuarantee.AT_LEAST_ONCE;
            config.PollMs        = 1;

            var consumeConfig = config.Clone();

            consumeConfig.ApplicationId = "consume-test";

            var serdes  = new StringSerDes();
            var builder = new StreamBuilder();

            builder.Stream <string, string>("topic").To("topic2");

            var topo = builder.Build();

            var supplier = new MockKafkaSupplier(4);
            var producer = supplier.GetProducer(consumeConfig.ToProducerConfig());
            var consumer = supplier.GetConsumer(consumeConfig.ToConsumerConfig("test-consum"), null);

            consumer.Subscribe("topic2");

            var thread = StreamThread.Create(
                "thread-0", "c0",
                topo.Builder, config,
                supplier, supplier.GetAdmin(config.ToAdminConfig("admin")),
                0) as StreamThread;

            allStates.Add(thread.State);
            thread.StateChanged += (t, o, n) =>
            {
                Assert.IsInstanceOf <ThreadState>(n);
                allStates.Add(n as ThreadState);
            };

            thread.Start(source.Token);
            // WAIT PARTITONS ASSIGNED
            System.Threading.Thread.Sleep(50);

            var thread2 = StreamThread.Create(
                "thread-1", "c1",
                topo.Builder, config,
                supplier, supplier.GetAdmin(config.ToAdminConfig("admin")),
                1) as StreamThread;

            thread2.Start(source.Token);
            // WAIT PARTITONS REBALANCING
            System.Threading.Thread.Sleep(50);

            producer.Produce("topic", new Confluent.Kafka.Message <byte[], byte[]>
            {
                Key   = serdes.Serialize("key1"),
                Value = serdes.Serialize("coucou")
            });
            //WAIT STREAMTHREAD PROCESS MESSAGE
            System.Threading.Thread.Sleep(100);
            var message = consumer.Consume(100);

            // 2 CONSUMER FOR THE SAME GROUP ID => TOPIC WITH 4 PARTITIONS
            Assert.AreEqual(2, thread.ActiveTasks.Count());
            Assert.AreEqual(2, thread2.ActiveTasks.Count());

            source.Cancel();
            thread.Dispose();
            thread2.Dispose();

            Assert.AreEqual("key1", serdes.Deserialize(message.Message.Key));
            Assert.AreEqual("coucou", serdes.Deserialize(message.Message.Value));
            Assert.AreEqual(expectedStates, allStates);
            // Destroy in memory cluster
            supplier.Destroy();
        }