diff --git a/pom.xml b/pom.xml deleted file mode 100644 index 6ae8cf5..0000000 --- a/pom.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - 4.0.0 - - com.snake - Snake - 1.0 - - - 17 - 17 - - - \ No newline at end of file diff --git a/res/ProjetL12024.pdf b/res/ProjetL12024.pdf new file mode 100644 index 0000000..6c6de99 Binary files /dev/null and b/res/ProjetL12024.pdf differ diff --git a/src/Main.java b/src/Main.java index cc7ade5..582cc31 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - System.out.println("hello world!"); + } } diff --git a/src/Reseau.java b/src/Reseau.java new file mode 100644 index 0000000..c54b61c --- /dev/null +++ b/src/Reseau.java @@ -0,0 +1,279 @@ +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import javax.net.ssl.HttpsURLConnection; + +import static java.util.Arrays.copyOfRange; + +/** + *

Cette classe est destiné un projet de fin d'année de L1, ne pas l'utiliser dans + * d'autre circonstance que dans ce projet. Elle contient plusieurs fonctions qui est + * utile pour la communication entre la machine et le site Padifac. + * + * @author Loïc GUEZO + * @version 1.0.0 + * @since 2024 + * @see Padiflac + */ +@SuppressWarnings("deprecation") + public class Reseau { + private static final String ADDRESS = "https://prog-reseau-m1.lacl.fr/padiflac/"; + private String CHANNEL; + private URL url; + + private ArrayList wordArrayList; + private ArrayList newerWordArrayList; + + private int index; + + /** + *

Le constructeur defini quelque information cruciale puis appelle directement la + * fonction {@code #searchContent()}. + * + * @param channel Ce string est utilisé pour se connecter à un channel dans le serveur web + */ + public Reseau(String channel) { + this.CHANNEL = channel; + this.wordArrayList = new ArrayList(); + this.newerWordArrayList = new ArrayList(); + + this.connexion(); + } + + /** + *

cette fonction redefini quelque information cruciale puis appelle directement la + * fonction {@code #searchContentSorted()}. + * + * @param channel Ce string est utilisé pour se reconnecter à un channel dans le serveur web + */ + public void reconnexion(String channel) { + this.CHANNEL = channel; + this.wordArrayList.clear(); + this.newerWordArrayList.clear(); + + this.connexion(); + } + + /** + * @return cette fonction renvoie tout ce qui est disponible sur le site quand on appelle la + * fonction searchContent et qui mets tout le texte dans le {@link ArrayList} + */ + public ArrayList getArrayContent() { + return this.wordArrayList; + } + + /** + * @return cette fonction renvoie uniquement les nouveauté de qui est disponible sur le site + * quand on appelle la fonction searchContent et qui mets tout le texte dans le {@link ArrayList} + */ + public ArrayList getNewArrayListContent() { + return this.newerWordArrayList; + } + + /** + *

cette fonction est en parallèle avec {@code #getLastedContent()} qui récupère le dernier indice + * utilisé lors de l'appelle de {@code #getLastedContent()}. + * @return renvoie le dernier indice + */ + public int getIndex() { + return this.index; + } + + /** + *

cette fonction est en parallèle avec {@code #getLastedContent()} et {@code #getIndex()}. Elle va + * reset l'indice. + */ + public void resetIndex() { + this.index = 0; + } + + /** + * @return cette fonction renvoie le String de l'indice puis l'incrémenter. Si c'est la fin de la liste, + * il renvoie null sans incrémenter + */ + public String getLastedContent() { + return (this.getArrayContent().size() > this.index) ? getArrayContent().get(this.index++) : null; + } + + /** + *

cherche les informations et trie seulement les nouvelles entre eux : + *


+     * Reseau reseau = new Reseau("ChatTest"); // dans le serveur : {"arbre", "pomme", "chocolat", "arbre"}
+     *System.out.println(reseau.searchContentSorted().toString()); // {"arbre", "arbre", "pomme", "chocolat"}
+     * 
+ * @return il renvoie le {@link ArrayList} de la liste + */ + public ArrayList searchContentSorted() { + ArrayList localNewerArrayList = this.searchContent(); + if (this.getArrayContent().isEmpty()) this.addContentToContent(localNewerArrayList); + else this.searchDuplicates(localNewerArrayList); + return this.wordArrayList; + } + + /** + *

cherche les informations mais ne trie pas les nouvelles entre eux : + *


+     * Reseau reseau = new Reseau("ChatTest"); // dans le serveur : {"arbre", "pomme", "chocolat", "arbre"}
+     *System.out.println(reseau.searchContentSorted().toString()); // {"arbre", "arbre", "pomme", "chocolat"}
+     * 
+ *

attention, il peut y avoir des erreurs en rajoutant par exemple {"arbre", "pomme", "chocolat", "pomme"} + * @return + */ + public ArrayList searchArrayListNotSorted() { + ArrayList localNewerArrayList = this.searchContent(); + this.addContentToContent(localNewerArrayList); + return this.wordArrayList; + } + + /** + *

ce programme essaye d'envoyer le String en parametre. Attention, la communication entre le serveur et la machine + * peut prendre du temps ! + * @param content Le contenu de texte que vous voulez renvoyer + */ + public void sendContent(String content) { + try { + this.url = new URL(ADDRESS + CHANNEL + "?nonce=" + this.generateNonce()); + HttpsURLConnection connection = (HttpsURLConnection)this.url.openConnection(); + + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + + OutputStream outputStream = connection.getOutputStream(); + outputStream.write(serializeToString(content)); + outputStream.close(); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + InputStream inputStream = connection.getInputStream(); + inputStream.close(); + } else { + System.out.println("Erreur lors de l'envoi de la requête. Code de réponse : " + responseCode); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + + private ArrayList searchContent() { + try { + byte[] bytes = this.url.openConnection().getInputStream().readAllBytes(); + ArrayList localNewerArrayList = new ArrayList(); + + StringBuffer textContent = new StringBuffer(); + + for(int i = 0; i < bytes.length; i++) { + byte charByte = bytes[i]; + + if(charByte == '|' && textContent.length() > 0) { + int size = Integer.parseInt(textContent.toString()); + byte[] buffer = copyOfRange(bytes, i + 1, i + 1 + size); + + localNewerArrayList.add(new String(buffer)); + + i += size; + textContent.setLength(0); + + } else { + textContent.append((char)charByte); + } + } + return localNewerArrayList; + + } catch (Exception e) { + return null; + } + } + + private void addContentToContent(ArrayList localArrayList) { + int arrayListLength = this.getArrayContent().size(); + + this.newerWordArrayList.clear(); + + for(int i = arrayListLength; i < localArrayList.size(); i++) { + String arrayListValue = localArrayList.get(i); + this.wordArrayList.add(arrayListValue); + this.newerWordArrayList.add(arrayListValue); + } + } + + private void searchDuplicates(ArrayList localArrayList) { + HashMap counterHashMap = this.compareHashMap( + arrayListToHashmapCounter(this.wordArrayList), + arrayListToHashmapCounter(localArrayList) + ); + + this.newerWordArrayList.clear(); + + for(Map.Entry value : counterHashMap.entrySet()) { + String arrayListValue = value.getKey(); + + for(int i =0; i arrayListToHashmapCounter(ArrayList list) { + HashMap hashmapListCounter = new HashMap(); + + for(int i = 0; i compareHashMap(HashMap olderHashMap, HashMap newerHashMap) { + HashMap hashMapCompared = new HashMap(); + + for (Map.Entry entry : newerHashMap.entrySet()) { + String key = entry.getKey(); + Integer newValue = entry.getValue(); + Integer oldValue = olderHashMap.get(key); + + if (oldValue == null) { + hashMapCompared.put(key, newValue); + } else if (newValue > oldValue) { + hashMapCompared.put(key, newValue - oldValue); + } + } + + return hashMapCompared; + } + + private String generateNonce() { + return UUID.randomUUID().toString().replace("-", "")+ + UUID.randomUUID().toString().replace("-", ""); + } + + private byte[] serializeToString(String text) throws IOException { + return text.getBytes(); + } + + private void connexion() { + try { + this.url = new URL(ADDRESS + CHANNEL); + + this.searchContentSorted(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file