private async Task CreateBandActorTask(BandActorGenerator bag, CancellationToken cancellationToken)
        {
            CryptoRandom random = new CryptoRandom();

            while (!cancellationToken.IsCancellationRequested && this.MaxBandsToCreatePerServiceInstance > 0)
            {
                bool created = false;
                while (!created && !cancellationToken.IsCancellationRequested)
                {
                    ActorId bandActorId;
                    ActorId doctorActorId;
                    int     randomCountyId = -1;
                    string  doctorName     = null;

                    randomCountyId = random.Next(0, bag.doctorsPerCounty.Keys.Count);
                    doctorName     = bag.GetRandomName(random);

                    CountyRecord randomCountyRecord = bag.doctorsPerCounty.Keys.ElementAt(randomCountyId);
                    BandInfo     bandActorInfo      = bag.GetRandomHealthStatus(randomCountyRecord, random);

                    try
                    {
                        bandActorId   = new ActorId(Guid.NewGuid());
                        doctorActorId = new ActorId(bandActorInfo.DoctorId);

                        IDoctorActor docActor = ActorProxy.Create <IDoctorActor>(doctorActorId, this.DoctorServiceUri);
                        await docActor.NewAsync(doctorName, randomCountyRecord);

                        IBandActor bandActor = ActorProxy.Create <IBandActor>(bandActorId, this.ActorServiceUri);
                        await bandActor.NewAsync(bandActorInfo);
                    }

                    catch (Exception e)
                    {
                        ServiceEventSource.Current.ServiceMessage(this, "Failed to iniitalize band or doctor. {0}", e.ToString());
                    }

                    created = true;
                }

                this.MaxBandsToCreatePerServiceInstance--;
                await Task.Delay(100, cancellationToken);
            }
        }
Esempio n. 2
0
 public DoctorCreationRecord(string name, Guid id, CountyRecord countyInfo)
 {
     this.DoctorName = name;
     this.DoctorId   = id;
     this.CountyInfo = countyInfo;
 }
        private async Task CreateDeviceActorAsync(DeviceActorGenerator bag, CancellationToken cancellationToken)
        {
            //TODO: Should be able to replace this with a normal Random
            CryptoRandom random = new CryptoRandom();

            while (!cancellationToken.IsCancellationRequested && this.MaxDevicesToCreatePerService > 0)
            {
                bool created = false;
                while (!created && !cancellationToken.IsCancellationRequested)
                {
                    ActorId bandActorId;
                    Guid    doctorId;
                    int     randomCountyId = -1;
                    string  doctorName     = null;

                    randomCountyId = random.Next(0, bag.doctorsPerCounty.Keys.Count);
                    doctorName     = bag.GetRandomName(random);

                    CountyRecord randomCountyRecord = bag.doctorsPerCounty.Keys.ElementAt(randomCountyId);
                    DeviceInfo   bandActorInfo      = bag.GetRandomHealthStatus(randomCountyRecord, random);

                    try
                    {
                        bandActorId = new ActorId(Guid.NewGuid());
                        doctorId    = bandActorInfo.DoctorId;
                        //doctorId = new ActorId(bandActorInfo.DoctorId);

                        var dcr = new DoctorCreationRecord(doctorName, doctorId, randomCountyRecord);
                        ServicePartitionKey key = new ServicePartitionKey(HashUtil.getLongHashCode(bandActorInfo.DoctorId.ToString()));

                        await FabricHttpClient.MakePostRequest <DoctorCreationRecord>(
                            this.DoctorServiceUri,
                            key,
                            "DoctorEndpoint",
                            "/doctor/new/" + doctorId,
                            dcr,
                            SerializationSelector.PBUF,
                            cancellationToken
                            );

                        IDeviceActor bandActor = ActorProxy.Create <IDeviceActor>(bandActorId, ActorServiceUri);
                        await bandActor.NewAsync(bandActorInfo);

                        ServiceEventSource.Current.Message("Actor created {0} verifying...", bandActorId);

                        created = true;
                    }

                    catch (Exception e)
                    {
                        ServiceEventSource.Current.ServiceMessage(this.Context, "Failed to iniitalize device or doctor. {0}", e.ToString());
                    }
                }

                this.MaxDevicesToCreatePerService--;

                ServiceEventSource.Current.ServiceMessage(this.Context, "Created Actors, {0} remaining", this.MaxDevicesToCreatePerService);

                await Task.Delay(100, cancellationToken);
            }
        }
Esempio n. 4
0
        private static async Task VerifyActors(
            HealthIndexCalculator hic, ActorId bandActorId, string doctorName, CountyRecord randomCountyRecord, BandInfo bandActorInfo, IDoctorActor docActor,
            IBandActor bandActor, CancellationToken ct)
        {
            ServiceEventSource.Current.Message("Verifying Actor {0}", bandActorId);

            bool bandVerified     = false;
            bool doctorVerified   = false;
            int  bandErrorCount   = 0;
            int  doctorErrorCount = 0;

            while (!ct.IsCancellationRequested && !bandVerified && !doctorVerified)
            {
                await Task.Delay(100);

                if (!bandVerified)
                {
                    try
                    {
                        BandDataViewModel view = await bandActor.GetBandDataAsync();

                        if (view.PersonName == bandActorInfo.PersonName)
                        {
                            if (view.CountyInfo == bandActorInfo.CountyInfo)
                            {
                                if (view.DoctorId == bandActorInfo.DoctorId)
                                {
                                    if (view.PersonId == bandActorId.GetGuidId())
                                    {
                                        if (view.HealthIndexValue == bandActorInfo.HealthIndex)
                                        {
                                            bandVerified = true;
                                            ServiceEventSource.Current.Message("Band actor verified.");
                                        }
                                        else
                                        {
                                            await bandActor.NewAsync(bandActorInfo);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        bandErrorCount++;
                        ServiceEventSource.Current.Message("Exception Count {0} verifying band actor, retrying. {1}", bandErrorCount, e);
                    }
                }
                else
                {
                    ServiceEventSource.Current.Message("band already verified, skipping");
                }


                if (!doctorVerified)
                {
                    try
                    {
                        Tuple <CountyRecord, string> info = await docActor.GetInfoAndNameAsync();

                        if (info.Item2 == String.Format("Dr. {0}", doctorName) &&
                            info.Item1 == randomCountyRecord)
                        {
                            doctorVerified = true;
                            ServiceEventSource.Current.Message("Doctor actor verified.");
                        }
                        else
                        {
                            await docActor.NewAsync(doctorName, randomCountyRecord);
                        }
                    }
                    catch (Exception e)
                    {
                        doctorErrorCount++;
                        ServiceEventSource.Current.Message("Exception Count {0} verifying doctor actor, retrying. {1}", doctorErrorCount, e);
                    }
                }
                else
                {
                    ServiceEventSource.Current.Message("doctor already verified, skipping");
                }
            }
        }
Esempio n. 5
0
        public async Task SendHealthReportToCountyAsync()
        {
            try
            {
                ConditionalValue <long> healthReportCountResult = await this.StateManager.TryGetStateAsync <long>("HealthReportCount");

                if (healthReportCountResult.HasValue)
                {
                    string name = await this.StateManager.GetStateAsync <string>("Name");

                    CountyRecord countyRecord = await this.StateManager.GetStateAsync <CountyRecord>("CountyRecord");

                    long healthReportCount = healthReportCountResult.Value;
                    Dictionary <Guid, DoctorPatientState> patientHealthReports =
                        await this.StateManager.GetStateAsync <Dictionary <Guid, DoctorPatientState> >("PersonHealthStatuses");

                    if (healthReportCount > 0)
                    {
                        DoctorStatsViewModel payload = new DoctorStatsViewModel(
                            patientHealthReports.Count,
                            healthReportCount,
                            await this.GetAveragePatientHealthInfoAsync(),
                            name);

                        ServicePartitionKey partitionKey = new ServicePartitionKey(countyRecord.CountyId);
                        Guid id = this.Id.GetGuidId();

                        ServicePartitionClient <HttpCommunicationClient> servicePartitionClient =
                            new ServicePartitionClient <HttpCommunicationClient>(
                                this.clientFactory,
                                this.countyServiceInstanceUri,
                                partitionKey);

                        await servicePartitionClient.InvokeWithRetryAsync(
                            client =>
                        {
                            Uri serviceAddress = new Uri(
                                client.BaseAddress,
                                string.Format(
                                    "county/health/{0}/{1}",
                                    partitionKey.Value.ToString(),
                                    id));

                            HttpWebRequest request   = WebRequest.CreateHttp(serviceAddress);
                            request.Method           = "POST";
                            request.ContentType      = "application/json";
                            request.KeepAlive        = false;
                            request.Timeout          = (int)client.OperationTimeout.TotalMilliseconds;
                            request.ReadWriteTimeout = (int)client.ReadWriteTimeout.TotalMilliseconds;

                            using (Stream requestStream = request.GetRequestStream())
                            {
                                using (BufferedStream buffer = new BufferedStream(requestStream))
                                {
                                    using (StreamWriter writer = new StreamWriter(buffer))
                                    {
                                        JsonSerializer serializer = new JsonSerializer();
                                        serializer.Serialize(writer, payload);
                                        buffer.Flush();
                                    }

                                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                                    {
                                        ActorEventSource.Current.Message("Doctor Sent Data to County: {0}", serviceAddress);
                                        return(Task.FromResult(true));
                                    }
                                }
                            }
                        }
                            );
                    }
                }
            }
            catch (Exception e)
            {
                ActorEventSource.Current.Message("DoctorActor failed to send health report to county service Outer Exception: {0}", e.ToString());
            }
        }