コード例 #1
0
        /// <summary>
        /// Runs a client that allows the user to search the database for records and update found records
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            int       port      = 8021;

            //Parse command line arguments
            int argc = args.GetUpperBound(0);

            if (argc > 2)
            {
                for (int i = 0; i < argc; i++)
                {
                    try
                    {
                        //Port
                        if (args[i] == "-p" && (i + 1) < argc)
                        {
                            port = int.Parse(args[i + 1]);
                        }
                        //Ip address
                        else if (args[i] == "-i" && (i + 1) < argc)
                        {
                            ipAddress = IPAddress.Parse(args[i + 1]);
                        }
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Failed to accept arguments: {0} {1}:", args[i], args[i + 1]);
                        // Print usage statement...
                        Console.WriteLine("Usage: Client1 [-p port] [-i ipaddress]");
                    }
                }
            }

            Console.WriteLine("(FindClient): Starting...");
            try
            {
                DatabaseClient client = new DatabaseClient();
                Console.WriteLine("(FindClient): Connecting");
                client.Connect(ipAddress.ToString(), port);

                //Find loop
                while (true)
                {
                    int    memid = 0;
                    string memid_input;
                    bool   success = false;

                    do
                    {
                        success = true;
                        Console.Write("Enter an integer value ({0} to quit): ", kQuitValue);
                        memid_input = Console.ReadLine();

                        try
                        {
                            memid = int.Parse(memid_input);
                        }
                        catch (FormatException)
                        {
                            success = false;
                        }
                        catch (OverflowException)
                        {
                            success = false;
                        }
                    }while (!success);

                    if (memid == kQuitValue)
                    {
                        break;
                    }

                    try
                    {
                        //Query server for record
                        Console.WriteLine("(FindClient): Checking for Record {0}", memid);
                        DataRecord record = client.Find(memid);

                        Console.WriteLine("MemberID:{0}", record.MemberID);
                        Console.WriteLine("First name:{0}", record.FirstName);
                        Console.WriteLine("Last name:{0}", record.LastName);
                        Console.WriteLine("DOB:{0}", record.DateOfBirth);

                        Console.WriteLine("Update record?(y/n):");
                        string answer = Console.ReadLine();

                        //Update record
                        if (answer == "y" || answer == "Y")
                        {
                            try
                            {
                                Console.WriteLine("New first name(Blank for original):");
                                string newFirstName = Console.ReadLine();
                                Console.WriteLine("New lastfirst name(Blank for original):");
                                string newLastName = Console.ReadLine();
                                Console.WriteLine("New DOB( MM/DD/YYYY HH:MM:SS )(Blank for original):");
                                string newDOB = Console.ReadLine();
                                if (newDOB != "")
                                {
                                    record.DateOfBirth = DateTime.Parse(newDOB);
                                }
                                if (newFirstName != "")
                                {
                                    record.FirstName = newFirstName;
                                }
                                if (newLastName != "")
                                {
                                    record.LastName = newLastName;
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.Write("(FindClient): Error, ");
                                Console.WriteLine(ex.Message);
                                continue;
                            }
                            client.Update(record);
                        }
                    }
                    catch (ArgumentException)
                    {
                        Console.WriteLine("(FindClient): Error, bad response");
                        continue;
                    }
                    catch (OutOfMemoryException)
                    {
                        Console.WriteLine("(FindClient): Database is full");
                        continue;
                    }
                    catch (KeyNotFoundException)
                    {
                        Console.WriteLine("(FindClient): Record with MemberID {0} does not exist", memid);
                        continue;
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("(FindClient): Failure to recieve valid response");
                        continue;
                    }

                    catch (DatabaseException)
                    {
                        Console.WriteLine("(DataClient): Database Error");
                    }
                }

                Console.WriteLine("(FindClient): Disconnecting");
                client.Disconnect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("(FindClient): Shutting Down");
            Console.ReadKey();
        }
コード例 #2
0
ファイル: Client1.cs プロジェクト: Wiles/rd_assign1
        /// <summary>
        /// Runs a client that connects to the server and automatically feeds in random records to add to the database
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
            int       port      = 8021;
            int       delayms   = 100;

            //Parse Command Line arguments
            int argc = args.GetUpperBound(0);

            if (argc > 2)
            {
                for (int i = 0; i < argc; i++)
                {
                    try
                    {
                        //delay
                        if (args[i] == "-d" && (i + 1) < argc)
                        {
                            delayms = int.Parse(args[i + 1]);
                        }
                        //Port
                        else if (args[i] == "-p" && (i + 1) < argc)
                        {
                            port = int.Parse(args[i + 1]);
                        }
                        //ip address
                        else if (args[i] == "-i" && (i + 1) < argc)
                        {
                            ipAddress = IPAddress.Parse(args[i + 1]);
                        }
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Failed to accept arguments: {0} {1}:", args[i], args[i + 1]);
                        // Print usage statement...
                        Console.WriteLine("Usage: Client1 [-d delay][-p port][-i ipaddress]");
                    }
                }
            }

            Console.WriteLine("(DataClient): Starting...");
            try
            {
                Random         rand   = new Random();
                DatabaseClient client = new DatabaseClient();
                Console.WriteLine("(DataClient): Connecting");
                client.Connect(ipAddress.ToString(), port);

                for (int i = 1; i <= 40000; i++)
                {
                    int namemin = kRandomNames.GetLowerBound(0);
                    int namemax = kRandomNames.GetUpperBound(0);

                    string firstname = kRandomNames[rand.Next(namemin, namemax)];
                    string lastname  = kRandomNames[rand.Next(namemin, namemax)];

                    string   datestr = rand.Next(int.MaxValue / 4).ToString() + rand.Next(int.MaxValue / 4).ToString();
                    DateTime date    = DateTime.FromFileTime(long.Parse(datestr));

                    Console.WriteLine("(DataClient): Inserting Record {0}", i);
                    Console.WriteLine("(DataClient):\tFirstName: {0}\tLastName: {1}\tDate: {2}", firstname, lastname, date);

                    try
                    {
                        DataRecord record = new DataRecord(0, firstname, lastname, date);
                        client.Insert(record);
                    }
                    catch (ArgumentException)
                    {
                        Console.WriteLine("(DataClient): Error, bad response");
                    }
                    catch (OutOfMemoryException)
                    {
                        Console.WriteLine("(DataClient): Database is full");
                    }
                    catch (DatabaseException)
                    {
                        Console.WriteLine("(DataClient): Database Error");
                    }

                    Thread.Sleep(delayms);
                }

                Console.WriteLine("(DataClient): Disconnecting");
                client.Disconnect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("(DataClient): Shutting Down");
            Console.ReadKey();
        }