From a34a6230036916581be24a31ad675864b819a567 Mon Sep 17 00:00:00 2001 From: "Notmoo-PC\\Notmoo" Date: Sun, 8 Oct 2017 13:15:54 +0200 Subject: [PATCH] Module Client : ajout service NetworkService --- .../module/ClientBackEndModuleManager.java | 15 +- .../client/module/network/NetworkService.java | 128 ++++++++++++++++++ .../module/network/ServerConfigCache.java | 77 +++++++++++ .../listeners/INetworkServiceListener.java | 9 ++ 4 files changed, 225 insertions(+), 4 deletions(-) create mode 100644 Workspace/client/src/main/java/com/pqt/client/module/network/NetworkService.java create mode 100644 Workspace/client/src/main/java/com/pqt/client/module/network/ServerConfigCache.java create mode 100644 Workspace/client/src/main/java/com/pqt/client/module/network/listeners/INetworkServiceListener.java 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 9f2a0fb5..c2666208 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 @@ -2,6 +2,7 @@ package com.pqt.client.module; import com.pqt.client.module.account.AccountService; import com.pqt.client.module.connection.ConnectionService; +import com.pqt.client.module.network.NetworkService; import com.pqt.client.module.query.QueryExecutor; import com.pqt.client.module.sale.SaleService; import com.pqt.client.module.stat.StatService; @@ -9,10 +10,11 @@ import com.pqt.client.module.stock.StockService; public class ClientBackEndModuleManager { - private SaleService saleService; - private StockService stockService; - private AccountService accountService; - private StatService statService; + private final SaleService saleService; + private final StockService stockService; + private final AccountService accountService; + private final StatService statService; + private final NetworkService networkService; public ClientBackEndModuleManager(String serverUrl) { ConnectionService connectionService = new ConnectionService(serverUrl); @@ -21,6 +23,7 @@ public class ClientBackEndModuleManager { stockService = new StockService(queryExecutor); accountService = new AccountService(queryExecutor); statService = new StatService(queryExecutor); + networkService = new NetworkService(queryExecutor); } public SaleService getSaleService() { @@ -38,4 +41,8 @@ public class ClientBackEndModuleManager { public StatService getStatService() { return statService; } + + public NetworkService getNetworkService() { + return networkService; + } } 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 new file mode 100644 index 00000000..9dee5ebe --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/network/NetworkService.java @@ -0,0 +1,128 @@ +package com.pqt.client.module.network; + +import com.pqt.client.module.network.listeners.INetworkServiceListener; +import com.pqt.client.module.query.QueryExecutor; +import com.pqt.client.module.query.query_callback.IMapItemMessageCallback; +import com.pqt.client.module.query.query_callback.INoItemMessageCallback; +import com.pqt.core.entities.server_config.ConfigFields; +import com.pqt.core.entities.server_config.ServerConfig; + +import javax.swing.event.EventListenerList; +import java.util.*; + +//TODO ajout javadoc + +/* + * Ce service doit permettre de faire des ping et de récupérer la config d'un serveur distant + */ +public class NetworkService { + + private final QueryExecutor queryExecutor; + private final EventListenerList listenerList; + private final ServerConfigCache configCache; + + public NetworkService(QueryExecutor queryExecutor) { + this.queryExecutor = queryExecutor; + listenerList = new EventListenerList(); + configCache = new ServerConfigCache(); + } + + public void addListener(INetworkServiceListener l){ + listenerList.add(INetworkServiceListener.class, l); + } + + public void removeListener(INetworkServiceListener l){ + listenerList.remove(INetworkServiceListener.class, l); + } + + public void sendPQTPing(String host, Integer port){ + checkData(host, port); + queryExecutor.executePingQuery(new INoItemMessageCallback() { + @Override + public void ack() { + Arrays.stream(listenerList.getListeners(INetworkServiceListener.class)) + .forEach(l->l.onPQTPingSuccessEvent(host, port)); + sendConfigRequest(host, port); + } + + @Override + public void err(Throwable cause) { + Arrays.stream(listenerList.getListeners(INetworkServiceListener.class)) + .forEach(l->l.onPQTPingFailureEvent(host, port, cause)); + } + + @Override + public void ref(Throwable cause) { + Arrays.stream(listenerList.getListeners(INetworkServiceListener.class)) + .forEach(l->l.onPQTPingFailureEvent(host, port, cause)); + } + }); + } + + public ServerConfig getServerConfig(String host, Integer port){ + checkData(host, port); + return configCache.getConfig(host, port); + } + + private void sendConfigRequest(String host, Integer port){ + queryExecutor.executeConfigListQuery(new IMapItemMessageCallback(){ + + @Override + public void err(Throwable cause) { + //TODO ajouter log erreur + } + + @Override + public void ref(Throwable cause) { + //TODO ajouter log erreur + } + + @Override + public void ack(Map obj) { + configCache.addServerConfig(host, port, convertToServerConfig(obj)); + Arrays.stream(listenerList.getListeners(INetworkServiceListener.class)) + .forEach(INetworkServiceListener::onNewServerConfigData); + } + }); + } + + private ServerConfig convertToServerConfig(Map data){ + + ServerConfig serverConfig = new ServerConfig(); + List allowedFields = new ArrayList<>(); + EnumSet.allOf(ConfigFields.class).forEach(e->allowedFields.add(e.name())); + + data.keySet() + .stream() + .filter(allowedFields::contains) + .filter(key->isBoolean(data.get(key))) + .forEach(key->serverConfig.add(getMatchingConfigFields(key), Boolean.parseBoolean(data.get(key)))); + + return serverConfig; + } + + private boolean isBoolean(String str){ + return str.equals("true") || str.equals("false"); + } + + private ConfigFields getMatchingConfigFields(String str){ + ConfigFields match = null; + + EnumSet enumSet = EnumSet.allOf(ConfigFields.class); + for(ConfigFields field : enumSet){ + if(str.equals(field.name())) + match = field; + } + + return match; + } + + private void checkData(String host, Integer port){ + if(host==null || port == null) + throw new NullPointerException("Null value as server data is not allowed"); + if(host.isEmpty()) + throw new IllegalArgumentException("host cannot be empty"); + if(port<1 || port>65535) + throw new IllegalArgumentException("port number must be an unsigned 16-bit integer (0 cache; + + public ServerConfigCache() { + cache = new HashMap<>(); + } + + public void addServerConfig(String host, Integer port, ServerConfig config){ + ServerData match = cache.keySet().stream().filter(key->key.getHost().equals(host)&&key.getPort().equals(port)).findFirst().orElse(null); + if(match==null){ + cache.put(new ServerData(host, port), config); + }else{ + cache.replace(match, config); + } + } + + public void removeServerData(String host, Integer port){ + ServerData data = new ServerData(host, port); + if(cache.containsKey(data)) + cache.remove(data); + } + + public boolean hasConfig(String host, Integer port){ + return cache.containsKey(new ServerData(host, port)); + } + + public ServerConfig getConfig(String host, Integer port){ + if(hasConfig(host, port)) + return cache.get(new ServerData(host, port)); + + return null; + } + + private class ServerData{ + private String host; + private Integer port; + + public ServerData(String host, Integer port) { + this.host = host; + this.port = port; + } + + public String getHost() { + return host; + } + + public Integer getPort() { + return port; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ServerData that = (ServerData) o; + + if (host != null ? !host.equals(that.host) : that.host != null) return false; + return port != null ? port.equals(that.port) : that.port == null; + } + + @Override + public int hashCode() { + int result = host != null ? host.hashCode() : 0; + result = 31 * result + (port != null ? port.hashCode() : 0); + return result; + } + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/network/listeners/INetworkServiceListener.java b/Workspace/client/src/main/java/com/pqt/client/module/network/listeners/INetworkServiceListener.java new file mode 100644 index 00000000..33ed8073 --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/network/listeners/INetworkServiceListener.java @@ -0,0 +1,9 @@ +package com.pqt.client.module.network.listeners; + +import java.util.EventListener; + +public interface INetworkServiceListener extends EventListener { + void onPQTPingSuccessEvent(String host, Integer port); + void onPQTPingFailureEvent(String host, Integer port, Throwable cause); + void onNewServerConfigData(); +}