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

org.bidib.wizard.gateway.netbidib.MessageHostAdapter Maven / Gradle / Ivy

There is a newer version: 2.0.25
Show newest version
package org.bidib.wizard.gateway.netbidib;

import org.bidib.jbidibc.messages.BidibMessageEvaluator;
import org.bidib.jbidibc.messages.helpers.Context;
import org.bidib.jbidibc.messages.helpers.DefaultContext;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.netbidib.server.adapter.DefaultHostAdapter;
import org.bidib.wizard.core.model.connection.ConnectionRegistry;
import org.bidib.wizard.gateway.service.ConnectionSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subjects.PublishSubject;

public class MessageHostAdapter extends DefaultHostAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageHostAdapter.class);

    private final BidibMessageEvaluator bidibMessageEvaluator;

    public MessageHostAdapter(final BidibMessageEvaluator bidibMessageEvaluator) {
        super(message -> message);
        this.bidibMessageEvaluator = bidibMessageEvaluator;
    }

    private final CompositeDisposable dispBidibMessages = new CompositeDisposable();

    private PublishSubject subjectBidibMessages;

    @Override
    public void initialize(final Context context) {
        LOGGER.info("Initialize the host adapter.");

        subjectBidibMessages = PublishSubject.create();

        super.initialize(context);

        // subscribe on single thread
        Disposable disposable = subjectBidibMessages.subscribeOn(Schedulers.single()).subscribe(bidibMessage -> {
            LOGGER.info("Call process bidib message: {}", bidibMessage);
            // call the BidibDistributedGatewayService instance
            this.bidibMessageEvaluator.processBidibMessageFromGuest(() -> 0, bidibMessage);
        });

        dispBidibMessages.add(disposable);

    }

    @Override
    public void forwardMessageToBackend(final BidibMessageInterface bidibMessage) {
        LOGGER.info("Forward message to backend: {}", bidibMessage);

        // called from the NetBidibServerHandler thread

        subjectBidibMessages.onNext(bidibMessage);
    }

    @Override
    public void signalConnectionOpened(Context context) {
        LOGGER.info("The remote connection was opened, context: {}", context);

        super.signalConnectionOpened(context);

        // make sure the backend is available
        if (bidibMessageEvaluator instanceof ConnectionSupport) {
            LOGGER.info("Make sure the backend is connected.");

            final Context connectContext = new DefaultContext();
            connectContext.register(Context.CONNECTION_ID, ConnectionRegistry.CONNECTION_ID_MAIN);

            ((ConnectionSupport) bidibMessageEvaluator).connectBackend(connectContext);
        }

    }

    @Override
    public void signalConnectionClosed(final Context context) {

        Long uniqueId = context.get(Context.UNIQUE_ID, Long.class, null);

        LOGGER.info("The connection was closed, uniqueId: {}", ByteUtils.formatHexUniqueId(uniqueId));

        this.bidibMessageEvaluator.connectionClosed(uniqueId);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy