Exemplo n.º 1
0
        public void StreamTransferSession_Upload_Sync()
        {
            StreamTransferSession clientSession;
            LeafRouter            router;

            InitServer();

            router = null;
            try
            {
                SetConfig("DefBlockSize=1000");

                router = new LeafRouter();
                router.Start();
                router.Dispatcher.AddTarget(this);

                clientSession      = StreamTransferSession.ClientUpload(router, "logical://Test/Upload", CreateStream(TransferSize));
                clientSession.Args = "Upload";
                clientSession.Transfer();

                Verify(msServer, TransferSize);
                Assert.IsTrue(serverNotify);
                Assert.IsNull(serverException);
                Assert.AreEqual("Upload", serverArgs);
            }
            finally
            {
                if (router != null)
                {
                    router.Stop();
                }

                Config.SetConfig(null);
            }
        }
Exemplo n.º 2
0
        public void StreamTransferSession_Upload_Async()
        {
            StreamTransferSession clientSession;
            LeafRouter            router;

            InitServer();

            router = null;
            try
            {
                SetConfig("DefBlockSize=1000");

                router = new LeafRouter();
                router.Start();
                router.Dispatcher.AddTarget(this);

                clientSession = StreamTransferSession.ClientUpload(router, "logical://Test/Upload", CreateStream(TransferSize));
                clientSession.BeginTransfer(new AsyncCallback(OnClientDone), clientSession);
                clientWait.WaitOne(TimeSpan.FromSeconds(30), false);

                Verify(msServer, TransferSize);
                Assert.IsTrue(serverNotify);
                Assert.IsNull(serverException);
            }
            finally
            {
                if (router != null)
                {
                    router.Stop();
                }

                Config.SetConfig(null);
            }
        }
Exemplo n.º 3
0
        public void OnMsg(ReliableTransferMsg msg)
        {
            if (netFail)
            {
                return;
            }

            StreamTransferSession session;
            ArgCollection         args;
            AppRef         appRef;
            AppPackageInfo packageInfo;
            string         path;

            args   = ArgCollection.Parse(msg.Args);
            appRef = AppRef.Parse(args["appref"]);

            if (msg.Direction == TransferDirection.Upload)
            {
                path    = packageFolder.BeginTransit(appRef);
                session = StreamTransferSession.ServerUpload(router, msg, path);
                session.BeginTransfer(onTransfer, new TransferInfo(session, path, msg.Direction));
            }
            else
            {
                packageInfo = packageFolder.GetPackageInfo(appRef);
                if (packageInfo == null)
                {
                    throw SessionException.Create(null, "Package [{0}] cannot be found.", appRef);
                }

                path    = packageInfo.FullPath;
                session = StreamTransferSession.ServerDownload(router, msg, path);
                session.BeginTransfer(onTransfer, new TransferInfo(session, path, msg.Direction));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Downloads an application package from a remote application store instance.
        /// </summary>
        /// <param name="storeEP">The application store endpoint.</param>
        /// <param name="appRef">The <see cref="AppRef" /> for the file.</param>
        /// <param name="path">The path to the output application package file.</param>
        private void DownloadPackage(MsgEP storeEP, AppRef appRef, string path)
        {
            StreamTransferSession session;

            session      = StreamTransferSession.ClientDownload(router, storeEP, path);
            session.Args = "appref=" + appRef.ToString();

            session.Transfer();
        }
Exemplo n.º 5
0
        public void OnMsgDownload(ReliableTransferMsg msg)
        {
            StreamTransferSession session;

            session = StreamTransferSession.ServerDownload(msg._Session.Router, msg, msServer);
            session.SimulateCancel = simServerCancel;
            session.SimulateError  = simServerError;
            session.BeginTransfer(new AsyncCallback(OnServerDone), session);
            serverArgs = msg.Args;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Uploads an application package to a remote application store instance.
        /// </summary>
        /// <param name="storeEP">
        /// The application store endpoint or <c>null</c> to query any
        /// application store instance in the cluster.
        /// </param>
        /// <param name="appRef">The <see cref="AppRef" /> for the file.</param>
        /// <param name="path">The path to the application package file.</param>
        public void UploadPackage(MsgEP storeEP, AppRef appRef, string path)
        {
            StreamTransferSession session;

            if (storeEP == null)
            {
                storeEP = settings.ClusterEP;
            }

            session      = StreamTransferSession.ClientUpload(router, storeEP, path);
            session.Args = "appref=" + appRef.ToString();

            session.Transfer();
        }
Exemplo n.º 7
0
        public void StreamTransferSession_Upload_ErrorOnClient()
        {
            StreamTransferSession clientSession;
            LeafRouter            router;
            IAsyncResult          ar;

            InitServer();

            router = null;
            try
            {
                SetConfig("DefBlockSize=1000");

                router = new LeafRouter();
                router.Start();
                router.Dispatcher.AddTarget(this);

                clientSession               = StreamTransferSession.ClientUpload(router, "logical://Test/Upload", CreateStream(TransferSize));
                clientSession.Delay         = 500;
                clientSession.SimulateError = true;
                ar = clientSession.BeginTransfer(null, null);

                Thread.Sleep(1000);
                clientSession.Cancel();

                clientSession.EndTransfer(ar);
                Assert.Fail("Expected an exception");
            }
            catch
            {
                // Expecting an exception
            }
            finally
            {
                if (router != null)
                {
                    router.Stop();
                }

                Config.SetConfig(null);
            }

            Thread.Sleep(1000);
            Assert.IsTrue(serverNotify);
            Assert.IsInstanceOfType(serverException, typeof(SessionException));
        }
Exemplo n.º 8
0
        public void StreamTransferSession_Download_ErrorOnServer()
        {
            StreamTransferSession clientSession;
            LeafRouter            router;
            EnhancedMemoryStream  msClient;

            InitServer();

            router = null;
            try
            {
                SetConfig("DefBlockSize=1000");

                router = new LeafRouter();
                router.Start();
                router.Dispatcher.AddTarget(this);

                msServer = CreateStream(TransferSize);
                msClient = new EnhancedMemoryStream();

                simServerError = true;

                clientSession       = StreamTransferSession.ClientDownload(router, "logical://Test/Download", msClient);
                clientSession.Delay = 500;
                clientSession.Transfer();
                Assert.Fail("Expecting a SessionException");
            }
            catch (Exception e)
            {
                Assert.IsInstanceOfType(e, typeof(SessionException));
            }
            finally
            {
                if (router != null)
                {
                    router.Stop();
                }

                Config.SetConfig(null);
            }

            Thread.Sleep(1000);
            Assert.IsTrue(serverNotify);
            Assert.IsInstanceOfType(serverException, typeof(Exception));
        }
Exemplo n.º 9
0
        public void StreamTransferSession_Cancel_OnServer()
        {
            StreamTransferSession clientSession;
            LeafRouter            router;

            InitServer();

            router = null;
            try
            {
                SetConfig("DefBlockSize=1000");

                router = new LeafRouter();
                router.Start();
                router.Dispatcher.AddTarget(this);

                simServerCancel = true;

                clientSession       = StreamTransferSession.ClientUpload(router, "logical://Test/Upload", CreateStream(TransferSize));
                clientSession.Delay = 500;
                clientSession.Transfer();
                Assert.Fail("Expected a CancelException");
            }
            catch (SessionException e)
            {
                SysLog.LogException(e);
            }
            catch (Exception e)
            {
                Assert.IsInstanceOfType(e, typeof(CancelException));
            }
            finally
            {
                if (router != null)
                {
                    router.Stop();
                }

                Config.SetConfig(null);
            }

            Thread.Sleep(1000);
            Assert.IsTrue(serverNotify);
            Assert.IsInstanceOfType(serverException, typeof(CancelException));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Downloads an application package from a remote application store instance.
        /// </summary>
        /// <param name="storeEP">
        /// The application store endpoint or <c>null</c> to query any
        /// application store instance in the cluster.
        /// </param>
        /// <param name="appRef">The <see cref="AppRef" /> for the file.</param>
        /// <param name="path">The path to the output application package file.</param>
        public void DownloadPackage(MsgEP storeEP, AppRef appRef, string path)
        {
            StreamTransferSession session;
            AppStoreAck           ack;

            if (storeEP == null)
            {
                storeEP = settings.ClusterEP;
            }

            ack     = (AppStoreAck)router.Query(storeEP, new AppStoreQuery(AppStoreQuery.DownloadCmd, appRef));
            storeEP = ack.StoreEP;

            session      = StreamTransferSession.ClientDownload(router, storeEP, path);
            session.Args = "appref=" + appRef.ToString();

            session.Transfer();
        }
Exemplo n.º 11
0
        public void StreamTransferSession_WrongDirection_Upload()
        {
            StreamTransferSession clientSession;
            LeafRouter            router;
            EnhancedMemoryStream  msClient;

            InitServer();

            router = null;
            try
            {
                SetConfig("DefBlockSize=1000");

                router = new LeafRouter();
                router.Start();
                router.Dispatcher.AddTarget(this);

                msServer = CreateStream(TransferSize);
                msClient = new EnhancedMemoryStream();

                clientSession = StreamTransferSession.ClientUpload(router, "logical://Test/Download", msClient);
                clientSession.Transfer();
                Assert.Fail("Expected a SessionException");
            }
            catch (Exception e)
            {
                Assert.IsInstanceOfType(e, typeof(SessionException));
            }
            finally
            {
                if (router != null)
                {
                    router.Stop();
                }

                Config.SetConfig(null);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Synchronizes the set of application packages held by this instance with
        /// the primary application store.
        /// </summary>
        private void SyncWithPrimary()
        {
            MsgEP primaryEP;

            if (mode == AppStoreMode.Primary)
            {
                return;     // This is the primary instance
            }
            primaryEP = this.PrimaryEP;
            if (primaryEP == null)
            {
                return;     // There is no known primary
            }
            // Synchronize

            Dictionary <AppRef, AppPackageInfo> primaryPackages;
            Dictionary <AppRef, AppPackageInfo> localPackages;

            primaryPackages = new Dictionary <AppRef, AppPackageInfo>();
            foreach (AppPackageInfo info in ListRemotePackages(primaryEP))
            {
                primaryPackages.Add(info.AppRef, info);
            }

            localPackages = new Dictionary <AppRef, AppPackageInfo>();
            foreach (AppPackageInfo info in packageFolder.GetPackages())
            {
                localPackages.Add(info.AppRef, info);
            }

            // Delete any packages present in the local folder that
            // are not present on the primary.

            foreach (AppPackageInfo info in localPackages.Values)
            {
                if (!primaryPackages.ContainsKey(info.AppRef))
                {
                    packageFolder.Remove(info.AppRef);
                }
            }

            // Download any packages found on the primary that are
            // not present locally or whose MD5 hash differs from
            // the local file.

            foreach (AppPackageInfo info in primaryPackages.Values)
            {
                bool           downloadPackage = false;
                AppPackageInfo localInfo;

                if (!localPackages.TryGetValue(info.AppRef, out localInfo))
                {
                    downloadPackage = true;
                }
                else
                {
                    downloadPackage = !Helper.ArrayEquals(info.MD5, localInfo.MD5);
                }

                if (downloadPackage)
                {
                    StreamTransferSession session;
                    string path;

                    path = packageFolder.BeginTransit(info.AppRef);

                    try
                    {
                        session      = StreamTransferSession.ClientDownload(router, primaryEP, path);
                        session.Args = "appref=" + info.AppRef.ToString();
                        session.Transfer();
                        packageFolder.EndTransit(path, true);
                    }
                    catch (Exception e)
                    {
                        packageFolder.EndTransit(path, false);

                        // I'm going to log the exception and then continue
                        // downloading any remaining packages.

                        SysLog.LogException(e);
                    }
                }
            }
        }
Exemplo n.º 13
0
 public TransferInfo(StreamTransferSession session, string path, TransferDirection direction)
 {
     this.Session   = session;
     this.Path      = path;
     this.Direction = direction;
 }