Esempio n. 1
0
        private static IAsyncResult QueryItemFromServerAsync(string key, ConfigType type, Action <ConfigItem, Exception> callback)
        {
            string url = new UrlBuilder()
                         .SetPath("/ActiveConfig/v1/GetKey")
                         .AddParam("appid", _appKey)
                         .AddParam("secretkey", _appSecret)
                         .AddParam("key", key + ":" + (int)type)
                         .GetUrl();

            return(Http.GetStringAsync(url, (s, e) =>
            {
                ConfigItem result = null;
                if (e == null)
                {
                    result = null;

                    try
                    {
                        var dict = JSON.Parse(s);
                        if (dict != null)
                        {
                            string code = dict["code"].Value;
                            string msg = dict["msg"].Value;
                            string data = dict["data"].Value;

                            if (code != "0")
                            {
                                Debug.WriteLine("Server return error {0}: {1}", code, msg);
                                e = new ActiveConfigException(msg);
                            }
                            else
                            {
                                if (dict["data"].Count == 1)
                                {
                                    var item = dict["data"][0];
                                    result = new ConfigItem();
                                    result.Key = item["key"].Value;
                                    result.Value = item["value"].Value;
                                    result.Type = (ConfigType)Enum.Parse(typeof(ConfigType), item["type"].Value, true);
                                    result.ExpireTime = Helper.ConvertTime(item["endtime"].Value);
                                    result.MD5 = item["md5"].Value;
                                    result.ID = string.Format("{0}:{1}", (int)result.Type, result.Key);
                                    if (result.Status == ItemStatus.KeyNotFound)
                                    {
                                        e = new KeyNotFoundException(key);
                                    }
                                }
                                else
                                {
                                    e = new KeyNotFoundException(key);
                                }
                            }
                        }
                        else
                        {
                            e = new ActiveConfigException("Parsing json failed");
                        }
                    }
                    catch (Exception err)
                    {
                        e = new ActiveConfigException("Parsing json failed", err);
                    }
                }

                callback(result, e);
            }));
        }
Esempio n. 2
0
        private static IAsyncResult QueryAll(ConfigItem[] keys, Action <ConfigItem[], Exception> callback)
        {
            string url = new UrlBuilder()
                         .SetPath("/ActiveConfig/v1/CheckUpdate")
                         .AddParam("appid", _appKey)
                         .AddParam("secretkey", _appSecret)
                         .AddParam("key", string.Join(";", keys.Select(x => x.Key + "," + x.MD5 + "," + (int)x.Type)))
                         .GetUrl();

            return(Http.GetStringAsync(url, (s, e) =>
            {
                List <ConfigItem> results = null;

                if (e == null)
                {
                    results = new List <ConfigItem>();
                    var dict = JSON.Parse(s);
                    if (dict != null)
                    {
                        string code = dict["code"].Value;
                        string msg = dict["msg"].Value;

                        if (code != "0")
                        {
                            Debug.WriteLine("Server return error {0}: {1}", code, msg);
                            e = new ActiveConfigException(msg);
                        }
                        else
                        {
                            foreach (var item in dict["data"].Childs)
                            {
                                ConfigItem c = new ConfigItem();
                                c.Key = item["key"].Value;
                                c.Type = (ConfigType)Enum.Parse(typeof(ConfigType), item["type"].Value, true);

                                var serverStatus = item["status"].Value != "" ? (ServerItemStatus)Enum.Parse(typeof(ServerItemStatus), item["status"].Value, true) : ServerItemStatus.Success;

                                if (serverStatus == ServerItemStatus.NoUpdate)
                                {
                                    continue;
                                }
                                else if (serverStatus == ServerItemStatus.Invalid)
                                {
                                    c.Status = ItemStatus.KeyNotFound;
                                }
                                else
                                {
                                    c.Value = item["value"].Value;
                                    c.ExpireTime = Helper.ConvertTime(item["endtime"].Value);
                                    c.MD5 = item["md5"].Value;
                                    c.Status = ItemStatus.OK;
                                }

                                results.Add(c);
                            }
                        }
                    }
                    else
                    {
                        e = new ActiveConfigException("Parsing json failed");
                    }
                }

                callback(results.ToArray(), e);
            }));
        }
Esempio n. 3
0
        public static void CheckUpdate()
        {
            if (!_isRegistered)
            {
                throw new InvalidOperationException("Call Register first");
            }

            if (CacheManger.Configs.Count() > 0)
            {
                QueryAll(CacheManger.Configs.ToArray(), (results, e) =>
                {
                    try
                    {
                        if (e == null)
                        {
                            foreach (var c in results)
                            {
                                ConfigItem hit = null;
                                lock (_databaseLock)
                                {
                                    hit = CacheManger.Configs.Single(x => x.ID == string.Format("{0}:{1}", (int)c.Type, c.Key));
                                }

                                if (c.MD5 != hit.MD5)
                                {
                                    hit.Value      = c.Value;
                                    hit.ExpireTime = c.ExpireTime;
                                    hit.MD5        = c.MD5;
                                    hit.Blob       = null;

                                    lock (_databaseLock)
                                    {
                                        CacheManger.SubmitChanges();
                                    }

                                    if (hit.Type == ConfigType.Image)
                                    {
                                        Http.GetBytesAsync(hit.Value, (bytes, err) =>
                                        {
                                            if (err == null)
                                            {
                                                hit.Blob = bytes;
                                                lock (_databaseLock)
                                                {
                                                    CacheManger.SubmitChanges();
                                                }

                                                if (ConfigUpdated != null)
                                                {
                                                    Helper.SafeInvoke(() =>
                                                    {
                                                        BitmapImage image = new BitmapImage();
                                                        image.SetSource(new MemoryStream(hit.Blob));

                                                        ConfigUpdated(null, new ConfigUpdatedEventArgs()
                                                        {
                                                            Key   = hit.Key,
                                                            Type  = ConfigType.Image,
                                                            Value = image
                                                        });
                                                    });
                                                }
                                            }
                                            else
                                            {
                                                Debug.WriteLine(err.Message);
                                            }
                                        });
                                    }
                                    else
                                    {
                                        if (ConfigUpdated != null)
                                        {
                                            Helper.SafeInvoke(() =>
                                            {
                                                ConfigUpdated(null, new ConfigUpdatedEventArgs()
                                                {
                                                    Key   = hit.Key,
                                                    Type  = ConfigType.Text,
                                                    Value = hit.Value
                                                });
                                            });
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            Debug.WriteLine(e.Message);
                        }
                    }
                    catch (Exception err)
                    {
                        Debug.WriteLine(err.Message);
                    }
                });
            }
        }