static void Main(string[] args) { Server server = new Server(); (String.Format("\n Starting Server where databases reside")).Wrap(); server.ProcessCommandLine(args); Console.Title = "Server"; Sender ServerSender = new Sender(UtilityMethods.makeUrl(server.ServerAddress, server.ServerPort)); Receiver ServerReceiver = new Receiver(server.ServerPort, server.ServerAddress); Console.WriteLine(" Listening on ServerPort {0}\n", server.ServerPort); server.LoadDB(); HiResTimer HRTimer = new HiResTimer(); bool first = true; int TotalNOC = 0, NOC = 0; // To know number of clients connected to server Action serviceAction = () => { Message ServerQuery = null; while (true) { bool a = ServerReceiver.IsEMPTY(); // checks whether receiving Q is empty if (!(first == false && a == true)) // This check helps in determining the time when server is done with the processing of all queries { ServerQuery = ServerReceiver.getMessage(); // note use of non-service method to deQ messages first = false; if (ServerQuery.MessageContent != "closeReceiver" && !ServerQuery.MessageContent.StartsWith("<NoOfClient")) { String.Format("\n Received new message from {0}", ServerQuery.FromURL).Wrap(); Console.Write("\n MessageContent is\n {0}", ServerQuery.MessageContent); Console.Write("\n Message was sent on {0}", ServerQuery.TimeSent); Console.Write("\n Message was received on {0}", DateTime.Now); Console.Write("\n Message took {0} milliseconds or {1} microseconds on communication channel\n", (DateTime.Now - ServerQuery.TimeSent).TotalMilliseconds, (DateTime.Now.Ticks / 10 - ServerQuery.TimeSent.Ticks / 10)); } if (ServerQuery.MessageContent == "connection start message") { continue; // don't send back start message } if (ServerQuery.MessageContent == "closeReceiver") { Console.Write(" Received closeReceiver"); break; } if (ServerQuery.MessageContent.StartsWith("<QueryType")) { ++TotalQueries; server.ProcessMessageQuery(ref ServerQuery, ref ServerSender, ref HRTimer); Thread.Sleep(100); } if (ServerQuery.MessageContent.StartsWith("<MessageStream")) { ++TotalQueries; server.ProcessMessageStream(ServerQuery.MessageContent); Thread.Sleep(100); } if ((ServerQuery.MessageContent.StartsWith("<NoOfClient"))) { TotalNOC = int.Parse(XDocument.Parse(ServerQuery.MessageContent).Root.Value); } if (ServerQuery.MessageContent.StartsWith("DONE")) // Increment counter as a client is done. If number of elements in ProxyDB is equal to this, it means all clients are done. { ++NOC; // Increment it every time a client is done "\n\n Please wait while performance is calculated. It may take some time as performance is being calculated at a very granular level".Wrap(); " Almost there. Just a peek of performance in XML format".Wrap(); ServerSender.Connect(UtilityMethods.makeUrl("localhost", "8081")); //// WPF Port Address. Change it if you change WPF local port. XDocument SendPerformance = server.PerformanceMeasurer(); // Message PM = new Message(); // If yes, send the performance results to WPF PM.MessageContent = SendPerformance.ToString(); // PM.FromURL = UtilityMethods.makeUrl(server.ServerAddress, server.ServerPort); // PM.ToURL = UtilityMethods.makeUrl("localhost", "8081"); Console.WriteLine("\n" + SendPerformance.ToString() + "\n" + "\n One may check the summary of server hosting the database under 'Summary' tab of UserInterface."); ServerSender.sendMessage(PM); } } else { //TotalNOC = TestExecutive.NoOfClients; if (NOC != 0 && TotalNOC != 0 && TotalNOC == NOC) // Check if all clients are done sending messages to server AND server is done sending all messages. If yes, shutdown receiver. // Requirement odf shutting down is met when clients are started using WPF { ServerReceiver.shutDown(); } } } }; if (ServerReceiver.StartService()) { ServerReceiver.doService(serviceAction); // This serviceAction asynchronous, so the call doesn't block. } // Test.DisplayDBafterProcessing(); // When readers and writers are initiated by WPF, it asks whether to display DBs after processing UtilityMethods.waitForUser(); }
// Method is used to process and send the query for operation on databases as it is received by receiver of server public void ProcessMessageQuery(ref Message ServerQuery, ref Sender ServerSender, ref HiResTimer HRTimer) { XDocument XMLDoc = XDocument.Parse(ServerQuery.MessageContent); var Queries = XMLDoc.Element("QueryType").Elements(); ////// string QueryType = null, DBType = null; // List <string> Results = new List <string>(); // XElement Query = XMLDoc.Root; // List <string> ParameterSelected = new List <string>(); // Query is forwarded as string of XML message to server from client List <string> ParameterValue = new List <string>(); // It fetches paramaters and values required to process the query on DB. QueryType = Query.Nodes().OfType <XText>().FirstOrDefault().Value; // DBType = Query.FirstAttribute.Value.ToString(); // if (Query.Elements("Parameters").Any()) // { ////////// foreach (var Param in Query.Element("Parameters").Descendants()) { ParameterSelected.Add(Param.Name.ToString()); ParameterValue.Add(Param.Value.ToString()); } } HRTimer.Start(); // Timer is started as it is forwarded to process the query on database Results = TestExecutive.QueryProcessing(DBType, QueryType, ParameterSelected, ParameterValue); HRTimer.Stop(); // Timer is stopped as database is done with the processing of query ServerQuery.ServerProcessTime = HRTimer.ElapsedMicroseconds; TimeKeeper(QueryType, HRTimer.ElapsedMicroseconds); foreach (string result in Results) { Query.Add(new XElement("Result", result)); // Results of query are added in the same received message } UtilityMethods.swapUrls(ref ServerQuery); // Same message is sent back with no update on TimeSent or TimeReceived property in order to ServerQuery.MessageContent = XMLDoc.ToString(); // calculate the round-trip time of the message, and that's the reason why same message is used instead of creating a new one ServerSender.sendMessage(ServerQuery); }