%AbstractWorker Base class for parallel pool worker objects

% Copyright 2012-2020 The MathWorks, Inc.
classdef AbstractWorker < parallel.internal.pool.SessionObject

    properties ( GetAccess = protected, SetAccess = immutable )
        NumWorkers  % How many workers in the pool.
    end
    properties ( SetAccess = immutable )
        WorkerIndex % Which worker number am I in the pool.
    end

    methods ( Abstract )
        stopLabAndDisconnect(obj);
    end

    methods
        function obj = AbstractWorker( idx, num )
            obj.WorkerIndex = idx;
            obj.NumWorkers  = num;
        end
    end

    methods ( Sealed )
        function setDependencyDir(obj, dependencyDir)
            % Ask the session to store the dependency dir
            obj.Session.setDependencyDir(dependencyDir);
        end

        function connectToClient(obj, buildFcn, broadcastFunctor)
            obj.Session = buildFcn(obj.WorkerIndex, obj.NumWorkers, broadcastFunctor);
        end
    end

    methods
        function delete(~)
            parallel.internal.pool.workerSession([]);
        end
    end
end
