public override void Run() { CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); //Initialize var storageAccount = CloudStorageAccount.FromConfigurationSetting("ConnectionString"); var queueClient = storageAccount.CreateCloudQueueClient(); var EventsQueue = queueClient.GetQueueReference("earthquakes"); var StationsQueue = queueClient.GetQueueReference("stations"); var ReducerQueue = queueClient.GetQueueReference("reducer"); while (true) { Thread.Sleep(10000); CloudQueueMessage EventMessage = EventsQueue.GetMessage(TimeSpan.FromMinutes(2)); if (EventMessage != null) { QuakeMessage quake = DeserialeMessage(EventMessage); quake.CalculateGeometry(); quake.CalculateCenter(); var service = storageAccount.CreateCloudTableClient().GetDataServiceContext(); service.IgnoreResourceNotFoundException = true; service.IgnoreMissingProperties = true; IQueryable <StationsInfo> StationsList; if (!String.IsNullOrEmpty(quake.StationsGroup)) { StationsList = (from c in service.CreateQuery <StationsInfo>("Stations").AsTableServiceQuery() where c.PartitionKey == quake.StationsGroup select c); } else { StationsList = CalculateGrid(quake); } int Counter = 0; foreach (var item in StationsList) { quake.StationCode = item.Code; quake.Station = item; CloudQueueMessage StationMessage = new CloudQueueMessage(quake.ToXml()); StationsQueue.AddMessage(StationMessage); Counter++; } CloudQueueMessage ReducerMessage = new CloudQueueMessage(String.Format("{0};{1};{2}", quake.EventID.ToLower(), Counter, quake.DeleteMe.ToString())); ReducerQueue.AddMessage(ReducerMessage); EventsQueue.DeleteMessage(EventMessage); } } }
public bool AddMessage(QuakeMessage data) { var service = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=cloudquake;AccountKey=Mm+fR9oqs12+fF/96ThXUtl3eMCkMlYMRvTf+dEP36dQRNqS6oKQoRrtELTF4AWaPEISkWsMBzo65ZI+PU2LGA=="); try { var container = service.CreateCloudBlobClient().GetContainerReference(data.EventID.ToLower()); container.CreateIfNotExist(); container.SetPermissions(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob }); var queue = service.CreateCloudQueueClient().GetQueueReference("earthquakes"); queue.AddMessage(new CloudQueueMessage(data.ToXml())); return(true); } catch (Exception) { return(false); } }
private IQueryable <StationsInfo> CalculateGrid(QuakeMessage quake) { List <StationsInfo> result = new List <StationsInfo>(); int iIndex = 1; for (double i = quake.Lat - 0.5; i <= quake.Lat + 0.5; i += 0.1) { for (double j = quake.Lon - 0.5; j <= quake.Lon + 0.5; j += 0.1) { result.Add(new StationsInfo() { Latitude = i, Longitude = j, Code = iIndex.ToString(), CrustalAmplification = "crustal_amps_sample.txt", SiteAmplification = "site_amps_d_klimis.txt", SoilCondition = "soft-soil", KappaCoeffient = 0.044 }); iIndex++; } } return(result.AsQueryable()); }
static void Main(string[] args) { CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(ConfigurationManager.AppSettings[configName]); }); var storageAccount = CloudStorageAccount.FromConfigurationSetting("ConnectionString"); var queueClient = storageAccount.CreateCloudQueueClient(); var queue = queueClient.GetQueueReference("earthquakes"); queue.CreateIfNotExist(); var s = queueClient.GetQueueReference("stations"); //s.Delete(); s.CreateIfNotExist(); var d = queueClient.GetQueueReference("reducer"); // d.Delete(); d.CreateIfNotExist(); InitializeStationsTable(storageAccount); var service = storageAccount.CreateCloudTableClient().GetDataServiceContext(); var stations = from c in service.CreateQuery <StationsInfo>("Stations").AsTableServiceQuery() select c; var ConfigurationContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("config"); StreamWriter sw = new StreamWriter("sta_grid.txt"); int i = 0; foreach (var item in stations) { switch (item.SoilCondition.ToLower()) { case "rock": i = 0; break; case "soil": i = 1; break; case "soft-soil": i = 2; break; } sw.WriteLine(String.Format("{0} {1} {2}", item.Latitude, item.Longitude, i)); } sw.Close(); ConfigurationContainer.GetBlobReference("sta_grid.txt").UploadFile("sta_grid.txt"); string message = String.Empty; QuakeMessage data = new QuakeMessage() { Depth = 0.7, Dip = 43, Stress = 55, Strike = 88, FaultType = "N", Flag = "1", StationsGroup = "benchmark", Lat = 40.45, Lon = 23, Magnitude = 5.1, EventID = "testmessage" }; queue.AddMessage(new CloudQueueMessage(data.ToXml())); System.Threading.Thread.Sleep(5000); using (Stream stream = new MemoryStream()) { StreamWriter writer = new StreamWriter(stream); writer.Write(queue.GetMessage().AsString); writer.Flush(); stream.Position = 0; object result = new XmlSerializer(typeof(QuakeMessage)).Deserialize(stream); QuakeMessage ddd = (QuakeMessage)result; Console.WriteLine(ddd.StationsGroup); } QuakeMessage ss = new QuakeMessage(); //queue.GetMessage().AsString() string title; while (true) { message = String.Empty; title = String.Empty; Console.WriteLine("Title : "); title = Console.ReadLine(); message += title + ";"; Console.WriteLine("Mangitude : "); message += Console.ReadLine() + ";"; Console.WriteLine("Latitude : "); message += Console.ReadLine() + ";"; Console.WriteLine("Longitude"); message += Console.ReadLine() + ";"; Console.WriteLine("Depth : "); message += Console.ReadLine(); // CloudQueueMessage m = new CloudQueueMessage(data.ToBinary()); // queue.AddMessage(new CloudQueueMessage(message)); Console.ReadKey(); try { var blobClient = storageAccount.CreateCloudBlobClient(); var blobContainer = blobClient.GetContainerReference(title); blobContainer.CreateIfNotExist(); blobContainer.SetPermissions(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Blob }); } catch (Exception x) { Console.WriteLine(x.InnerException.Message); } } }
public QuakeMessage CalculateGeometry(QuakeMessage q) { q.CalculateGeometry(); q.CalculateCenter(); return(q); }