public async Task <VisualObject> ReadAsync(CancellationToken cancellationToken)
        {
            VisualObject          visualObject = null;
            IReliableStateManager stateManager = ReliableCollectionsExtensions.GetReliableStateManager();

            if (stateManager == null)
            {
                Console.WriteLine($"Failed to get StateManager");
                return(VisualObject.CreateRandom(Guid.NewGuid().ToString()));
            }

            using (var txn = stateManager.CreateTransaction())
            {
                var visualObjectStore = await stateManager.GetOrAddAsync <IReliableDictionary <string, VisualObject> >(VISUAL_OBJECT_STORE);

                var enumerable = await visualObjectStore.CreateEnumerableAsync(txn);

                var enumerator = enumerable.GetAsyncEnumerator();

                while (await enumerator.MoveNextAsync(cancellationToken))
                {
                    visualObject = enumerator.Current.Value;
                }
                if (visualObject == null)
                {
                    Console.WriteLine($"Visual object does not exist. Creating new object.");
                    visualObject = VisualObject.CreateRandom(Guid.NewGuid().ToString());
                    await visualObjectStore.AddAsync(txn, visualObject.Name, visualObject);
                }
                await txn.CommitAsync();
            }

            return(visualObject);
        }
        public async Task <VisualObject> ReadAsync(CancellationToken cancellationToken)
        {
            if (!File.Exists(this.stateFilePath))
            {
                Console.WriteLine($"State file {this.stateFilePath} does not exist. Creating new object.");
                var obj = VisualObject.CreateRandom(Guid.NewGuid().ToString());
                await WriteAsync(obj, cancellationToken);
            }

            // file exists read it
            byte[] data = null;
            using (var stream = new FileStream(
                       this.stateFilePath,
                       FileMode.Open,
                       FileAccess.Read,
                       FileShare.None,
                       4096,
                       true))
            {
                data = new byte[stream.Length];
                await stream.ReadAsync(data, 0, data.Length, cancellationToken);
            }

            // deserialize
            try
            {
                return(this.Deserialize(data));
            }
            catch (Exception e)
            {
                Console.WriteLine($"Read error. recreating the object. {e.ToString()}");
                return(VisualObject.CreateRandom(Guid.NewGuid().ToString()));
            }
        }
Beispiel #3
0
        protected override async Task OnActivateAsync()
        {
            VisualObject newObject = VisualObject.CreateRandom(this.Id.ToString());

            ActorEventSource.Current.ActorMessage(this, "StateCheck {0}", (await this.StateManager.ContainsStateAsync(StatePropertyName)).ToString());

            VisualObject result = await this.StateManager.GetOrAddStateAsync <VisualObject>(StatePropertyName, newObject);

            this.jsonString = result.ToJson();

            // ACTOR MOVEMENT REFRESH
            this.updateTimer = this.RegisterTimer(this.MoveObject, null, TimeSpan.FromMilliseconds(10), TimeSpan.FromMilliseconds(10));
            return;
        }