classdef (ConstructOnLoad) MessageBroker < handle
    % MESSAGEBROKER receives data from Dispatcher thru shared MessageQueue,
    % and notifies listeners about Pipeline updates

    properties (Transient)
        MessageQueue parallel.pool.DataQueue
    end

    events
        PipelineUpdate
    end

    methods
        function obj = MessageBroker()
            % Initialize message queue to print messages

            obj.MessageQueue = parallel.pool.DataQueue;
            afterEach(obj.MessageQueue, @(dispatchDataStruct) obj.notifyPipelineUpdates(dispatchDataStruct));
        end

        function notifyPipelineUpdates(obj, dispatchDataStruct)
            % NOTIFYPIPELINEUPDATES receives data from Dispatcher thru
            % shared MessageQueue, and notifies listeners about
            % "PipelineUpdate"

            % Notify listeners of "PipelineUpdate"
            dispatchEvt = bioinfo.pipeline.internal.DispatcherEventData(...
                dispatchDataStruct.Level,...
                dispatchDataStruct.Message,...
                dispatchDataStruct.TimeStamp,...
                dispatchDataStruct.Type,...
                dispatchDataStruct.UUID,...
                dispatchDataStruct.MException);
            notify(obj, "PipelineUpdate", dispatchEvt);
        end
    end
end