Skip to content

Instantly share code, notes, and snippets.

@tony-o
Last active December 15, 2015 09:49
Show Gist options
  • Save tony-o/5240871 to your computer and use it in GitHub Desktop.
Save tony-o/5240871 to your computer and use it in GitHub Desktop.

controller.pm

package Sims::Controller;

use strict;
use warnings;
use AnyEvent;
use Data::Dump qw(dump);
my @sims;

sub new{
  my $class = shift;
  my $self = {
    INTERVAL => 5
    ,SIMS => []
  };
  bless $self, $class;
  return $self;
}

sub go{
  my ($self,$params) = @_;
  $self->{INTERVAL} = $params->{INTERVAL} || $self->{INTERVAL};
  $self->{TIMER} = AE::timer 0, $self->{INTERVAL}, sub{
    for my $sim ($self->{SIMS}){
      $sim->RUN;
    }
  };
  $self->{READY} = AE::cv;
  $self->{READY}->recv;
  return !0;
}

sub add_sim{
  my ($self,$sim) = @_;
  push(@{$self->{SIMS}}, $sim);
  return !0;
}

1;

simcore.pm

package Sims::SimCore;

use strict;
use warnings;

sub new{
  my $class = shift;
  my $self = {
    _ID => shift
  };
  $self->{RUN} = sub{
    print "R$self->{ID}\n";
  };
  bless $self, $class;
  return $self;
};


1;

runner.pl

use strict;
use warnings;

use Data::Dump qw(dump);
use Sims::Controller;
use Sims::SimCore;

my $controller = new Sims::Controller();
my @sims;

push(@sims, new Sims::SimCore(0));
$controller->add_sim($sims[0]);

$controller->go({
  INTERVAL => 6
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment