public static async Task<Processor> AddProcessorAsync(string BaseAddress, Processor processor)
        {
            Uri uri = new Uri(string.Concat(BaseAddress, "processor/", processor.Name));
            HttpClient client = new HttpClient();
            await doPreProcessing(client);

            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, uri);
            message.Content = new StringContent(JsonConvert.SerializeObject(processor), Encoding.UTF8, "application/json");
            return await GetHttpResponseAsProcessorAsync(await client.SendAsync(message));
        }
        /*
        this sends test events to processor (event hubs) 
        it should be removed from deployment version
        */

        public static void SendProcessorTestMessages(Processor processor, int NumOfMessages, int NumOfPublishers)
        {
            try
            {
                InternalFunctions.SendTestEventsToProcessorHubsAsync(processor, NumOfMessages, NumOfPublishers).Wait();
            }
            catch (AggregateException ae)
            {
                ae.ThrowPowerShell();
            }
        }
 public static Processor UpdateProcessor(string BaseAddress, Processor processor)
 {
     try
     {
         return InternalFunctions.UpdateProcessorAsync(BaseAddress, processor).Result;
     }
     catch (AggregateException ae)
     {
         ae.ThrowPowerShell();
     }
     return null;
 }
        public async Task<Processor> GetAssignedProcessorAsync()
        {
            // do we have it?
            if (null != this.assignedProcessor)
            {
                return this.assignedProcessor;
            }

            //is it in state
            Processor processor = await this.GetAssignedProcessorFromState();
            if (processor != null)
            {
                this.assignedProcessor = processor;
                return this.assignedProcessor;
            }

            // must be a new instance (or if we are debugging in VS.NET then use a manually created one)
#if _VS_DEPLOY
    // in this mode we load a mock up Processor and use it.
    // this mode is used only during single processor (set as a startup)
    // project 
            TraceWriter.TraceMessage("Processor is running in VS.NET Deploy Mode");

            var processor1 = new Processor()
            {
                Name = "One"
            };
            processor1.Hubs.Add(new EventHubDefinition()
            {
                ConnectionString = "// Service Bus Connection String //",
                EventHubName = "eh01",
                ConsumerGroupName = ""
            });

            
            return processor1;
#else
            if (null != this.ServiceInitializationParameters.InitializationData)
            {
                Processor initProcessor = Processor.FromBytes(this.ServiceInitializationParameters.InitializationData);
                Trace.WriteLine(
                    string.Format(
                        string.Format(
                            "Replica {0} Of Application {1} Got Processor {2}",
                            this.ServiceInitializationParameters.ReplicaId,
                            this.ServiceInitializationParameters.CodePackageActivationContext.ApplicationName,
                            initProcessor.Name)));

                this.assignedProcessor = await this.SaveProcessorToState(initProcessor); // this sets m_assignedprocessor

                return this.assignedProcessor;
            }

#endif
            throw new InvalidOperationException("Failed to load assigned processor from saved state and initialization data");
        }
        private async Task<Processor> SaveProcessorToState(Processor processor)
        {
            IReliableDictionary<string, string> dict = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, string>>(DefDictionary);

            string sValue = processor.AsJsonString();
            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                await dict.AddOrUpdateAsync(
                    tx,
                    AssignedProcessorEntryName,
                    sValue,
                    (k, v) => { return sValue; });

                await tx.CommitAsync();
            }
            return processor;
        }
        // updates the current assigned processor (the # of event hubs)
        public async Task SetAssignedProcessorAsync(Processor newProcessor)
        {
            // save the processor (replacing whatever we had)
            this.assignedProcessor = await this.SaveProcessorToState(newProcessor);

            // if we are in working mode, refresh listeners
            if (this.WorkManager.WorkManagerStatus == WorkManagerStatus.Working)
            {
                await this.RefreshListenersAsync();
            }
        }
        public void SafeUpdate(Processor other, bool OverwriteServiceFabricNames = false, bool OverrideHubsConfig = false)
        {
            if (this.Name != other.Name)
            {
                throw new InvalidOperationException(string.Format("Safe update failed: processor name {0}  != other name {1}", this.Name, other.Name));
            }

            if (this.ServiceFabricAppTypeName != other.ServiceFabricAppTypeName)
            {
                this.ServiceFabricAppTypeName = other.ServiceFabricAppTypeName;
            }

            if (OverwriteServiceFabricNames)
            {
                if (this.ServiceFabricAppTypeVersion != other.ServiceFabricAppTypeVersion)
                {
                    this.ServiceFabricAppTypeVersion = other.ServiceFabricAppTypeVersion;
                }

                if (this.ServiceFabricAppInstanceName != other.ServiceFabricAppInstanceName)
                {
                    this.ServiceFabricAppInstanceName = other.ServiceFabricAppInstanceName;
                }

                if (this.ServiceFabricServiceName != other.ServiceFabricServiceName)
                {
                    this.ServiceFabricServiceName = other.ServiceFabricServiceName;
                }
            }

            if (OverrideHubsConfig)
            {
                this.Hubs = other.Hubs;
            }


            this.ProcessorStatus |= other.ProcessorStatus;

            if (this.ErrorMessage != other.ErrorMessage)
            {
                this.ErrorMessage = other.ErrorMessage;
            }
        }
        public static async Task SendTestEventsToProcessorHubsAsync(
            Processor processor,
            int NumOfMessages,
            int NumOfPublishers)
        {
            string PublisherNameFormat = "sensor{0}";
            int NumberOfMessagesPerPublisher = NumOfMessages/NumOfPublishers;
            List<Task> tasks = new List<Task>();

            for (int i = 1; i <= NumOfPublishers; i++)
            {
                foreach (EventHubDefinition hub in processor.Hubs)
                {
                    string publisherName = string.Format(PublisherNameFormat, i);
                    tasks.Add(SendToEventsToEventHubAsync(NumberOfMessagesPerPublisher, publisherName, hub));
                }
            }
            await Task.WhenAll(tasks);
        }
 public async Task Update(Processor newProcessor)
 {
     await this.ProcessorService.SetAssignedProcessorAsync(newProcessor);
 }