public MainWindow() { InitializeComponent(); //bankClient = new RestClient(bankUrl); //minerClient = new RestClient(minerUrl); //existUsers = new Dictionary<int, uint>(); sendJobs = new List <string[]>(); clientJobResults = new List <string[]>(); currJobsLogged = new List <string[]>(); url = "https://localhost:44370/"; webPool = new RestClient(url); //create current client connection, AKA this block is the server thread currClient = new ClientDataStruct(); currClient.ip = "localhost"; currClient.port = (new Random().Next(6000, 9999)).ToString("D4");//random generation of port number ourServ = new PeerProgram(); currClient.port = ourServ.CreateServer(currClient.ip, currClient.port); ourRemoteThread = ConnectToRemote(currClient.ip, currClient.port); JoinPool(); ourBlockchain = ourRemoteThread.GetCurrentChain(); ourAddrText.Text = "Your Client Address is " + currClient.ip + ":" + currClient.port; MinerThread(); }
private void JoinPool()//join pool via web service { RestRequest req = new RestRequest("api/Client/AddClient"); encodeClient = new ClientDataStruct(); encodeClient.ip = EncodeTo64(currClient.ip); encodeClient.port = EncodeTo64(currClient.port); req.AddJsonBody(encodeClient); webPool.Post(req); }
/* * Algo to shuffle list to create fairness for peer connection * Reference: https://www.dotnetperls.com/fisher-yates-shuffle */ private void ShuffleList() { Random rand = new Random(); for (int i = clients.Count - 1; i > 0; i--) { int rnd = rand.Next(0, i); ClientDataStruct val = clients[rnd]; clients[rnd] = clients[i]; clients[i] = val; } }
public void RemoveClient(ClientDataStruct cl) { clients.Remove(clients.Find(x => x.ip == cl.ip && x.port == cl.port)); }
public void AddClient(ClientDataStruct cl) { clients.Add(cl); ShuffleList(); }
public void Delete([FromBody] ClientDataStruct cl) { pool.RemoveClient(cl); }
public void Post([FromBody] ClientDataStruct cl) { pool.AddClient(cl); }