Module Client : Ajout affichages temporaires des messages entrants-sortants en http; Modif StartupProcedureHandler pour qu'il retire ses listeners après la première notif

This commit is contained in:
Notmoo-PC\Notmoo 2017-11-02 17:20:07 +01:00
parent ee9b4428a8
commit 93ff19af60
2 changed files with 119 additions and 45 deletions

View File

@ -18,6 +18,9 @@ class StartupProcedureHandler {
private String host, username, password; private String host, username, password;
private Integer port; private Integer port;
private IAccountListener updateAccountListListener, connectAccountListener;
private INetworkServiceListener pingListener;
private IStartupProcedureEventFirerer firerer; private IStartupProcedureEventFirerer firerer;
StartupProcedureHandler(NetworkService networkService, AccountService accountService) { StartupProcedureHandler(NetworkService networkService, AccountService accountService) {
@ -40,79 +43,137 @@ class StartupProcedureHandler {
} }
private void testConnection(){ private void testConnection(){
networkService.addListener(getPingListener()); //TODO remove sysout
System.out.println("Test de connexion");
networkService.addListener(getPingListener(networkService));
networkService.setActiveServer(host, port); networkService.setActiveServer(host, port);
networkService.sendPQTPing(host, port); networkService.sendPQTPing(host, port);
} }
private void useRequestedServer(){ private void useRequestedServer(){
//Server found //Server found
//TODO remove sysout
System.out.println("Serveur trouvé");
firerer.fireServerFoundEvent(host, port); firerer.fireServerFoundEvent(host, port);
accountService.addListener(getUpdateAccountListListener()); accountService.addListener(getUpdateAccountListListener());
accountService.refreshAccounts(); accountService.refreshAccounts();
} }
private void connectAccount(){ private void connectAccount(){
//TODO remove sysout
System.out.println("Connexion de compte");
//TODO remove try-catch(Throwable)
try {
Account match = accountService.getAllAccounts().stream() Account match = accountService.getAllAccounts().stream()
.filter(account -> account.getUsername().equals(username)) .filter(account -> account.getUsername().equals(username))
.findFirst() .findFirst()
.orElse(null); .orElse(null);
//TODO remove sysout
System.out.println("Account match value : "+match);
if(match==null){ if(match==null){
//TODO remove sysout
System.out.println(" --> Compte inconnu");
//Compte spécifié inconnu //Compte spécifié inconnu
firerer.fireUserAccountUnknownEvent(username); firerer.fireUserAccountUnknownEvent(username);
}else{ }else{
//TODO remove sysout
System.out.println(" --> Compte connu");
accountService.setCurrentAccount(match); accountService.setCurrentAccount(match);
accountService.addListener(getConnectAccountListener()); accountService.addListener(getConnectAccountListener());
accountService.logInCurrentAccount(StartupProcedureHandler.this.password); accountService.logInCurrentAccount(StartupProcedureHandler.this.password);
} }
}catch(Throwable e){
e.printStackTrace();
}
} }
private INetworkServiceListener getPingListener(){ private INetworkServiceListener getPingListener(NetworkService networkService){
return new INetworkServiceListener() { if(pingListener==null) {
pingListener = new INetworkServiceListener() {
@Override @Override
public void onPQTPingSuccessEvent(String host, Integer port) { public void onPQTPingSuccessEvent(String host, Integer port) {
if (StartupProcedureHandler.this.host.equals(host) if (StartupProcedureHandler.this.host.equals(host)
&& StartupProcedureHandler.this.port.equals(port)) { && StartupProcedureHandler.this.port.equals(port)) {
useRequestedServer(); useRequestedServer();
} }
StartupProcedureHandler.this.removePingListener();
} }
@Override @Override
public void onPQTPingFailureEvent(String host, Integer port, Throwable cause) { public void onPQTPingFailureEvent(String host, Integer port, Throwable cause) {
StartupProcedureHandler.this.removePingListener();
} }
@Override @Override
public void onNewServerConfigData() { public void onNewServerConfigData() {
StartupProcedureHandler.this.removePingListener();
} }
}; };
} }
return pingListener;
}
private void removePingListener(){
if(pingListener!=null){
networkService.removeListener(pingListener);
pingListener = null;
}
}
private IAccountListener getUpdateAccountListListener(){ private IAccountListener getUpdateAccountListListener(){
return new AccountListenerAdapter(){ if(updateAccountListListener==null){
updateAccountListListener = new AccountListenerAdapter(){
@Override @Override
public void onAccountListChangedEvent(){ public void onAccountListChangedEvent(){
connectAccount(); connectAccount();
StartupProcedureHandler.this.removeUpdateAccountListListener();
} }
}; };
} }
return updateAccountListListener;
}
private void removeUpdateAccountListListener(){
if(updateAccountListListener!=null){
accountService.removeListener(updateAccountListListener);
updateAccountListListener=null;
}
}
private IAccountListener getConnectAccountListener(){ private IAccountListener getConnectAccountListener(){
return new AccountListenerAdapter(){ if(connectAccountListener==null){
connectAccountListener = new AccountListenerAdapter(){
@Override @Override
public void onAccountStatusChangedEvent(boolean status) { public void onAccountStatusChangedEvent(boolean status) {
if(status){ if(status){
//Compte connecté //Compte connecté
//TODO remove sysout
System.out.println("Connecté en tant que '"+username+"'");
StartupProcedureHandler.this.removeConnectAccountListener();
firerer.fireUserAccountConnectedEvent(username); firerer.fireUserAccountConnectedEvent(username);
}else{ }else{
//Compte non-connecté //Compte non-connecté
//TODO remove sysout
System.out.println("Non-connecté en tant que '"+username+"'");
StartupProcedureHandler.this.removeConnectAccountListener();
firerer.fireUserAccountDisconnectedEvent(username); firerer.fireUserAccountDisconnectedEvent(username);
} }
} }
}; };
} }
return connectAccountListener;
}
private void removeConnectAccountListener(){
if(connectAccountListener!=null){
accountService.removeListener(connectAccountListener);
connectAccountListener = null;
}
}
public StartupProcedureHandler addListener(IStartupProcedureListener l){ public StartupProcedureHandler addListener(IStartupProcedureListener l){
firerer.addListener(l); firerer.addListener(l);

View File

@ -17,6 +17,12 @@ public class HttpTextSender implements ITextSender{
try { try {
String trueURL = String.format("http://%s?message=%s", url, text); 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(); HttpURLConnection con = (HttpURLConnection) new URL(trueURL).openConnection();
con.setRequestMethod("GET"); con.setRequestMethod("GET");
@ -41,6 +47,13 @@ public class HttpTextSender implements ITextSender{
while ((inputLine = in.readLine()) != null) { while ((inputLine = in.readLine()) != null) {
content.append(inputLine); content.append(inputLine);
} }
//TODO remove sysout
{
System.out.println("Received : ");
System.out.println(content);
}
listener.onMessageReceivedEvent(content.toString()); listener.onMessageReceivedEvent(content.toString());
} }