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

org.jivesoftware.smack.NonzaCallback Maven / Gradle / Ivy

There is a newer version: 4.5.0-beta5
Show newest version
/**
 *
 * Copyright 2018-2019 Florian Schmaus
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smack;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;

import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.FailedNonzaException;
import org.jivesoftware.smack.packet.Nonza;
import org.jivesoftware.smack.util.XmppElementUtil;

public class NonzaCallback {

    protected final AbstractXMPPConnection connection;
    protected final Map> filterAndListeners;

    private NonzaCallback(Builder builder) {
        this.connection = builder.connection;
        this.filterAndListeners = builder.filterAndListeners;
        install();
    }

    void onNonzaReceived(Nonza nonza) throws IOException {
        QName key = nonza.getQName();
        ClassAndConsumer classAndConsumer = filterAndListeners.get(key);

        classAndConsumer.accept(nonza);
    }

    public void cancel() {
        for (Map.Entry> entry : filterAndListeners.entrySet()) {
            QName filterKey = entry.getKey();
            synchronized (connection.nonzaCallbacksMap) {
                connection.nonzaCallbacksMap.removeOne(filterKey, this);
            }
        }
    }

    protected void install() {
        if (filterAndListeners.isEmpty()) {
            return;
        }

        for (QName key : filterAndListeners.keySet()) {
            synchronized (connection.nonzaCallbacksMap) {
                connection.nonzaCallbacksMap.put(key, this);
            }
        }
    }

    private static final class NonzaResponseCallback extends NonzaCallback {

        private SN successNonza;
        private FN failedNonza;

        private NonzaResponseCallback(Class successNonzaClass, Class failedNonzaClass,
                        Builder builder) {
            super(builder);

            final QName successNonzaKey = XmppElementUtil.getQNameFor(successNonzaClass);
            final QName failedNonzaKey = XmppElementUtil.getQNameFor(failedNonzaClass);

            final NonzaListener successListener = new NonzaListener() {
                @Override
                public void accept(SN successNonza) {
                    NonzaResponseCallback.this.successNonza = successNonza;
                    notifyResponse();
                }
            };
            final ClassAndConsumer successClassAndConsumer = new ClassAndConsumer<>(successNonzaClass,
                            successListener);

            final NonzaListener failedListener = new NonzaListener() {
                @Override
                public void accept(FN failedNonza) {
                    NonzaResponseCallback.this.failedNonza = failedNonza;
                    notifyResponse();
                }
            };
            final ClassAndConsumer failedClassAndConsumer = new ClassAndConsumer<>(failedNonzaClass,
                            failedListener);

            filterAndListeners.put(successNonzaKey, successClassAndConsumer);
            filterAndListeners.put(failedNonzaKey, failedClassAndConsumer);

            install();
        }

        private void notifyResponse() {
            synchronized (this) {
                notifyAll();
            }
        }

        private boolean hasReceivedSuccessOrFailedNonza() {
            return successNonza != null || failedNonza != null;
        }

        private SN waitForResponse() throws NoResponseException, InterruptedException, FailedNonzaException {
            final long deadline = System.currentTimeMillis() + connection.getReplyTimeout();
            synchronized (this) {
                while (!hasReceivedSuccessOrFailedNonza()) {
                    final long now = System.currentTimeMillis();
                    if (now >= deadline) break;
                    wait(deadline - now);
                }
            }

            if (!hasReceivedSuccessOrFailedNonza()) {
                throw NoResponseException.newWith(connection, "Nonza Listener");
            }

            if (failedNonza != null) {
                throw new XMPPException.FailedNonzaException(failedNonza);
            }

            assert successNonza != null;
            return successNonza;
        }
    }

    public static final class Builder {
        private final AbstractXMPPConnection connection;

        private Map> filterAndListeners = new HashMap<>();

        Builder(AbstractXMPPConnection connection) {
            this.connection = connection;
        }

        public  Builder listenFor(Class nonza, NonzaListener nonzaListener) {
            QName key = XmppElementUtil.getQNameFor(nonza);
            ClassAndConsumer classAndConsumer = new ClassAndConsumer<>(nonza, nonzaListener);
            filterAndListeners.put(key, classAndConsumer);
            return this;
        }

        public NonzaCallback install() {
            return new NonzaCallback(this);
        }
    }

    public interface NonzaListener {
        void accept(N nonza) throws IOException;
    }

    private static final class ClassAndConsumer {
        private final Class clazz;
        private final NonzaListener consumer;

        private ClassAndConsumer(Class clazz, NonzaListener consumer) {
            this.clazz = clazz;
            this.consumer = consumer;
        }

        private void accept(Object object) throws IOException {
            N nonza = clazz.cast(object);
            consumer.accept(nonza);
        }
    }

    static  SN sendAndWaitForResponse(NonzaCallback.Builder builder, Nonza nonza, Class successNonzaClass,
                    Class failedNonzaClass)
                    throws NoResponseException, NotConnectedException, InterruptedException, FailedNonzaException {
        NonzaResponseCallback nonzaCallback = new NonzaResponseCallback<>(successNonzaClass,
                        failedNonzaClass, builder);

        SN successNonza;
        try {
            nonzaCallback.connection.sendNonza(nonza);
            successNonza = nonzaCallback.waitForResponse();
        }
        finally {
            nonzaCallback.cancel();
        }

        return successNonza;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy