예제 #1
0
 public Server(Paxos pxs, int port)
 {
     this.pxs = pxs;
     UpdatePort(port);
     this.udpListener.Client.ReceiveTimeout = 1000; //one second
     this.listenThread = new Thread(new ThreadStart(ListenForClients));
     this.listenThread.Start();
     AnnounceHost();
 }
예제 #2
0
 public Client(Paxos pxs, Server srv)
 {
     this.pxs    = pxs;
     this.server = srv;
 }
예제 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("#################################### PAXOS #####################################");

            Paxos pxs = new Paxos(24805);

            string[] cmd;
            while (true)
            {
                cmd = Console.ReadLine().Split(' ');
                if (cmd.Length > 0)
                {
                    if (cmd[0] == "port")
                    {
                        if (cmd.Length == 2)
                        {
                            try { pxs.server.UpdatePort(int.Parse(cmd[1])); }
                            catch (Exception) {}
                        }
                        else if (cmd.Length == 1)
                        {
                            pxs.server.AnnounceHost();
                        }
                    }
                    else if (cmd[0] == "add")
                    {
                        if (cmd.Length > 1)
                        {
                            if (cmd[1] == "proposer")
                            {
                                pxs.pro = new Proposer <int>();
                                pxs.proposers.Add(pxs.localhost);
                                Console.WriteLine("Proposer role added");
                            }
                            else if (cmd[1] == "acceptor")
                            {
                                pxs.acp = new Acceptor <int>();
                                pxs.acceptors.Add(pxs.localhost);
                                Console.WriteLine("Acceptor role added");
                            }
                            else if (cmd[1] == "learner")
                            {
                                pxs.lrn = new Learner <int>();
                                pxs.learners.Add(pxs.localhost);
                                Console.WriteLine("Learner role added");
                            }
                        }
                        else if (cmd.Length == 1)
                        {
                            pxs.server.AnnounceHost();
                        }
                    }
                    else if (cmd[0] == "connect" && cmd.Length == 3)
                    {
                        try {
                            var        ip   = IPAddress.Parse(cmd[1]);
                            var        port = int.Parse(cmd[2]);
                            IPEndPoint peer = new IPEndPoint(ip, port);
                            pxs.client.Send(peer, "connect");
                        }
                        catch (FormatException) { }
                    }
                    else if (cmd[0] == "propose" && cmd.Length == 3)
                    {
                        int round = int.Parse(cmd[1]);
                        int value = int.Parse(cmd[2]);
                        pxs.pro.__ctor(round, value);
                        pxs.client.Broadcast(pxs.acceptors, "prepare " + round + " " + value);
                    }
                    else
                    {
                        Console.WriteLine("Unknown command");
                    }
                }
            }
        }