// It process a message stream which contains a number of queries // It splits the message stream into individual query messages which are sent one by one to server to be processed. public void ProcessStream(ref Message ReceivedMessage, ref WriteClient Writeclient, ref Sender WCSender) { XDocument XMLDoc = XDocument.Parse(ReceivedMessage.MessageContent); Console.WriteLine("\n Latest message stream to WriteClient has been currently saved in the directory\n"); " XML Message stream being sent to the server for processing".Wrap(); var Queries = XMLDoc.Element("MessageStream").Elements(); try { foreach (var Query in Queries) { int NumberofQ = int.Parse(Query.LastAttribute.Value); // Last attribute contains information about number of queries of this particular type in stream Query.LastAttribute.Remove(); // It is removed as every message is sent to server as a standalone entity for (int i = 1; i <= NumberofQ; ++i) { Message MSG = new Message(); MSG.FromURL = ReceivedMessage.ToURL; MSG.ToURL = Writeclient.RemoteURL; MSG.MessageContent = Query.ToString(); MSG.TimeSent = DateTime.Now; if (Writeclient.LogMessages == true) // Requirement 6 is met here as sent messages are logged to console only if command prompt argument says so { String.Format(" Sending a message to {0}", MSG.ToURL).Wrap(); Console.Write("\n Sender is {0}", MSG.FromURL); Console.Write("\n MessageContent is\n {0}", MSG.MessageContent); } WCSender.sendMessage(MSG); // message is sent across using sender of write client Thread.Sleep(100); } } } catch (Exception e) { Console.WriteLine("\n " + e.Message + "\n"); } }
static void Main(string[] args) { "\n Starting Write Client or writer for database".Wrap(); Console.Title = "WriteClient"; WriteClient Writeclient = new WriteClient(); Writeclient.processCommandLine(args); String.Format(" Url of Write Client is {0}", Writeclient.LocalURL).Wrap(); string localPort = UtilityMethods.urlPort(Writeclient.LocalURL); string localAddr = UtilityMethods.urlAddress(Writeclient.LocalURL); Receiver WCReceiver = new Receiver(localPort, localAddr); Sender WCSender = new Sender(Writeclient.LocalURL); int NumberofQ = 0; XDocument XMLDoc = new XDocument(); Message MessageStream = new Message(); // New service action is defined for receiver which handles the way receiver behaves on received message in WriteClient Action WCserviceAction = () => { try { Message ReceivedMessage = new Message(); while (true) { ReceivedMessage = WCReceiver.getMessage(); // note use of non-service method to deQ messages if (ReceivedMessage.ToURL != ReceivedMessage.FromURL) { ReceivedMessage.TimeReceived = DateTime.Now; ReceivedMessage.TravelTime = DateTime.Now - ReceivedMessage.TimeSent; "\n Received a new message".Wrap(); Console.Write("\n Sender is {0}", ReceivedMessage.FromURL); Console.Write("\n MessageContent is\n {0}", ReceivedMessage.MessageContent); Console.Write("\n Message was sent on {0}", ReceivedMessage.TimeSent); Console.Write("\n Message was received on {0}", DateTime.Now); //Demonstarting another way to convert milliseconds to microseconds. Although, all server measurments of time are done using high resolution timer. Console.Write("\n Message took {0} milliseconds or {1} microseconds on communication channel (round-trip, including its processing)\n", (DateTime.Now - ReceivedMessage.TimeSent).TotalMilliseconds, (DateTime.Now.Ticks / 10 - ReceivedMessage.TimeSent.Ticks / 10)); if (ReceivedMessage.FromURL == Writeclient.RemoteURL) { Console.Write("\n Server took {0} microseconds to process this query)\n", ReceivedMessage.ServerProcessTime); } } if (ReceivedMessage.MessageContent.StartsWith("<MessageStream>")) { Writeclient.ProcessStream(ref ReceivedMessage, ref Writeclient, ref WCSender); "XML Message stream have been sent to the server for processing".Wrap(); MessageStream = ReceivedMessage; XMLDoc = XDocument.Parse(ReceivedMessage.MessageContent); foreach (var element in XMLDoc.Element("MessageStream").Elements()) // To know the total number of queries in original message { NumberofQ += int.Parse(element.LastAttribute.Value); } XMLDoc.Element("MessageStream").Elements().Remove(); // As client would be receiving two types of messages. } // One is MessageStream XML message and other is Reply messages from server if (ReceivedMessage.MessageContent.StartsWith("<QueryType")) { XElement QueryResult = XElement.Parse(ReceivedMessage.MessageContent); XMLDoc.Root.Add(QueryResult); --NumberofQ; // Reference for received message stream is saved in order to send the same message back to its source if (NumberofQ == 0) // NumberofQ counted the number of queries. When all are processed, { // server is notified about that. Message Final = new Message(); Final.ToURL = Writeclient.RemoteURL; Final.FromURL = Writeclient.LocalURL; Final.MessageContent = "DONE !! All queries from this client have been processed."; Final.TimeSent = DateTime.Now; WCSender.sendMessage(Final); MessageStream.MessageContent = XMLDoc.ToString(); XMLDoc.Save("Results.xml"); if (MessageStream.ToURL != MessageStream.FromURL) { UtilityMethods.swapUrls(ref MessageStream); WCSender.sendMessage(MessageStream); } } } if (ReceivedMessage.MessageContent == "closeReceiver") { break; } } } catch (Exception e) { Console.WriteLine(" " + e.Message); } }; if (WCReceiver.StartService()) { WCReceiver.doService(WCserviceAction); // Receiver for write client is started to perform the action defined } // Comment following lines till 209 to enable this client to be able to process user defined query // comment following lines in order to disable this client to perform queries automatically Message LoadXMLMessage = new Message(); LoadXMLMessage.ToURL = Writeclient.LocalURL; LoadXMLMessage.FromURL = Writeclient.LocalURL; string ID = UtilityMethods.urlPort(Writeclient.LocalURL).Substring(2); LoadXMLMessage.MessageID = "WC" + ID; LoadXMLMessage.MessageContent = XDocument.Load("WCMessageStream.xml").ToString(); // It is used to load the previously created message stream. It is fed to its processing queue. MessageStream = LoadXMLMessage; // Reference is saved in order to avoid any loss of information. WCSender.sendMessage(LoadXMLMessage); Thread.Sleep(100); UtilityMethods.waitForUser(); Console.Write("\n\n"); }