1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Remove duplicate queue

# Conflicts:
#	app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java
This commit is contained in:
Andreas Böhler 2019-02-27 09:21:41 +01:00
parent 88ac816393
commit 325add3f0a
4 changed files with 76 additions and 71 deletions

View File

@ -0,0 +1,49 @@
/* Copyright (C) 2015-2019 Andreas Shimokawa, Carsten Pfeiffer, Andreas Boehler
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.btle;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public abstract class AbstractTransaction {
private final String mName;
private final long creationTimestamp = System.currentTimeMillis();
public AbstractTransaction(String taskName) {
this.mName = taskName;
}
public String getTaskName() {
return mName;
}
protected String getCreationTime() {
return DateFormat.getTimeInstance(DateFormat.MEDIUM).format(new Date(creationTimestamp));
}
public int getActionSize() {
return 0;
}
@Override
public String toString() {
return String.format(Locale.US, "%s: Transaction task: %s with %d actions", getCreationTime(), getTaskName(), getActionSize());
}
}

View File

@ -38,10 +38,10 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
@ -57,15 +57,13 @@ public final class BtLEQueue {
private static final Logger LOG = LoggerFactory.getLogger(BtLEQueue.class); private static final Logger LOG = LoggerFactory.getLogger(BtLEQueue.class);
private final Object mGattMonitor = new Object(); private final Object mGattMonitor = new Object();
private final Object mTransactionMonitor = new Object();
private final GBDevice mGbDevice; private final GBDevice mGbDevice;
private final BluetoothAdapter mBluetoothAdapter; private final BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mBluetoothGatt; private BluetoothGatt mBluetoothGatt;
private BluetoothGattServer mBluetoothGattServer; private BluetoothGattServer mBluetoothGattServer;
private final Set<BluetoothGattService> mSupportedServerServices; private final Set<BluetoothGattService> mSupportedServerServices;
private final Queue<Transaction> mTransactions = new ConcurrentLinkedQueue<>(); private final BlockingQueue<AbstractTransaction> mTransactions = new LinkedBlockingQueue<>();
private final Queue<ServerTransaction> mServerTransactions = new ConcurrentLinkedQueue<>();
private volatile boolean mDisposed; private volatile boolean mDisposed;
private volatile boolean mCrashed; private volatile boolean mCrashed;
private volatile boolean mAbortTransaction; private volatile boolean mAbortTransaction;
@ -88,17 +86,7 @@ public final class BtLEQueue {
while (!mDisposed && !mCrashed) { while (!mDisposed && !mCrashed) {
try { try {
if(mTransactions.isEmpty() && mServerTransactions.isEmpty()) { AbstractTransaction qTransaction = mTransactions.take();
synchronized (mTransactionMonitor) {
try {
mTransactionMonitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Transaction transaction = mTransactions.poll();
ServerTransaction serverTransaction = mServerTransactions.poll();
if (!isConnected()) { if (!isConnected()) {
LOG.debug("not connected, waiting for connection..."); LOG.debug("not connected, waiting for connection...");
@ -115,7 +103,8 @@ public final class BtLEQueue {
mConnectionLatch = null; mConnectionLatch = null;
} }
if(serverTransaction != null) { if(qTransaction instanceof ServerTransaction) {
ServerTransaction serverTransaction = (ServerTransaction)qTransaction;
internalGattServerCallback.setTransactionGattCallback(serverTransaction.getGattCallback()); internalGattServerCallback.setTransactionGattCallback(serverTransaction.getGattCallback());
mAbortServerTransaction = false; mAbortServerTransaction = false;
@ -144,7 +133,8 @@ public final class BtLEQueue {
} }
} }
if(transaction != null) { if(qTransaction instanceof Transaction) {
Transaction transaction = (Transaction)qTransaction;
internalGattCallback.setTransactionGattCallback(transaction.getGattCallback()); internalGattCallback.setTransactionGattCallback(transaction.getGattCallback());
mAbortTransaction = false; mAbortTransaction = false;
// Run all actions of the transaction until one doesn't succeed // Run all actions of the transaction until one doesn't succeed
@ -308,10 +298,9 @@ public final class BtLEQueue {
if (mWaitForServerActionResultLatch != null) { if (mWaitForServerActionResultLatch != null) {
mWaitForServerActionResultLatch.countDown(); mWaitForServerActionResultLatch.countDown();
} }
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
boolean wasInitialized = mGbDevice.isInitialized(); boolean wasInitialized = mGbDevice.isInitialized();
setDeviceConnectionState(State.NOT_CONNECTED); setDeviceConnectionState(State.NOT_CONNECTED);
// either we've been disconnected because the device is out of range // either we've been disconnected because the device is out of range
@ -368,9 +357,6 @@ public final class BtLEQueue {
LOG.debug("about to add: " + transaction); LOG.debug("about to add: " + transaction);
if (!transaction.isEmpty()) { if (!transaction.isEmpty()) {
mTransactions.add(transaction); mTransactions.add(transaction);
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
} }
} }
@ -382,10 +368,7 @@ public final class BtLEQueue {
public void add(ServerTransaction transaction) { public void add(ServerTransaction transaction) {
LOG.debug("about to add: " + transaction); LOG.debug("about to add: " + transaction);
if(!transaction.isEmpty()) { if(!transaction.isEmpty()) {
mServerTransactions.add(transaction); mTransactions.add(transaction);
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
} }
} }
@ -399,26 +382,19 @@ public final class BtLEQueue {
public void insert(Transaction transaction) { public void insert(Transaction transaction) {
LOG.debug("about to insert: " + transaction); LOG.debug("about to insert: " + transaction);
if (!transaction.isEmpty()) { if (!transaction.isEmpty()) {
List<Transaction> tail = new ArrayList<>(mTransactions.size() + 2); List<AbstractTransaction> tail = new ArrayList<>(mTransactions.size() + 2);
//mTransactions.drainTo(tail); //mTransactions.drainTo(tail);
for( Transaction t : mTransactions) { for( AbstractTransaction t : mTransactions) {
tail.add(t); tail.add(t);
} }
mTransactions.clear(); mTransactions.clear();
mTransactions.add(transaction); mTransactions.add(transaction);
mTransactions.addAll(tail); mTransactions.addAll(tail);
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
} }
} }
public void clear() { public void clear() {
mTransactions.clear(); mTransactions.clear();
mServerTransactions.clear();
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
} }
/** /**

View File

@ -16,10 +16,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.btle; package nodomain.freeyourgadget.gadgetbridge.service.btle;
import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -31,20 +29,14 @@ import androidx.annotation.Nullable;
* *
* @author TREND * @author TREND
*/ */
public class ServerTransaction { public class ServerTransaction extends AbstractTransaction {
private final String mName;
private final List<BtLEServerAction> mActions = new ArrayList<>(4); private final List<BtLEServerAction> mActions = new ArrayList<>(4);
private final long creationTimestamp = System.currentTimeMillis();
private private
@Nullable @Nullable
GattServerCallback gattCallback; GattServerCallback gattCallback;
public ServerTransaction(String taskName) { public ServerTransaction(String taskName) {
this.mName = taskName; super(taskName);
}
public String getTaskName() {
return mName;
} }
public void add(BtLEServerAction action) { public void add(BtLEServerAction action) {
@ -59,10 +51,6 @@ public class ServerTransaction {
return mActions.isEmpty(); return mActions.isEmpty();
} }
protected String getCreationTime() {
return DateFormat.getTimeInstance(DateFormat.MEDIUM).format(new Date(creationTimestamp));
}
@Override @Override
public String toString() { public String toString() {
return String.format(Locale.US, "%s: Transaction task: %s with %d actions", getCreationTime(), getTaskName(), mActions.size()); return String.format(Locale.US, "%s: Transaction task: %s with %d actions", getCreationTime(), getTaskName(), mActions.size());
@ -80,4 +68,9 @@ public class ServerTransaction {
GattServerCallback getGattCallback() { GattServerCallback getGattCallback() {
return gattCallback; return gattCallback;
} }
@Override
public int getActionSize() {
return mActions.size();
}
} }

View File

@ -17,12 +17,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.btle; package nodomain.freeyourgadget.gadgetbridge.service.btle;
import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -32,20 +29,14 @@ import androidx.annotation.Nullable;
* *
* @author TREND * @author TREND
*/ */
public class Transaction { public class Transaction extends AbstractTransaction {
private final String mName;
private final List<BtLEAction> mActions = new ArrayList<>(4); private final List<BtLEAction> mActions = new ArrayList<>(4);
private final long creationTimestamp = System.currentTimeMillis();
private private
@Nullable @Nullable
GattCallback gattCallback; GattCallback gattCallback;
public Transaction(String taskName) { public Transaction(String taskName) {
this.mName = taskName; super(taskName);
}
public String getTaskName() {
return mName;
} }
public void add(BtLEAction action) { public void add(BtLEAction action) {
@ -60,15 +51,6 @@ public class Transaction {
return mActions.isEmpty(); return mActions.isEmpty();
} }
protected String getCreationTime() {
return DateFormat.getTimeInstance(DateFormat.MEDIUM).format(new Date(creationTimestamp));
}
@Override
public String toString() {
return String.format(Locale.US, "%s: Transaction task: %s with %d actions", getCreationTime(), getTaskName(), mActions.size());
}
public void setGattCallback(@Nullable GattCallback callback) { public void setGattCallback(@Nullable GattCallback callback) {
gattCallback = callback; gattCallback = callback;
} }
@ -81,4 +63,9 @@ public class Transaction {
GattCallback getGattCallback() { GattCallback getGattCallback() {
return gattCallback; return gattCallback;
} }
@Override
public int getActionSize() {
return mActions.size();
}
} }