public static ClientInformation GetClientInformation()
        {
            ClientInformation clientInfo = null;
            try
            {
                // access client table in Azure storage
                CloudStorageAccount cloudStorageAccount =
                    CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
                var ctx = new ClientDataContext(
                    cloudStorageAccount.TableEndpoint.ToString(),
                    cloudStorageAccount.Credentials);

                // return the first (and only) record or nothing
                if (cloudStorageAccount.CreateCloudTableClient().DoesTableExist("client"))
                    clientInfo = ctx.Clients.FirstOrDefault<ClientInformation>();
            }
            catch (Exception ex)
            {
                Trace.TraceWarning(String.Format("Exception when accessing client data: {0} | {1}", ex.Message, ex.StackTrace));
                clientInfo = null;

                throw;
            }

            return clientInfo;
        }
        protected void cbStart_Click(object sender, EventArgs e)
        {
            // create/confirm client table exists
            CloudStorageAccount cloudStorageAccount =
                CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
            CloudTableClient cloudClient = cloudStorageAccount.CreateCloudTableClient();
            cloudClient.CreateTableIfNotExist("client");

            // if table exists
            if (cloudClient.DoesTableExist("client"))
            {
                // create a new client info record to persist to table storage
                ClientInformation clientInfo = new ClientInformation(
                            txtName.Text,
                            PASSKEY,
                            TEAM_NUMBER,
                            Double.Parse(txtLatitudeValue.Value),
                            Double.Parse(txtLongitudeValue.Value),
                            Request.ServerVariables["SERVER_NAME"]);

                // add client info record
                var ctx = new ClientDataContext(
                   cloudStorageAccount.TableEndpoint.ToString(),
                   cloudStorageAccount.Credentials);
                ctx.AddObject("client", clientInfo);
                ctx.SaveChanges();

                // redirect to the status page
                Response.Redirect("/Status.aspx");
            }
        }
 public AzureAtHomeClientDataRepository(string dataConnectionString)
 {
     StorageAccount =
         CloudStorageAccount.Parse(dataConnectionString);
     _tableClient = StorageAccount.CreateCloudTableClient();
     _dataContext = new ClientDataContext(
            StorageAccount.TableEndpoint.ToString(),
            StorageAccount.Credentials);
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            var clientInfo = FoldingClient.GetClientInformation();
            litName.Text = clientInfo != null ? clientInfo.UserName : "******";

            // ensure workunit table exists
            CloudStorageAccount cloudStorageAccount =
                CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
            CloudTableClient cloudClient = cloudStorageAccount.CreateCloudTableClient();

            if (cloudClient.DoesTableExist("workunit"))
            {
                ClientDataContext ctx = new ClientDataContext(
                    cloudClient.BaseUri.ToString(),
                    cloudClient.Credentials);
                var workUnitList = ctx.WorkUnits.ToList<WorkUnit>();

                InProgress.DataSource =
                    (from w in workUnitList
                     where w.CompleteTime == null
                     let duration = (DateTime.UtcNow - w.StartTime.ToUniversalTime())
                     orderby w.InstanceId ascending
                     select new
                     {
                         w.Name,
                         w.Tag,
                         w.InstanceId,
                         w.Progress,
                         Duration = String.Format("{0:#0} h {1:00} m",
                            duration.TotalHours, duration.Minutes)
                     });
                InProgress.DataBind();

                GridViewCompleted.DataSource =
                    (from w in workUnitList
                     where w.CompleteTime != null
                     let duration = (w.CompleteTime.Value.ToUniversalTime() - w.StartTime.ToUniversalTime())
                     orderby w.StartTime descending
                     select new
                     {
                         w.Name,
                         w.Tag,
                         w.StartTime,
                         Duration = String.Format("{0:#0} h {1:00} m",
                            duration.TotalHours, duration.Minutes)
                     });
                GridViewCompleted.DataBind();
            }

            litNoProgress.Visible = InProgress.Items.Count == 0;
            litCompletedTitle.Visible = GridViewCompleted.Rows.Count > 0;
        }
        public void UpdateLocalStatus(FoldingClientStatus status)
        {
            var cloudStorageAccount =
                CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));

            // ensure workunit table exists
            var cloudClient = new CloudTableClient(
                cloudStorageAccount.TableEndpoint.ToString(),
                cloudStorageAccount.Credentials);
            cloudClient.CreateTableIfNotExist("workunit");

            // select info for given workunit
            var ctx = new ClientDataContext(
                    cloudClient.BaseUri.ToString(),
                    cloudClient.Credentials);
            var workUnit = (from w in ctx.WorkUnits.ToList<WorkUnit>()
                            where w.PartitionKey == RoleEnvironment.CurrentRoleInstance.Id &&
                              w.RowKey == w.MakeKey(status.Name, status.Tag, status.DownloadTime)
                            select w).FirstOrDefault<WorkUnit>();

            // if it's a new one, add it
            if (workUnit == null)
            {
                workUnit = new WorkUnit(status.Name, status.Tag, status.DownloadTime, RoleEnvironment.CurrentRoleInstance.Id) { Progress = status.Progress, StartTime = DateTime.UtcNow };
                ctx.AddObject("workunit", workUnit);
            }

            // otherwise, update it
            else
            {
                workUnit.Progress = status.Progress;
                if (workUnit.Progress == 100)
                    workUnit.CompleteTime = DateTime.UtcNow;
                ctx.UpdateObject(workUnit);
            }
            ctx.SaveChanges();
        }