// main function 
    static void Main(string[] args)
    {
      Console.Title = "Simple Server";
      String.Format("Simple Server Started listing on {0}", port).title('=');
      SimpleSender sndr = new SimpleSender();
      Receiver rcvr = new Receiver(port, address);
      rcvr.StartService();
      while(true)      {
        Message msg = rcvr.getMessage();
        Console.Write("\n  Simple Server received:");
        Utilities.showMessage(msg);
        if (msg.content == "done")        {
          Console.WriteLine();
          rcvr.shutDown();
          sndr.shutdown();
          break;        }
        if (msg.content == "connection start message")          continue;
        msg.content = "Simple Server received: " + msg.content;
        Utilities.swapUrls(ref msg);
        if(sndr.goodStatus == true)        {
                    #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
          int count = 0;
          for (int i = 0; i < 1000; ++i)            {
            Message testMsg = new Message();
            testMsg.toUrl = msg.toUrl;
            testMsg.fromUrl = msg.fromUrl;
            testMsg.content = String.Format("test message #{0}", ++count);
            Console.Write("\n  sending testMsg: {0}", testMsg.content);
            sndr.sendMessage(testMsg);          }
#else
          /////////////////////////////////////////////////
          // Use the statement below for normal operation
          sndr.sendMessage(msg);
#endif
        }        else        {
          Console.Write("\n  closing\n");
          rcvr.shutDown();
          sndr.shutdown();          break;        }
        Console.WriteLine();      }    } }}
    //----< get Receiver and Sender running >----------------------------

    void setupChannel()
    {
      rcvr = new Receiver(localPort, localAddress);
      Action serviceAction = () =>
      {
        try
        {
          Message rmsg = null;
          while (true)
          {
            rmsg = rcvr.getMessage();
            Action act = () => { postRcvMsg(rmsg.content); };
            Dispatcher.Invoke(act, System.Windows.Threading.DispatcherPriority.Background);
          }
        }
        catch(Exception)
        {
        //  Action act = () => { lStat.Text = ex.Message; };
       //   Dispatcher.Invoke(act);
        }
      };
      if (rcvr.StartService())
      {
        rcvr.doService(serviceAction);
      }

    //  sndr = new wpfSender(lStat, this.Dispatcher);
    }
 //Main function parses and sends the messages to server//
 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));
     Receiver rcvr = new Receiver(srvr.port, srvr.address);
     Action serviceAction = () =>               {
         Message msg = null;
         DBEngine<int, DBElement<int, string>> dbserver = new DBEngine<int, DBElement<int, string>>(); //new DBEngine
         QueryEngine QE = new QueryEngine();
         HiResTimer timer = new HiResTimer(); //new object for timer
         while (true)                 {
             msg = rcvr.getMessage();   // note use of non-service method to deQ messages
             Console.Write("\n  Received message:");
             Console.Write("\n  sender is {0}", msg.fromUrl);
             Console.Write("\n  content is {0}\n", msg.content);
             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; }
             timer.Start();                  //start timer
             XElement insertelem = XElement.Parse(msg.content);
             XElement res = new XElement("result", "not found");
             processor rdbserver = new processor();
             Console.WriteLine("\n----------write client operations----------");
             Console.WriteLine("\n");
             //----------select the required method to perform operations------------//
             if (insertelem.Element("Type").Value.Equals("Insert"))
                 res = rdbserver.insert(insertelem, dbserver);
                else if (insertelem.Element("Type").Value.Equals("Delete"))
                 res = rdbserver.Delete(insertelem, dbserver);
              else if (insertelem.Element("Type").Value.Equals("EditName"))
                 res = rdbserver.EditName(insertelem, dbserver);
              else if (insertelem.Element("Type").Value.Equals("getvalue"))
                 res = rdbserver.getvalue(insertelem, dbserver, QE);
              else if (insertelem.Element("Type").Value.Equals("EditDescr"))
                 res = rdbserver.editdescr(insertelem, dbserver);
              else if (insertelem.Element("Type").Value.Equals("getchildren"))
                 res = rdbserver.getchildren(insertelem, dbserver, QE);
             else if (insertelem.Element("Type").Value.Equals("Persist"))
                 res = rdbserver.persistdb(insertelem, dbserver);
             else   Console.Write("   operation failed   ");
              Console.WriteLine("\n-------------server response----------");
             XElement response = new XElement("resonse");
             response.Add(res);
             timer.Stop();              //stop timer
             Console.WriteLine("the time taken for operation is {0}", timer.ElapsedMicroseconds + " MicroSeconds ");
             srvr.send_wpf_msg(timer.ElapsedMicroseconds, sndr);
             Util.swapUrls(ref msg);
             msg.content = response.ToString();
             sndr.sendMessage(msg);             //sending message    
               }   };
     if (rcvr.StartService())
     rcvr.doService(serviceAction); // This serviceAction is asynchronous so the call doesn't block.
     Util.waitForUser();        }    }
 public Action doAction(Receiver rcvr)
 {
     Action receiveAction = () =>
       {
           Message msg = null;
           while (true)
           {
               msg = rcvr.getMessage();
               Console.WriteLine("\n message received");
               Console.Write("\n");
               if (msg.content == "closereceiver")
                   break;
               if (msg.content == "connection start message")
                   continue;
               XElement dbe = XElement.Parse(msg.content);
               string resp = dbe.Element("result").Value;
               Console.WriteLine("\n----------server response----------");
               Console.WriteLine(resp);
           }
       };
     return receiveAction;
 }
 // main function to send messages
 static void Main(string[] args)         {
     Thread.Sleep(2000);
     Console.Write("\n  starting CommService client");
     Console.Write("\n =============================\n");
     Console.Title = "Client #1";
     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);
     if (rcvr.StartService())            {
         Action newact = clnt.doAction(rcvr);
         rcvr.doService(newact);             }
     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;            }
     XDocument doc = XDocument.Load("./../../../dbload.xml");
     Console.WriteLine(doc.ToString());
     Console.WriteLine(" xml  is loaded ");
     Console.WriteLine();
     XElement dbe = doc.Element("root");
     int count = Int32.Parse(dbe.Element("count").Value);
     for (int i = 0; i < count; i++)             {
         foreach (var a in dbe.Elements("ClientMessage"))                 {
             msg = new Message();
             msg.fromUrl = clnt.localUrl;
             msg.toUrl = clnt.remoteUrl;
             Console.Write("\n  sending {0}\n", msg.content);
             Console.Write("\n");
             msg.content = a.ToString();
             sndr.sendMessage(msg);
             Thread.Sleep(200);                 }             }
     Thread.Sleep(200);   // 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
     rcvr.shutDown();            sndr.shutdown();            Console.Write("\n\n");
 }    } }
    //----< Test Stub >--------------------------------------------------

#if (TEST_RECEIVER)

    static void Main(string[] args)
    {
      Util.verbose = true;

      Console.Title = "CommService Receiver";
      Console.Write("\n  Starting CommService Receiver");
      Console.Write("\n ===============================\n");

      Receiver rcvr = new Receiver();
      rcvr.ProcessCommandLine(args);

      Console.Write("\n  Receiver url = {0}\n", Util.makeUrl(rcvr.address, rcvr.port));

      // serviceAction defines what the server does with received messages

      if (rcvr.StartService())
      {
        //rcvr.doService();
        rcvr.doService(rcvr.defaultServiceAction());  // equivalent to rcvr.doService()
      }
      Console.Write("\n  press any key to exit: ");
      Console.ReadKey();
      Console.Write("\n\n");
    }