static void Main(string[] args)
 {
   Console.Write("\n  starting CommService client");
   Console.Write("\n =============================\n");
   Client clnt = new Client();
   clnt.processCommandLine(args);
   string localPort = Util.urlPort(clnt.localUrl);
   string localAddr = Util.urlAddress(clnt.localUrl);
   Receiver rcvr = new Receiver(localPort, localAddr);
   Console.Title = "Read Client: " + localPort;
   if (rcvr.StartService())
   {
     rcvr.doService(doserviceAction(rcvr, clnt));
   }
   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);
     shutdown(rcvr, sndr);
     return;
   }
   "Reading read1.xml file".title();
   string path = Path.GetFullPath("../../../Read Client/bin/Debug/read1.xml");
   XDocument newDoc = XDocument.Load(path);
   Parser p = new Parser();
   clnt.read_client_latency.Start();
   p.parse(newDoc, ref msg, sndr);
   Message msg1 = new Message();
   msg1.fromUrl = clnt.localUrl;
   msg1.toUrl = clnt.remoteUrl;
   msg1.content = "done";
   sndr.sendMessage(msg1);
   while (clnt.flag) ; //wait till last message is received from server
   Message msg2 = new Message();
   msg2.fromUrl = clnt.localUrl;
   msg2.toUrl = clnt.remoteUrl;
   msg2.content = "read-client," + clnt.read_client_latency.ElapsedMicroseconds;
   sndr.sendMessage(msg2);
   // 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
   shutdown(rcvr, sndr);
   Console.Write("\n\n");
 }
Esempio n. 2
0
 static void Main(string[] args) { 
     Console.Write("\n  starting CommService client");
     Console.Write("\n =============================\n");
     HiResTimer timerReader = new HiResTimer();
     Client clnt = new Client();
     clnt.processCommandLine(args);
     string localPort = args[0];
     clnt.localUrl = "http://localhost:" + localPort + "/CommService";
     string localAddr = Util.urlAddress(clnt.localUrl);
     Receiver rcvr = new Receiver(localPort, localAddr);
     rcvr.setTimerFromClient(timerReader);
     clnt.startRcvrService(rcvr);
     Sender sndr = new Sender(clnt.localUrl);  // Sender needs localUrl for start message
     MessageMaker readerInput = new MessageMaker();
     string fileName = ".\\ReaderClientInput.xml";
     Message msg = new Message();
     int numRequests = Convert.ToInt32(args[1]);
     XDocument xmldoc = XDocument.Load(fileName);
     var requests = xmldoc.Descendants("Request");
     int requestCount = 0, i = 0; List<XElement> totalrequest = new List<XElement>();
     while (i < numRequests) {    //set the specified number of requests to send
         totalrequest.Add(requests.ElementAt(i)); i++;}
     timerReader.Start();
     foreach (var request in totalrequest) {   //send each request to the message maker to create a message
         msg = new Message();
         msg = readerInput.makeMessage(clnt.localUrl, clnt.remoteUrl, request);
         Console.Title = "Reader Client to query the NoSQl database: Querying " + (++requestCount) + " requests";
         if (!sndr.Connect(msg.toUrl)) {      //send url of the destination to the sender
             Console.Write("\n  could not connect in {0} attempts", sndr.MaxConnectAttempts);
             sndr.shutdown();
             rcvr.shutDown();
             return;
         }
         Thread.Sleep(700);
         while (true) {
                 clnt.printMessage(msg);         //print the message contents
                 if (!sndr.sendMessage(msg))     // send the message to the sender
                     return;
                 Thread.Sleep(100);
                 break;}
     sndr.sendLatencyReader(rcvr.avgLatency);}
     Console.Write("\n  Sender's url is {0}", msg.fromUrl);
     Console.Write("\n  Attempting to connect to {0}\n", msg.toUrl);
     msg.content = "done";
     sndr.sendMessage(msg);
     Util.waitForUser();
     rcvr.shutDown();
     sndr.shutdown();
 } } }
 //--------< Define action to be performed on receiving message >------
 private static Action doserviceAction(Receiver rcvr, Client clnt)
 {
   Action serviceAction = () =>
   {
     if (Util.verbose)
       Console.Write("\n  starting Receiver.defaultServiceAction");
     Message msg1 = null;
     while (true)
     {
       msg1 = rcvr.getMessage();   // note use of non-service method to deQ messages
       Console.Write("\n  Received message:");
       Console.Write("\n  sender is {0}", msg1.fromUrl);
       Console.Write("\n  content is {0}\n", msg1.content);
       if (msg1.content == "done")
       {
         clnt.read_client_latency.Stop();
         clnt.flag = false;
         Console.WriteLine("\n\n Read Client latency: " + clnt.read_client_latency.ElapsedMicroseconds + " microseconds\n\n");
       }
       if (msg1.content == "closeReceiver")
         break;
     }
   };
   return serviceAction;
 }