From b9de88a43f29d116d2884c1a1609d80f340149a7 Mon Sep 17 00:00:00 2001 From: "Notmoo-PC\\Notmoo" Date: Wed, 8 Nov 2017 15:00:45 +0100 Subject: [PATCH] =?UTF-8?q?Module=20Client=20:=20ajout=20de=20m=C3=A9thds?= =?UTF-8?q?=20shutdown()=20=C3=A0=20tous=20les=20services;=20appel=20des?= =?UTF-8?q?=20m=C3=A9thds=20shutdown()=20lors=20d'un=20event=20de=20fermet?= =?UTF-8?q?ure=20de=20la=20fen=C3=AAtre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/pqt/client/Main.java | 6 +++- .../java/com/pqt/client/gui/FrameManager.java | 7 +++- .../module/ClientBackEndModuleManager.java | 24 +++++++++++-- .../client/module/account/AccountService.java | 35 +++++++++++++++++++ .../module/connection/ConnectionService.java | 4 +++ .../client/module/network/NetworkService.java | 4 +++ .../client/module/query/QueryExecutor.java | 4 +++ .../pqt/client/module/sale/SaleService.java | 17 ++++++--- .../pqt/client/module/stat/StatService.java | 3 ++ .../pqt/client/module/stock/StockService.java | 4 +++ 10 files changed, 100 insertions(+), 8 deletions(-) diff --git a/Workspace/client/src/main/java/com/pqt/client/Main.java b/Workspace/client/src/main/java/com/pqt/client/Main.java index 9bc6ca24..2790ba37 100644 --- a/Workspace/client/src/main/java/com/pqt/client/Main.java +++ b/Workspace/client/src/main/java/com/pqt/client/Main.java @@ -12,6 +12,10 @@ public class Main extends Application{ @Override public void start(Stage primaryStage) throws Exception { - new FrameManager(primaryStage).show(); + FrameManager fm = new FrameManager(primaryStage); + + primaryStage.setOnCloseRequest(event->fm.onCloseEvent()); + + fm.show(); } } diff --git a/Workspace/client/src/main/java/com/pqt/client/gui/FrameManager.java b/Workspace/client/src/main/java/com/pqt/client/gui/FrameManager.java index 5685cb9a..3eb21e04 100644 --- a/Workspace/client/src/main/java/com/pqt/client/gui/FrameManager.java +++ b/Workspace/client/src/main/java/com/pqt/client/gui/FrameManager.java @@ -19,11 +19,12 @@ public class FrameManager { private FrameScene mainFrameScene; private FrameScene startupFrameScene; private Stage stage; + private ClientBackEndModuleManager moduleManager; public FrameManager(Stage stage) { this.stage = stage; - ClientBackEndModuleManager moduleManager = new ClientBackEndModuleManager(null); + moduleManager = new ClientBackEndModuleManager(null); MainFrame mainFrame = new MainFrame(moduleManager.getAccountService()); mainFrame.addModule(new SaleScreen(moduleManager.getAccountService(), moduleManager.getStockService(), moduleManager.getSaleService()), true); @@ -68,4 +69,8 @@ public class FrameManager { Platform.exit(); } } + + public void onCloseEvent() { + moduleManager.shutdown(); + } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/ClientBackEndModuleManager.java b/Workspace/client/src/main/java/com/pqt/client/module/ClientBackEndModuleManager.java index 929402d0..280bc04f 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/ClientBackEndModuleManager.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/ClientBackEndModuleManager.java @@ -12,6 +12,8 @@ import com.pqt.client.module.stock.StockService; public class ClientBackEndModuleManager { + private final ConnectionService connectionService; + private final QueryExecutor queryExecutor; private final SaleService saleService; private final StockService stockService; private final AccountService accountService; @@ -19,8 +21,8 @@ public class ClientBackEndModuleManager { private final NetworkService networkService; public ClientBackEndModuleManager(String serverUrl) { - ConnectionService connectionService = new ConnectionService(serverUrl); - QueryExecutor queryExecutor = new QueryExecutor(connectionService); + connectionService = new ConnectionService(serverUrl); + queryExecutor = new QueryExecutor(connectionService); saleService = new SaleService(queryExecutor); stockService = new StockService(queryExecutor); accountService = new AccountService(queryExecutor); @@ -88,4 +90,22 @@ public class ClientBackEndModuleManager { public NetworkService getNetworkService() { return networkService; } + + public void shutdown() { + shutHighLevelServices(); + shutLowLevelServices(); + } + + private void shutHighLevelServices(){ + saleService.shutdown(); + stockService.shutdown(); + statService.shutdown(); + networkService.shutdown(); + accountService.shutdown(); + } + + private void shutLowLevelServices(){ + queryExecutor.shutdown(); + connectionService.shutdown(); + } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/account/AccountService.java b/Workspace/client/src/main/java/com/pqt/client/module/account/AccountService.java index 08c35435..6453040d 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/account/AccountService.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/account/AccountService.java @@ -1,5 +1,6 @@ package com.pqt.client.module.account; +import com.pqt.client.module.account.listeners.AccountListenerAdapter; import com.pqt.client.module.query.QueryExecutor; import com.pqt.client.module.query.query_callback.ICollectionItemMessageCallback; import com.pqt.client.module.query.query_callback.INoItemMessageCallback; @@ -12,6 +13,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.concurrent.CountDownLatch; //TODO écrire javadoc //TODO add log lines @@ -120,4 +122,37 @@ public class AccountService { } }); } + + public void shutdown() { + if(connected) { + try { + CountDownLatch latch = new CountDownLatch(1); + new Thread(() ->{ + this.addListener(new AccountListenerAdapter() { + @Override + public void onAccountStatusChangedEvent(boolean status) { + //TODO ajouter des logs + latch.countDown(); + } + + @Override + public void onAccountStatusNotChangedEvent(Throwable cause) { + //TODO ajouter des logs + cause.printStackTrace(); + latch.countDown(); + } + }); + logOutCurrentAccount(); + }).start(); + latch.await(); // Wait for thread to call latch.countDown() + } catch (InterruptedException e) { + //TODO ajouter des logs + e.printStackTrace(); + }finally { + listenerList = null; + } + }else{ + //Nothing to do + } + } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/connection/ConnectionService.java b/Workspace/client/src/main/java/com/pqt/client/module/connection/ConnectionService.java index c072920e..4ec9679a 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/connection/ConnectionService.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/connection/ConnectionService.java @@ -63,4 +63,8 @@ public class ConnectionService { throw new IllegalStateException("Service was shut down : unable to send text"); executor.submit(()->textSender.send(serverUrl, "message="+text, listener)); } + + public void shutdown() { + stop(false); + } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/network/NetworkService.java b/Workspace/client/src/main/java/com/pqt/client/module/network/NetworkService.java index def9bb1d..9287ec3c 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/network/NetworkService.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/network/NetworkService.java @@ -139,4 +139,8 @@ public class NetworkService { if(port<1 || port>65535) throw new IllegalArgumentException("port number must be an unsigned 16-bit integer (0 getSaleTypes() { - //TODO - return null; + public Collection getSaleTypes() { + //TODO faire en sorte que cette liste soit donnée par le serveur + + Set types = new HashSet<>(); + types.add(SaleType.CASH); + types.add(SaleType.OFFERED_GUEST); + types.add(SaleType.OFFERED_STAFF_MEMBER); + return types; + } + + public void shutdown() { + //Nothing to do } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/stat/StatService.java b/Workspace/client/src/main/java/com/pqt/client/module/stat/StatService.java index e8278ca3..fb09f6ce 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/stat/StatService.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/stat/StatService.java @@ -31,4 +31,7 @@ public class StatService { dao.removeListener(listener); } + public void shutdown() { + //Nothing to do + } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/stock/StockService.java b/Workspace/client/src/main/java/com/pqt/client/module/stock/StockService.java index e3fed577..348d1ede 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/stock/StockService.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/stock/StockService.java @@ -129,4 +129,8 @@ public class StockService { public void removeListener(IStockListener listener) { dao.removeListener(listener); } + + public void shutdown() { + //Nothing to do + } }