mirror of
https://github.com/klmp200/PQT_Gestionnaire_vente_stock.git
synced 2024-12-22 15:31:08 +00:00
Module Client : ajout service NetworkService
This commit is contained in:
parent
96a45958de
commit
a34a623003
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<String, String>(){
|
||||
|
||||
@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<String, String> obj) {
|
||||
configCache.addServerConfig(host, port, convertToServerConfig(obj));
|
||||
Arrays.stream(listenerList.getListeners(INetworkServiceListener.class))
|
||||
.forEach(INetworkServiceListener::onNewServerConfigData);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ServerConfig convertToServerConfig(Map<String, String> data){
|
||||
|
||||
ServerConfig serverConfig = new ServerConfig();
|
||||
List<String> 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<ConfigFields> 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<n<65536)");
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package com.pqt.client.module.network;
|
||||
|
||||
import com.pqt.core.entities.server_config.ServerConfig;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ServerConfigCache {
|
||||
|
||||
private final Map<ServerData, ServerConfig> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user