From 41ab3818fe93d4afe7035a25def1fa4a4fc8f004 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Mon, 8 Oct 2018 22:18:33 +0200 Subject: [PATCH] Send XMPP command on separated thread. --- .../mytrackingdog/MainActivity.java | 2 +- .../xmpp/commands/send/SendCommand.java | 19 +++ .../commands/send/SendLocationCommand.java | 18 +++ .../send/SendObjectLocationCommand.java | 33 +++++ .../send/SendRealTimeModeCommand.java | 34 +++++ .../commands/send/SendStartTrailCommand.java | 23 ++++ .../commands/send/SendStopTrailCommand.java | 23 ++++ .../send/SendTrailLocationCommand.java | 34 +++++ .../services/ServiceTrackingDog.java | 4 +- .../mytrackingdog/services/ServiceXmpp.java | 118 ++++++++++-------- 10 files changed, 256 insertions(+), 52 deletions(-) create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendCommand.java create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendLocationCommand.java create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendObjectLocationCommand.java create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendRealTimeModeCommand.java create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendStartTrailCommand.java create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendStopTrailCommand.java create mode 100644 app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendTrailLocationCommand.java diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java index a2669c7..354aba7 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/MainActivity.java @@ -189,7 +189,7 @@ public class MainActivity extends AppCompatActivity implements IOrientationConsu } else { serviceTrackingDog.sendXmppCommandStopTrail(); } - } catch (XmppStringprepException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException | SmackException.NoResponseException e) { + } catch (XmppStringprepException e) { e.printStackTrace(); } } diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendCommand.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendCommand.java new file mode 100644 index 0000000..cf623b1 --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendCommand.java @@ -0,0 +1,19 @@ +package fr.chteufleur.mytrackingdog.models.xmpp.commands.send; + +import org.jivesoftware.smackx.commands.AdHocCommandManager; +import org.jxmpp.jid.FullJid; + +public abstract class SendCommand { + + public static final String TAG = SendCommand.class.getName(); + + protected final FullJid to; + protected final AdHocCommandManager commandManager; + + public SendCommand(AdHocCommandManager commandManager, FullJid to) { + this.commandManager = commandManager; + this.to = to; + } + + public abstract void executeCommand() throws Exception; +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendLocationCommand.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendLocationCommand.java new file mode 100644 index 0000000..c517a6b --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendLocationCommand.java @@ -0,0 +1,18 @@ +package fr.chteufleur.mytrackingdog.models.xmpp.commands.send; + +import org.jivesoftware.smackx.commands.AdHocCommandManager; +import org.jxmpp.jid.FullJid; + +public abstract class SendLocationCommand extends SendCommand { + + protected final double latitude; + protected final double longitude; + protected final long time; + + public SendLocationCommand(AdHocCommandManager commandManager, FullJid to, double lat, double lon, long time) { + super(commandManager, to); + this.latitude = lat; + this.longitude = lon; + this.time = time; + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendObjectLocationCommand.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendObjectLocationCommand.java new file mode 100644 index 0000000..68228db --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendObjectLocationCommand.java @@ -0,0 +1,33 @@ +package fr.chteufleur.mytrackingdog.models.xmpp.commands.send; + +import android.util.Log; + +import org.jivesoftware.smackx.commands.AdHocCommandManager; +import org.jivesoftware.smackx.commands.RemoteCommand; +import org.jivesoftware.smackx.xdata.Form; +import org.jxmpp.jid.FullJid; + +import fr.chteufleur.mytrackingdog.models.xmpp.commands.ObjectGeolocCommand; +import fr.chteufleur.mytrackingdog.services.ServiceXmpp; + +public class SendObjectLocationCommand extends SendLocationCommand { + + public SendObjectLocationCommand(AdHocCommandManager commandManager, FullJid to, double lat, double lon, long time) { + super(commandManager, to, lat, lon, time); + } + + @Override + public void executeCommand() throws Exception { + Log.i(TAG, "Send command object"); + RemoteCommand command = commandManager.getRemoteCommand(to, ServiceXmpp.XMPP_NODE_OBJECT_GEOLOC); + command.execute(); + if (command.getForm() == null) { + throw new Exception("Didn't get form back"); + } + Form form = command.getForm().createAnswerForm(); + form.setAnswer(ObjectGeolocCommand.FIELD_PARAM_LATITUDE, latitude); + form.setAnswer(ObjectGeolocCommand.FIELD_PARAM_LONGITUDE, longitude); + form.setAnswer(ObjectGeolocCommand.FIELD_PARAM_TIME, time); + command.next(form); + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendRealTimeModeCommand.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendRealTimeModeCommand.java new file mode 100644 index 0000000..2c8b25a --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendRealTimeModeCommand.java @@ -0,0 +1,34 @@ +package fr.chteufleur.mytrackingdog.models.xmpp.commands.send; + +import android.util.Log; + +import org.jivesoftware.smackx.commands.AdHocCommandManager; +import org.jivesoftware.smackx.commands.RemoteCommand; +import org.jivesoftware.smackx.xdata.Form; +import org.jxmpp.jid.FullJid; + +import fr.chteufleur.mytrackingdog.models.xmpp.commands.RealTimeModeCommand; +import fr.chteufleur.mytrackingdog.services.ServiceXmpp; + +public class SendRealTimeModeCommand extends SendCommand { + + private final boolean isRealTimeMode; + + public SendRealTimeModeCommand(AdHocCommandManager commandManager, FullJid to, boolean isRealTimeMode) { + super(commandManager, to); + this.isRealTimeMode = isRealTimeMode; + } + + @Override + public void executeCommand() throws Exception { + Log.i(TAG, "Send commande real time mode"); + RemoteCommand command = commandManager.getRemoteCommand(to, ServiceXmpp.XMPP_NODE_REAL_TIME_MODE); + command.execute(); + if (command.getForm() == null) { + throw new Exception("Didn't get form back"); + } + Form form = command.getForm().createAnswerForm(); + form.setAnswer(RealTimeModeCommand.FIELD_PARAM_IS_ACTIVE, isRealTimeMode); + command.next(form); + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendStartTrailCommand.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendStartTrailCommand.java new file mode 100644 index 0000000..33e61e8 --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendStartTrailCommand.java @@ -0,0 +1,23 @@ +package fr.chteufleur.mytrackingdog.models.xmpp.commands.send; + +import android.util.Log; + +import org.jivesoftware.smackx.commands.AdHocCommandManager; +import org.jivesoftware.smackx.commands.RemoteCommand; +import org.jxmpp.jid.FullJid; + +import fr.chteufleur.mytrackingdog.services.ServiceXmpp; + +public class SendStartTrailCommand extends SendCommand { + + public SendStartTrailCommand(AdHocCommandManager commandManager, FullJid to) { + super(commandManager, to); + } + + @Override + public void executeCommand() throws Exception { + Log.i(TAG, "Send command start"); + RemoteCommand command = commandManager.getRemoteCommand(to, ServiceXmpp.XMPP_NODE_START_TRAIL_GEOLOC); + command.execute(); + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendStopTrailCommand.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendStopTrailCommand.java new file mode 100644 index 0000000..3443311 --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendStopTrailCommand.java @@ -0,0 +1,23 @@ +package fr.chteufleur.mytrackingdog.models.xmpp.commands.send; + +import android.util.Log; + +import org.jivesoftware.smackx.commands.AdHocCommandManager; +import org.jivesoftware.smackx.commands.RemoteCommand; +import org.jxmpp.jid.FullJid; + +import fr.chteufleur.mytrackingdog.services.ServiceXmpp; + +public class SendStopTrailCommand extends SendCommand { + + public SendStopTrailCommand(AdHocCommandManager commandManager, FullJid to) { + super(commandManager, to); + } + + @Override + public void executeCommand() throws Exception { + Log.i(TAG, "Send command stop"); + RemoteCommand command = commandManager.getRemoteCommand(to, ServiceXmpp.XMPP_NODE_STOP_TRAIL_GEOLOC); + command.execute(); + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendTrailLocationCommand.java b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendTrailLocationCommand.java new file mode 100644 index 0000000..657eb69 --- /dev/null +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/models/xmpp/commands/send/SendTrailLocationCommand.java @@ -0,0 +1,34 @@ +package fr.chteufleur.mytrackingdog.models.xmpp.commands.send; + +import android.util.Log; + +import org.jivesoftware.smackx.commands.AdHocCommandManager; +import org.jivesoftware.smackx.commands.RemoteCommand; +import org.jivesoftware.smackx.xdata.Form; +import org.jxmpp.jid.FullJid; + +import fr.chteufleur.mytrackingdog.models.xmpp.commands.ObjectGeolocCommand; +import fr.chteufleur.mytrackingdog.models.xmpp.commands.TrailGeolocCommand; +import fr.chteufleur.mytrackingdog.services.ServiceXmpp; + +public class SendTrailLocationCommand extends SendLocationCommand { + + public SendTrailLocationCommand(AdHocCommandManager commandManager, FullJid to, double lat, double lon, long time) { + super(commandManager, to, lat, lon, time); + } + + @Override + public void executeCommand() throws Exception { + Log.i(TAG, "Send command location"); + RemoteCommand command = commandManager.getRemoteCommand(to, ServiceXmpp.XMPP_NODE_TRAIL_GEOLOC); + command.execute(); + if (command.getForm() == null) { + throw new Exception("Didn't get form back"); + } + Form form = command.getForm().createAnswerForm(); + form.setAnswer(TrailGeolocCommand.FIELD_PARAM_LATITUDE, latitude); + form.setAnswer(TrailGeolocCommand.FIELD_PARAM_LONGITUDE, longitude); + form.setAnswer(TrailGeolocCommand.FIELD_PARAM_TIME, time); + command.next(form); + } +} diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java index d663f67..3e90276 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceTrackingDog.java @@ -395,12 +395,12 @@ public class ServiceTrackingDog implements Observer { return ret; } // - public void sendXmppCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { + public void sendXmppCommandStartTrail() throws XmppStringprepException { if (serviceXmpp != null) { serviceXmpp.sendCommandStartTrail(); } } - public void sendXmppCommandStopTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { + public void sendXmppCommandStopTrail() throws XmppStringprepException { if (serviceXmpp != null) { serviceXmpp.sendCommandStopTrail(); } diff --git a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java index 30b6860..147d04f 100644 --- a/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java +++ b/app/src/main/java/fr/chteufleur/mytrackingdog/services/ServiceXmpp.java @@ -19,14 +19,12 @@ import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; import org.jivesoftware.smackx.commands.AdHocCommandManager; import org.jivesoftware.smackx.commands.LocalCommand; import org.jivesoftware.smackx.commands.LocalCommandFactory; -import org.jivesoftware.smackx.commands.RemoteCommand; import org.jivesoftware.smackx.filetransfer.FileTransfer; import org.jivesoftware.smackx.filetransfer.FileTransferListener; import org.jivesoftware.smackx.filetransfer.FileTransferManager; import org.jivesoftware.smackx.filetransfer.FileTransferRequest; import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer; import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer; -import org.jivesoftware.smackx.xdata.Form; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.FullJid; import org.jxmpp.jid.Jid; @@ -37,6 +35,7 @@ import java.io.File; import java.io.IOException; import java.net.Inet4Address; import java.net.UnknownHostException; +import java.util.LinkedList; import java.util.Observable; import fr.chteufleur.mytrackingdog.QRCodeGeneratorActivity; @@ -47,8 +46,14 @@ import fr.chteufleur.mytrackingdog.models.xmpp.commands.RealTimeModeCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.StartTrailGeolocCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.StopTrailGeolocCommand; import fr.chteufleur.mytrackingdog.models.xmpp.commands.TrailGeolocCommand; +import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendCommand; +import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendObjectLocationCommand; +import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendRealTimeModeCommand; +import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendStartTrailCommand; +import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendStopTrailCommand; +import fr.chteufleur.mytrackingdog.models.xmpp.commands.send.SendTrailLocationCommand; -public class ServiceXmpp extends Observable implements PresenceEventListener, FileTransferListener { +public class ServiceXmpp extends Observable implements Runnable, PresenceEventListener, FileTransferListener { public static final String TAG = ServiceXmpp.class.getName(); @@ -67,11 +72,11 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi public static final String NOTIF_NEW_PRESENCE_RECEIVED_VALUE_JID = NOTIF_NEW_PRESENCE_RECEIVED+".value.jid"; public static final String NOTIF_NEW_REAL_TIME_MODE_VALUE = NOTIF_NEW_REAL_TIME_MODE+".value"; - private static final String XMPP_NODE_TRAIL_GEOLOC = "trail_geoloc"; - private static final String XMPP_NODE_OBJECT_GEOLOC = "object_geoloc"; - private static final String XMPP_NODE_START_TRAIL_GEOLOC = "start_trail_geoloc"; - private static final String XMPP_NODE_STOP_TRAIL_GEOLOC = "stop_trail_geoloc"; - private static final String XMPP_NODE_REAL_TIME_MODE = "real_time_mode"; + public static final String XMPP_NODE_TRAIL_GEOLOC = "trail_geoloc"; + public static final String XMPP_NODE_OBJECT_GEOLOC = "object_geoloc"; + public static final String XMPP_NODE_START_TRAIL_GEOLOC = "start_trail_geoloc"; + public static final String XMPP_NODE_STOP_TRAIL_GEOLOC = "stop_trail_geoloc"; + public static final String XMPP_NODE_REAL_TIME_MODE = "real_time_mode"; private static final String XMPP_IP_SERVER = "51.254.205.203"; private static final String XMPP_DOMAIN_SERVER = "anon.xmpp.kingpenguin.tk"; @@ -87,6 +92,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi private String jid; private String otherJid; private final ServiceXmpp thsi; + private boolean run = false; private boolean isEnable = false; private boolean isRealTimeMode = false; @@ -103,6 +109,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi .setKeystoreType(null) .build(); this.thsi = this; + new Thread(this).start(); } public void setAppName(String appName) { @@ -149,6 +156,11 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi } public void close() { + disconnect(); + run = false; + } + + private void disconnect() { if (otherJid != null) { try { sendPresenceUnavailable(otherJid); @@ -168,7 +180,29 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi public void disable() { this.isEnable = false; - close(); + disconnect(); + } + + @Override + public void run() { + run = true; + while (run) { + if (connection != null && connection.isAuthenticated()) { + SendCommand command = getNextCommand(); + if (command != null) { + try { + command.executeCommand(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } } public boolean isEnabled() { @@ -258,6 +292,7 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi // // + private final LinkedList listSendCommand = new LinkedList<>(); private void registerCommands() { commandManager.registerCommand(XMPP_NODE_TRAIL_GEOLOC, XMPP_NODE_TRAIL_GEOLOC, new LocalCommandFactory() { @Override @@ -324,69 +359,54 @@ public class ServiceXmpp extends Observable implements PresenceEventListener, Fi notifyObservers(new Notification(NOTIF_NEW_REAL_TIME_MODE).addExtra(NOTIF_NEW_REAL_TIME_MODE_VALUE, isRealTimeMode)); } - public void sendCommandStartTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { + public void sendCommandStartTrail() throws XmppStringprepException { if (isOtherJidSet() && isRealTimeMode) { - Log.i(TAG, "Send command start"); - RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_START_TRAIL_GEOLOC); - command.execute(); + sendCommand(new SendStartTrailCommand(commandManager, JidCreate.fullFrom(otherJid))); } } - public void sendCommandStopTrail() throws XmppStringprepException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { + public void sendCommandStopTrail() throws XmppStringprepException { if (isOtherJidSet() && isRealTimeMode) { - Log.i(TAG, "Send command stop"); - RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_STOP_TRAIL_GEOLOC); - command.execute(); + sendCommand(new SendStopTrailCommand(commandManager, JidCreate.fullFrom(otherJid))); } } public void sendCommandObjectTrail(double lat, double lon, long time) throws Exception { if (isOtherJidSet() && isRealTimeMode) { - Log.i(TAG, "Send command object"); - RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_OBJECT_GEOLOC); - command.execute(); - if (command.getForm() == null) { - throw new Exception("Didn't get form back"); - } - Form form = command.getForm().createAnswerForm(); - form.setAnswer(ObjectGeolocCommand.FIELD_PARAM_LATITUDE, lat); - form.setAnswer(ObjectGeolocCommand.FIELD_PARAM_LONGITUDE, lon); - form.setAnswer(ObjectGeolocCommand.FIELD_PARAM_TIME, time); - command.next(form); + sendCommand(new SendObjectLocationCommand(commandManager, JidCreate.fullFrom(otherJid), lat, lon, time)); } } public void sendCommandLocationTrail(double lat, double lon, long time) throws Exception { if (isOtherJidSet() && isRealTimeMode) { - Log.i(TAG, "Send command location"); - RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_TRAIL_GEOLOC); - command.execute(); - if (command.getForm() == null) { - throw new Exception("Didn't get form back"); - } - Form form = command.getForm().createAnswerForm(); - form.setAnswer(TrailGeolocCommand.FIELD_PARAM_LATITUDE, lat); - form.setAnswer(TrailGeolocCommand.FIELD_PARAM_LONGITUDE, lon); - form.setAnswer(TrailGeolocCommand.FIELD_PARAM_TIME, time); - command.next(form); + sendCommand(new SendTrailLocationCommand(commandManager, JidCreate.fullFrom(otherJid), lat, lon, time)); } } public void sendCommandeRealTimeMode() throws Exception { if (isOtherJidSet()) { - Log.i(TAG, "Send commande real time mode"); - RemoteCommand command = commandManager.getRemoteCommand(JidCreate.fullFrom(otherJid), XMPP_NODE_REAL_TIME_MODE); - command.execute(); - if (command.getForm() == null) { - throw new Exception("Didn't get form back"); - } - Form form = command.getForm().createAnswerForm(); - form.setAnswer(RealTimeModeCommand.FIELD_PARAM_IS_ACTIVE, isRealTimeMode); - command.next(form); + sendCommand(new SendRealTimeModeCommand(commandManager, JidCreate.fullFrom(otherJid), isRealTimeMode)); } } - // + private void sendCommand(SendCommand command) { + if (command != null) { + synchronized (listSendCommand) { + listSendCommand.addLast(command); + } + } + } + + private SendCommand getNextCommand() { + SendCommand ret = null; + synchronized (listSendCommand) { + if (!listSendCommand.isEmpty()) { + ret = listSendCommand.removeFirst(); + } + } + return ret; + } + // // // Example: https://github.com/igniterealtime/Smack/blob/master/documentation/extensions/filetransfer.md