コード例 #1
0
        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");
        }
コード例 #2
0
        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();
        }