All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmuc.jdlms.ResponseQueue Maven / Gradle / Ivy

Go to download

jDLMS is a library implementing the DLMS/COSEM (IEC 62056) communication standard.

There is a newer version: 1.8.0
Show newest version
/*
 * Copyright 2012-16 Fraunhofer ISE
 *
 * This file is part of jDLMS.
 * For more information visit http://www.openmuc.org
 *
 * jDLMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jDLMS 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jDLMS.  If not, see .
 *
 */
package org.openmuc.jdlms;

import java.io.IOException;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

import org.openmuc.jdlms.internal.asn1.axdr.AxdrType;

class ResponseQueue {

    private final BlockingDeque queue = new LinkedBlockingDeque<>();

    public void put(int invokeId, E data) throws InterruptedException {
        queue.putFirst(new Entry(invokeId, data));
    }

    public E poll(int invokeId, long timeout) throws IOException {

        Entry tmp;
        try {
            tmp = queue.poll(timeout, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            throw new IOException("Interrupted while waiting for incoming response");
        }

        if (tmp == null) {
            throw new ResponseTimeoutException("Timed out while waiting for incoming response.");
        }

        return tmp.data;

    }

    private class Entry {
        private final E data;

        public Entry(int invokeId, E pdu) {
            this.data = pdu;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy