Fix reconnect issues
This commit is contained in:
parent
067a265adc
commit
4e7479a51f
7 changed files with 55 additions and 52 deletions
|
@ -47,16 +47,6 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
|
|||
tracker.setTrackerListener(this);
|
||||
storage = new OsMoGroupsStorage(this, settings.OSMO_GROUPS);
|
||||
storage.load();
|
||||
for(OsMoDevice d : storage.getMainGroup().getGroupUsers()) {
|
||||
if(d.isEnabled()) {
|
||||
connectDeviceImpl(d);
|
||||
}
|
||||
}
|
||||
for(OsMoGroup g : storage.getGroups()) {
|
||||
if(!g.isMainGroup() && g.isEnabled()) {
|
||||
connectGroupImpl(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setUiListener(OsMoGroupsUIListener uiListener) {
|
||||
|
@ -73,7 +63,20 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
|
|||
if(!service.getMyGroupTrackerId().equals(d.getTrackerId())) {
|
||||
tracker.startTrackingId(d);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reconnect() {
|
||||
for(OsMoDevice d : storage.getMainGroup().getGroupUsers()) {
|
||||
if(d.isEnabled()) {
|
||||
connectDeviceImpl(d);
|
||||
}
|
||||
}
|
||||
for(OsMoGroup g : storage.getGroups()) {
|
||||
if(!g.isMainGroup() && g.isEnabled()) {
|
||||
connectGroupImpl(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String connectGroupImpl(OsMoGroup g) {
|
||||
|
@ -348,4 +351,10 @@ public class OsMoGroups implements OsMoReactor, OsmoTrackerListener {
|
|||
storage.save();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String nextSendCommand(OsMoThread tracker) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -46,7 +46,8 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer
|
|||
|
||||
public OsMoPlugin(final OsmandApplication app) {
|
||||
service = new OsMoService(app);
|
||||
tracker = new OsMoTracker(service, app.getSettings().OSMO_SAVE_TRACK_INTERVAL);
|
||||
tracker = new OsMoTracker(service, app.getSettings().OSMO_SAVE_TRACK_INTERVAL,
|
||||
app.getSettings().OSMO_AUTO_SEND_LOCATIONS);
|
||||
this.app = app;
|
||||
ApplicationMode.regWidget("osmo_control", (ApplicationMode[])null);
|
||||
}
|
||||
|
@ -54,9 +55,6 @@ public class OsMoPlugin extends OsmandPlugin implements MonitoringInfoControlSer
|
|||
@Override
|
||||
public boolean init(final OsmandApplication app) {
|
||||
service.connect(true);
|
||||
if(app.getSettings().OSMO_AUTO_SEND_LOCATIONS.get()) {
|
||||
tracker.enableTracker();
|
||||
}
|
||||
groups = new OsMoGroups(service, tracker, app.getSettings());
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -5,5 +5,9 @@ import org.json.JSONObject;
|
|||
public interface OsMoReactor {
|
||||
|
||||
public boolean acceptCommand(String command, String id, String data, JSONObject obj, OsMoThread tread);
|
||||
|
||||
|
||||
public String nextSendCommand(OsMoThread tracker);
|
||||
|
||||
public void reconnect();
|
||||
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
package net.osmand.plus.osmo;
|
||||
|
||||
public interface OsMoSender {
|
||||
|
||||
public String nextSendCommand(OsMoThread tracker);
|
||||
}
|
|
@ -30,9 +30,8 @@ import org.json.JSONObject;
|
|||
import android.os.Build;
|
||||
import android.provider.Settings.Secure;
|
||||
|
||||
public class OsMoService implements OsMoSender, OsMoReactor {
|
||||
public class OsMoService implements OsMoReactor {
|
||||
private OsMoThread thread;
|
||||
private List<OsMoSender> listSenders = new java.util.concurrent.CopyOnWriteArrayList<OsMoSender>();
|
||||
private List<OsMoReactor> listReactors = new java.util.concurrent.CopyOnWriteArrayList<OsMoReactor>();
|
||||
private ConcurrentLinkedQueue<String> commands = new ConcurrentLinkedQueue<String>();
|
||||
private OsmandApplication app;
|
||||
|
@ -43,7 +42,6 @@ public class OsMoService implements OsMoSender, OsMoReactor {
|
|||
|
||||
public OsMoService(OsmandApplication app) {
|
||||
this.app = app;
|
||||
listSenders.add(this);
|
||||
listReactors.add(this);
|
||||
}
|
||||
|
||||
|
@ -86,7 +84,7 @@ public class OsMoService implements OsMoSender, OsMoReactor {
|
|||
}
|
||||
thread.stopConnection();
|
||||
}
|
||||
thread = new OsMoThread(this, listSenders, listReactors);
|
||||
thread = new OsMoThread(this, listReactors);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -96,11 +94,6 @@ public class OsMoService implements OsMoSender, OsMoReactor {
|
|||
}
|
||||
}
|
||||
|
||||
public void registerSender(OsMoSender sender) {
|
||||
if(!listSenders.contains(sender)) {
|
||||
listSenders.add(sender);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerReactor(OsMoReactor reactor) {
|
||||
if(!listReactors.contains(reactor)) {
|
||||
|
@ -108,10 +101,6 @@ public class OsMoService implements OsMoSender, OsMoReactor {
|
|||
}
|
||||
}
|
||||
|
||||
public void removeSender(OsMoSender s) {
|
||||
listSenders.remove(s);
|
||||
}
|
||||
|
||||
public void removeReactor(OsMoReactor s) {
|
||||
listReactors.remove(s);
|
||||
}
|
||||
|
@ -269,5 +258,9 @@ public class OsMoService implements OsMoSender, OsMoReactor {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reconnect() {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class OsMoThread {
|
|||
private static final long HEARTBEAT_DELAY = 100;
|
||||
private static final long HEARTBEAT_FAILED_DELAY = 10000;
|
||||
private static final long TIMEOUT_TO_RECONNECT = 60 * 1000;
|
||||
private static final long TIMEOUT_TO_PING = 2 * 60 * 1000;
|
||||
private static final long TIMEOUT_TO_PING = 5 * 60 * 1000;
|
||||
private static final long LIMIT_OF_FAILURES_RECONNECT = 10;
|
||||
private static final long SELECT_TIMEOUT = 500;
|
||||
private static int HEARTBEAT_MSG = 3;
|
||||
|
@ -52,7 +52,6 @@ public class OsMoThread {
|
|||
private boolean reconnect;
|
||||
private Selector selector;
|
||||
|
||||
private List<OsMoSender> listSenders;
|
||||
private List<OsMoReactor> listReactors;
|
||||
|
||||
private int authorized = 0; // 1 - send, 2 - authorized
|
||||
|
@ -73,9 +72,8 @@ public class OsMoThread {
|
|||
|
||||
|
||||
|
||||
public OsMoThread(OsMoService service, List<OsMoSender> listSenders, List<OsMoReactor> listReactors) {
|
||||
public OsMoThread(OsMoService service, List<OsMoReactor> listReactors) {
|
||||
this.service = service;
|
||||
this.listSenders = listSenders;
|
||||
this.listReactors = listReactors;
|
||||
// start thread to receive events from OSMO
|
||||
HandlerThread h = new HandlerThread("OSMo Service");
|
||||
|
@ -109,6 +107,9 @@ public class OsMoThread {
|
|||
}
|
||||
this.activeChannel = activeChannel;
|
||||
key.attach(new Integer(++activeConnectionId));
|
||||
for(OsMoReactor sender : listReactors) {
|
||||
sender.reconnect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -251,7 +252,6 @@ public class OsMoThread {
|
|||
private void processReadMessages() {
|
||||
while(!queueOfMessages.isEmpty()){
|
||||
String cmd = queueOfMessages.poll();
|
||||
log.info("OSMO get:"+cmd);
|
||||
cmd(cmd, false);
|
||||
int k = cmd.indexOf('|');
|
||||
String header = cmd;
|
||||
|
@ -363,7 +363,6 @@ public class OsMoThread {
|
|||
private ByteBuffer getNewPendingSendCommand() throws UnsupportedEncodingException {
|
||||
if(authorized == 0) {
|
||||
String auth = "TOKEN|"+ sessionInfo.token;
|
||||
log.info("OSMO send:" + auth);
|
||||
cmd(auth, true);
|
||||
authorized = 1;
|
||||
return ByteBuffer.wrap(prepareCommand(auth).toString().getBytes("UTF-8"));
|
||||
|
@ -371,12 +370,11 @@ public class OsMoThread {
|
|||
if(authorized == 1) {
|
||||
return null;
|
||||
}
|
||||
for (OsMoSender s : listSenders) {
|
||||
for (OsMoReactor s : listReactors) {
|
||||
String l = s.nextSendCommand(this);
|
||||
if (l != null) {
|
||||
cmd(l, true);
|
||||
StringBuilder res = prepareCommand(l);
|
||||
log.info("OSMO send " + res);
|
||||
return ByteBuffer.wrap(res.toString().getBytes("UTF-8"));
|
||||
}
|
||||
}
|
||||
|
@ -384,7 +382,6 @@ public class OsMoThread {
|
|||
if(pingSent == 0) {
|
||||
pingSent = System.currentTimeMillis();
|
||||
cmd(PING_CMD, true);
|
||||
log.info("OSMO send " + PING_CMD);
|
||||
return ByteBuffer.wrap(prepareCommand(PING_CMD).toString().getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
|
@ -397,6 +394,7 @@ public class OsMoThread {
|
|||
}
|
||||
|
||||
private void cmd(String cmd, boolean send) {
|
||||
log.info("OsMO" + (send ? "> " : ">> ") + cmd);
|
||||
last100Commands.add((send ? "> " : ">> ") + df.format(new Date()) + " " + cmd);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
package net.osmand.plus.osmo;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
import net.osmand.Location;
|
||||
import net.osmand.plus.OsmandSettings.CommonPreference;
|
||||
import net.osmand.plus.OsmandSettings.OsmandPreference;
|
||||
import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class OsMoTracker implements OsMoSender, OsMoReactor {
|
||||
public class OsMoTracker implements OsMoReactor {
|
||||
private ConcurrentLinkedQueue<Location> bufferOfLocations = new ConcurrentLinkedQueue<Location>();
|
||||
private boolean startSendingLocations;
|
||||
private OsMoService service;
|
||||
|
@ -22,19 +20,21 @@ public class OsMoTracker implements OsMoSender, OsMoReactor {
|
|||
private OsmoTrackerListener trackerListener = null;
|
||||
private Location lastSendLocation;
|
||||
private Location lastBufferLocation;
|
||||
private CommonPreference<Integer> pref;
|
||||
private OsmandPreference<Integer> pref;
|
||||
private String sessionURL;
|
||||
private Map<String, OsMoDevice> trackingDevices = new java.util.concurrent.ConcurrentHashMap<String, OsMoGroupsStorage.OsMoDevice>();
|
||||
private OsmandPreference<Boolean> autoStart;
|
||||
|
||||
public interface OsmoTrackerListener {
|
||||
|
||||
public void locationChange(String trackerId, Location location);
|
||||
}
|
||||
|
||||
public OsMoTracker(OsMoService service, CommonPreference<Integer> pref) {
|
||||
public OsMoTracker(OsMoService service, OsmandPreference<Integer> interval,
|
||||
OsmandPreference<Boolean> autoStart) {
|
||||
this.service = service;
|
||||
this.pref = pref;
|
||||
service.registerSender(this);
|
||||
this.pref = interval;
|
||||
this.autoStart = autoStart;
|
||||
service.registerReactor(this);
|
||||
}
|
||||
|
||||
|
@ -216,6 +216,13 @@ public class OsMoTracker implements OsMoSender, OsMoReactor {
|
|||
return trackingDevices.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reconnect() {
|
||||
if(autoStart.get()) {
|
||||
enableTracker();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue