| 1 | package Zing::Mailbox; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry 'Zing::Types'; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | extends 'Zing::PubSub'; | ||
| 15 | |||
| 16 | our $VERSION = '0.26'; # VERSION | ||
| 17 | |||
| 18 | # METHODS | ||
| 19 | |||
| 20 | method recv() { | ||
| 21 | return $self->store->lpull($self->term); | ||
| 22 | } | ||
| 23 | |||
| 24 | method message(HashRef $value) { | ||
| 25 | return { data => $value, from => $self->term }; | ||
| 26 | } | ||
| 27 | |||
| 28 | method reply(HashRef $msg, HashRef $value) { | ||
| 29 | return $self->send($msg->{from}, $value); | ||
| 30 | } | ||
| 31 | |||
| 32 | method send(Str $key, HashRef $value) { | ||
| 33 | return $self->store->rpush($self->term($key), $self->message($value)); | ||
| 34 | } | ||
| 35 | |||
| 36 | method size() { | ||
| 37 | return $self->store->size($self->term); | ||
| 38 | } | ||
| 39 | |||
| 40 | method term(Maybe[Str] $name) { | ||
| 41 | return $self->app->term($name || $self)->mailbox; | ||
| 42 | } | ||
| 43 | |||
| 44 | 1; | ||
| 45 | |||
| 46 | =encoding utf8 | ||
| 47 | |||
| 48 | =head1 NAME | ||
| 49 | |||
| 50 | Zing::Mailbox - Process Mailbox | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | =head1 ABSTRACT | ||
| 55 | |||
| 56 | Interprocess Communication Mechanism | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | =head1 SYNOPSIS | ||
| 61 | |||
| 62 | use Zing::Mailbox; | ||
| 63 | |||
| 64 | my $mailbox = Zing::Mailbox->new(name => rand); | ||
| 65 | |||
| 66 | # $mailbox->recv; | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | =head1 DESCRIPTION | ||
| 71 | |||
| 72 | This package provides represents a process mailbox, the default mechanism of | ||
| 73 | interprocess communication. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | =head1 INHERITS | ||
| 78 | |||
| 79 | This package inherits behaviors from: | ||
| 80 | |||
| 81 | L<Zing::PubSub> | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | =head1 LIBRARIES | ||
| 86 | |||
| 87 | This package uses type constraints from: | ||
| 88 | |||
| 89 | L<Zing::Types> | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | =head1 ATTRIBUTES | ||
| 94 | |||
| 95 | This package has the following attributes: | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | =head2 name | ||
| 100 | |||
| 101 | name(Str) | ||
| 102 | |||
| 103 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | =head1 METHODS | ||
| 108 | |||
| 109 | This package implements the following methods: | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | =head2 recv | ||
| 114 | |||
| 115 | recv() : Maybe[HashRef] | ||
| 116 | |||
| 117 | The recv method receives a single new message from the mailbox. | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item recv example #1 | ||
| 122 | |||
| 123 | # given: synopsis | ||
| 124 | |||
| 125 | $mailbox->recv; | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =over 4 | ||
| 130 | |||
| 131 | =item recv example #2 | ||
| 132 | |||
| 133 | # given: synopsis | ||
| 134 | |||
| 135 | $mailbox->send($mailbox->term, { status => 'hello' }); | ||
| 136 | |||
| 137 | $mailbox->recv; | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | =head2 reply | ||
| 144 | |||
| 145 | reply(HashRef $message, HashRef $value) : Int | ||
| 146 | |||
| 147 | The reply method sends a message to the mailbox represented by the C<$bag> | ||
| 148 | received and returns the size of the recipient mailbox. | ||
| 149 | |||
| 150 | =over 4 | ||
| 151 | |||
| 152 | =item reply example #1 | ||
| 153 | |||
| 154 | # given: synopsis | ||
| 155 | |||
| 156 | $mailbox->send($mailbox->term, { status => 'hello' }); | ||
| 157 | |||
| 158 | my $message = $mailbox->recv; | ||
| 159 | |||
| 160 | $mailbox->reply($message, { status => 'thank you' }); | ||
| 161 | |||
| 162 | =back | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | =head2 send | ||
| 167 | |||
| 168 | send(Str $key, HashRef $value) : Int | ||
| 169 | |||
| 170 | The send method sends a new message to the mailbox specified and returns the | ||
| 171 | size of the recipient mailbox. | ||
| 172 | |||
| 173 | =over 4 | ||
| 174 | |||
| 175 | =item send example #1 | ||
| 176 | |||
| 177 | # given: synopsis | ||
| 178 | |||
| 179 | $mailbox->send($mailbox->term, { status => 'hello' }); | ||
| 180 | |||
| 181 | =back | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | =head2 size | ||
| 186 | |||
| 187 | size() : Int | ||
| 188 | |||
| 189 | The size method returns the message count of the mailbox. | ||
| 190 | |||
| 191 | =over 4 | ||
| 192 | |||
| 193 | =item size example #1 | ||
| 194 | |||
| 195 | # given: synopsis | ||
| 196 | |||
| 197 | my $size = $mailbox->size; | ||
| 198 | |||
| 199 | =back | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | =head1 AUTHOR | ||
| 204 | |||
| 205 | Al Newkirk, C<awncorp@cpan.org> | ||
| 206 | |||
| 207 | =head1 LICENSE | ||
| 208 | |||
| 209 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 210 | |||
| 211 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 212 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 213 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 214 | |||
| 215 | =head1 PROJECT | ||
| 216 | |||
| 217 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 218 | |||
| 219 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 220 | |||
| 221 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 222 | |||
| 223 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 224 | |||
| 225 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 226 | |||
| 227 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 228 | |||
| 229 | =cut |