private async Task ProcessNextRequest(Client client)
        {
            Log.Info($"Awaiting request message from {client.EndPoint.Host}.");
            Request request;

            try
            {
                request = await client.ReadRequest();
            }
            catch (ObjectDisposedException e)
            {
                throw new OperationCanceledException(e.Message, e);
            }
            catch (IOException e)
            {
                throw new OperationCanceledException(e.Message, e);
            }
            catch (InvalidDataException e)
            {
                throw new OperationCanceledException(e.Message, e);
            }

            Log.Info($"Request received from {client.EndPoint.Host}: Id: {request.Id}, R: {request.RealCenter}, I: {request.ImaginaryCenter}");
            Log.Info($"Will send {request.HeightPixels * request.WidthPixels * sizeof(int)} bytes.");
            var stopwatch = Stopwatch.StartNew();
            var fractal   = await Task.Run(() => _mandelbrotProcessor.ComputeSet(
                                               request.RealLowerBound,
                                               request.Width,
                                               request.ImaginaryLowerBound,
                                               request.Height,
                                               1 / request.Resolution,
                                               request.MaxMagnitude,
                                               request.MaxIterations));

            Log.Info($"Calculated mandelbrot set in {stopwatch.ElapsedMilliseconds} ms.");
            stopwatch.Restart();

            var data = fractal.SelectMany(values => values)
                       .SelectMany(value => BitConverter.GetBytes(value))
                       .ToArray();

            Log.Info($"Serialized mandelbrot set in {stopwatch.ElapsedMilliseconds} ms.");
            stopwatch.Stop();

            var result = new Result(request, data);

            Log.Info($"Sending result for {result.OriginatingRequest.Id} to client {client.EndPoint.Host}.");

            try
            {
                await client.SendResult(result);
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            Log.Info($"Application started with args: {string.Join(',', args)}");
            var mandelbrot = new MandelbrotProcessor();

            Log.Info($"Calculating mandelbrot set...");
            var mandelbrotSet = mandelbrot.ComputeSet(-2, 3, -1, 2, 1000, 2, 85);

            Log.Info($"Converting to portable gray map...");
            var fractalGrayMap = BitmapFormatter.GeneratePortableGraymap(mandelbrotSet, 85);

            Log.Info($"Writing to file...");
            using (var fs = new FileStream(args[Array.IndexOf(args, "-o") + 1], FileMode.Create))
            {
                using (var bw = new BinaryWriter(fs))
                {
                    bw.Write(fractalGrayMap);
                    bw.Flush();
                }
            }
        }