diff --git a/.idea/mainframer_state.xml b/.idea/mainframer_state.xml new file mode 100644 index 0000000..5e3c89c --- /dev/null +++ b/.idea/mainframer_state.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/RMI/ClientRMI.java b/src/RMI/ClientRMI.java index c6e9eb6..51a0db2 100644 --- a/src/RMI/ClientRMI.java +++ b/src/RMI/ClientRMI.java @@ -24,20 +24,27 @@ public ClientRMI(){ } } - public boolean ajouterData(String data){ + public Bloque creerChaine(String data){ + try { + return this.serveur.nouvelleChaine(data); + }catch (RemoteException e){ + + } + return null; + } + public Bloque ajouterData(String data){ try { return this.serveur.ajouterData(data); }catch (RemoteException e){ e.printStackTrace(); } - return true; + return null; } public ArrayList ChercherUnMot(String mot){ try { System.out.println("Chercher un mot dans clientRMI"); - System.out.println(this.serveur.occurenceMot(mot)); return this.serveur.occurenceMot(mot); }catch (RemoteException e){ e.printStackTrace(); diff --git a/src/RMI/Noeud.java b/src/RMI/Noeud.java index cf6eda2..65f44c2 100644 --- a/src/RMI/Noeud.java +++ b/src/RMI/Noeud.java @@ -82,6 +82,13 @@ public ArrayList afficherChaineBloque() throws RemoteException{ return this.chaineBloque.getListeBloque(); } + @Override + public boolean nouvelleChaine(Bloque nouveauBloqueMine) throws RemoteException { + this.chaineBloque=new ChaineBloque(5); + this.chaineBloque.getListeBloque().add(nouveauBloqueMine); + System.out.println("Creation d'une nouvelle chaine de bloque "); + return true; + } public void enregistreMoi(){ diff --git a/src/RMI/RemoteNoeud.java b/src/RMI/RemoteNoeud.java index 7cdd069..78b7e5d 100644 --- a/src/RMI/RemoteNoeud.java +++ b/src/RMI/RemoteNoeud.java @@ -13,4 +13,6 @@ public interface RemoteNoeud extends Remote{ public void arreterMining(Bloque bloque) throws RemoteException; public ArrayList rechercher(String mot) throws RemoteException; public ArrayList afficherChaineBloque() throws RemoteException; + + public boolean nouvelleChaine(Bloque nouveauBloqueMine) throws RemoteException; } diff --git a/src/RMI/RemoteServeur.java b/src/RMI/RemoteServeur.java index 29adaa9..57f9234 100644 --- a/src/RMI/RemoteServeur.java +++ b/src/RMI/RemoteServeur.java @@ -7,9 +7,9 @@ import java.util.ArrayList; public interface RemoteServeur extends Remote{ - + public Bloque nouvelleChaine(String data) throws RemoteException; public boolean enregistrerNoeud(String addresse) throws RemoteException; - public boolean ajouterData(String data) throws RemoteException; + public Bloque ajouterData(String data) throws RemoteException; public ArrayList occurenceMot(String mot) throws RemoteException; public ArrayList afficherChaineBloques() throws RemoteException; diff --git a/src/RMI/Serveur.java b/src/RMI/Serveur.java index 02102f7..ea790bb 100644 --- a/src/RMI/Serveur.java +++ b/src/RMI/Serveur.java @@ -34,6 +34,40 @@ public static void main(String [] args) { } + @Override + public Bloque nouvelleChaine(String data) throws RemoteException { + + MiningCountdown countdown=new MiningCountdown(1); + for (String addresse: + listeAddresseNoeuds) { + + Thread thread=new Thread(new MiningRunnable(addresse,data,countdown )); + thread.start(); + } + + + try { + countdown.await(); + for (String addresse:listeAddresseNoeuds + ) { + RemoteNoeud r=(RemoteNoeud) Naming.lookup(addresse); + r.nouvelleChaine(countdown.getNouveauBloqueMine()); + + } + + } catch (InterruptedException e) { + e.printStackTrace(); + } + catch (NotBoundException e) { + e.printStackTrace(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + + System.out.println("Nouveau bloc miné avec succés"); + return countdown.getNouveauBloqueMine(); + } + @Override public boolean enregistrerNoeud(String addresse) throws RemoteException { System.out.println("Ajout de neoud avec success"); @@ -43,7 +77,7 @@ public boolean enregistrerNoeud(String addresse) throws RemoteException { } @Override - public boolean ajouterData(String data) throws RemoteException { + public Bloque ajouterData(String data) throws RemoteException { MiningCountdown countdown=new MiningCountdown(1); for (String addresse: listeAddresseNoeuds) { @@ -73,21 +107,22 @@ public boolean ajouterData(String data) throws RemoteException { } System.out.println("Nouveau bloc miné avec succés"); - return true; + return countdown.getNouveauBloqueMine(); } @Override public ArrayList occurenceMot(String mot) throws RemoteException { System.out.println("Occurence mot dans Serveur"); - RechercheCountdown countdown=new RechercheCountdown(0); + RechercheCountdown countdown=new RechercheCountdown(1); for (String addresse:listeAddresseNoeuds ) { Thread thread=new Thread(new RechercheRunnable(addresse,countdown,mot)); thread.start(); } try { - ArrayList res=countdown.getResultat(); countdown.await(); + ArrayList res=countdown.getResultat(); + System.out.println("resultat : "+res); return res; } catch (InterruptedException e) { e.printStackTrace(); @@ -99,6 +134,7 @@ public ArrayList occurenceMot(String mot) throws RemoteException { public ArrayList afficherChaineBloques() throws RemoteException{ try{ RemoteNoeud n=(RemoteNoeud) Naming.lookup(this.listeAddresseNoeuds.get(0)); + System.out.println(n.afficherChaineBloque().size()); return n.afficherChaineBloque(); }catch (NotBoundException e){ e.printStackTrace(); diff --git a/src/Structure/Bloque.java b/src/Structure/Bloque.java index f7138bd..8e6fe3b 100644 --- a/src/Structure/Bloque.java +++ b/src/Structure/Bloque.java @@ -46,7 +46,6 @@ public void minerBlockaDistance(int difficulte) { hash = calculerHash(); } if(encore) System.out.println("Structure.Bloque Resolu!!! : " + hash); - else System.out.println("Lqaweha chatryn "); } // Verifie si le bloque est valide ou pas diff --git a/src/Vues/AffichageBloque.java b/src/Vues/AffichageBloque.java new file mode 100644 index 0000000..c9b4880 --- /dev/null +++ b/src/Vues/AffichageBloque.java @@ -0,0 +1,48 @@ +package Vues; + +import Structure.Bloque; + +import javax.swing.*; +import java.util.Iterator; +import java.util.Spliterator; + +public class AffichageBloque extends JPanel { + final int LENGTH=16; + + public AffichageBloque(Bloque bloque, int indice){ + super(); + String data = bloque.getData(); + addText("----------------------------------\n"); + addText("| Bloque Num : "+indice+ " |\n"); + addText("----------------------------------\n"); + addText("| Contenu de Bloque\t|\n"); + addText("----------------------------------\n"); + afficher(data); + this.setLayout(new BoxLayout(this,BoxLayout + .Y_AXIS)); + addText("----------------------------------\n"); + + addText("| Hash |\n"); + addText("----------------------------------\n"); + data=bloque.getHash(); + afficher(data); + addText("----------------------------------\n"); + data=bloque.getHashPrecedent(); + addText("| Hash Précédent |\n"); + addText("----------------------------------\n"); + afficher(data); + addText("----------------------------------\n"); + + } + + private void afficher(String data) { + for (int i = 0; i< data.length(); i+=LENGTH) { + int indice=Math.min(i+LENGTH,data.length()); + addText(" "+data.substring(i,indice)+" "); + } + } + + private void addText(String texte){ + this.add(new JLabel(texte)); + } +} diff --git a/src/Vues/AfficherChaine.java b/src/Vues/AfficherChaine.java index 79e8f2a..328bc84 100644 --- a/src/Vues/AfficherChaine.java +++ b/src/Vues/AfficherChaine.java @@ -3,33 +3,50 @@ import Structure.Bloque; import javax.swing.*; +import javax.swing.text.ComponentView; +import javax.swing.text.View; import java.awt.*; import java.util.ArrayList; public class AfficherChaine extends JFrame { + JScrollPane scrollPane; public AfficherChaine(ClientRMI clientRMI){ this.setTitle("Afficher la chaine de bloques"); - this.setSize(800,800); + this.setSize(640,480); this.setLocationRelativeTo(null); + JPanel pan=new JPanel(null); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); - JPanel pan=new JPanel(); ArrayList chaineDeBloque=clientRMI.afficherChaine(); - if(chaineDeBloque==null){ + + if(chaineDeBloque.size()==0){ + pan.setLayout(new BorderLayout()); JTextArea vide=new JTextArea("La chaine est vide"); vide.setFont(new Font("Arial", Font.PLAIN, 40)); - pan.add(vide); + pan.add(vide,BorderLayout.CENTER); } else{ + JPanel liste=new JPanel(); + BoxLayout horizontalLayout=new BoxLayout(liste,BoxLayout.X_AXIS); + liste.setLayout(horizontalLayout); + int indice=1; for(Bloque b:chaineDeBloque){ - JTextArea donnesBloque=new JTextArea(b.getData()); - donnesBloque.setFont(new Font("Arial", Font.PLAIN, 40)); - pan.add(donnesBloque); + liste.add(new JLabel("---------->")); + AffichageBloque donnesBloque=new AffichageBloque(b,indice); + liste.add(donnesBloque); + indice++; + } + scrollPane=new JScrollPane(liste); + scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); +// scrollPane.add(liste); + //scrollPane.setBounds(50,50,500,400); + pan.add(scrollPane); + } + this.setContentPane(scrollPane); - this.setContentPane(pan); setVisible(true); } } diff --git a/src/Vues/AjouterNouvelleDonnees.java b/src/Vues/AjouterNouvelleDonnees.java index e98335f..08cf731 100644 --- a/src/Vues/AjouterNouvelleDonnees.java +++ b/src/Vues/AjouterNouvelleDonnees.java @@ -1,5 +1,6 @@ package Vues; import RMI.ClientRMI; +import Structure.Bloque; import javax.swing.*; import java.awt.*; @@ -23,7 +24,20 @@ public AjouterNouvelleDonnees(ClientRMI clientRMI){ ajouterBoutton.setFont(new Font("Arial", Font.PLAIN, 20)); ajouterBoutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - clientRMI.ajouterData(inputNouvelleDonnee.getText()); + Bloque nouveauBloque=clientRMI.ajouterData(inputNouvelleDonnee.getText()); + + pan.removeAll(); + pan.add(new JLabel("Nouveau bloque miné"),BorderLayout.NORTH); + pan.add(new AffichageBloque(nouveauBloque,1),BorderLayout.CENTER); + JButton retour=new JButton("Retour"); + retour.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + close(); + } + }); + pan.add(retour,BorderLayout.PAGE_END); + raffraichir(pan); } } ); @@ -34,4 +48,13 @@ public void actionPerformed(ActionEvent e) { } + + private void raffraichir(JPanel pan) { + this.setContentPane(pan); + this.repaint(); + } + + private void close() { + this.dispose(); + } } diff --git a/src/Vues/ChercherDonneeDansLaChaine.java b/src/Vues/ChercherDonneeDansLaChaine.java index 91edb28..bb325cb 100644 --- a/src/Vues/ChercherDonneeDansLaChaine.java +++ b/src/Vues/ChercherDonneeDansLaChaine.java @@ -21,12 +21,16 @@ public ChercherDonneeDansLaChaine(ClientRMI clientRMI){ JButton chercherBoutton=new JButton("Chercher"); - chercherBoutton.setPreferredSize(new Dimension(400,100)); - chercherBoutton.setFont(new Font("Arial", Font.PLAIN, 20)); + chercherBoutton.setPreferredSize(new Dimension(180,60)); + chercherBoutton.setFont(new Font("Arial", Font.PLAIN, 14)); + chercherBoutton.addActionListener(new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { ArrayList res=clientRMI.ChercherUnMot(inputRecherche.getText()); - if(res==null){ + System.out.println("res dans client "+res); + pan.removeAll(); + if(res.size()==0){ JTextArea vide=new JTextArea("Cette données n'exite pas dans la chaine"); vide.setPreferredSize(new Dimension(400,100)); vide.setFont(new Font("Arial", Font.PLAIN, 20)); @@ -38,13 +42,30 @@ public void actionPerformed(ActionEvent e) { pan.add(donneeUnBloque); } } + rafraichier(pan); } - } ); + + + + }); pan.add(inputRecherche); pan.add(chercherBoutton); this.setContentPane(pan); this.setVisible(true); + } + + + public void rafraichier(JPanel panel) { + System.out.println("Rafraichissement"); + this.setContentPane(panel); + this.repaint(); + } + + + + } + diff --git a/src/Vues/CreerNouvelleChaine.java b/src/Vues/CreerNouvelleChaine.java index 56b04e6..9d64ebe 100644 --- a/src/Vues/CreerNouvelleChaine.java +++ b/src/Vues/CreerNouvelleChaine.java @@ -1,5 +1,6 @@ package Vues; import RMI.ClientRMI; +import Structure.Bloque; import javax.swing.*; import java.awt.*; @@ -8,30 +9,62 @@ public class CreerNouvelleChaine extends JFrame { public CreerNouvelleChaine(ClientRMI clientRMI){ - this.setTitle("Création d'une nouvelle chaine"); - this.setSize(600,400); + this.setTitle("Création d'une nouvelle chaine de bloques"); + this.setSize(640,480); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); JPanel pan=new JPanel(); - JTextField donnesBloqueInitial=new JTextField("Introduire la donneé du premier bloque"); - donnesBloqueInitial.setPreferredSize(new Dimension(400,100)); - donnesBloqueInitial.setFont(new Font("Arial", Font.PLAIN, 20)); + JLabel labelCreation=new JLabel("Introduire la donneé du premier bloque"); + JLabel avertissementVide=new JLabel("Veuillez introduire des données .. ne laissez pas ce champ vide"); + JTextField dataBlockInitial=new JTextField(25); + + labelCreation.setPreferredSize(new Dimension(400,100)); + labelCreation.setFont(new Font("Arial", Font.PLAIN, 20)); JButton bouttonCreer=new JButton("Créer"); - bouttonCreer.setPreferredSize(new Dimension(400,100)); - bouttonCreer.setFont(new Font("Arial", Font.PLAIN, 20)); + + bouttonCreer.setPreferredSize(new Dimension(120,70)); + bouttonCreer.setFont(new Font("Arial", Font.PLAIN, 14)); bouttonCreer.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - clientRMI.ajouterData(donnesBloqueInitial.getText()); + public void actionPerformed(ActionEvent e) + { + if(dataBlockInitial.getText().length()==0){ + avertissementVide.setVisible(true); + return; + } + Bloque premierBloque=clientRMI.creerChaine(dataBlockInitial.getText()); + pan.removeAll(); + pan.add(new JLabel("Nouveau bloque miné"),BorderLayout.NORTH); + pan.add(new AffichageBloque(premierBloque,1),BorderLayout.CENTER); + JButton retour=new JButton("Retour"); + retour.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + close(); + } + }); + pan.add(retour,BorderLayout.PAGE_END); + raffraichir(pan); } } ); - - pan.add(donnesBloqueInitial); - pan.add(bouttonCreer); + pan.setLayout(new BorderLayout()); + pan.add(labelCreation,BorderLayout.NORTH); + pan.add(dataBlockInitial,BorderLayout.CENTER); + pan.add(avertissementVide,BorderLayout.SOUTH); + avertissementVide.setVisible(false); + pan.add(bouttonCreer,BorderLayout.PAGE_END); this.setContentPane(pan); this.setVisible(true); } + + private void raffraichir(JPanel panel) { + setContentPane(panel); + repaint(); + } + private void close(){ + this.dispose(); + } } diff --git a/src/Vues/FenetrePrincipal.java b/src/Vues/FenetrePrincipal.java index 47b0147..2d17f4e 100644 --- a/src/Vues/FenetrePrincipal.java +++ b/src/Vues/FenetrePrincipal.java @@ -7,6 +7,7 @@ import java.awt.event.ActionListener; public class FenetrePrincipal extends JFrame { + public static final Dimension PREFERRED_SIZE = new Dimension(180, 60); private ClientRMI clientRMI; public FenetrePrincipal(ClientRMI clientRMI){ @@ -19,7 +20,7 @@ public FenetrePrincipal(ClientRMI clientRMI){ JPanel pan=new JPanel(); JButton boutton1=new JButton("Créer une nouvelle chaine de bloques"); - boutton1.setPreferredSize(new Dimension(400,100)); + boutton1.setPreferredSize(PREFERRED_SIZE); boutton1.setFont(new Font("Arial", Font.PLAIN, 20)); boutton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -28,7 +29,7 @@ public void actionPerformed(ActionEvent e) { } ); JButton boutton2=new JButton("Afficher la chaine de bloques"); - boutton2.setPreferredSize(new Dimension(400,100)); + boutton2.setPreferredSize(PREFERRED_SIZE); boutton2.setFont(new Font("Arial", Font.PLAIN, 20)); boutton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -38,7 +39,7 @@ public void actionPerformed(ActionEvent e) { JButton boutton3=new JButton("Ajouter une nouvelle donnée à la chaine"); - boutton3.setPreferredSize(new Dimension(400,100)); + boutton3.setPreferredSize(PREFERRED_SIZE); boutton3.setFont(new Font("Arial", Font.PLAIN, 20)); boutton3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -49,7 +50,7 @@ public void actionPerformed(ActionEvent e) { JButton boutton4=new JButton("Chercher une données dans la chaine"); - boutton4.setPreferredSize(new Dimension(400,100)); + boutton4.setPreferredSize(PREFERRED_SIZE); boutton4.setFont(new Font("Arial", Font.PLAIN, 20)); boutton4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -57,7 +58,11 @@ public void actionPerformed(ActionEvent e) { } } ); - + this.setLayout(new BorderLayout()); + GridLayout mgr = new GridLayout(5,1); + mgr.setHgap(60); + mgr.setVgap(20); + pan.setLayout(mgr); pan.add(boutton1); pan.add(boutton2); diff --git a/src/Vues/NouveauBloqueMine.java b/src/Vues/NouveauBloqueMine.java new file mode 100644 index 0000000..56c3a1b --- /dev/null +++ b/src/Vues/NouveauBloqueMine.java @@ -0,0 +1,43 @@ +package Vues; + +import Structure.Bloque; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class NouveauBloqueMine extends JPanel { + private JFrame fenetre; + private Bloque bloque; + public NouveauBloqueMine(JFrame fenetre, Bloque bloque){ + this.fenetre=fenetre; + this.bloque=bloque; + init(); + } + + public void init() { + JPanel pan=new JPanel(); + pan.add(new JLabel("Nouveau bloque miné"), BorderLayout.NORTH); + pan.add(new AffichageBloque(bloque,0),BorderLayout.CENTER); + JButton retour=new JButton("Retour"); + retour.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + close(); + } + }); + System.out.println("Affichage jdyd"); + pan.add(retour,BorderLayout.PAGE_END); + raffraichir(pan); + } + + private void raffraichir(JPanel pan) { + this.fenetre.setContentPane(pan); + this.fenetre.repaint(); + } + + private void close() { + this.fenetre.dispose(); + } +}