public static string SendEndSignal(string configUUID, string host_addr, CustomLogger clog) { // Create a request using a URL that can receive a post. WebRequest analysisRequest = WebRequest.Create(host_addr); analysisRequest.Method = "GET"; // Set the Config-GUID header analysisRequest.Headers["Config-GUID"] = configUUID; string analysisResponse = "[ProducerAzure] Default analysis response"; try { HttpWebResponse endResponse = (HttpWebResponse)analysisRequest.GetResponse(); if (endResponse.StatusCode != HttpStatusCode.OK) { clog.RawLog(LogLevel.ERROR, "[ProducerAzure] Bad consumer analysis status"); } using (Stream responseStream = endResponse.GetResponseStream()) { // Open the stream using a StreamReader for easy access and read the content analysisResponse = new StreamReader(responseStream).ReadToEnd(); } //analysisResponse = endResponse.StatusDescription; endResponse.Close(); } catch (WebException) { clog.RawLog(LogLevel.ERROR, "Got Web exception!"); } return(analysisResponse); }
public static string SendConsumerConfig(string configUUID, string configData, string host_addr, CustomLogger clog) { // Create a request using a URL that can receive a post. WebRequest configRequest = WebRequest.Create(host_addr); configRequest.Method = "POST"; // Convert POST data to a byte array. byte[] byteConfigArray = Encoding.UTF8.GetBytes(configData); // Set the ContentType property of the WebRequest. configRequest.ContentType = "application/json"; // Set the Config-GUID header configRequest.Headers["Config-GUID"] = configUUID; // Set the ContentLength property of the WebRequest. configRequest.ContentLength = byteConfigArray.Length; // Get the request stream. Stream configDataStream = configRequest.GetRequestStream(); // Write the data to the request stream. configDataStream.Write(byteConfigArray, 0, byteConfigArray.Length); // Close the Stream object. configDataStream.Close(); // Get the response. string consumerResponse = "[SendConsumerConfig] Default Consumer Response"; try { HttpWebResponse response = (HttpWebResponse)configRequest.GetResponse(); using (Stream cfgDataStream = response.GetResponseStream()) { // Open the stream using a StreamReader for easy access & Read content. consumerResponse = new StreamReader(cfgDataStream).ReadToEnd(); clog.RawLog(LogLevel.INFO, consumerResponse); } response.Close(); } catch (WebException) { clog.RawLog(LogLevel.ERROR, "[SendConsumerConfig] Got WebException when sending consumer config!"); } return(consumerResponse); }
// Sends one data record public void SendRecord(IProducerToConsumerAdpt adpt, string uuid, string receiver_addr, CustomLogger log) { JObject record = recordGenerator.GenerateRecord(); log.RawLog(LogLevel.INFO, $"[Producer] Generated Data Record: {record}"); adpt.Send(record, uuid, receiver_addr); //TODO: Uncomment me to test connection with consumer! this.Amount--; }
public static void ProduceAndSend(string configUUID, FullConfig producerConfig, string host_addr, CustomLogger log) { Producer producer = new Producer(producerConfig.records_count, producerConfig.fields, producerConfig.error_rate); // Generate and Send Data Records host_addr = host_addr ?? LOCAL_HOST; ProducerToDefaultConsumerAddpt adapter = new ProducerToDefaultConsumerAddpt(); for (int counter = producerConfig.records_count; counter > 0; counter--) { try { producer.SendRecord(adapter, configUUID, host_addr, log); } catch (WebException webExcp) { log.RawLog(LogLevel.ERROR, $"[ERROR] Got WebException {webExcp} while sending {counter}th record!"); } } ; }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req) { customLog.RawLog(LogLevel.INFO, "ProducerAzure HTTP trigger function start to process a request..."); // Get Configuration json string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic httpData = JsonConvert.DeserializeObject(requestBody); JObject jConfigData = (JObject)httpData; // Parse Configuration json string?cfgName = (string)jConfigData.GetValue("config_name"); if (cfgName == null) { customLog.RawLog(LogLevel.WARN, "[ProducerAzure] Got empty Config Name, using default name"); cfgName = "Default Config"; } string configHostAddr = (string)jConfigData.GetValue("config_url"); if (configHostAddr == null) { return(new BadRequestObjectResult( "[ProducerAzure] Please specify consumer CONFIG endpoint in the config input!")); } string dataHostAddr = (string)jConfigData.GetValue("data_url"); if (dataHostAddr == null) { return(new BadRequestObjectResult( "[ProducerAzure] Please specify consumer DATA endpoint in the config input!")); } // NOTE: consumer behavior differs only in whether it sends back analysis string team = (string)jConfigData.GetValue("consumer_team"); if (team == null) { return(new BadRequestObjectResult( "[ProducerAzure] Please specify WHICH consumer you are procuding to!")); } string analysisHostAddr = (string)jConfigData.GetValue("analysis_url"); if (analysisHostAddr == null & team == "A") { return(new BadRequestObjectResult( "[ProducerAzure] Please specify consumer ANALYSIS endpoint in the config input!")); } JObject consumerCfg = (JObject)jConfigData.GetValue("consumer_config"); if (consumerCfg == null) { return(new BadRequestObjectResult( "[ProducerAzure] Consumer config cannot be null!")); } JObject producerCfg = (JObject)jConfigData.GetValue("producer_config"); FullConfig?parsedProducerCfg = Util.ParseConfig(producerCfg); if (parsedProducerCfg == null) { return(new BadRequestObjectResult( "[ProducerAzure] Incorrect Producer config format!")); } customLog.RawLog(LogLevel.INFO, $"[ProducerAzure] Successfully parsed configuration: {cfgName}"); // Send ConsumerConfig as JSON string string configUUID = Guid.NewGuid().ToString(); string configResponse = MessageSender.SendConsumerConfig(configUUID, consumerCfg.ToString(), configHostAddr, customLog); customLog.RawLog(LogLevel.INFO, $"[ProducerAzure] Config Response: {configResponse}"); // Producer generates data and send to consumer customLog.RawLog(LogLevel.INFO, "[ProducerAzure] Start to generate data and send to consumer..."); MessageSender.ProduceAndSend(configUUID, parsedProducerCfg.Value, dataHostAddr, customLog); // Producer sends end signal string?analysisResponse = null; if (team == "A") { analysisResponse = MessageSender.SendEndSignal(configUUID, analysisHostAddr, customLog); customLog.RawLog(LogLevel.INFO, $"[ProducerAzure] End Signal Response: {analysisResponse}"); } return((cfgName != null & dataHostAddr != null & configHostAddr != null) ? (ActionResult) new OkObjectResult( Util.FormatProducerResult(configUUID, cfgName, jConfigData.ToString(), analysisResponse)) : new BadRequestObjectResult( $"[ProducerAzure] Got exception during data transfer!")); }