mirror of
https://github.com/klmp200/PQT_Gestionnaire_vente_stock.git
synced 2024-11-26 02:54:15 +00:00
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:
parent
ee9b4428a8
commit
93ff19af60
@ -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,78 +43,136 @@ 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(){
|
||||||
Account match = accountService.getAllAccounts().stream()
|
|
||||||
.filter(account -> account.getUsername().equals(username))
|
|
||||||
.findFirst()
|
|
||||||
.orElse(null);
|
|
||||||
|
|
||||||
if(match==null){
|
//TODO remove sysout
|
||||||
//Compte spécifié inconnu
|
System.out.println("Connexion de compte");
|
||||||
firerer.fireUserAccountUnknownEvent(username);
|
|
||||||
}else{
|
//TODO remove try-catch(Throwable)
|
||||||
accountService.setCurrentAccount(match);
|
try {
|
||||||
accountService.addListener(getConnectAccountListener());
|
Account match = accountService.getAllAccounts().stream()
|
||||||
accountService.logInCurrentAccount(StartupProcedureHandler.this.password);
|
.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(){
|
private INetworkServiceListener getPingListener(NetworkService networkService){
|
||||||
return new INetworkServiceListener() {
|
if(pingListener==null) {
|
||||||
@Override
|
pingListener = new INetworkServiceListener() {
|
||||||
public void onPQTPingSuccessEvent(String host, Integer port) {
|
@Override
|
||||||
if(StartupProcedureHandler.this.host.equals(host)
|
public void onPQTPingSuccessEvent(String host, Integer port) {
|
||||||
&& StartupProcedureHandler.this.port.equals(port)){
|
if (StartupProcedureHandler.this.host.equals(host)
|
||||||
useRequestedServer();
|
&& StartupProcedureHandler.this.port.equals(port)) {
|
||||||
|
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
|
||||||
|
public void onNewServerConfigData() {
|
||||||
|
StartupProcedureHandler.this.removePingListener();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return pingListener;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
private void removePingListener(){
|
||||||
public void onNewServerConfigData() {
|
if(pingListener!=null){
|
||||||
|
networkService.removeListener(pingListener);
|
||||||
}
|
pingListener = null;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IAccountListener getUpdateAccountListListener(){
|
private IAccountListener getUpdateAccountListListener(){
|
||||||
return new AccountListenerAdapter(){
|
if(updateAccountListListener==null){
|
||||||
@Override
|
updateAccountListListener = new AccountListenerAdapter(){
|
||||||
public void onAccountListChangedEvent(){
|
@Override
|
||||||
connectAccount();
|
public void onAccountListChangedEvent(){
|
||||||
}
|
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){
|
||||||
@Override
|
connectAccountListener = new AccountListenerAdapter(){
|
||||||
public void onAccountStatusChangedEvent(boolean status) {
|
@Override
|
||||||
if(status){
|
public void onAccountStatusChangedEvent(boolean status) {
|
||||||
//Compte connecté
|
if(status){
|
||||||
firerer.fireUserAccountConnectedEvent(username);
|
//Compte connecté
|
||||||
}else{
|
//TODO remove sysout
|
||||||
//Compte non-connecté
|
System.out.println("Connecté en tant que '"+username+"'");
|
||||||
firerer.fireUserAccountDisconnectedEvent(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){
|
public StartupProcedureHandler addListener(IStartupProcedureListener l){
|
||||||
|
@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user