Example #1
0
        /// <summary>
        /// Uploads CAN object to CAN server.
        /// </summary>
        /// <param name="ObjectData">CAN object to upload.</param>
        /// <returns>Structure describing whether the function succeeded and response provided by CAN server.</returns>
        public async Task <CanUploadResult> CanUploadObject(byte[] ObjectData)
        {
            log.Trace("(ObjectData.Length:{0})", ObjectData.Length);

            CanApiResult apiResult = await SendRequest("add", new NameValueCollection(), "file", "object", ObjectData);

            CanUploadResult res = CanUploadResult.FromApiResult(apiResult);

            if (res.Success)
            {
                log.Trace("(-):*.Success={0},*.Hash='{1}'", res.Success, res.Hash.ToBase58());
            }
            else
            {
                log.Trace("(-):*.Success={0},*.Message='{1}'", res.Success, res.Message);
            }
            return(res);
        }
Example #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 CanUploadResult FromApiResult(CanApiResult ApiResult)
        {
            log.Trace("()");

            CanUploadResult res = new CanUploadResult(ApiResult);

            if (res.Success)
            {
                bool error = false;
                try
                {
                    CanUploadObjectResponse response = JsonConvert.DeserializeObject <CanUploadObjectResponse>(res.DataStr);
                    if (!string.IsNullOrEmpty(response.Hash))
                    {
                        res.Hash = Base58Encoding.Encoder.DecodeRaw(response.Hash);
                        if (res.Hash == null)
                        {
                            log.Error("Unable to decode hash '{0}'.", response.Hash);
                            error = true;
                        }
                    }
                    else
                    {
                        log.Error("Empty hash in CAN response.");
                        error = true;
                    }
                }
                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;
                }
            }

            log.Trace("(-)");
            return(res);
        }
Example #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("(-)");
        }