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 71757842..48ddabc8 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 @@ -1,46 +1,66 @@ package com.pqt.client.module.connection; import com.pqt.client.module.connection.listeners.IConnectionListener; +import com.pqt.client.module.connection.senders.HttpTextSender; +import com.pqt.client.module.connection.senders.ITextSender; + +import java.util.concurrent.*; -//TODO écrire contenu méthodes //TODO écrire javadoc -//TODO add log lines? public class ConnectionService { - public boolean isConnectedToServer() { - return false; + private String serverUrl; + private ExecutorService executor; + private ITextSender textSender; + + public ConnectionService(String serverUrl) { + executor = new ThreadPoolExecutor(1, 1, 1000, + TimeUnit.SECONDS, new LinkedBlockingQueue<>()); + this.serverUrl = serverUrl; + this.textSender = new HttpTextSender(); + } + + public String getServerUrl() { + return serverUrl; } - public void connectToServer(String ip_address) { - - } - - public void disconnectFromServer() { - - } - - public String getCurrentServerIpAddress() { - return null; - } - - public void start() { - - } - - public void stop() { - - } - - public void sendText(String text) { - - } - - public void addListener(IConnectionListener listener) { - - } - - public void removeListener(IConnectionListener listener) { + public void setServerUrl(String url){ + this.serverUrl = url; + } + /** + * Arrête le service d'envoi. + *

+ * Si ce service est arrêté, il ne sera plus possible de l'utiliser, et ne pourra pas être redémarré. Une + * autre isntance devra être utilisée pour pouvoir recommencer à se servir de ce service. + *

+ * Si {@code force} vaut true, le service tentera d'interrompre tous les envois de texte en cours, même s'il ne + * sont pas terminés. Sinon, le service attendra que tous les envois en cours soient terminé avant de s'arrêter, mais + * n'acceptera pas de nouveaux envois. + * @param force {@code true} si l'arrêt du service doit être forcé, {@code false} sinon + */ + public void stop(boolean force) { + if(executor!=null) + if(force) + executor.shutdownNow(); + else + executor.shutdown(); } + /** + * Envoie la chaîne de caractères {@code text} au serveur de donnée correspondant à l'URL {@link #getServerUrl()}, et + * utilise {@code listener} pour notifier l'avancement de l'envoi. + * + * @param text texte à envoyer + * @param listener listener à utiliser pour notifier l'avancement de l'envoi (voir {@link IConnectionListener}) + * @throws IllegalStateException Si l'url du serveur vaut {@code null} (à spécifier à la constructyion ou avec + * {@link #setServerUrl(String)}, ou si le service à été arrêté via la méthode {@link #stop(boolean)}. + */ + public void sendText(String text, IConnectionListener listener) throws IllegalStateException{ + if(serverUrl==null) + throw new IllegalStateException("No url specified for data server"); + if(executor.isShutdown() || executor.isTerminated()) + throw new IllegalStateException("Service was shut down : unable to send text"); + executor.submit(()->textSender.send(serverUrl, text, listener)); + } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/ConnectionListenerAdapter.java b/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/ConnectionListenerAdapter.java index 1b99f8fa..a681450a 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/ConnectionListenerAdapter.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/ConnectionListenerAdapter.java @@ -1,7 +1,5 @@ package com.pqt.client.module.connection.listeners; -//TODO écrire contenu méthodes -//TODO écrire javadoc public class ConnectionListenerAdapter implements IConnectionListener { @@ -10,6 +8,7 @@ public class ConnectionListenerAdapter implements IConnectionListener { * * */ + @Override public void onMessageReceivedEvent(String msg) { } @@ -20,6 +19,7 @@ public class ConnectionListenerAdapter implements IConnectionListener { * * */ + @Override public void onConnectedEvent() { } @@ -30,8 +30,20 @@ public class ConnectionListenerAdapter implements IConnectionListener { * * */ + @Override public void onDisconnectedEvent() { } + + /** + * @see com.pqt.client.module.connection.listeners.IConnectionListener#onTimeOutEvent() + * + * + */ + @Override + public void onTimeOutEvent() { + + } + } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/IConnectionFirerer.java b/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/IConnectionFirerer.java index 4afdde4d..a9a24609 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/IConnectionFirerer.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/IConnectionFirerer.java @@ -2,14 +2,16 @@ package com.pqt.client.module.connection.listeners; public interface IConnectionFirerer { - public abstract void fireMessageReceivedEvent(String msg); + void fireMessageReceivedEvent(String msg); - public abstract void fireConnectedEvent(); + void fireConnectedEvent(); - public abstract void fireDisconnectedEvent(); + void fireDisconnectedEvent(); - public abstract void addListener(IConnectionListener listener); + void fireTimeOutEvent(); - public abstract void removeListener(IConnectionListener listener); + void addListener(IConnectionListener listener); + + void removeListener(IConnectionListener listener); } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/IConnectionListener.java b/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/IConnectionListener.java index c04b3b52..1444d908 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/IConnectionListener.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/IConnectionListener.java @@ -4,10 +4,12 @@ import java.util.EventListener; public interface IConnectionListener extends EventListener { - public abstract void onMessageReceivedEvent(String msg); + void onMessageReceivedEvent(String msg); - public abstract void onConnectedEvent(); + void onConnectedEvent(); - public abstract void onDisconnectedEvent(); + void onDisconnectedEvent(); + + void onTimeOutEvent(); } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/SimpleConnectionFirerer.java b/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/SimpleConnectionFirerer.java index d8bbf425..e4b2f47f 100644 --- a/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/SimpleConnectionFirerer.java +++ b/Workspace/client/src/main/java/com/pqt/client/module/connection/listeners/SimpleConnectionFirerer.java @@ -1,49 +1,67 @@ package com.pqt.client.module.connection.listeners; -//TODO écrire contenu méthodes -//TODO écrire javadoc +import javax.swing.event.EventListenerList; +import java.util.Arrays; + public class SimpleConnectionFirerer implements IConnectionFirerer { + private EventListenerList listenerList; + + public SimpleConnectionFirerer() { + listenerList = new EventListenerList(); + } /** * @see com.pqt.client.module.connection.listeners.IConnectionFirerer#fireMessageReceivedEvent(String) - * - * + * */ + @Override public void fireMessageReceivedEvent(String msg) { - + Arrays.stream(listenerList.getListeners(IConnectionListener.class)).forEach(listener->listener.onMessageReceivedEvent(msg)); } /** * @see com.pqt.client.module.connection.listeners.IConnectionFirerer#fireConnectedEvent() */ + @Override public void fireConnectedEvent() { - + Arrays.stream(listenerList.getListeners(IConnectionListener.class)).forEach(IConnectionListener::onConnectedEvent); } /** * @see com.pqt.client.module.connection.listeners.IConnectionFirerer#fireDisconnectedEvent() */ + @Override public void fireDisconnectedEvent() { - + Arrays.stream(listenerList.getListeners(IConnectionListener.class)).forEach(IConnectionListener::onDisconnectedEvent); } + /** + * @see com.pqt.client.module.connection.listeners.IConnectionFirerer#fireTimeOutEvent() + */ + @Override + public void fireTimeOutEvent() { + Arrays.stream(listenerList.getListeners(IConnectionListener.class)).forEach(IConnectionListener::onDisconnectedEvent); + } - /** + + /** * @see com.pqt.client.module.connection.listeners.IConnectionFirerer#addListener(com.pqt.client.module.connection.listeners.IConnectionListener) */ + @Override public void addListener(IConnectionListener listener) { - + listenerList.add(IConnectionListener.class, listener); } /** * @see com.pqt.client.module.connection.listeners.IConnectionFirerer#removeListener(com.pqt.client.module.connection.listeners.IConnectionListener) */ + @Override public void removeListener(IConnectionListener listener) { - + listenerList.remove(IConnectionListener.class, listener); } } diff --git a/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/HttpTextSender.java b/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/HttpTextSender.java new file mode 100644 index 00000000..993d434f --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/HttpTextSender.java @@ -0,0 +1,53 @@ +package com.pqt.client.module.connection.senders; + +import com.pqt.client.module.connection.listeners.IConnectionListener; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; + +public class HttpTextSender implements ITextSender{ + @Override + public void send(String url, String text, IConnectionListener listener) { + try { + HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("Content-Type", "application/json"); + con.setConnectTimeout(5000); + con.setReadTimeout(5000); + con.setInstanceFollowRedirects(true); + + String params = URLEncoder.encode("message="+text, "UTF-8"); + con.setDoOutput(true); + try(DataOutputStream out = new DataOutputStream(con.getOutputStream())) { + out.writeBytes(params); + out.flush(); + } + con.connect(); + listener.onConnectedEvent(); + + try(BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream()))) { + StringBuilder content = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + listener.onMessageReceivedEvent(content.toString()); + } + + con.disconnect(); + + }catch (java.net.SocketTimeoutException e){ + listener.onTimeOutEvent(); + }catch (IOException e) { + e.printStackTrace(); + }finally { + listener.onDisconnectedEvent(); + } + } +} diff --git a/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/ITextSender.java b/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/ITextSender.java new file mode 100644 index 00000000..e269dbcd --- /dev/null +++ b/Workspace/client/src/main/java/com/pqt/client/module/connection/senders/ITextSender.java @@ -0,0 +1,7 @@ +package com.pqt.client.module.connection.senders; + +import com.pqt.client.module.connection.listeners.IConnectionListener; + +public interface ITextSender { + void send(String url, String text, IConnectionListener listener); +}