Example #1
0
        private void Calculate()
        {
            Bitmap bitmap = new Bitmap(pcbFractal.Width, pcbFractal.Height);

            pcbFractal.Image = bitmap;
            pcbFractal.Refresh();

            realWidth = realDelta * pcbFractal.Width;
            imgHeight = imgDelta * pcbFractal.Height;

            realMin = realCenter - realWidth / 2;
            imgMin  = imgCenter - imgHeight / 2;

            int width  = pcbFractal.Width;
            int height = pcbFractal.Height;

            Guid id = Guid.NewGuid();

            SectorInfo sectorinfo = new SectorInfo()
            {
                Id            = id,
                FromX         = 0,
                FromY         = 0,
                Width         = width,
                Height        = height,
                RealMinimum   = realMin,
                ImgMinimum    = imgMin,
                Delta         = realDelta,
                MaxIterations = colors.Length,
                MaxValue      = 4
            };

            Calculator calculator = new Calculator();

            this.queue.AddMessage(SectorUtilities.FromSectorInfoToMessage(sectorinfo));
            //Sector sector = calculator.CalculateSector(sectorinfo);
            //this.DrawValues(sector.FromX, sector.FromY, sector.Width, sector.Height, sector.Values);
        }
Example #2
0
        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("FractalWorkerRole entry point called", "Information");
            CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

            CloudBlobClient    blobClient    = account.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = blobClient.GetContainerReference("fractalsectors");

            blobContainer.CreateIfNotExist();
            QueueUtilities qutil    = new QueueUtilities(account);
            CloudQueue     queue    = qutil.CreateQueueIfNotExists("fractaltoprocess");
            CloudQueue     outqueue = qutil.CreateQueueIfNotExists("fractalsectors");

            Calculator calculator = new Calculator();

            while (true)
            {
                CloudQueueMessage msg = queue.GetMessage();

                if (msg != null)
                {
                    Trace.WriteLine(string.Format("Processing {0}", msg.AsString));
                    SectorInfo info = SectorUtilities.FromMessageToSectorInfo(msg);

                    if (info.Width > 100 || info.Height > 100)
                    {
                        Trace.WriteLine("Splitting message...");
                        for (int x = 0; x < info.Width; x += 100)
                        {
                            for (int y = 0; y < info.Height; y += 100)
                            {
                                SectorInfo newinfo = info.Clone();
                                newinfo.FromX  = x + info.FromX;
                                newinfo.FromY  = y + info.FromY;
                                newinfo.Width  = Math.Min(100, info.Width - x);
                                newinfo.Height = Math.Min(100, info.Height - y);
                                CloudQueueMessage newmsg = SectorUtilities.FromSectorInfoToMessage(newinfo);
                                queue.AddMessage(newmsg);
                            }
                        }
                    }
                    else
                    {
                        Trace.WriteLine("Processing message...");
                        Sector       sector   = calculator.CalculateSector(info);
                        string       blobname = string.Format("{0}.{1}.{2}.{3}.{4}", info.Id, sector.FromX, sector.FromY, sector.Width, sector.Height);
                        CloudBlob    blob     = blobContainer.GetBlobReference(blobname);
                        MemoryStream stream   = new MemoryStream();
                        BinaryWriter writer   = new BinaryWriter(stream);

                        foreach (int value in sector.Values)
                        {
                            writer.Write(value);
                        }

                        writer.Flush();
                        stream.Seek(0, SeekOrigin.Begin);

                        blob.UploadFromStream(stream);

                        stream.Close();

                        CloudQueueMessage outmsg = new CloudQueueMessage(blobname);
                        outqueue.AddMessage(outmsg);
                    }

                    queue.DeleteMessage(msg);
                }
                else
                {
                    Thread.Sleep(10000);
                    Trace.WriteLine("Working", "Information");
                }
            }
        }