Beispiel #1
0
        /// <summary>
        /// Deletes CAN object from CAN server.
        /// </summary>
        /// <param name="ObjectPath">CAN path to the object.</param>
        /// <returns>Structure describing whether the function succeeded and response provided by CAN server.</returns>
        public async Task <CanDeleteResult> CanDeleteObject(string ObjectPath)
        {
            log.Trace("(ObjectPath:'{0}')", ObjectPath);

            NameValueCollection args = new NameValueCollection();

            args.Add("arg", ObjectPath);
            CanApiResult apiResult = await SendRequest("pin/rm", args);

            CanDeleteResult res = CanDeleteResult.FromApiResult(apiResult);

            log.Trace("(-):{0}", res);
            return(res);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new object based on a result from CAN API including validation checks.
        /// </summary>
        /// <param name="ApiResult">CAN API result object to copy values from.</param>
        /// <returns>Structure describing result of CAN upload operation.</returns>
        public static CanDeleteResult FromApiResult(CanApiResult ApiResult)
        {
            log.Trace("()");

            CanDeleteResult res = new CanDeleteResult(ApiResult);

            if (res.Success)
            {
                bool error = false;
                try
                {
                    CanDeleteObjectResponse response = JsonConvert.DeserializeObject <CanDeleteObjectResponse>(res.DataStr);
                    res.Pins = response.Pins;

                    // If the object was deleted previously, we might have empty Pins in response.
                    // We are thus OK if we receive success response and no more validation is done.
                }
                catch (Exception e)
                {
                    log.Error("Exception occurred: {0}", e.ToString());
                    error = true;
                }

                if (error)
                {
                    res.Success    = false;
                    res.Message    = "Invalid CAN response.";
                    res.IsCanError = false;
                }
            }
            else if (res.Message.ToLowerInvariant() == "not pinned")
            {
                res.Success = true;
                res.Pins    = null;
            }

            log.Trace("(-)");
            return(res);
        }
Beispiel #3
0
        /// <summary>
        /// Thread that is initializes CAN objects during the profile server startup.
        /// </summary>
        private async void InitThread()
        {
            log.Info("()");

            initThreadFinished.Reset();

            if (Base.Configuration.CanProfileServerContactInformationHash != null)
            {
                log.Debug("Old CAN object hash is '{0}', object {1} change.", Base.Configuration.CanProfileServerContactInformationHash.ToBase58(), Base.Configuration.CanProfileServerContactInformationChanged ? "DID" : "did NOT");
            }
            else
            {
                log.Debug("No CAN object found.");
            }

            bool deleteOldObject = Base.Configuration.CanProfileServerContactInformationChanged && (Base.Configuration.CanProfileServerContactInformationHash != null);

            byte[] canObject = canContactInformation.ToByteArray();
            log.Trace("CAN object: {0}", canObject.ToHex());

            while (!ShutdownSignaling.IsShutdown)
            {
                // First delete old CAN object if there is any.
                bool error = false;
                if (deleteOldObject)
                {
                    string          objectPath = CanApi.CreateIpfsPathFromHash(Base.Configuration.CanProfileServerContactInformationHash);
                    CanDeleteResult cres       = await canApi.CanDeleteObject(objectPath);

                    if (cres.Success)
                    {
                        log.Info("Old CAN object hash '{0}' deleted.", Base.Configuration.CanProfileServerContactInformationHash.ToBase58());
                    }
                    else
                    {
                        log.Warn("Failed to delete old CAN object hash '{0}', error message '{1}', will retry.", Base.Configuration.CanProfileServerContactInformationHash.ToBase58(), cres.Message);
                        error = true;
                    }
                }
                else
                {
                    log.Trace("No old object to delete.");
                }

                if (ShutdownSignaling.IsShutdown)
                {
                    break;
                }
                if (!error)
                {
                    if (Base.Configuration.CanProfileServerContactInformationChanged)
                    {
                        // Now upload the new object.
                        CanUploadResult cres = await canApi.CanUploadObject(canObject);

                        if (cres.Success)
                        {
                            canContactInformationHash = cres.Hash;
                            log.Info("New CAN object hash '{0}' added.", canContactInformationHash.ToBase58());
                            break;
                        }

                        log.Warn("Unable to add new object to CAN, error message: '{0}'", cres.Message);
                    }
                    else
                    {
                        canContactInformationHash = Base.Configuration.CanProfileServerContactInformationHash;
                        log.Info("CAN object unchanged since last time, hash is '{0}'.", canContactInformationHash.ToBase58());
                        break;
                    }
                }

                // Retry in 10 seconds.
                try
                {
                    await Task.Delay(10000, ShutdownSignaling.ShutdownCancellationTokenSource.Token);
                }
                catch
                {
                    // Catch cancellation exception.
                }
            }


            if (canContactInformationHash != null)
            {
                if (Base.Configuration.CanProfileServerContactInformationChanged)
                {
                    // Save the new data to the database.
                    if (!await SaveProfileServerContactInformation())
                    {
                        log.Error("Failed to save new profile server contact information values to database.");
                    }
                }

                // Finally, start IPNS record refreshing timer.
                await IpnsRecordRefresh();
            }


            initThreadFinished.Set();

            log.Info("(-)");
        }