Exemplo n.º 1
0
        public void AppStoreMsgs_Ack()
        {
            EnhancedStream es = new EnhancedMemoryStream();

            AppPackageInfo[] packages = new AppPackageInfo[] {
                new AppPackageInfo(AppRef.Parse("appref://myapps/app01.zip?version=1.2.3.4"), "app01.zip", "", new byte[] { 0, 1, 2 }, 55, DateTime.MinValue),
                new AppPackageInfo(AppRef.Parse("appref://myapps/app02.zip?version=5.6.7.8"), "app02.zip", "", new byte[] { 3, 4, 5 }, 66, DateTime.MinValue)
            };

            AppStoreAck msgIn, msgOut;

            msgOut          = new AppStoreAck();
            msgOut.StoreEP  = "logical://foo/bar";
            msgOut.Packages = packages;

            Msg.Save(es, msgOut);
            es.Position = 0;
            msgIn       = (AppStoreAck)Msg.Load(es);

            Assert.IsNotNull(msgIn);
            Assert.AreEqual("logical://foo/bar", msgIn.StoreEP);

            Assert.AreEqual(2, msgIn.Packages.Length);

            Assert.AreEqual(AppRef.Parse("appref://myapps/app01.zip?version=1.2.3.4"), msgIn.Packages[0].AppRef);
            CollectionAssert.AreEqual(new byte[] { 0, 1, 2 }, msgIn.Packages[0].MD5);
            Assert.AreEqual(55, msgIn.Packages[0].Size);

            Assert.AreEqual(AppRef.Parse("appref://myapps/app02.zip?version=5.6.7.8"), msgIn.Packages[1].AppRef);
            CollectionAssert.AreEqual(new byte[] { 3, 4, 5 }, msgIn.Packages[1].MD5);
            Assert.AreEqual(66, msgIn.Packages[1].Size);
        }
Exemplo n.º 2
0
        public void OnMsg(AppStoreQuery msg)
        {
            if (netFail)
            {
                return;
            }

            var ack = new AppStoreAck();

            try
            {
                switch (msg.Command)
                {
                case AppStoreQuery.GetPrimaryCmd:

                    ack.StoreEP = this.PrimaryEP;
                    break;

                case AppStoreQuery.ListCmd:

                    ack.Packages = packageFolder.GetPackages();
                    break;

                case AppStoreQuery.RemoveCmd:

                    packageFolder.Remove(msg.AppRef);
                    break;

                case AppStoreQuery.SyncCmd:

                    using (TimedLock.Lock(syncLock))
                    {
                        this.forceSync       = true;
                        this.primaryPollTime = SysTime.Now;
                    }
                    break;

                case AppStoreQuery.DownloadCmd:

                    ack.StoreEP = cluster.InstanceEP;

                    MsgEP           primaryEP;
                    PendingDownload pending;

                    using (TimedLock.Lock(syncLock))
                    {
                        if (packageFolder.GetPackageInfo(msg.AppRef) != null)
                        {
                            break;      // The package is ready for downloading
                        }
                        if (mode == AppStoreMode.Primary || this.PrimaryEP == null)
                        {
                            throw SessionException.Create(null, "Package [{0}] cannot be found.", msg.AppRef);
                        }

                        primaryEP = this.PrimaryEP;
                        downloads.TryGetValue(msg.AppRef, out pending);
                    }

                    if (pending != null)
                    {
                        // A download is already pending for this package so wait
                        // for it to complete.

                        pending.Wait();
                    }
                    else
                    {
                        // Try downloading the requested package from the primary
                        // application store.

                        string transitPath = null;

                        pending = new PendingDownload(msg.AppRef);
                        using (TimedLock.Lock(syncLock))
                            downloads.Add(msg.AppRef, pending);

                        try
                        {
                            transitPath = packageFolder.BeginTransit(msg.AppRef);
                            DownloadPackage(primaryEP, msg.AppRef, transitPath);
                            packageFolder.EndTransit(transitPath, true);

                            pending.Done(null);
                        }
                        catch (Exception e)
                        {
                            packageFolder.EndTransit(transitPath, false);
                            pending.Done(e);
                            throw;
                        }
                        finally
                        {
                            using (TimedLock.Lock(syncLock))
                            {
                                try
                                {
                                    downloads.Remove(msg.AppRef);
                                }
                                catch
                                {
                                    // Ignore errors
                                }
                            }
                        }
                    }
                    break;

                default:

                    throw SessionException.Create("Unexpected AppStore command [{0}].", msg.Command);
                }
            }
            catch (Exception e)
            {
                ack.Exception = e.Message;
                SysLog.LogException(e);
            }

            router.ReplyTo(msg, ack);
        }