Esempio n. 1
0
    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();
    }
Esempio n. 2
0
    //----< 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);
    }
Esempio n. 3
0
        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");
            
        }