Send XMPP command on separated thread.
This commit is contained in:
parent
4499278583
commit
41ab3818fe
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -395,12 +395,12 @@ public class ServiceTrackingDog implements Observer {
|
|||
return ret;
|
||||
}
|
||||
//<editor-fold defaultstate="collapsed" desc="Commands">
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
|||
//</editor-fold>
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="Commands">
|
||||
private final LinkedList<SendCommand> 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));
|
||||
}
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
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;
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold defaultstate="collapsed" desc="File transfert">
|
||||
// Example: https://github.com/igniterealtime/Smack/blob/master/documentation/extensions/filetransfer.md
|
||||
|
|
|
|||
Loading…
Reference in New Issue