classdef am_serverA < atomic
  %% Description
  %  processes one entity in time tS
  %% Ports
  %  inputs: 
  %    in   incoming entities
  %  outputs: 
  %    out      outgoing entities
  %    working  true/false
  %    n        current number of entities in server
  %% States
  %  phase: 0|1|2 == idle|goProcessing|processing
  %  entity: id of processed entity
  %% System Parameters
  %  tS:  service time
  %  debug: flag to enable debug information

  methods
    function obj = am_serverA(name, tS, debug)
      if nargin == 3
        x = {'in'};
        y = {'out', 'working', 'n'};
        s = {'phase','entity'};
        sysparams = struct('tS', tS, 'debug', debug);
      else
        error('mistake at constructor method for class am_serverA');
      end
      obj = obj@atomic(name, x, y, s, 0, sysparams); % call parent
      obj.s.phase = 0;
      obj.s.entity = 0;
    end
        
    function ta = tafun(obj)
      switch obj.s.phase
        case 0    % idle
          ta = inf;
        case 1    % goProcessing
          ta = 0;
        case 2    % processing
          ta = obj.sysparams.tS;
        otherwise
          fprintf('wrong phase %d in %s\n', obj.s.phase, obj.name);
      end
    end
        
    function deltaconffun(obj,gt)
      if obj.sysparams.debug    
        fprintf('%4.1f %8s con, in phase %d\n', gt, obj.name, obj.s.phase)
      end
      deltaintfun(obj);
      deltaextfun(obj,gt);
    end
    
    function deltaextfun(obj,gt)
      if obj.s.phase == 0 
        obj.s.phase = 1;
        obj.s.entity = obj.x.in{1};
      else
        fprintf('dExt: wrong phase %d in %s\n', obj.s.phase, obj.name);
      end  
      if obj.sysparams.debug
        fprintf('%4.1f %8s ext, going to phase %d\n', gt, obj.name, obj.s.phase)
      end
    end
        
    function deltaintfun(obj)
      switch obj.s.phase
        case 1    % goProcessing
          obj.s.phase = 2;
        case 2    % processing
          obj.s.phase = 0;
          obj.s.entity = 0;
        otherwise
          fprintf('dInt: wrong phase %d in %s\n', obj.s.phase, obj.name);
      end
      if obj.sysparams.debug
        fprintf('%4.1f %8s int, going to phase %d\n', ...
		obj.tnext, obj.name, obj.s.phase)
      end
    end
               
    function lambdafun(obj)
      switch obj.s.phase
        case 1    % goProcessing
          obj.y.working = true;
        case 2    % processing
          obj.y.out = {obj.s.entity};
          obj.y.working = false;
        otherwise
          fprintf('la: wrong phase %d in %s\n', obj.s.phase, obj.name);
      end
      obj.y.n = double(obj.y.working);
      
      if obj.sysparams.debug
        fprintf('%4.1f %8s OUT, working=', obj.tnext, obj.name)
        fprintf('%2d', obj.y.working)
        fprintf(' out=')
        fprintf('%2d', cell2mat(obj.y.out))
        fprintf('\n')
      end
    end
    
  end  % methods
end   % classdef
