Пример #1
0
        //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();
        }
 //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();        }    }