コード例 #1
0
ファイル: Program.cs プロジェクト: stejacobs/BChain
        static void Main(string[] args)
        {
            BCDemo.InitializeChain();

            if (args.Length >= 1)
            {
                Port = int.Parse(args[0]);
            }
            if (args.Length >= 2)
            {
                name = args[1];
            }

            if (Port > 0)
            {
                Server = new P2PServer();
                Server.Start();
            }

            if (name != "unkown")
            {
                Console.WriteLine($"Current user is {name}");
            }


            int selection = 0;

            while (selection != 4)
            {
                switch (selection)
                {
                case 1:
                    Console.WriteLine("Please enter the server URL (enter 0 to cancel the operation)");
                    string serverURL = Console.ReadLine();
                    if (serverURL == "0")
                    {
                        break;
                    }
                    Client.Connect($"{serverURL}/bcdemo");
                    break;

                case 2:
                    Console.WriteLine("Please enter the office name (enter 0 to cancel the operation)");
                    string receiverName = Console.ReadLine();
                    if (receiverName == "0")
                    {
                        break;
                    }

                    Console.WriteLine("Please enter the property owner name (enter 0 to cancel the operation)");
                    string ownerName = Console.ReadLine();
                    if (ownerName == "0")
                    {
                        break;
                    }

                    Console.WriteLine("Please enter the property tax amount (enter 0 to cancel the operation)");
                    string amount = Console.ReadLine();
                    if (amount == "0")
                    {
                        break;
                    }

                    BCDemo.CreateTransaction(new Transaction(name, receiverName, int.Parse(amount), ownerName));
                    BCDemo.ProcessPendingTransactions(name, receiverName, ownerName);

                    int prime = BCDemo.GetBalance(name);
                    int recv  = BCDemo.GetBalance(receiverName);

                    if (prime < 0)
                    {
                        Console.WriteLine(name + " negative balance:  " + prime);
                    }
                    else
                    {
                        Console.WriteLine(name + " balance:  " + prime);
                    }

                    Console.WriteLine(receiverName + " balance:  " + recv);

                    Client.Broadcast(JsonConvert.SerializeObject(BCDemo));
                    break;

                case 3:
                    Console.WriteLine("Blockchain");
                    var obj = JsonConvert.SerializeObject(BCDemo, Formatting.Indented);
                    Console.WriteLine(obj);
                    string filename = @"c:\test\" + name + "-" + DateTime.Now.ToString("mmddyyyyhhmmss") + ".json";
                    File.WriteAllText(filename, JsonConvert.SerializeObject(BCDemo, Formatting.Indented));

                    JournalOutput(obj);

                    break;
                }

                Console.WriteLine(Environment.NewLine + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
                Console.WriteLine("1. Join the network");
                Console.WriteLine("2. Add a transaction");
                Console.WriteLine("3. Display Current Blockchain");
                Console.WriteLine("4. Exit");
                Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + Environment.NewLine);
                Console.WriteLine("Please select an option....");
                string action = Console.ReadLine();
                selection = int.Parse(action);
            }

            if (Client != null)
            {
                try
                {
                    Client.Close();
                }
                finally
                {
                    Client = null;
                }
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            BCDemo.InitializeChain();

            string ws         = ConfigurationManager.AppSettings["ws"];
            string uripath    = ConfigurationManager.AppSettings["uripath"];
            string port       = ConfigurationManager.AppSettings["port"];
            string user       = ConfigurationManager.AppSettings["user"];
            string bchainpath = ConfigurationManager.AppSettings["bchainpath"];

            if (args.Length >= 1)
            {
                Port = int.Parse(args[0]);
            }
            if (args.Length >= 2)
            {
                name = args[1];
            }

            Program.Port = int.Parse(port);

            if (Port > 0)
            {
                Server = new P2PServer();
                Server.Start();
            }

            name = user;

            if (name != "unkown")
            {
                Console.WriteLine($"Current user is {name}");
            }



            int selection = 0;

            while (selection != 4)
            {
                switch (selection)
                {
                case 1:
                    Console.WriteLine("Please enter the server URL (enter 0 to cancel the operation)");
                    string serverURL = Console.ReadLine();
                    if (serverURL == "0")
                    {
                        break;
                    }
                    P2PClient.Connect($"{serverURL}{uripath}");
                    break;

                case 2:
                    Transaction trans = new Transaction();
                    Console.WriteLine("Please enter street address (enter 0 to cancel the operation)");
                    trans.streetAddress = Console.ReadLine();
                    if (trans.streetAddress == "0")
                    {
                        break;
                    }

                    Console.WriteLine("Please enter the city (enter 0 to cancel the operation)");
                    trans.city = Console.ReadLine();
                    if (trans.city == "0")
                    {
                        break;
                    }

                    Console.WriteLine("Please enter the state (enter 0 to cancel the operation)");
                    trans.state = Console.ReadLine();
                    if (trans.state == "0")
                    {
                        break;
                    }


                    Console.WriteLine("Please enter the zip (enter 0 to cancel the operation)");
                    trans.zip = Console.ReadLine();
                    if (trans.zip == "0")
                    {
                        break;
                    }

                    Console.WriteLine("Please enter the property value (enter 0 to cancel the operation)");
                    trans.propertyValue = int.Parse(Console.ReadLine());
                    if (trans.propertyValue == 0)
                    {
                        break;
                    }

                    Console.WriteLine("Please enter the lender (enter 0 to cancel the operation)");
                    trans.lender = Console.ReadLine();
                    if (trans.lender == "0")
                    {
                        break;
                    }

                    Console.WriteLine("Please enter the customer (enter 0 to cancel the operation)");
                    trans.customer = Console.ReadLine();
                    if (trans.customer == "0")
                    {
                        break;
                    }


                    trans.clip = Guid.NewGuid().ToString("N").ToUpper();



                    Program.BCDemo.CreateTransaction(trans);
                    Program.BCDemo.ProcessPendingTransactions(
                        trans.clip,
                        trans.streetAddress,
                        trans.city,
                        trans.state,
                        trans.zip,
                        trans.propertyValue,
                        trans.lender,
                        trans.customer);

                    //BCDemo.CreateTransaction(new Transaction(name, receiverName, int.Parse(amount), ownerName));
                    //BCDemo.ProcessPendingTransactions(name, receiverName, ownerName);

                    //int prime = BCDemo.GetBalance(name);
                    //int recv = BCDemo.GetBalance(receiverName);

                    //if (prime < 0)
                    //{
                    //    Console.WriteLine(name + " negative balance:  " + prime);
                    //}
                    //else
                    //{
                    //    Console.WriteLine(name + " balance:  " + prime);

                    //}

                    //Console.WriteLine(receiverName + " balance:  " + recv);

                    P2PClient.Broadcast(JsonConvert.SerializeObject(BCDemo));
                    break;

                case 3:
                    Console.WriteLine("Blockchain");
                    var obj = JsonConvert.SerializeObject(BCDemo, Formatting.Indented);
                    Console.WriteLine(obj);
                    ;
                    if (!Directory.Exists(bchainpath))
                    {
                        Directory.CreateDirectory(bchainpath);
                    }
                    string filename = bchainpath + name + "-" + DateTime.Now.ToString("mmddyyyyhhmmss") + ".json";
                    File.WriteAllText(filename, JsonConvert.SerializeObject(BCDemo, Formatting.Indented));

                    JournalOutput(obj);

                    break;
                }

                Console.WriteLine(Environment.NewLine + "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
                Console.WriteLine("1. Join the network");
                Console.WriteLine("2. Add a transaction");
                Console.WriteLine("3. Display Current Blockchain");
                Console.WriteLine("4. Exit");
                Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + Environment.NewLine);
                Console.WriteLine("Please select an option....");
                string action = Console.ReadLine();
                selection = int.Parse(action);
            }

            if (Client != null)
            {
                try
                {
                    Client.Close();
                }
                finally
                {
                    Client = null;
                }
            }
        }
コード例 #3
0
ファイル: P2PServer.cs プロジェクト: stejacobs/P2PNetwork
        protected override void OnMessage(MessageEventArgs e)
        {
            //if (Client == null)
            //{
            //   Client = new P2PClient();

            //}

            if (e.Data.Contains("Hi Server"))
            {
                Console.WriteLine(e.Data);
                Send($"From {Program.Port}: Hi Client");
            }



            string[] selection = e.Data.Split(",");

            switch (selection[0])
            {
            case "1":
                P2PClient.Connect(selection[1]);
                break;

            case "3":
                var obj = JsonConvert.SerializeObject(Program.BCDemo, Formatting.Indented);
                Console.WriteLine(obj);
                Send(obj);
                break;

            case "2":
                string[] x = e.Data.Split(",");

                Transaction trans = new Transaction();
                trans.clip          = Guid.NewGuid().ToString("N").ToUpper();
                trans.streetAddress = x[1];
                trans.city          = x[2];
                trans.state         = x[3];
                trans.zip           = x[4];
                trans.propertyValue = int.Parse(x[5]);
                trans.lender        = x[6];
                trans.customer      = x[7];



                Program.BCDemo.CreateTransaction(trans);
                Program.BCDemo.ProcessPendingTransactions(
                    trans.clip,
                    trans.streetAddress,
                    trans.city,
                    trans.state,
                    trans.zip,
                    trans.propertyValue,
                    trans.lender,
                    trans.customer);

                //int prime = Program.BCDemo.GetBalance(name);
                //int recv = Program.BCDemo.GetBalance(receiverName);

                //if (prime < 0)
                //{
                //    Console.WriteLine(name + " negative balance:  " + prime);
                //}
                //else
                //{
                //    Console.WriteLine(name + " balance:  " + prime);

                //}

                //Console.WriteLine(receiverName + " balance:  " + recv);

                //Create Journal File
                var    journal = JsonConvert.SerializeObject(Program.BCDemo, Formatting.Indented);
                string jf      = Program.JournalOutput(journal);
                Send(jf);
                string dir = ConfigurationManager.AppSettings["bchainpath"];
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                string filename = dir + "CLBChain-" + DateTime.Now.ToString("mmddyyyyhhmmss") + ".json";
                File.WriteAllText(filename, JsonConvert.SerializeObject(Program.BCDemo, Formatting.Indented));
                trans = null;
                break;
            }

            P2PClient.Broadcast(JsonConvert.SerializeObject(Program.BCDemo));
            Send(JsonConvert.SerializeObject(Program.BCDemo));
            //Blockchain newChain = JsonConvert.DeserializeObject<Blockchain>(JsonConvert.SerializeObject(Program.BCDemo));

            //if (newChain.IsValid() && newChain.Chain.Count > Program.BCDemo.Chain.Count)
            //{
            //    Program.BCDemo.Chain = newChain.Chain;
            //}



            //if (!chainSynched)
            //{
            //    Send(JsonConvert.SerializeObject(Program.BCDemo));
            //    chainSynched = true;
            //}
        }