private void BtnRegister_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(tbUserName.Text) || string.IsNullOrEmpty(tbPassword.Password)) { MessageBox.Show("Login lub hasło nie mogą być puste!", "Ostrzeżenie", MessageBoxButton.OK, MessageBoxImage.Warning); tbUserName.BorderBrush = Brushes.Red; tbUserName.Background = Brushes.MistyRose; tbPassword.Clear(); tbPassword.BorderBrush = Brushes.Red; tbPassword.Background = Brushes.MistyRose; return; } bool userExist = false; foreach (var p in credentials.Chain) { if (p.DataCredentials.Login == tbUserName.Text) { userExist = true; break; } } if (userExist == false) { counter = 0; acceptedRequest = 0; acceptedAccount = false; MessagePack mp = new MessagePack(); BlockchainCredentials bc = new BlockchainCredentials(); bc.Login = tbUserName.Text; bc.Password = ComputeSha256Hash(tbPassword.Password); MessageBoxResult boxResult = MessageBox.Show("Czy chcesz pełnić rolę producenta?", "Wybór roli", MessageBoxButton.YesNoCancel, MessageBoxImage.Question); if (boxResult == MessageBoxResult.Yes) { bc.UserType = true; } else if (boxResult == MessageBoxResult.No) { bc.UserType = false; } else { return; } mp.SmObj = new Block(DateTime.Now, null, bc); mp.Text = "createNewUserRequest"; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); MessageBox.Show("Prośba o dołączenie do systemu została wysłana do innych użytkowników. Zaczekaj, aż inni użytkownicy zaakceptują Twoją prośbę!", "Informacja", MessageBoxButton.OK, MessageBoxImage.Information); tbUserName.BorderBrush = Brushes.Gray; tbUserName.Background = Brushes.White; tbPassword.BorderBrush = Brushes.Gray; tbPassword.Background = Brushes.White; } else { MessageBox.Show("Użytkownik o podanym loginie istnieje już w systemie!", "Ostrzeżenie", MessageBoxButton.OK, MessageBoxImage.Warning); tbUserName.BorderBrush = Brushes.Red; tbUserName.Background = Brushes.MistyRose; tbPassword.Clear(); tbPassword.BorderBrush = Brushes.Red; tbPassword.Background = Brushes.MistyRose; return; } }
private void MessageReceived(byte[] message) { MessagePack mp = (Blockchain.ByteArrayToObject(message)) as MessagePack; switch (mp.Text) { //SYNCHRONIZACJA DANYCH LOGOWANIA case "credentialsRequest": if (mp.Sender != userName) { string hash = credentials.Chain[credentials.Chain.Count() - 1].Hash; if (hash != mp.Hash) { List <Block> newBlocks = new List <Block>(); foreach (Block el in credentials.Chain) { newBlocks.Add(el); } mp.listOfBlocks = newBlocks; } else { mp.listOfBlocks = null; } mp.Text = "userCredentialsData"; mp.Receiver = mp.Sender; mp.Sender = userName; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } break; //SYNCHRONIZACJA DANYCH DOTYCZĄCYCH ZASOBÓW case "requestUpdate": if (mp.Sender != userName) { mp.Text = "peerReady"; mp.Receiver = mp.Sender; mp.Sender = userName; mp.Hash = chain.Chain[chain.Chain.Count() - 1].Hash; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } break; case "peerReady": peerNames.Add(mp.Sender); if (mp.Sender != userName && mp.Receiver == userName && peerNames[0] == mp.Sender) { int length = chain.Chain.Count(); string hash = chain.Chain[chain.Chain.Count() - 1].Hash; if (hash != mp.Hash && length != 1) { mp.Text = "chainRequest"; mp.Receiver = mp.Sender; mp.Sender = userName; mp.Hash = hash; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } else if (hash == mp.Hash) { if (chain.IsValid() == false || chain.Chain.Count() == 0) { chain = new Blockchain(); chain.AddGenesisBlock(); mp.Text = "requestUpdate"; mp.Sender = userName; mp.Receiver = userName; mp.Hash = chain.Chain[chain.Chain.Count() - 1].Hash; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } else { MessageBox.Show("Synchronizacja zakończona pomyślnie. Posiadasz aktualną kopię blockchaina!", "Informacja", MessageBoxButton.OK, MessageBoxImage.Information); } } else { chain = new Blockchain(); mp.Text = "chainRequest"; mp.Receiver = mp.Sender; mp.Sender = userName; mp.Hash = hash; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } } break; case "chainRequest": if (mp.Sender != userName && mp.Receiver == userName) { List <Block> newBlocks = new List <Block>(); if (chain.Chain.Find(i => i.Hash == mp.Hash) != null) { Block lastBlock = chain.Chain.Find(i => i.Hash == mp.Hash); int startIndex = lastBlock.Index + 1; int finishIndex = lastBlock.Index + 10; int lastIndex = chain.Chain[chain.Chain.Count() - 1].Index; if ((lastIndex - lastBlock.Index) < 10) { finishIndex = lastIndex; } for (int i = startIndex; i <= finishIndex; i++) { newBlocks.Add(chain.Chain[i]); } } else { int lastIndex = chain.Chain[chain.Chain.Count() - 1].Index; int finishIndex = (lastIndex < 9) ? lastIndex : 9; for (int i = 0; i <= finishIndex; i++) { newBlocks.Add(chain.Chain[i]); } } mp.listOfBlocks = newBlocks; mp.Text = "newBlockchain"; mp.Receiver = mp.Sender; mp.Sender = userName; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); } break; case "newBlockchain": if (mp.Sender != userName && mp.Receiver == userName) { foreach (Block el in mp.listOfBlocks) { chain.Chain.Add(el); } mp.Text = "requestUpdate"; mp.Sender = userName; mp.Receiver = userName; mp.Hash = chain.Chain[chain.Chain.Count() - 1].Hash; byte[] data = Blockchain.ObjectToByteArray(mp); sendingClient.Send(data, data.Length); Blockchain.SerializeToJson(chain); peerNames.Clear(); // Załadowanie danych do listview for (int i = 0; i < chain.Chain.Count(); i++) { chain.Chain[i].Data.WartoscZamortyzowana = CountCurrentDepreciationValue(chain.Chain[i]); } var temp = from p in chain.Chain select p; lstProducts.ItemsSource = temp.GroupBy(x => x.Data.NumerInwentaryzacyjny).Select(y => y.Last()); CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lstProducts.ItemsSource); view.Filter = UserFilter; } break; //DODAWANIE NOWEGO UŻYTKOWNIKA DO SYSTEMU case "createNewUserRequest": MessageBoxResult decision = MessageBox.Show("Czy zgadzasz się na dodanie użytkownika o loginie '" + mp.SmObj.DataCredentials.Login.ToString() + "' do systemu", "Akceptacja nowego użytkownika", MessageBoxButton.YesNo, MessageBoxImage.Question); if (decision != MessageBoxResult.Yes) { mp.SmObj = null; } mp.Text = "createNewUserDecision"; byte[] accountData = Blockchain.ObjectToByteArray(mp); sendingClient.Send(accountData, accountData.Length); break; case "createNewUserAccepted": credentials.AddBlock(mp.SmObj); Blockchain.SerializeToJsonCredentials(credentials); break; //DODAWANIE NOWEGO ZASOBU DO SYSTEMU case "newBlock": chain.AddBlock(mp.SmObj); chain.Chain[chain.Chain.Count() - 1].Data.WartoscZamortyzowana = CountCurrentDepreciationValue(chain.Chain[chain.Chain.Count() - 1]); CollectionViewSource.GetDefaultView(lstProducts.ItemsSource).Refresh(); MessageBox.Show("Nowy rekord został dodany do systemu!", "Informacja", MessageBoxButton.OK, MessageBoxImage.Information); Blockchain.SerializeToJson(chain); break; } }