Exemplo n.º 1
0
 // function to getchildren
 public XElement getchildren(XElement dbe, DBEngine <int, DBElement <int, string> > db, QueryEngine QE)
 {
     Console.WriteLine("\n The children List is obtained ");
     QE.querychildren <int, DBElement <int, string>, string>(db, 1);
     if (QE.Equals(null))
     {
         XElement f = new XElement("result", " no children in list ");
         return(f);
     }
     else
     {
         XElement s = new XElement("result", " children obtained from the specified key ");
         return(s);
     }
 }
        // function to edit instances
        public static bool editInstance <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Data new_instance)
        {
            Value value;

            if (dbedit.getValue(key1, out value))
            {
                DBElement <Key, Data> elem = value as DBElement <Key, Data>;
                elem.payload = new_instance;
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public Value queryvalue <Key, Value, Data>(DBEngine <Key, Value> db, Key key)
        {
            Value getqueryvalue;
            bool  key_present = db.getValue(key, out getqueryvalue);

            if (key_present)                                 // check if key is present
            {
                return(getqueryvalue);                       //if present return value
            }
            else
            {
                Console.WriteLine("key not present");        // else error message
            }
            return(default(Value));
        }
        // Function to perform editing of name in metedata
        public static bool editName <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, string new_name)
        {
            Value value;

            if (dbedit.getValue(key1, out value))
            {
                DBElement <Key, Data> elem = value as DBElement <Key, Data>;
                elem.name = new_name;
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public List <key> querychildren <key, value, Data>(DBEngine <key, value> db, key Key)
        {
            value getqueryvalue;
            bool  key_present          = db.getValue(Key, out getqueryvalue);    // check if key present
            DBElement <key, Data> temp = getqueryvalue as DBElement <key, Data>; // create new element to store value

            if (key_present)
            {
                return(temp.children);
            }
            else
            {
                Console.WriteLine("invalid key");                               // if key not present error message
            }
            return(null);
        }
Exemplo n.º 6
0
        //function to getvalue
        public XElement getvalue(XElement dbe, DBEngine <int, DBElement <int, string> > db, QueryEngine QE)
        {
            Console.WriteLine("\n value of the  particular key is returned ");
            DBElement <int, string> dbelem = new DBElement <int, string>();

            QE.queryvalue <int, DBElement <int, string>, string>(db, 2);
            if (QE.Equals(null))
            {
                XElement f = new XElement("result", "\n no key present ");
                return(f);
            }
            else
            {
                XElement s = new XElement("result", "\n Value obtained ");
                return(s);
            }
        }
    {                  // Function to perform addition of relationships
        public static bool addRelations <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Key key2)
        {
            Value val1, val2;
            bool  key1_present = dbedit.getValue(key1, out val1);

            // check if key1 is present
            if (key1_present)
            {
                bool key2_present = dbedit.getValue(key2, out val2);
                // check if key2 is present
                if (key2_present)
                {
                    DBElement <Key, Data> element = val1 as DBElement <Key, Data>;
                    element.children.Add(key1);
                }
            }
            return(true);
        }
Exemplo n.º 8
0
        //function to edit description
        public XElement editdescr(XElement dbe, DBEngine <int, DBElement <int, string> > db)
        {
            Console.Write("\n ----------Edit description Operation----------");
            Console.Write("\nediting the description  of key= 4");
            Console.WriteLine("\n");
            Console.WriteLine("\n  Bachelors changed to BE ");
            bool result = db.editDescr <int, DBElement <int, string>, string>(Int32.Parse((dbe.Element("key").Value)), " BE ");

            db.showDB();
            if (result == true)
            {
                XElement t = new XElement("result", "\n  element edited successfully ");
                return(t);
            }
            else
            {
                XElement f = new XElement("result", " Editing Failure ");
                return(f);
            }
        }
Exemplo n.º 9
0
        // function to edit name
        public XElement EditName(XElement dbe, DBEngine <int, DBElement <int, string> > db)
        {
            Console.Write("\n----------Edit Operation----------");
            Console.Write("\nediting the name of key==5");
            Console.WriteLine("\n name :Dogs is changed to Cats");
            bool result = db.editName <int, DBElement <int, string>, string>(Int32.Parse((dbe.Element("key").Value)), "Cats");

            db.showDB();
            Console.WriteLine("\n");
            if (result == true)
            {
                XElement t = new XElement("result", "\n  element edited successfully ");
                return(t);
            }
            else
            {
                XElement f = new XElement("result", " failure ");
                return(f);
            }
        }
Exemplo n.º 10
0
        // function to delete from database
        public XElement Delete(XElement dbe, DBEngine <int, DBElement <int, string> > db)
        {
            DBElement <int, string> elem = new DBElement <int, string>();

            Console.WriteLine("\n----------Delete Operation----------");
            Console.Write("\n Now deleting the element with key=3");
            bool result = db.remove(Int32.Parse((dbe.Element("key").Value)));

            db.showDB();
            if (result == true)
            {
                XElement s = new XElement("result", "\n Element deleted successfully ");
                return(s);
            }
            else
            {
                XElement f = new XElement("result", "Failure");
                return(f);
            }
        }
Exemplo n.º 11
0
        // main method to test DBExtensions package
        static void Main(string[] args)
        {
            "Testing DBExtensions Package".title('=');
            WriteLine();

            // creation of new element
            Write("\n --- Test DBElement<int,string> ---");
            DBElement <int, string> elem1 = new DBElement <int, string>();

            elem1.payload = "a payload";
            Write(elem1.showElement <int, string>());

            // creation of new database
            DBEngine <int, DBElement <int, string> > dbs = new DBEngine <int, DBElement <int, string> >();

            dbs.insert(1, elem1);
            dbs.show <int, DBElement <int, string>, string>();
            WriteLine();

            Write("\n --- Test DBElement<string,List<string>> ---");
            // creation of new element
            DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >();

            newelem1.name     = "newelem1";
            newelem1.descr    = "test new type";
            newelem1.children = new List <string> {
                "Key1", "Key2"
            };
            newelem1.payload = new List <string> {
                "one", "two", "three"
            };
            Write(newelem1.showElement <string, List <string>, string>());

            DBEngine <string, DBElement <string, List <string> > > dbe = new DBEngine <string, DBElement <string, List <string> > >();

            dbe.insert("key1", newelem1);
            dbe.show <string, DBElement <string, List <string> >, List <string>, string>();

            Write("\n\n");
        }
Exemplo n.º 12
0
        // test stub for processor
        // main function
        static void Main(string[] args)
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();

            "Testing the Package".title('=');
            Console.WriteLine();
            Console.WriteLine("\n remote operations are done are and tested");
            Console.Write("\n");
            DBElement <int, string> elem = new DBElement <int, string>();

            Console.WriteLine("\n----------Delete Operation----------");
            Console.Write("\n Now deleting the element with key=3");
            bool result = db.remove(2);

            db.showDB();
            if (result == true)
            {
                XElement s = new XElement("result", "\n Element deleted successfully ");
            }
            else
            {
                XElement f = new XElement("result", "Failure");
            }
        }
 public static void showEnumerableDB(this DBEngine <string, DBElement <string, List <string> > > db)
 {
     db.show <string, DBElement <string, List <string> >, List <string>, string>();
 }
        static void Main(string[] args)
        {
            "Testing ItemEditor Package".title('=');
            WriteLine();

            Write("\n --- Test DBElement<int,string> ---");

            // new database created to test the functionaity
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();

            // new elements are created and inserted to the database
            DBElement <int, string> elem1 = new DBElement <int, string>("first element", "first element description");
            DBElement <int, string> elem2 = new DBElement <int, string>("second element", "second element description");
            DBElement <int, string> elem3 = new DBElement <int, string>("third element", "third element description");

            elem1.payload = "Payload of 1";
            elem1.children.AddRange(new List <int> {
                1, 2, 3
            });

            elem2.payload = "Payload of 2";
            elem2.children.AddRange(new List <int> {
                11, 22, 33
            });

            elem3.payload = "Payload of 3";
            elem3.children.AddRange(new List <int> {
                111, 222, 333
            });

            // inserting elements to database
            db.insert(1, elem1);
            db.insert(2, elem2);
            db.insert(3, elem3);
            db.showDB();

            // testing  addition of relationships to elements
            Write("\n\n  Testing of addition of new relationships");
            bool addr1 = db.addRelations <int, DBElement <int, string>, string>(1, 2);
            bool addr2 = db.addRelations <int, DBElement <int, string>, string>(2, 3);
            bool addr3 = db.addRelations <int, DBElement <int, string>, string>(3, 111);

            db.showDB();

            // testing  removing relationships to elements
            Write("\n\n  Testing of removal  of relations");
            bool remr1 = db.removeRelation <int, DBElement <int, string>, string>(2, 11);
            bool remr2 = db.removeRelation <int, DBElement <int, string>, string>(1, 3);

            db.showDB();
            if (remr1 && remr2)
            {
                Write("\n \n Removal succeded");
            }

            // Testing editing of text data
            Write("\n\n  Testing  of editing of text data");
            bool ed_name1 = db.editName <int, DBElement <int, string>, string>(1, "renaming of element 1");
            bool ed_descr = db.editDescr <int, DBElement <int, string>, string>(1, "editing description of element 1");
            bool ed_inst  = db.editInstance <int, DBElement <int, string>, string>(1, "new instance for element 1");

            db.showDB();

            Write("\n\n --- Test DBElement<string,List<string>> ---");

            // creating elements to new database of type string,List of strings
            DBElement <string, List <string> > new_elem1 = new DBElement <string, List <string> >("new element 1", "Description of 1");
            DBElement <string, List <string> > new_elem2 = new DBElement <string, List <string> >("new element 2", "Description of 2");
            DBElement <string, List <string> > new_elem3 = new DBElement <string, List <string> >("new element 3", "Description of 3");
            //creating  new database
            DBEngine <string, DBElement <string, List <string> > > new_db = new DBEngine <string, DBElement <string, List <string> > >();

            new_elem1.payload = new List <string> {
                "First data in payload ", "Second data in payload", "Third data in payload"
            };
            new_elem1.children.AddRange(new List <string> {
                "one", "two", "three"
            });

            new_elem2.payload = new List <string> {
                "DP", "SMA", "OOD"
            };
            new_elem2.children.AddRange(new List <string> {
                "four", "five", "six"
            });

            new_elem3.payload = new List <string> {
                "CE", "CS", "EE"
            };
            new_elem3.children.AddRange(new List <string> {
                "seven", "eight", "nine"
            });

            // inserting elements to database
            new_db.insert("One", new_elem1);
            new_db.insert("Two", new_elem2);
            new_db.insert("Three", new_elem3);
            new_db.showEnumerableDB();

            // testing  addition of relationships to elements
            Write("\n\n  testing addition of relationship ");
            bool add1 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("One", "two");
            bool add2 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("Two", "three");
            bool add3 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("Three", "one");

            new_db.showEnumerableDB();
            if (add1 && add2 && add3)
            {
                Write("\n \n Adding relationship  successful.");
            }

            // testing  removing relationships to elements
            Write("\n \n  Now going to test removing of relationships in DB-element: Element-One");
            bool rem1 = new_db.removeRelation <string, DBElement <string, List <string> >, List <string> >("One", "Nine");
            bool rem2 = new_db.removeRelation <string, DBElement <string, List <string> >, List <string> >("One", "two");

            new_db.showEnumerableDB();
            if (rem1 && rem2)
            {
                Write("\n \n Deleting of relationships succeeded ");
            }

            // Testing   editing of text data
            Write("\n \n  Now going to test edition of name, description and replacing instance of payload with new instance in Element-One.");
            new_db.editName <string, DBElement <string, List <string> >, List <string> >("One", "Edited name for Element-One");
            new_db.editDescr <string, DBElement <string, List <string> >, List <string> >("One", "New description for Element-One");
            new_db.editInstance <string, DBElement <string, List <string> >, List <string> >("One", new List <string> {
                "New payload - String One", "New payload - String Two", "New payload - String Three"
            });
            new_db.showEnumerableDB();
            WriteLine();
        }
        static void Main(string[] args)
        {
            "Testing DBEngine Package".title('=');;
            WriteLine();

            "Test db of scalar elements".title();
            WriteLine();
            //creation of new elements
            DBElement <int, string> elem1 = new DBElement <int, string>();

            elem1.payload = "a payload";

            DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord");

            elem2.payload = "The Empire strikes back!";

            var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot");

            elem3.payload = "X-Wing fighter in swamp - Oh oh!";

            if (verbose)
            {
                Write("\n --- Test DBElement<int,string> ---");
                WriteLine();
                elem1.showElement();
                WriteLine();
                elem2.showElement();
                WriteLine();
                elem3.showElement();
                WriteLine();
            }

            Write("\n --- Test DBEngine<int,DBElement<int,string>> ---");
            WriteLine();

            int        key    = 0;
            Func <int> keyGen = () => { ++key; return(key); };  // function to generate keys

            // new database created
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            bool p1 = db.insert(keyGen(), elem1);
            bool p2 = db.insert(keyGen(), elem2);
            bool p3 = db.insert(keyGen(), elem3);

            if (p1 && p2 && p3)
            {
                Write("\n  all inserts succeeded");
            }
            else
            {
                Write("\n  at least one insert failed");
            }
            db.showDB();
            WriteLine();

            "Test db of enumerable elements".title();
            WriteLine();
            //new element created
            DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >();

            newelem1.name    = "newelem1";
            newelem1.descr   = "test new type";
            newelem1.payload = new List <string> {
                "one", "two", "three"
            };
            //new element created
            DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >();

            newerelem1.name    = "newerelem1";
            newerelem1.descr   = "better formatting";
            newerelem1.payload = new List <string> {
                "alpha", "beta", "gamma"
            };
            newerelem1.payload.Add("delta");
            newerelem1.payload.Add("epsilon");
            //new element created
            DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >();

            newerelem2.name  = "newerelem2";
            newerelem2.descr = "better formatting";
            newerelem2.children.AddRange(new List <string> {
                "first", "second"
            });
            newerelem2.payload = new List <string> {
                "a", "b", "c"
            };
            newerelem2.payload.Add("d");
            newerelem2.payload.Add("e");

            if (verbose)
            {
                Write("\n --- Test DBElement<string,List<string>> ---");
                WriteLine();
                newelem1.showEnumerableElement();
                WriteLine();
                newerelem1.showEnumerableElement();
                WriteLine();
                newerelem2.showEnumerableElement();
                WriteLine();
            }

            Write("\n --- Test DBEngine<string,DBElement<string,List<string>>> ---");

            int           seed    = 0;
            string        skey    = seed.ToString();
            Func <string> skeyGen = () =>
            {
                ++seed;
                skey = "string" + seed.ToString();
                skey = skey.GetHashCode().ToString();
                return(skey);
            };

            DBEngine <string, DBElement <string, List <string> > > newdb =
                new DBEngine <string, DBElement <string, List <string> > >();

            newdb.insert(skeyGen(), newelem1);
            newdb.insert(skeyGen(), newerelem1);
            newdb.insert(skeyGen(), newerelem2);
            newdb.showEnumerableDB();
            Write("\n\n");
        }
Exemplo n.º 16
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();
        }
 public static void showDB(this DBEngine <int, DBElement <int, string> > db)
 {
     db.show <int, DBElement <int, string>, string>();
 }