//----< Test Stub >-------------------------------------------------- #if (TEST_SENDER) static void Main(string[] args) { Util.verbose = false; Console.Write("\n starting CommService Sender"); Console.Write("\n =============================\n"); Console.Title = "CommService Sender"; Sender sndr = new Sender("http://localhost:8081/CommService"); sndr.processCommandLine(args); int numMsgs = 5; int counter = 0; Message msg = null; while (true) { msg = new Message(); msg.fromUrl = sndr.localUrl; msg.toUrl = sndr.remoteUrl; msg.content = "Message #" + (++counter).ToString(); Console.Write("\n sending {0}", msg.content); sndr.sendMessage(msg); Thread.Sleep(30); if (counter >= numMsgs) { break; } } // demonstrate: // - sending to listener // - attempting to send to nolistener // - sending to listener msg = new Message(); msg.fromUrl = sndr.localUrl; msg.toUrl = "http://localhost:9999/CommService"; msg.content = "no listener for this message"; Console.Write("\n sending {0}", msg.content); sndr.sendMessage(msg); msg = new Message(); msg.fromUrl = sndr.localUrl; msg.toUrl = sndr.remoteUrl; msg.content = "Message #" + (++counter).ToString(); Console.Write("\n sending {0}", msg.content); sndr.sendMessage(msg); msg = new Message(); msg.fromUrl = sndr.localUrl; msg.toUrl = sndr.remoteUrl; msg.content = "closeSender"; // message for self and Receiver Console.Write("\n sending {0}", msg.content); sndr.sendMessage(msg); }
//----< Test Stub >-------------------------------------------------- #if (TEST_SENDER) static void Main(string[] args) { Util.verbose = false; Console.Write("\n starting CommService Sender"); Console.Write("\n =============================\n"); Console.Title = "CommService Sender"; Sender sndr = new Sender("http://localhost:8081/CommService"); sndr.processCommandLine(args); int numMsgs = 5; int counter = 0; Message msg = null; while (true) { msg = new Message(); msg.fromUrl = sndr.localUrl; msg.toUrl = sndr.remoteUrl; msg.content = "Message #" + (++counter).ToString(); Console.Write("\n sending {0}", msg.content); sndr.sendMessage(msg); Thread.Sleep(30); if (counter >= numMsgs) break; } // demonstrate: // - sending to listener // - attempting to send to nolistener // - sending to listener msg = new Message(); msg.fromUrl = sndr.localUrl; msg.toUrl = "http://localhost:9999/CommService"; msg.content = "no listener for this message"; Console.Write("\n sending {0}", msg.content); sndr.sendMessage(msg); msg = new Message(); msg.fromUrl = sndr.localUrl; msg.toUrl = sndr.remoteUrl; msg.content = "Message #" + (++counter).ToString(); Console.Write("\n sending {0}", msg.content); sndr.sendMessage(msg); msg = new Message(); msg.fromUrl = sndr.localUrl; msg.toUrl = sndr.remoteUrl; msg.content = "closeSender"; // message for self and Receiver Console.Write("\n sending {0}", msg.content); sndr.sendMessage(msg); }
static void Main(string[] args) { Util.verbose = false; Server srvr = new Server(); srvr.ProcessCommandLine(args); Console.Title = "Server"; Console.Write(String.Format("\n Starting CommService server listening on port {0}", srvr.port)); Console.Write("\n ====================================================\n"); Sender sndr = new Sender(Util.makeUrl(srvr.address, srvr.port)); //Sender sndr = new Sender(); Receiver rcvr = new Receiver(srvr.port, srvr.address); //srvr.addValue(2); // - serviceAction defines what the server does with received messages // - This serviceAction just announces incoming messages and echos them // back to the sender. // - Note that demonstrates sender routing works if you run more than // one client. Action serviceAction = () => { Message msg = null; int key; while (true) { msg = rcvr.getMessage(); // note use of non-service method to deQ messages Message testMsg = new Message(); Console.Write("\n Received message:"); Console.Write("\n sender is {0}", msg.fromUrl); Console.Write("\n Content is: {0}", msg.content); if (msg.content.StartsWith("<"))//handling regular message { XDocument xml = XDocument.Parse(msg.content); XElement element = xml.Element("Message").Element("Type"); switch(element.Value){ case "Add"://add key/value pair testMsg.content = srvr.addValue(xml); break; case "Delete"://delete key/value pair element = xml.Element("Message").Element("Key"); testMsg.content = "Value with key "+ element.Value +" has been deleted\n\n"; testMsg.content += srvr.deleteData(xml); break; case "Edit"://edit value key = srvr.editValue(xml); if(key<0) testMsg.content = "Value cannot be found"; else testMsg.content = "Value with key "+ key +" has be edited"; testMsg.content += srvr.returnDBAsString("int"); break; case "ToXML"://persist database to xml srvr.persistDB(xml); testMsg.content = "The content of database has been converted to XML and saved in Test.xml"; break; case "RecoverDB"://recover database from xml srvr.recoverDB(xml); testMsg.content = "The database has been recover from a XML file"; break; case "Query"://handling a query testMsg.content = srvr.query(xml); break; default: testMsg.content = "Invalid request."; break; } } if (msg.content == "connection start message") { continue; // don't send back start message } if (msg.content == "done") { Console.Write("\n client has finished\n"); continue; } if (msg.content == "closeServer") { Console.Write("received closeServer"); break; } msg.content = "received " + msg.content + " from " + msg.fromUrl; // swap urls for outgoing message Util.swapUrls(ref msg); #if (TEST_WPFCLIENT) ///////////////////////////////////////////////// // The statements below support testing the // WpfClient as it receives a stream of messages // - for each message received the Server // sends back 1000 messages // testMsg.toUrl = msg.toUrl; testMsg.fromUrl = msg.fromUrl; Console.Write("\n sending reply: {0}", testMsg.content); WriteLine(); sndr.sendMessage(testMsg);//sending replies #else ///////////////////////////////////////////////// // Use the statement below for normal operation sndr.sendMessage(msg); #endif } }; if (rcvr.StartService()) { rcvr.doService(serviceAction); // This serviceAction is asynchronous, } // so the call doesn't block. Util.waitForUser(); //srvr.showDB(); }
static void Main(string[] args) { WriteRequestParser re = new WriteRequestParser(); Console.Write("\n starting CommService client"); Console.Write("\n =============================\n"); Console.Title = "Write Client"; WriteClient clnt = new WriteClient(); clnt.processCommandLine(args); string localPort = Util.urlPort(clnt.localUrl); string localAddr = Util.urlAddress(clnt.localUrl); Receiver rcvr = new Receiver(localPort, localAddr); if (rcvr.StartService()) { rcvr.doService(rcvr.defaultServiceAction()); } Sender sndr = new Sender(clnt.localUrl); // Sender needs localUrl for start message Message msg = new Message(); msg.fromUrl = clnt.localUrl; msg.toUrl = clnt.remoteUrl; Console.Write("\n sender's url is {0}", msg.fromUrl); Console.Write("\n attempting to connect to {0}\n", msg.toUrl); if (!sndr.Connect(msg.toUrl)) { Console.Write("\n could not connect in {0} attempts", sndr.MaxConnectAttempts); sndr.shutdown(); rcvr.shutDown(); return; } clnt.request = re.parse("writeRequest.xml"); HiResTimer hrt = new HiResTimer(); ulong total = 0; foreach (string i in clnt.request) { msg = new Message(); msg.fromUrl = clnt.localUrl; msg.toUrl = clnt.remoteUrl; msg.content = i; if (clnt.verbose == 1) { Console.Write("\n Sending: {0}", msg.content); } hrt.Start(); if (!sndr.sendMessage(msg)) { break; } hrt.Stop(); total += hrt.ElapsedMicroseconds; Thread.Sleep(100); } //hrt.Stop(); /*Use the following for testing the client * * int numMsgs = 5; * int counter = 0; * while (true) * { * msg.content = "Hello"; * Console.Write("\n sending {0}", msg.content); * if (!sndr.sendMessage(msg)) * return; * Thread.Sleep(100); ++counter; * if (counter >= numMsgs) * break; * } * */ Console.Write("\n Total time taken for sending all write messages {0} microseconds\n", total); msg = new Message(); msg.fromUrl = clnt.localUrl; msg.toUrl = clnt.remoteUrl; msg.content = "done"; sndr.sendMessage(msg); // Wait for user to press a key to quit. // Ensures that client has gotten all server replies. Util.waitForUser(); // shut down this client's Receiver and Sender by sending close messages try { rcvr.shutDown(); sndr.shutdown(); } catch { Console.Write("\n Problem with closing sender and/or receiver\n"); } Console.Write("\n\n"); }
static void Main(string[] args) { RequestEngine re = new RequestEngine(); QueryRequestEngine qre = new QueryRequestEngine(); DBEngine <string, DBElement <string, List <string> > > db = new DBEngine <string, DBElement <string, List <string> > >(); Util.verbose = false; Server srvr = new Server(); srvr.ProcessCommandLine(args); Console.Title = "Server"; Console.Write(String.Format("\n Starting CommService server listening on port {0}", srvr.port)); Console.Write("\n ====================================================\n"); Sender sndr = new Sender(Util.makeUrl(srvr.address, srvr.port)); //Sender sndr = new Sender(); Receiver rcvr = new Receiver(srvr.port, srvr.address); // - serviceAction defines what the server does with received messages // - This serviceAction just announces incoming messages and echos them // back to the sender. // - Note that demonstrates sender routing works if you run more than // one client. HiResTimer hrt = new HiResTimer(); PerfTest pt = new PerfTest(); Action serviceAction = () => { Message msg = null; while (true) { msg = rcvr.getMessage(); // note use of non-service method to deQ messages //insert(); int length = msg.content.IndexOf(",") + 1; string from = ""; if (msg.content != "connection start message") { from = msg.content.Substring(0, length); //figure out where the request came from } string[] msgList = msg.content.Split(','); Console.Write("\n Received message:"); Console.Write("\n Request type is: {0}", from); //denotes where the request came from Console.Write("\n sender is {0}", msg.fromUrl); Console.Write("\n content is {0}\n", msg.content); string reply = ""; //------------< do processing based on the request type >------------ if (from == "write,") { hrt.Start(); re.parseRequest(msg.content, out reply); hrt.Stop(); pt.add(msgList[1], hrt.ElapsedMicroseconds); msg.content = reply; } else if (from == "read,") { hrt.Start(); db = re.getDB(); qre.parseRequest(db, msg.content, out reply); hrt.Stop(); pt.add(msgList[1], hrt.ElapsedMicroseconds); msg.content = reply; } else if (from == "perf,") { reply = pt.printTimesWpf(msgList[1]); msg.content = reply; } if (msg.content == "connection start message") { continue; // don't send back start message } if (msg.content == "done") { Console.Write("\n client has finished\n"); Console.WriteLine(pt.printTimes()); continue; } if (msg.content == "closeServer") { Console.Write("received closeServer"); break; } // swap urls for outgoing message Util.swapUrls(ref msg); #if (TEST_WPFCLIENT) ///////////////////////////////////////////////// // The statements below support testing the // WpfClient as it receives a stream of messages // - for each message received the Server // sends back 1000 messages // Use the code to test the server /*int count = 0; * for (int i = 0; i < 1000; ++i) * { * Message testMsg = new Message(); * testMsg.toUrl = msg.toUrl; * testMsg.fromUrl = msg.fromUrl; * testMsg.content = String.Format("test message #{0}", ++count); * Console.Write("\n sending testMsg: {0}", testMsg.content); * sndr.sendMessage(testMsg); * }*/ Message testMsg = new Message(); testMsg.toUrl = msg.toUrl; testMsg.fromUrl = msg.fromUrl; testMsg.content = String.Format("test message"); Console.Write("\n sending testMsg: {0}", testMsg.content); sndr.sendMessage(testMsg); #else ///////////////////////////////////////////////// // Use the statement below for normal operation sndr.sendMessage(msg); //re.db.show<string, DBElement<string, List<string>>, List<string>, string>(); #endif } }; if (rcvr.StartService()) { rcvr.doService(serviceAction); // This serviceAction is asynchronous, } // so the call doesn't block. Util.waitForUser(); }
static void Main(string[] args) { Console.Write("\n starting Read Client for requirement #7"); Console.Write("\n =============================\n"); Console.Title = "ReadClient"; ReadClient clnt = new ReadClient(); clnt.processCommandLine(args); Console.Title = "ReadClient"; string localPort = Util.urlPort(clnt.localUrl); string localAddr = Util.urlAddress(clnt.localUrl); Receiver rcvr = new Receiver(localPort, localAddr); if (rcvr.StartService()) { rcvr.doService(rcvr.defaultServiceAction()); } Sender sndr = new Sender(clnt.localUrl); // Sender needs localUrl for start message Message msg = new Message(); msg.fromUrl = clnt.localUrl; msg.toUrl = clnt.remoteUrl; Console.Write("\n sender's url is {0}", msg.fromUrl); Console.Write("\n attempting to connect to {0}\n", msg.toUrl); if (!sndr.Connect(msg.toUrl)) { Console.Write("\n could not connect in {0} attempts", sndr.MaxConnectAttempts); sndr.shutdown(); rcvr.shutDown(); return; } XmlDocument doc = new XmlDocument(); doc.Load("readClientTest.xml"); XmlNodeList xmlnode = doc.GetElementsByTagName("Message"); Stopwatch watch = new Stopwatch(); watch.Start();//start watch while (true)//read and send messages { msg = new Message(); msg.fromUrl = clnt.localUrl; msg.toUrl = clnt.remoteUrl; for (int i = 0; i < xmlnode.Count; i++) { msg.content = "<?xml version=\"1.0\" encoding=\"utf - 8\" standalone=\"yes\"?>" + xmlnode[i].OuterXml; Console.Write("\n sending {0}\n", msg.content); if (!sndr.sendMessage(msg)) return; Thread.Sleep(100); } break; } msg.content = "done"; sndr.sendMessage(msg); // Wait for user to press a key to quit. // Ensures that client has gotten all server replies. watch.Stop(); string time = watch.ElapsedMilliseconds.ToString(); Console.Write("Elapsed time: " + time + " millionsecondns\n\n");//print time elapsed Util.waitForUser(); // shut down this client's Receiver and Sender by sending close messages rcvr.shutDown(); sndr.shutdown(); Console.Write("\n\n"); }