예제 #1
0
        /// <summary>
        /// Generates patients using the provided options.
        /// </summary>
        /// <param name="options">The options to use to generate patients.</param>
        /// <returns>Returns a GenerationResponse.</returns>
        public GenerationResponse GeneratePatients(Demographic options)
        {
            GenerationResponse response = new GenerationResponse();

            IEnumerable <IResultDetail> details = ValidationUtil.ValidateMessage(options);

            if (details.Count(x => x.Type == ResultDetailType.Error) > 0)
            {
                response.Messages.AddRange(details.Select(x => x.Message));
                response.HasErrors = true;
            }
            else
            {
                // no validation errors, save the options
                persistenceService?.Save(options);

                // send to fhir endpoints
                fhirSenderService?.Send(options);

                // send to hl7v2 endpoints
                hl7v2SenderService?.Send(options);

                // send to hl7v3 endpoints
                hl7v3SenderService?.Send(options);
            }

            return(response);
        }
예제 #2
0
        /// <summary>
        /// Generates patients using a randomized dataset.
        /// </summary>
        /// <param name="count">The number of patients to generate.</param>
        /// <returns>Returns a GenerationResponse.</returns>
        public GenerationResponse GeneratePatients(int count)
        {
            GenerationResponse response = new GenerationResponse();

            List <Patient> patients = new List <Patient>();

            for (int i = 0; i < count; i++)
            {
                patients.Add(randomizerService.GetRandomPatient());
            }

            foreach (Patient patient in patients)
            {
                // send to fhir endpoints
                fhirSenderService?.Send(patient);

                // send to hl7v2 endpoints
                hl7v2SenderService?.Send(patient);

                // send to hl7v3 endpoints
                hl7v3SenderService?.Send(patient);
            }

            return(response);
        }
예제 #3
0
        /// <summary>
        /// Try to compile using the server. Returns a null-containing Task if a response
        /// from the server cannot be retrieved.
        /// </summary>
        private static async Task <GenerationResponse> TryGeneration(NamedPipeClientStream pipeStream,
                                                                     GenerationRequest request,
                                                                     CancellationToken cancellationToken)
        {
            GenerationResponse response;

            using (pipeStream)
            {
                // Write the request
                try
                {
                    _log.Debug("Begin writing request");
                    await request.WriteAsync(pipeStream, cancellationToken).ConfigureAwait(false);

                    _log.Debug("End writing request");
                }
                catch (Exception e)
                {
                    _log.Error($"Error writing build request. {e.Message}", e);
                    return(new RejectedGenerationResponse());
                }

                // Wait for the compilation and a monitor to detect if the server disconnects
                var serverCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

                _log.Debug("Begin reading response");

                var responseTask = GenerationResponse.ReadAsync(pipeStream, serverCts.Token);
                var monitorTask  = CreateMonitorDisconnectTask(pipeStream, "client", serverCts.Token);
                await Task.WhenAny(responseTask, monitorTask).ConfigureAwait(false);

                _log.Debug("End reading response");

                if (responseTask.IsCompleted)
                {
                    // await the task to log any exceptions
                    try
                    {
                        response = await responseTask.ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        _log.Error("Error reading response", e);
                        response = new RejectedGenerationResponse();
                    }
                }
                else
                {
                    _log.Debug("Server disconnect");
                    response = new RejectedGenerationResponse();
                }

                // Cancel whatever task is still around
                serverCts.Cancel();
                Debug.Assert(response != null);
                return(response);
            }
        }
예제 #4
0
        public static async void GetGeneration(GetGenerationCallback callback)
        {
            HttpResponseMessage res = await client.GetAsync(url + "/getGeneration");

            res.EnsureSuccessStatusCode();
            string content = await res.Content.ReadAsStringAsync();

            GenerationResponse generationRespose = JsonUtility.FromJson <GenerationResponse>(content);

            callback(generationRespose.generation);
        }
예제 #5
0
        /// <summary>
        /// Shutting down the server is an inherently racy operation.  The server can be started or stopped by
        /// external parties at any time.
        ///
        /// This function will return success if at any time in the function the server is determined to no longer
        /// be running.
        /// </summary>
        internal async Task <int> RunShutdownAsync(string pipeName, bool waitForProcess = true, TimeSpan?timeout = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (WasServerRunning(pipeName) == false)
            {
                // The server holds the mutex whenever it is running, if it's not open then the
                // server simply isn't running.
                return(CommonGenerator.Succeeded);
            }

            try
            {
                var realTimeout = timeout != null
                                        ? (int)timeout.Value.TotalMilliseconds
                                        : Timeout.Infinite;
                using (var client = await ConnectForShutdownAsync(pipeName, realTimeout).ConfigureAwait(false))
                {
                    var request = GenerationRequest.CreateShutdown();
                    await request.WriteAsync(client, cancellationToken).ConfigureAwait(false);

                    var response = await GenerationResponse.ReadAsync(client, cancellationToken).ConfigureAwait(false);

                    var shutdownResponse = (ShutdownGenerationResponse)response;

                    if (waitForProcess)
                    {
                        try
                        {
                            var process = Process.GetProcessById(shutdownResponse.ServerProcessId);
                            process.WaitForExit();
                        }
                        catch (Exception)
                        {
                            // There is an inherent race here with the server process.  If it has already shutdown
                            // by the time we try to access it then the operation has succeed.
                        }
                    }
                }

                return(CommonGenerator.Succeeded);
            }
            catch (Exception)
            {
                if (WasServerRunning(pipeName) == false)
                {
                    // If the server was in the process of shutting down when we connected then it's reasonable
                    // for an exception to happen.  If the mutex has shutdown at this point then the server
                    // is shut down.
                    return(CommonGenerator.Succeeded);
                }

                return(CommonGenerator.Failed);
            }
        }
        /// <summary>
        /// Creates the barcode and save the barcode image to the local path provided
        /// Examples:
        /// Save(SaveLocation.Local, "c:\\code128.png", ImageFormat.PNG);
        /// Save(SaveLocation.Server, "test-1234.png", ImageFormat.PNG);
        /// </summary>
        /// <param name="SaveLocation">Location where barcode needs to be saved, local or Saaspose server</param>
        /// <param name="outputPath">Location where barcode is to be saved</param>
        /// <param name="ImageFormat">Image format</param>
        public GenerationResponse Save(SaveLocation saveLocation, string outputPath, ImageFormat imageFormat)
        {
            PerformValidations();

            // If image needs to be saved locally
            if (saveLocation == SaveLocation.Local)
            {
                // Build URL with querystring request parameters
                string uri = UriBuilder("");

                // Send the request to Saaspose server
                Stream responseStream = Utils.ProcessCommand(Utils.Sign(uri), "GET");
                // Read the response, in this case the response is a Stream that contains barcode image
                // So, just save the response stream to a local image file
                using (Stream file = File.OpenWrite(outputPath))
                {
                    CopyStream(responseStream, file);
                }
                responseStream.Close();

                GenerationResponse response = new GenerationResponse();
                response.Status = "OK";
                return(response);
            }
            else if (saveLocation == SaveLocation.Server)
            {
                // Build URL with querystring request parameters
                string uri = UriBuilder(outputPath);

                // Send the request to Saaspose server
                Stream       responseStream = Utils.ProcessCommand(Utils.Sign(uri), "PUT");
                StreamReader reader         = new StreamReader(responseStream);
                // Read the response
                string strJSON = reader.ReadToEnd();


                //Parse the json string to JObject
                JObject parsedJSON = JObject.Parse(strJSON);


                //Deserializes the JSON to a object.
                GenerationResponse barcodeGenerationResponse = JsonConvert.DeserializeObject <GenerationResponse>(parsedJSON.ToString());

                return(barcodeGenerationResponse);
            }

            // Return null, if anything goes wrong
            return(null);
        }
        /// <summary>
        /// Creates the barcode and save the barcode image to the supplied stream
        /// Example: Save(SaveLocation.Local, imgStream, ImageFormat.PNG);
        /// </summary>
        /// <param name="imageStream">Stream where image will be saved</param>
        /// <param name="ImageFormat">Image format</param>
        public GenerationResponse Save(SaveLocation saveLocation, Stream imageStream, ImageFormat imageFormat)
        {
            PerformValidations();

            // Build URL with querystring request parameters
            string uri = UriBuilder("");

            // Send the request to Saaspose server
            Stream responseStream = Utils.ProcessCommand(Utils.Sign(uri), "GET");

            // Read the response, in this case the response is a Stream that contains barcode image
            // Just copy the response stream to the image stream that user passed
            CopyStream(responseStream, imageStream);
            // close the response stream
            responseStream.Close();

            GenerationResponse response = new GenerationResponse();

            response.Status = "OK";
            return(response);
        }
        /// <summary>
        /// Creates the barcode and save the barcode image to the local path provided
        /// Examples:
        /// Save(SaveLocation.Local, "c:\\code128.png", ImageFormat.PNG);
        /// Save(SaveLocation.Server, "test-1234.png", ImageFormat.PNG);
        /// </summary>
        /// <param name="SaveLocation">Location where barcode needs to be saved, local or Saaspose server</param>
        /// <param name="outputPath">Location where barcode is to be saved</param>
        /// <param name="ImageFormat">Image format</param>
        public GenerationResponse Save(SaveLocation saveLocation, string outputPath, ImageFormat imageFormat)
        {
            PerformValidations();
            
            // If image needs to be saved locally
            if (saveLocation == SaveLocation.Local)
            {
                // Build URL with querystring request parameters
                string uri = UriBuilder("");
                
                // Send the request to Saaspose server
                Stream responseStream = Utils.ProcessCommand(Utils.Sign(uri), "GET");
                // Read the response, in this case the response is a Stream that contains barcode image
                // So, just save the response stream to a local image file
                using (Stream file = File.OpenWrite(outputPath))
                {
                    CopyStream(responseStream, file);
                }
                responseStream.Close();

                GenerationResponse response = new GenerationResponse();
                response.Status = "OK";
                return response;
            }
            else if (saveLocation == SaveLocation.Server)
            {
                // Build URL with querystring request parameters
                string uri = UriBuilder(outputPath);

                // Send the request to Saaspose server
                Stream responseStream = Utils.ProcessCommand(Utils.Sign(uri), "PUT");
                StreamReader reader = new StreamReader(responseStream);
                // Read the response
                string strJSON = reader.ReadToEnd();


                //Parse the json string to JObject
                JObject parsedJSON = JObject.Parse(strJSON);


                //Deserializes the JSON to a object. 
                GenerationResponse barcodeGenerationResponse = JsonConvert.DeserializeObject<GenerationResponse>(parsedJSON.ToString());

                return barcodeGenerationResponse;
            }

            // Return null, if anything goes wrong
            return null;
        }
        /// <summary>
        /// Creates the barcode and save the barcode image to the supplied stream
        /// Example: Save(SaveLocation.Local, imgStream, ImageFormat.PNG);
        /// </summary>
        /// <param name="imageStream">Stream where image will be saved</param>
        /// <param name="ImageFormat">Image format</param>
        public GenerationResponse Save(SaveLocation saveLocation, Stream imageStream, ImageFormat imageFormat)
        {
            PerformValidations();

            // Build URL with querystring request parameters
            string uri = UriBuilder("");

            // Send the request to Saaspose server
            Stream responseStream = Utils.ProcessCommand(Utils.Sign(uri), "GET");
            // Read the response, in this case the response is a Stream that contains barcode image
            // Just copy the response stream to the image stream that user passed
            CopyStream(responseStream, imageStream);
            // close the response stream
            responseStream.Close();

            GenerationResponse response = new GenerationResponse();
            response.Status = "OK";
            return response;
        }