//initiates the TextQueryHandler to get the text files that belong to a category protected override void ProcessMessages() { while (true) { ServiceMessage msg = bq.deQ(); Console.Write("\n {0} Recieved Message:\n", msg.TargetCommunicator); msg.ShowMessage(); TextQueryHandler textqueryhandler = new TextQueryHandler(); /*List<List<string>> dummy = new List<List<string>>(); * dummy.Add(msg.category);*/ List <string> textfiles = textqueryhandler.getTextFiles(msg.category); ServiceMessage reply = ServiceMessage.MakeTextFilesMessage("client-echo", "textfiles", textfiles, "reply from textfiles"); reply.TargetUrl = msg.SourceUrl; reply.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply); //break; } }
//initiates the QueryHandler for metadata query processing and text query processing protected override void ProcessMessages() { while (true) { ServiceMessage msg = bq.deQ(); if (!msg.ResourceName.Equals("textquery")) { QueryHandler queryhandler = new QueryHandler(); List <List <string> > results = queryhandler.MetaQuery(msg.nlist); Console.Write("\n {0} Recieved Message:\n", msg.TargetCommunicator); msg.ShowMessage(); //Console.Write("\n Query processing is an exercise for students\n"); if (msg.Contents == "quit") { break; } ServiceMessage reply = ServiceMessage.MakeQueryMessage("client-echo", "query", results, "reply from query"); reply.TargetUrl = msg.SourceUrl; reply.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply); } else { TextQueryHandler textqueryhandler = new TextQueryHandler(); List <List <string> > results = textqueryhandler.textQuery(msg.nlist); if (msg.Contents == "quit") { break; } ServiceMessage reply = ServiceMessage.MakeQueryMessage("client-echo", "query", results, "reply from tquery"); reply.TargetUrl = msg.SourceUrl; reply.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply); } } }
//----< get and process message from client>---------------- protected override void ProcessMessages() { MappingTool mt = new MappingTool(); while (true) { ServiceMessage msg = bq.deQ(); Console.Write("\n {0} Recieved Message:\n", msg.TargetCommunicator); //msg.ShowMessage(); //Console.Write("\n Navigation processing is an exercise for students\n"); if (msg.Contents == "quit") { break; } /////////////////////////////////////////////////////////////////////////////////////// // find files relationship and construct parents and child map when client // connect with server if (msg.Contents == "mapping") { mt.Mapping(); ServiceMessage reply1 = ServiceMessage.MakeMessage("client-nav", "nav", "mapping finished", "mapping finished"); reply1.TargetUrl = msg.SourceUrl; reply1.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply1); } ////////////////////////////////////////////////////////////////////////////////////// // client send current file name to server, server find the parents and children // then reply if (msg.ResourceName == "file relationship") { string parents = null; parents = mt.findParents(msg.Contents); ServiceMessage reply2 = ServiceMessage.MakeMessage("client-nav", "nav", parents, "found parents"); reply2.TargetUrl = msg.SourceUrl; reply2.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply2); } } }
//----------< extract query information from message from client >---------- public List <string> queryProcessing(ServiceMessage msg) { //List<string> foundFiles = new List<string>(); //foundFiles.Clear(); int i, j; string message = msg.Contents; string categories = null; string contents = null; string[] queryCategories = null; string[] queryContents = null; for (i = 0; message[i] != '&'; i++) { categories += message[i]; } for (j = i + 1; j < message.Length; j++) { contents += message[j]; } char[] separator = { ' ' }; queryCategories = categories.Split(separator); queryContents = contents.Split(separator); if (msg.ResourceName == "textqueryA") { //how to send the query result to the client UI? TextQuery(queryCategories, queryContents, true); } else if (msg.ResourceName == "textqueryO") { TextQuery(queryCategories, queryContents, false); } else if (msg.ResourceName == "metadataquery") { XmlQuery(queryCategories, queryContents); } return(foundFiles); }
//----< get and process message from server>---------------- protected override void ProcessMessages() { while (true) { ServiceMessage msg = bq.deQ(); ///////////////////////////////////////////////////////////////// // receive query results and show in UI mainWin.dispatcher.Invoke( new Action <string>(mainWin.AddQueryResult), System.Windows.Threading.DispatcherPriority.Background, new string[] { msg.Contents } ); if (msg.Contents == "quit") { msg.TargetCommunicator = "dispatcher"; AbstractMessageDispatcher.GetInstance().PostMessage(msg); break; } } }
//not using this public void getFileContent(ServiceMessage msg) { Server s = new Server(); //s.GetFileContent(msg); string file = msg.Contents; long blockSize = 512; string filename = @"..\\DocumentVault\\" + file; try { FileStream fs; fs = File.Open(filename, FileMode.Open, FileAccess.Read); int bytesRead = 0; while (true) { long remainder = (int)(fs.Length - fs.Position); if (remainder == 0) { break; } long size = Math.Min(blockSize, remainder); byte[] block = new byte[size]; bytesRead = fs.Read(block, 0, block.Length); ServiceMessage msg4 = ServiceMessage.MakeMessage("client-echo", "file_Comm", System.Text.Encoding.Default.GetString(block), "FileContent"); msg4.TargetUrl = msg.SourceUrl; msg4.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher1 = AbstractMessageDispatcher.GetInstance(); dispatcher1.PostMessage(msg4); } fs.Flush(); fs.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } }
protected override void ProcessMessages() { while (true) { ServiceMessage msg = bq.deQ(); Console.Write("\n {0} Recieved Message:\n", msg.TargetCommunicator); msg.ShowMessage(); if (msg.Contents == "quit") { if (Verbose) { Console.Write("\n Echo shutting down"); } // shut down dispatcher msg.TargetCommunicator = "dispatcher"; AbstractMessageDispatcher.GetInstance().PostMessage(msg); break; } } }
//initiates the FileHandler o create a new for the uploaded file and create corresponding metadata file protected override void ProcessMessages() { while (true) { ServiceMessage msg = bq.deQ(); var appDomain = AppDomain.CurrentDomain; string temp1 = appDomain.BaseDirectory; Directory.SetCurrentDirectory(temp1); Directory.SetCurrentDirectory(@"..\\Testfiles\\textfiles"); if (msg.ResourceName != "metadata") { string fileName = msg.ResourceName.Remove(msg.ResourceName.IndexOf(",")); FileStream fs = null; if (File.Exists(fileName)) { byte[] block_data = Encoding.UTF8.GetBytes(msg.Contents); fs = File.Open(fileName, FileMode.Append, FileAccess.Write); fs.Write(block_data, 0, block_data.Length); } else { byte[] block_data = Encoding.UTF8.GetBytes(msg.Contents); fs = File.Open(fileName, FileMode.Create, FileAccess.Write); fs.Write(block_data, 0, block_data.Length); } fs.Close(); } else { MetadataHandler mdhandler = new MetadataHandler(); mdhandler.createMetadata(msg.list); } } }
//initiates the EditMetadataHandler for editing metadata protected override void ProcessMessages() { while (true) { ServiceMessage msg = bq.deQ(); EditMetadataHandler queryhandler = new EditMetadataHandler(); //string message = queryhandler.editMetadata(msg.tag); //Console.Write("\n {0} Recieved Message:\n", msg.TargetCommunicator); //msg.ShowMessage(); //Console.Write("\n Query processing is an exercise for students\n"); if (msg.Contents == "quit") { break; } ServiceMessage reply = ServiceMessage.MakeMessage("client-echo", "query", " ", "reply from editmetadata"); reply.TargetUrl = msg.SourceUrl; reply.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply); } }
//----< default behavior of MessageDispatchers >----------- protected override void ProcessMessages() { while (true) { ServiceMessage msg = bq.deQ(); if (Verbose) { string txt = "MessageDispatcher processing message for " + msg.TargetCommunicator + "\n"; ServiceMessage.ShowText(txt); } if (CommunicatorLookup.ContainsKey(msg.TargetCommunicator)) { CommunicatorLookup[msg.TargetCommunicator].PostMessage(msg); // send on to TargetCommunicator } else if (msg.TargetCommunicator == this.Name && msg.Contents == "quit") // quit if sent to me { if (Verbose) { Console.Write("\n dispatcher stopping"); } break; } else // can't find TargetCommunicator { if (CommunicatorLookup.ContainsKey("sender")) { CommunicatorLookup["sender"].PostMessage(msg); } else { Console.Write("\n message destination \"{0}\" unknown\n", msg.TargetCommunicator); } } } }
public void PostMessage(ServiceMessage msg) { bq.enQ(msg); }
//----< Post a message to the MessageDispatcher >---------------- public void PostMessage(ServiceMessage servMsg) { //Console.Write("\n CommService.PostMessage called with Message:"); //msg.ShowMessage(); dispatcher.PostMessage(servMsg); }
//----< get and process message from server>---------------- protected override void ProcessMessages() { while (true) { ServiceMessage msg = bq.deQ(); ////////////////////////////////////////////////////////////////////////////////// // receive message which contains category information // then show in UI if (msg.ResourceName == "Categories") { mainWin.dispatcher.Invoke( new Action <string>(mainWin.AddCategory), System.Windows.Threading.DispatcherPriority.Background, new string[] { msg.Contents } ); string message = "Connection successful!"; mainWin.dispatcher.Invoke( new Action <string>(mainWin.connected), System.Windows.Threading.DispatcherPriority.Background, new string[] { message } ); } /////////////////////////////////////////////////////////////////////////////////// // receive message which contains files information in specified category // then show in UI if (msg.ResourceName == "files found in category") { mainWin.dispatcher.Invoke( new Action <string>(mainWin.AddFile), System.Windows.Threading.DispatcherPriority.Background, new string[] { msg.Contents } ); } //////////////////////////////////////////////////////////////////////////////////// // receive message which contains textfile content // then show in UI if (msg.ResourceName == "textfile") { mainWin.dispatcher.Invoke( new Action <string>(mainWin.showText), System.Windows.Threading.DispatcherPriority.Background, new string[] { msg.Contents } ); } //////////////////////////////////////////////////////////////////////////////////// // receive message which contains xmlfile content // then show in UI if (msg.ResourceName == "xmlfile") { mainWin.dispatcher.Invoke( new Action <string>(mainWin.showXml), System.Windows.Threading.DispatcherPriority.Background, new string[] { msg.Contents } ); mainWin.dispatcher.Invoke( new Action <string>(mainWin.showChildren), System.Windows.Threading.DispatcherPriority.Background, new string[] { msg.Contents } ); } //////////////////////////////////////////////////////////////////////////////////// if (msg.Contents == "quit") { msg.TargetCommunicator = "dispatcher"; AbstractMessageDispatcher.GetInstance().PostMessage(msg); break; } //////////////////////////////////////////////////////////////////////////////////// } }
public void Stop() { ServiceMessage msg = ServiceMessage.MakeMessage(this.Name, this.Name, "quit"); this.PostMessage(msg); }
//----< get and process message from client>---------------- protected override void ProcessMessages() { while (true) { ShowFiles sf = new ShowFiles(); ServiceMessage msg = bq.deQ(); Console.Write("\n {0} Recieved Message:\n", msg.TargetCommunicator); Console.Write("\n Echo processing completed\n"); if (msg.Contents == "connect to server") { ClientURL = msg.ResourceName; } //////////////////////////////////////////////////////////////////////////////////////////// // client ask for category root in repositpry, find categories repository files belong to // and send the msg back to client, msg contain categories if (msg.ResourceName == "category") { List <string> Categories = new List <string>(); Categories = sf.FindCategories(); foreach (string cate in Categories) { ServiceMessage reply = ServiceMessage.MakeMessage("client-echo", "ServiceServer", cate, "Categories"); reply.TargetUrl = msg.SourceUrl; reply.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply); } } /////////////////////////////////////////////////////////////////////////////////////////// // client require for files in the specified category // server find files and send back if (msg.ResourceName == "files in this category") { List <string> filesInCate = new List <string>(); filesInCate = sf.FindFiles(msg.Contents); foreach (string file in filesInCate) { ServiceMessage reply = ServiceMessage.MakeMessage("client-echo", "ServiceServer", file, "files found in category"); reply.TargetUrl = msg.SourceUrl; reply.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply); } } /////////////////////////////////////////////////////////////////////////////////////////// //send the xml and text file content back if (msg.ResourceName == "extract file") { ExtractFile ef = new ExtractFile(); string textfile = ef.TextFile(msg.Contents); string xmlfile = ef.XmlFile(msg.Contents); ServiceMessage reply1 = ServiceMessage.MakeMessage("client-echo", "ServiceServer", textfile, "textfile"); reply1.TargetUrl = msg.SourceUrl; reply1.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher1 = AbstractMessageDispatcher.GetInstance(); dispatcher1.PostMessage(reply1); ServiceMessage reply2 = ServiceMessage.MakeMessage("client-echo", "ServiceServer", xmlfile, "xmlfile"); reply2.TargetUrl = msg.SourceUrl; reply2.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher2 = AbstractMessageDispatcher.GetInstance(); dispatcher2.PostMessage(reply2); } if (msg.Contents == "quit") { break; } } }
//Note:The analyzer is showing the function lines as 328 not sure why for this function void filedownloader(ServiceMessage msg,string filename) { long blockSize = 512; try { FileStream fs = File.Open(@"..\..\DocumentVault\" + filename, FileMode.Open, FileAccess.Read); int bytesRead = 0; while (true) { long remainder = (int)(fs.Length - fs.Position); if (remainder == 0) break; long size = Math.Min(blockSize, remainder); byte[] block = new byte[size]; bytesRead = fs.Read(block, 0, block.Length); ServiceMessage reply = ServiceMessage.MakeMessage("client-echo", "echo", Encoding.ASCII.GetString(block), "download" + "," + filename); reply.TargetUrl = msg.SourceUrl; reply.SourceUrl = msg.TargetUrl; AbstractMessageDispatcher dispatcher = AbstractMessageDispatcher.GetInstance(); dispatcher.PostMessage(reply); } fs.Close(); } catch (Exception ex) { Console.Write("\n can't open {0} for writing - {1}", msg.Contents, ex.Message); } }
static void Main(string[] args) { Console.Write("\n Starting CommService Client"); Console.Write("\n =============================\n"); string ServerUrl = "http://localhost:8000/CommService"; Sender sender = null; Console.Write("\n Press key to start client: "); Console.ReadKey(); sender = new Sender(); sender.Connect(ServerUrl); sender.Start(); string ClientUrl = "http://localhost:8001/CommService"; Receiver receiver = new Receiver(ClientUrl); // Don't need to start receiver unless you want // to send it messages, which we won't as all // our messages go to the server //receiver.Start(); EchoCommunicator echo = new EchoCommunicator(); echo.Name = "client-echo"; receiver.Register(echo); echo.Start(); ServiceMessage msg1 = ServiceMessage.MakeMessage("echo", "ServiceClient", "<root>some echo stuff</root>", "no name"); msg1.SourceUrl = ClientUrl; msg1.TargetUrl = ServerUrl; Console.Write("\n Posting message to \"{0}\" component", msg1.TargetCommunicator); sender.PostMessage(msg1); ServiceMessage msg2 = ServiceMessage.MakeMessage("query", "ServiceClient", "<root>some query stuff</root>", "no name"); msg2.SourceUrl = ClientUrl; msg2.TargetUrl = ServerUrl; Console.Write("\n Posting message to \"{0}\" component", msg2.TargetCommunicator); sender.PostMessage(msg2); ServiceMessage msg3 = ServiceMessage.MakeMessage("nav", "ServiceClient", "<root>some nav stuff</root>", "no name"); msg3.SourceUrl = ClientUrl; msg3.TargetUrl = ServerUrl; Console.Write("\n Posting message to \"{0}\" component", msg3.TargetCommunicator); sender.PostMessage(msg3); // wait for all server replies to be sent back Console.Write("\n Wait for Server replies, then press a key to exit: "); Console.ReadKey(); sender.Stop(); // this function sends a quit message to client-echo sender.Wait(); echo.Stop(); echo.Wait(); receiver.Close(); Console.Write("\n\n"); }
public static ServiceMessage MakeMessage(string TargetCommunicator, string SourceCommunicator, string contents, string resName = "") { ServiceMessage msg = new ServiceMessage(); msg.TargetCommunicator = TargetCommunicator; msg.SourceCommunicator = SourceCommunicator; msg.ResourceName = resName; msg.Contents = contents; return msg; }