
%

%   Copyright 2014 The MathWorks, Inc.

classdef CloudStateNotifier < handle
    properties (Access = private)
        State = parallel.internal.types.MJSComputeCloudStates.Stopped;
    end
    events
        ClusterOnline
        ClusterOffline
    end
    
    methods (Access = private)
        function obj = CloudStateNotifier()
        end
        
        function notifyIfStateChanged(obj, notifiedState, eventName)
            if obj.State ~= notifiedState
                obj.State = notifiedState;
                notify(obj, eventName); % Broadcast notice of event
                dctSchedulerMessage(6, 'Notification of event %s has been sent', eventName);
            end
        end
    end
    
    methods (Static)
        function obj = getInstance()
            mlock;
            persistent cloudState
            
            if isempty(cloudState)
                cloudState = parallel.internal.cloud.CloudStateNotifier();
            end
            obj = cloudState;
        end
    end
    
    methods
        function notifyOnline(obj)
            notifiedState = parallel.internal.types.MJSComputeCloudStates.Ready; 
            obj.notifyIfStateChanged(notifiedState, 'ClusterOnline');
        end
        
        function notifyOffline(obj)
            notifiedState = parallel.internal.types.MJSComputeCloudStates.Stopped;
            obj.notifyIfStateChanged(notifiedState, 'ClusterOffline');
        end        
    end
end
