/*----< 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);
        }
Exemplo n.º 2
0
        /*----< test Comm instance >-----------------------------------*/

        public static bool testComm()
        {
            TestUtilities.vbtitle("testing Comm");
            bool test = true;

            Comm        comm    = new Comm("http://localhost", 8081);
            CommMessage csndMsg = new CommMessage(CommMessage.MessageType.request);

            csndMsg.command = Msg.Command.show;
            csndMsg.author  = "Jim Fawcett";
            csndMsg.to      = "http://localhost:8081/IPluggableComm";
            csndMsg.from    = "http://localhost:8081/IPluggableComm";

            comm.postMessage(csndMsg);
            CommMessage crcvMsg = comm.getMessage();

            if (ClientEnvironment.verbose)
            {
                crcvMsg.show();
            }

            crcvMsg = comm.getMessage();
            if (ClientEnvironment.verbose)
            {
                crcvMsg.show();
            }
            if (!compareMsgs(csndMsg, crcvMsg))
            {
                test = false;
            }
            TestUtilities.checkResult(test, "csndMsg equals crcvMsg");
            TestUtilities.putLine();

            TestUtilities.vbtitle("testing file transfer");

            bool testFileTransfer = true;

            List <string> names = getClientFileList();

            foreach (string name in names)
            {
                TestUtilities.putLine(string.Format("transferring file \"{0}\"", name));
                bool transferSuccess = comm.postFile(name);
                TestUtilities.checkResult(transferSuccess, "transfer");
            }

            foreach (string name in names)
            {
                if (!compareFileBytes(name))
                {
                    testFileTransfer = false;
                    break;
                }
            }
            TestUtilities.checkResult(testFileTransfer, "file transfers");
            TestUtilities.putLine();

            TestUtilities.vbtitle("test receiver close");
            csndMsg.type = CommMessage.MessageType.closeReceiver;
            comm.postMessage(csndMsg);
            crcvMsg = comm.getMessage();
            if (ClientEnvironment.verbose)
            {
                crcvMsg.show();
            }
            if (!compareMsgs(csndMsg, crcvMsg))
            {
                test = false;
            }
            TestUtilities.checkResult(test, "closeReceiver");
            TestUtilities.putLine();

            csndMsg.type = CommMessage.MessageType.closeSender;
            comm.postMessage(csndMsg);
            if (ClientEnvironment.verbose)
            {
                csndMsg.show();
            }
            // comm.getMessage() would fail because server has shut down
            // no rcvMsg so no compare

            TestUtilities.putLine("last message received\n");

            return(test && testFileTransfer);
        }
Exemplo n.º 3
0
        /*----< here server responses to messages are defined >--------*/

        void initializeDispatcher()
        {
            // login
            Func <Msg, Msg> action1 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command = CommMessage.Command.login;
                replyMsg.author  = "server";
                replyMsg.to      = msg.from;
                replyMsg.from    = msg.to;

                if (Authentication.authenticateUser(msg.author, XMLHelper.getSingleValueFromString(msg.stringBody, "Password")))
                {
                    replyMsg.stringBody = "<StringBody><Result>" + "ok" + "</Result></StringBody>";
                }
                else
                {
                    replyMsg.stringBody = "<StringBody><Result>" + "error" + "</Result></StringBody>";
                }

                return(replyMsg);
            };

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

            // annouceBatch
            Func <Msg, Msg> action2 = (Msg msg) =>
            {
                //Console.WriteLine("received announce, need to save batch {0}", XMLHelper.getSingleValueFromString(msg.stringBody, "BatchName"));

                System.IO.Directory.CreateDirectory(ServerEnvironment.storagePath);
                System.IO.Directory.CreateDirectory(ServerEnvironment.storagePath + msg.author);


                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.command    = CommMessage.Command.announceBatch;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = "<StringBody><Result>" + "ok" + "</Result></StringBody>";
                return(replyMsg);
            };

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

            // annouceFile
            Func <Msg, Msg> action3 = (Msg msg) =>
            {
                XMLHelper.filesAddNode(
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Filename"),
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Batch"),
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Owner")
                    );

                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.command    = CommMessage.Command.announceFile;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = "<StringBody><Result>" + "ok" + "</Result></StringBody>";
                return(replyMsg);
            };

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

            // getBatchList
            Func <Msg, Msg> action4 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command    = CommMessage.Command.getBatchList;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getBatchesXMLList(msg.author);
                return(replyMsg);
            };

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

            // getBatchList
            Func <Msg, Msg> action5 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command    = CommMessage.Command.getBatchFileList;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getBatchesFileListXMLList(
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Batch"),
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Owner")
                    );
                return(replyMsg);
            };

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

            // download files
            Func <Msg, Msg> action6 = (Msg msg) =>
            {
                string result = msg.stringBody;

                XElement xmlTree = XElement.Parse(result);
                var      element = (from e in xmlTree.Descendants("File")
                                    select e);

                foreach (var s in element)
                {
                    string fileName  = s.Element("Name").Value;
                    string batchName = s.Element("Batch").Value;


                    if (batchName.Contains("Granted Access Batch"))
                    {
                        string[] batchParts         = batchName.Split('-');
                        string   grantedAccessLabel = batchParts[0].Trim();
                        string   linkedUsername     = batchParts[1].Trim();
                        string   linkedBatchName    = batchParts[2].Trim();

                        batchName = linkedBatchName;
                    }

                    string realFileOwner = XMLHelper.getFileOwner(fileName, batchName);
                    comm_.postFile(fileName, realFileOwner, batchName, false, fileName);
                }


                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.command    = CommMessage.Command.downloadFiles;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = "";
                return(replyMsg);
            };

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

            // getUsersList
            Func <Msg, Msg> action7 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command    = CommMessage.Command.getUsersList;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getUsersXMLList();
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.getUsersList, action7);

            // grantFileAccess
            Func <Msg, Msg> action8 = (Msg msg) =>
            {
                string result = msg.stringBody;

                XElement xmlTree = XElement.Parse(result);
                var      element = (from e in xmlTree.Descendants("File")
                                    select e);

                foreach (var e in element)
                {
                    XMLHelper.grantFileAccess(e.Element("Name").Value, msg.author, e.Element("GrantAccess").Value, e.Element("Batch").Value);
                }

                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command    = CommMessage.Command.greatFileAccess;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getUsersXMLList();
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.greatFileAccess, action8);

            // execAnalytics
            Func <Msg, Msg> action9 = (Msg msg) =>
            {
                string batchName = XMLHelper.getSingleValueFromString(msg.stringBody, "BatchName");

                string fullPath   = "..\\..\\Storage\\" + msg.author + "\\" + batchName;
                string outputFile = "..\\..\\Storage\\" + msg.author + "\\" + batchName + "\\" + batchName + ".xml";
                execAnayzer(fullPath, "*.cs", outputFile);

                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.command    = CommMessage.Command.execAnalytics;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = "<StringBody><Result>" + "ok" + "</Result></StringBody>";
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.execAnalytics, action9);

            // getAnalytics
            Func <Msg, Msg> action10 = (Msg msg) =>
            {
                string batchName = XMLHelper.getSingleValueFromString(msg.stringBody, "BatchName");

                if (batchName.Contains("Granted Access Batch"))
                {
                    string[] batchParts         = batchName.Split('-');
                    string   grantedAccessLabel = batchParts[0].Trim();
                    string   linkedUsername     = batchParts[1].Trim();
                    string   linkedBatchName    = batchParts[2].Trim();

                    batchName = linkedBatchName;
                }

                string batchOwner    = XMLHelper.getBatchOwner(batchName);
                string analyticsFile = "..\\..\\Storage\\" + batchOwner + "\\" + batchName + "\\" + batchName + ".xml";

                List <string> filenameList = XMLHelper.getFileNamesInAnalytics(analyticsFile);

                foreach (string filename in filenameList)
                {
                    string metrixXML = XMLHelper.getAnalyticsForFile(analyticsFile, filename);

                    Msg replyMsg = new Msg(Msg.MessageType.request);
                    replyMsg.command    = CommMessage.Command.getAnalytics;
                    replyMsg.author     = "server";
                    replyMsg.to         = msg.from;
                    replyMsg.from       = msg.to;
                    replyMsg.stringBody = XMLHelper.getAnalyticsForFileXML(analyticsFile, filename);
                    comm_.postMessage(replyMsg);
                }

                Msg reply = new Msg(Msg.MessageType.noReply);
                return(reply);
            };

            dispatcher_.addCommand(Msg.Command.getAnalytics, action10);

            // searchBatch
            Func <Msg, Msg> action11 = (Msg msg) =>
            {
                string searchTerm = XMLHelper.getSingleValueFromString(msg.stringBody, "SearchTerm");

                Msg replyMsg = new Msg(Msg.MessageType.request);
                replyMsg.command    = CommMessage.Command.searchBatch;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getSearchForBatchXML(msg.author, searchTerm);
                comm_.postMessage(replyMsg);

                Msg reply = new Msg(Msg.MessageType.noReply);
                return(reply);
            };

            dispatcher_.addCommand(Msg.Command.searchBatch, action11);

            // searchFiles
            Func <Msg, Msg> action12 = (Msg msg) =>
            {
                string searchTerm = XMLHelper.getSingleValueFromString(msg.stringBody, "SearchTerm");

                Msg replyMsg = new Msg(Msg.MessageType.request);
                replyMsg.command    = CommMessage.Command.searchFiles;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getSearchForFilesXML(msg.author, searchTerm);
                comm_.postMessage(replyMsg);

                Msg reply = new Msg(Msg.MessageType.noReply);
                return(reply);
            };

            dispatcher_.addCommand(Msg.Command.searchFiles, action12);

            // addComment
            Func <Msg, Msg> action13 = (Msg msg) =>
            {
                string batch    = XMLHelper.getSingleValueFromString(msg.stringBody, "Batch");
                string filename = XMLHelper.getSingleValueFromString(msg.stringBody, "Name");
                string comment  = XMLHelper.getSingleValueFromString(msg.stringBody, "Comment");

                XMLHelper.commentsAddNode(msg.author, filename, batch, comment);

                Msg reply = new Msg(Msg.MessageType.noReply);
                return(reply);
            };

            dispatcher_.addCommand(Msg.Command.addComment, action13);

            // getComment
            Func <Msg, Msg> action14 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.request);
                replyMsg.command    = CommMessage.Command.getComments;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getCommentsXML(msg.author);
                comm_.postMessage(replyMsg);

                Msg reply = new Msg(Msg.MessageType.noReply);
                return(reply);
            };

            dispatcher_.addCommand(Msg.Command.getComments, action14);
        }
Exemplo n.º 4
0
        /*----< here server responses to messages are defined >--------*/

        void initializeDispatcher()
        {
            // login
            Func <Msg, Msg> action1 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command = CommMessage.Command.login;
                replyMsg.author  = "server";
                replyMsg.to      = msg.from;
                replyMsg.from    = msg.to;

                if (Authentication.authenticateUser(msg.author, XMLHelper.getSingleValueFromString(msg.stringBody, "Password")))
                {
                    replyMsg.stringBody = "<StringBody><Result>" + "ok" + "</Result></StringBody>";
                }
                else
                {
                    replyMsg.stringBody = "<StringBody><Result>" + "error" + "</Result></StringBody>";
                }

                return(replyMsg);
            };

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

            // annouceBatch
            Func <Msg, Msg> action2 = (Msg msg) =>
            {
                //Console.WriteLine("received announce, need to save batch {0}", XMLHelper.getSingleValueFromString(msg.stringBody, "BatchName"));

                System.IO.Directory.CreateDirectory(ServerEnvironment.storagePath);
                System.IO.Directory.CreateDirectory(ServerEnvironment.storagePath + msg.author);


                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.command    = CommMessage.Command.announceBatch;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = "<StringBody><Result>" + "ok" + "</Result></StringBody>";
                return(replyMsg);
            };

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

            // annouceFile
            Func <Msg, Msg> action3 = (Msg msg) =>
            {
                XMLHelper.filesAddNode(
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Filename"),
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Batch"),
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Owner")
                    );

                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.command    = CommMessage.Command.announceFile;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = "<StringBody><Result>" + "ok" + "</Result></StringBody>";
                return(replyMsg);
            };

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

            // getBatchList
            Func <Msg, Msg> action4 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command    = CommMessage.Command.getBatchList;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getBatchesXMLList(msg.author);
                return(replyMsg);
            };

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

            // getBatchList
            Func <Msg, Msg> action5 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command    = CommMessage.Command.getBatchFileList;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getBatchesFileListXMLList(
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Batch"),
                    XMLHelper.getSingleValueFromString(msg.stringBody, "Owner")
                    );
                return(replyMsg);
            };

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

            // download files
            Func <Msg, Msg> action6 = (Msg msg) =>
            {
                string result = msg.stringBody;

                XElement xmlTree = XElement.Parse(result);
                var      element = (from e in xmlTree.Descendants("File")
                                    select e);

                foreach (var s in element)
                {
                    string fileName      = s.Element("Name").Value;
                    string batchName     = s.Element("Batch").Value;
                    string realFileOwner = XMLHelper.getFileOwner(fileName, batchName);
                    comm_.postFile(fileName, realFileOwner, false);
                }


                Msg replyMsg = new Msg(Msg.MessageType.noReply);
                replyMsg.command    = CommMessage.Command.downloadFiles;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = "";
                return(replyMsg);
            };

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

            // getUsersList
            Func <Msg, Msg> action7 = (Msg msg) =>
            {
                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command    = CommMessage.Command.getUsersList;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getUsersXMLList();
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.getUsersList, action7);

            // grantFileAccess
            Func <Msg, Msg> action8 = (Msg msg) =>
            {
                string result = msg.stringBody;

                XElement xmlTree = XElement.Parse(result);
                var      element = (from e in xmlTree.Descendants("File")
                                    select e);

                foreach (var e in element)
                {
                    XMLHelper.grantFileAccess(e.Element("Name").Value, msg.author, e.Element("GrantAccess").Value);
                }

                Msg replyMsg = new Msg(Msg.MessageType.reply);
                replyMsg.command    = CommMessage.Command.greatFileAccess;
                replyMsg.author     = "server";
                replyMsg.to         = msg.from;
                replyMsg.from       = msg.to;
                replyMsg.stringBody = XMLHelper.getUsersXMLList();
                return(replyMsg);
            };

            dispatcher_.addCommand(Msg.Command.greatFileAccess, action8);
        }