From 93ff19af608758b52d06a959f924f42e4d65f8e6 Mon Sep 17 00:00:00 2001 From: "Notmoo-PC\\Notmoo" Date: Thu, 2 Nov 2017 17:20:07 +0100 Subject: [PATCH] =?UTF-8?q?Module=20Client=20:=20Ajout=20affichages=20temp?= =?UTF-8?q?oraires=20des=20messages=20entrants-sortants=20en=20http;=20Mod?= =?UTF-8?q?if=20StartupProcedureHandler=20pour=20qu'il=20retire=20ses=20li?= =?UTF-8?q?steners=20apr=C3=A8s=20la=20premi=C3=A8re=20notif?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StartupProcedureHandler.java | 151 ++++++++++++------ .../connection/senders/HttpTextSender.java | 13 ++ 2 files changed, 119 insertions(+), 45 deletions(-) diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/startup_frame/StartupProcedureHandler.java b/Workspace/client/src/main/java/com/pqt/client/gui/startup_frame/StartupProcedureHandler.java index b3a323a2..2e5b04c0 100644 --- a/Workspace/client/src/main/java/com/pqt/client/gui/startup_frame/StartupProcedureHandler.java +++ b/Workspace/client/src/main/java/com/pqt/client/gui/startup_frame/StartupProcedureHandler.java @@ -18,6 +18,9 @@ class StartupProcedureHandler { private String host, username, password; private Integer port; + private IAccountListener updateAccountListListener, connectAccountListener; + private INetworkServiceListener pingListener; + private IStartupProcedureEventFirerer firerer; StartupProcedureHandler(NetworkService networkService, AccountService accountService) { @@ -40,78 +43,136 @@ class StartupProcedureHandler { } private void testConnection(){ - networkService.addListener(getPingListener()); + //TODO remove sysout + System.out.println("Test de connexion"); + networkService.addListener(getPingListener(networkService)); networkService.setActiveServer(host, port); networkService.sendPQTPing(host, port); } private void useRequestedServer(){ //Server found + //TODO remove sysout + System.out.println("Serveur trouvé"); firerer.fireServerFoundEvent(host, port); accountService.addListener(getUpdateAccountListListener()); accountService.refreshAccounts(); } private void connectAccount(){ - Account match = accountService.getAllAccounts().stream() - .filter(account -> account.getUsername().equals(username)) - .findFirst() - .orElse(null); - if(match==null){ - //Compte spécifié inconnu - firerer.fireUserAccountUnknownEvent(username); - }else{ - accountService.setCurrentAccount(match); - accountService.addListener(getConnectAccountListener()); - accountService.logInCurrentAccount(StartupProcedureHandler.this.password); + //TODO remove sysout + System.out.println("Connexion de compte"); + + //TODO remove try-catch(Throwable) + try { + Account match = accountService.getAllAccounts().stream() + .filter(account -> account.getUsername().equals(username)) + .findFirst() + .orElse(null); + + //TODO remove sysout + System.out.println("Account match value : "+match); + + if(match==null){ + //TODO remove sysout + System.out.println(" --> Compte inconnu"); + //Compte spécifié inconnu + firerer.fireUserAccountUnknownEvent(username); + }else{ + //TODO remove sysout + System.out.println(" --> Compte connu"); + accountService.setCurrentAccount(match); + accountService.addListener(getConnectAccountListener()); + accountService.logInCurrentAccount(StartupProcedureHandler.this.password); + } + }catch(Throwable e){ + e.printStackTrace(); } } - private INetworkServiceListener getPingListener(){ - return new INetworkServiceListener() { - @Override - public void onPQTPingSuccessEvent(String host, Integer port) { - if(StartupProcedureHandler.this.host.equals(host) - && StartupProcedureHandler.this.port.equals(port)){ - useRequestedServer(); + private INetworkServiceListener getPingListener(NetworkService networkService){ + if(pingListener==null) { + pingListener = new INetworkServiceListener() { + @Override + public void onPQTPingSuccessEvent(String host, Integer port) { + if (StartupProcedureHandler.this.host.equals(host) + && StartupProcedureHandler.this.port.equals(port)) { + useRequestedServer(); + } + StartupProcedureHandler.this.removePingListener(); } - } - @Override - public void onPQTPingFailureEvent(String host, Integer port, Throwable cause) { + @Override + public void onPQTPingFailureEvent(String host, Integer port, Throwable cause) { + StartupProcedureHandler.this.removePingListener(); + } - } + @Override + public void onNewServerConfigData() { + StartupProcedureHandler.this.removePingListener(); + } + }; + } + return pingListener; + } - @Override - public void onNewServerConfigData() { - - } - }; + private void removePingListener(){ + if(pingListener!=null){ + networkService.removeListener(pingListener); + pingListener = null; + } } private IAccountListener getUpdateAccountListListener(){ - return new AccountListenerAdapter(){ - @Override - public void onAccountListChangedEvent(){ - connectAccount(); - } - }; + if(updateAccountListListener==null){ + updateAccountListListener = new AccountListenerAdapter(){ + @Override + public void onAccountListChangedEvent(){ + connectAccount(); + StartupProcedureHandler.this.removeUpdateAccountListListener(); + } + }; + } + return updateAccountListListener; + } + + private void removeUpdateAccountListListener(){ + if(updateAccountListListener!=null){ + accountService.removeListener(updateAccountListListener); + updateAccountListListener=null; + } } private IAccountListener getConnectAccountListener(){ - return new AccountListenerAdapter(){ - @Override - public void onAccountStatusChangedEvent(boolean status) { - if(status){ - //Compte connecté - firerer.fireUserAccountConnectedEvent(username); - }else{ - //Compte non-connecté - firerer.fireUserAccountDisconnectedEvent(username); + if(connectAccountListener==null){ + connectAccountListener = new AccountListenerAdapter(){ + @Override + public void onAccountStatusChangedEvent(boolean status) { + if(status){ + //Compte connecté + //TODO remove sysout + System.out.println("Connecté en tant que '"+username+"'"); + StartupProcedureHandler.this.removeConnectAccountListener(); + firerer.fireUserAccountConnectedEvent(username); + }else{ + //Compte non-connecté + //TODO remove sysout + System.out.println("Non-connecté en tant que '"+username+"'"); + StartupProcedureHandler.this.removeConnectAccountListener(); + firerer.fireUserAccountDisconnectedEvent(username); + } } - } - }; + }; + } + return connectAccountListener; + } + + private void removeConnectAccountListener(){ + if(connectAccountListener!=null){ + accountService.removeListener(connectAccountListener); + connectAccountListener = null; + } } public StartupProcedureHandler addListener(IStartupProcedureListener l){ diff --git a/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/HttpTextSender.java b/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/HttpTextSender.java index 95042041..7c348bae 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/HttpTextSender.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/HttpTextSender.java @@ -17,6 +17,12 @@ public class HttpTextSender implements ITextSender{ try { String trueURL = String.format("http://%s?message=%s", url, text); + //TODO remove sysout + { + System.out.println(" --- --- ---"); + System.out.println("Sending : "); + System.out.println(trueURL); + } HttpURLConnection con = (HttpURLConnection) new URL(trueURL).openConnection(); con.setRequestMethod("GET"); @@ -41,6 +47,13 @@ public class HttpTextSender implements ITextSender{ while ((inputLine = in.readLine()) != null) { content.append(inputLine); } + + //TODO remove sysout + { + System.out.println("Received : "); + System.out.println(content); + } + listener.onMessageReceivedEvent(content.toString()); }