1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15: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.Collections;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import androidx.annotation.Nullable;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
@ -57,15 +57,13 @@ public final class BtLEQueue {
private static final Logger LOG = LoggerFactory.getLogger(BtLEQueue.class);
private final Object mGattMonitor = new Object();
private final Object mTransactionMonitor = new Object();
private final GBDevice mGbDevice;
private final BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattServer mBluetoothGattServer;
private final Set<BluetoothGattService> mSupportedServerServices;
private final Queue<Transaction> mTransactions = new ConcurrentLinkedQueue<>();
private final Queue<ServerTransaction> mServerTransactions = new ConcurrentLinkedQueue<>();
private final BlockingQueue<AbstractTransaction> mTransactions = new LinkedBlockingQueue<>();
private volatile boolean mDisposed;
private volatile boolean mCrashed;
private volatile boolean mAbortTransaction;
@ -88,17 +86,7 @@ public final class BtLEQueue {
while (!mDisposed && !mCrashed) {
try {
if(mTransactions.isEmpty() && mServerTransactions.isEmpty()) {
synchronized (mTransactionMonitor) {
try {
mTransactionMonitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Transaction transaction = mTransactions.poll();
ServerTransaction serverTransaction = mServerTransactions.poll();
AbstractTransaction qTransaction = mTransactions.take();
if (!isConnected()) {
LOG.debug("not connected, waiting for connection...");
@ -115,7 +103,8 @@ public final class BtLEQueue {
mConnectionLatch = null;
}
if(serverTransaction != null) {
if(qTransaction instanceof ServerTransaction) {
ServerTransaction serverTransaction = (ServerTransaction)qTransaction;
internalGattServerCallback.setTransactionGattCallback(serverTransaction.getGattCallback());
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());
mAbortTransaction = false;
// Run all actions of the transaction until one doesn't succeed
@ -308,10 +298,9 @@ public final class BtLEQueue {
if (mWaitForServerActionResultLatch != null) {
mWaitForServerActionResultLatch.countDown();
}
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
boolean wasInitialized = mGbDevice.isInitialized();
setDeviceConnectionState(State.NOT_CONNECTED);
// 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);
if (!transaction.isEmpty()) {
mTransactions.add(transaction);
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
}
}
@ -382,10 +368,7 @@ public final class BtLEQueue {
public void add(ServerTransaction transaction) {
LOG.debug("about to add: " + transaction);
if(!transaction.isEmpty()) {
mServerTransactions.add(transaction);
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
mTransactions.add(transaction);
}
}
@ -399,26 +382,19 @@ public final class BtLEQueue {
public void insert(Transaction transaction) {
LOG.debug("about to insert: " + transaction);
if (!transaction.isEmpty()) {
List<Transaction> tail = new ArrayList<>(mTransactions.size() + 2);
List<AbstractTransaction> tail = new ArrayList<>(mTransactions.size() + 2);
//mTransactions.drainTo(tail);
for( Transaction t : mTransactions) {
for( AbstractTransaction t : mTransactions) {
tail.add(t);
}
mTransactions.clear();
mTransactions.add(transaction);
mTransactions.addAll(tail);
synchronized(mTransactionMonitor) {
mTransactionMonitor.notify();
}
}
}
public void 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/>. */
package nodomain.freeyourgadget.gadgetbridge.service.btle;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
@ -31,20 +29,14 @@ import androidx.annotation.Nullable;
*
* @author TREND
*/
public class ServerTransaction {
private final String mName;
public class ServerTransaction extends AbstractTransaction {
private final List<BtLEServerAction> mActions = new ArrayList<>(4);
private final long creationTimestamp = System.currentTimeMillis();
private
@Nullable
GattServerCallback gattCallback;
public ServerTransaction(String taskName) {
this.mName = taskName;
}
public String getTaskName() {
return mName;
super(taskName);
}
public void add(BtLEServerAction action) {
@ -59,10 +51,6 @@ public class ServerTransaction {
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());
@ -80,4 +68,9 @@ public class ServerTransaction {
GattServerCallback getGattCallback() {
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/>. */
package nodomain.freeyourgadget.gadgetbridge.service.btle;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import androidx.annotation.Nullable;
@ -32,20 +29,14 @@ import androidx.annotation.Nullable;
*
* @author TREND
*/
public class Transaction {
private final String mName;
public class Transaction extends AbstractTransaction {
private final List<BtLEAction> mActions = new ArrayList<>(4);
private final long creationTimestamp = System.currentTimeMillis();
private
@Nullable
GattCallback gattCallback;
public Transaction(String taskName) {
this.mName = taskName;
}
public String getTaskName() {
return mName;
super(taskName);
}
public void add(BtLEAction action) {
@ -60,15 +51,6 @@ public class Transaction {
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) {
gattCallback = callback;
}
@ -81,4 +63,9 @@ public class Transaction {
GattCallback getGattCallback() {
return gattCallback;
}
@Override
public int getActionSize() {
return mActions.size();
}
}