Example #1
0
        static void Main(string[] args)
        {
            ClientEnvironment.verbose = true;

            FileSynch fs   = new FileSynch(".");
            bool      test = fs.testComponent();

            TestUtilities.checkResult(test, "FileSynchronizer");
            TestUtilities.putLine("\n");
        }
        /*----< here server responses to messages are defined >--------*/

        void initializeDispatcher()
        {
            // doTest
            Func <Msg, Msg> action1 = (Msg msg) =>
            {
                testComponent();
                Msg returnMsg = new Msg(Msg.MessageType.noReply);
                returnMsg.to      = msg.from;
                returnMsg.from    = msg.to;
                returnMsg.command = msg.command;
                return(returnMsg);
            };

            dispatcher_.addCommand(Msg.Command.doTest, action1);

            // getCategories
            Func <Msg, Msg> action2 = (Msg msg) =>
            {
                DirList dirList   = getCategories();
                Msg     returnMsg = new Msg(Msg.MessageType.reply);
                returnMsg.to       = msg.from;
                returnMsg.from     = msg.to;
                returnMsg.argument = msg.argument;
                foreach (DirName cat in dirList)
                {
                    returnMsg.arguments.Add(cat);
                }
                returnMsg.command = msg.command;
                return(returnMsg);
            };

            dispatcher_.addCommand(Msg.Command.getCategories, action2);

            // getFiles
            Func <Msg, Msg> action3 = (Msg msg) =>
            {
                FileList fileList  = getFiles(msg.argument);
                Msg      returnMsg = new Msg(Msg.MessageType.reply);
                returnMsg.to       = msg.from;
                returnMsg.from     = msg.to;
                returnMsg.argument = msg.argument;
                foreach (FileName file in fileList)
                {
                    returnMsg.arguments.Add(file);
                }
                //returnMsg.arguments = fileList;
                returnMsg.command = msg.command;
                return(returnMsg);
            };

            dispatcher_.addCommand(Msg.Command.getFiles, action3);

            // synchLocal
            Func <Msg, Msg> action4 = (Msg msg) =>
            {
                string    synchPath = FileNameEditor.pathCombine(ServerEnvironment.storagePath, msg.argument);
                FileSynch fs        = new FileSynch(synchPath);
                fs.isSynched(msg.arguments);
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.to       = msg.from;
                replyMsg.from     = msg.to;
                replyMsg.argument = msg.argument;
                foreach (string file in fs.notInList)
                {
                    replyMsg.arguments.Add(file);
                }
                replyMsg.command = msg.command;
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.synchLocal, action4);

            // synchRemote
            Func <Msg, Msg> action5 = (Msg msg) =>
            {
                string    synchPath = FileNameEditor.pathCombine(ServerEnvironment.storagePath, msg.argument);
                FileSynch fs        = new FileSynch(synchPath);
                fs.isSynched(msg.arguments);
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.to       = msg.from;
                replyMsg.from     = msg.to;
                replyMsg.argument = msg.argument;
                foreach (string file in fs.notInSyncDir)
                {
                    replyMsg.arguments.Add(file);
                }
                replyMsg.command = msg.command;
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.synchRemote, action5);

            // sendFile
            Func <Msg, Msg> action6 = (Msg msg) =>
            {
                string fileRef  = msg.argument;
                string fileSpec = FileNameEditor.pathCombine(CommEnvironment.serverStoragePath, fileRef);
                fileSpec = System.IO.Path.GetFullPath(fileSpec);
                comm_.postFile(fileSpec);
                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.to   = msg.from;
                replyMsg.from = msg.to;
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.sendFile, action6);

            // acceptFile
            Func <Msg, Msg> action7 = (Msg msg) =>
            {
                comm_.sndr.setFileDestinationPath(ServerEnvironment.storagePath);
                //string fileRef = msg.argument;
                //string fileSpec = FileNameEditor.pathCombine(CommEnvironment.serverStoragePath, fileRef);
                //fileSpec = System.IO.Path.GetFullPath(fileSpec);
                //comm_.postFile(fileSpec);
                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.to   = msg.from;
                replyMsg.from = msg.to;
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.acceptFile, action7);
        }