2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.paradoxalarm.internal.communication;
15 import java.util.concurrent.ArrayBlockingQueue;
16 import java.util.concurrent.BlockingQueue;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * The {@link SyncQueue} is used to synchronize communication to/from Paradox system. All requests go into sendQueue and
23 * upon send are popped from send queue and are pushed into receiveQueue.
24 * Due to nature of Paradox communication receive queue is with priority, i.e. if there is anything in receive queue we
25 * attempt to read the socket first and only after receive queue is empty then we attempt to send. We never send any
26 * packet if we have something to read.
27 * For more details about usage see method {@link AbstractCommunicator.submitRequest()}
29 * @author Konstantin Polihronov - Initial contribution
31 public class SyncQueue {
33 private final Logger logger = LoggerFactory.getLogger(SyncQueue.class);
35 private BlockingQueue<IRequest> sendQueue = new ArrayBlockingQueue<>(1000, true);
36 private BlockingQueue<IRequest> receiveQueue = new ArrayBlockingQueue<>(10, true);
38 private static SyncQueue syncQueue;
43 public static SyncQueue getInstance() {
44 SyncQueue temp = syncQueue;
46 synchronized (SyncQueue.class) {
49 syncQueue = new SyncQueue();
56 public synchronized void add(IRequest request) {
57 logger.trace("Adding to queue request={}", request);
58 sendQueue.add(request);
61 public synchronized void moveRequest() {
62 IRequest request = sendQueue.poll();
63 request.setTimeStamp();
64 logger.trace("Moving from Tx to RX queue request={}", request);
65 receiveQueue.add(request);
68 public synchronized IRequest poll() {
69 IRequest request = receiveQueue.poll();
70 logger.trace("Removing from queue request={}", request);
74 public synchronized IRequest removeSendRequest() {
75 IRequest request = sendQueue.poll();
76 logger.trace("Removing from queue request={}", request);
80 public synchronized IRequest peekSendQueue() {
81 return sendQueue.peek();
84 public IRequest peekReceiveQueue() {
85 return receiveQueue.peek();
88 public synchronized boolean hasPacketToReceive() {
89 return receiveQueue.peek() != null;
92 public synchronized boolean hasPacketsToSend() {
93 return sendQueue.peek() != null;
96 public synchronized boolean canSend() {
97 return receiveQueue.isEmpty();