#!/usr/bin/env perl

use strict;
use warnings;

use Zing::Zang;

=pod explain

- see ./examples/00-zing-zang

- zing-zang is simple a Zing/Process
- it provides on_perform() and on_receive() handlers
- the on_receive() handler is called whenever a message is received ...
- and is passed the process name of the sender and the message
- the signature is on_receive(self, from, message)

=cut

my $y = Zing::Zang->new(
  name => 'y-process',
  on_receive => sub {
    my ($self, $from, $data) = @_;

    warn $from, ' ', 'says', ' ', $data->{text};

    # mailbox has a reply method but that does something else
    $self->mailbox->send($from, {text => join(' ', "it's", time)});

    return;
  },
);

my $z = Zing::Zang->new(
  name => 'z-process',
  on_receive => sub {
    my ($self, $from, $data) = @_;

    warn $from, ' ', 'says', ' ', $data->{text};

    # mailbox has a reply method but that does something else
    $self->mailbox->send($from, {text => join(' ', "it's", time)});

    return;
  },
);


# initial send
# note: z-process mailbox doesn't actually exist yet
$y->mailbox->send('z-process', {text => join(' ', "it's", time)});
$y->exercise; # no message
$z->exercise; # has message, replies
$y->exercise; # has message, replies
$z->exercise; # has message, replies

warn 'should have printed 3 messages';

$y->destroy;
$z->destroy;
