Пример #1
0
        /// <summary>
        /// This function is responsible for continually asking for status updates from OSBIDE
        /// </summary>
        private void CheckStatus()
        {
            while (IsSendingData)
            {
                //this block checks to make sure that our authentication key is up to date
                string webServiceKey;
                lock (_cache)
                {
                    webServiceKey = _cache[StringConstants.AuthenticationCacheKey] as string;

                    var result = false;
                    try
                    {
                        var task = AsyncServiceClient.IsValidKey(webServiceKey);
                        result = task.Result;
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    //if result is false, our key has gone stale.  Try to login again
                    if (result == false)
                    {
                        var userName      = _cache[StringConstants.UserNameCacheKey] as string;
                        var passwordBytes = _cache[StringConstants.PasswordCacheKey] as byte[];
                        var encoderKey    = _cache[StringConstants.AesKeyCacheKey] as byte[];
                        var encoderVector = _cache[StringConstants.AesVectorCacheKey] as byte[];
                        var password      = string.Empty;
                        try
                        {
                            password = AesEncryption.DecryptStringFromBytes_Aes(passwordBytes, encoderKey, encoderVector);
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                        if (userName != null && password != null)
                        {
                            var task = AsyncServiceClient.Login(userName, password);
                            webServiceKey = task.Result;
                            _cache[StringConstants.AuthenticationCacheKey] = webServiceKey;
                        }
                        else
                        {
                            IsSendingData = false;
                        }
                    }
                }

                //this block checks for recent user profile activity
                DateTime     lastLocalProfileUpdate;
                const string lastProfileActivityKey = "LastProfileActivity";

                //get last cached value
                lock (_cache)
                {
                    if (_cache.Contains(lastProfileActivityKey) == false)
                    {
                        _cache[lastProfileActivityKey] = DateTime.MinValue;
                    }
                    try
                    {
                        lastLocalProfileUpdate = (DateTime)_cache[lastProfileActivityKey];
                    }
                    catch (Exception)
                    {
                        lastLocalProfileUpdate = DateTime.MinValue;
                        _cache.Remove(lastProfileActivityKey);
                    }
                }

                //get last server value
                if (IsSendingData)
                {
                    DateTime lastServerProfileUpdate;
                    try
                    {
                        var task = AsyncServiceClient.GetMostRecentSocialActivity(webServiceKey);
                        lastServerProfileUpdate = task.Result;
                    }
                    catch (Exception)
                    {
                        lastServerProfileUpdate = DateTime.MinValue;
                    }

                    if (lastLocalProfileUpdate < lastServerProfileUpdate)
                    {
                        //notify client of new social activity
                        if (ReceivedNewSocialActivity != null)
                        {
                            ReceivedNewSocialActivity(this, EventArgs.Empty);
                        }

                        lock (_cache)
                        {
                            _cache[lastProfileActivityKey] = lastServerProfileUpdate;
                        }
                    }
                }

                Thread.Sleep(new TimeSpan(0, 0, 3, 0, 0));
            }
        }