| 1 | package Zing::Channel; | ||
| 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 | |||
| 13 | extends 'Zing::PubSub'; | ||
| 14 | |||
| 15 | our $VERSION = '0.26'; # VERSION | ||
| 16 | |||
| 17 | # BUILDERS | ||
| 18 | |||
| 19 | fun BUILD($self) { | ||
| 20 | $self->{position} = $self->size if !$self->isa('Zing::Table'); | ||
| 21 | |||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | # METHODS | ||
| 26 | |||
| 27 | method recv() { | ||
| 28 | $self->{position}++ if ( | ||
| 29 | my $data = $self->store->slot($self->term, int($self->{position})) | ||
| 30 | ); | ||
| 31 | return $data; | ||
| 32 | } | ||
| 33 | |||
| 34 | method renew() { | ||
| 35 | return $self->reset if $self->{position} > $self->size; | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | |||
| 39 | method reset() { | ||
| 40 | return !($self->{position} = 0); | ||
| 41 | } | ||
| 42 | |||
| 43 | method send(HashRef $value) { | ||
| 44 | return $self->store->rpush($self->term, $value); | ||
| 45 | } | ||
| 46 | |||
| 47 | method size() { | ||
| 48 | return $self->store->size($self->term); | ||
| 49 | } | ||
| 50 | |||
| 51 | method term() { | ||
| 52 | return $self->app->term($self)->channel; | ||
| 53 | } | ||
| 54 | |||
| 55 | 1; | ||
| 56 | |||
| 57 | =encoding utf8 | ||
| 58 | |||
| 59 | =head1 NAME | ||
| 60 | |||
| 61 | Zing::Channel - Shared Communication | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | =head1 ABSTRACT | ||
| 66 | |||
| 67 | Multi-process Communication Mechanism | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | =head1 SYNOPSIS | ||
| 72 | |||
| 73 | use Zing::Channel; | ||
| 74 | |||
| 75 | my $chan = Zing::Channel->new(name => 'share'); | ||
| 76 | |||
| 77 | # $chan->recv; | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | =head1 DESCRIPTION | ||
| 82 | |||
| 83 | This package represents a mechanism of interprocess communication and | ||
| 84 | synchronization via message passing. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | =head1 INHERITS | ||
| 89 | |||
| 90 | This package inherits behaviors from: | ||
| 91 | |||
| 92 | L<Zing::PubSub> | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | =head1 LIBRARIES | ||
| 97 | |||
| 98 | This package uses type constraints from: | ||
| 99 | |||
| 100 | L<Zing::Types> | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | =head1 ATTRIBUTES | ||
| 105 | |||
| 106 | This package has the following attributes: | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | =head2 name | ||
| 111 | |||
| 112 | name(Str) | ||
| 113 | |||
| 114 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | =head1 METHODS | ||
| 119 | |||
| 120 | This package implements the following methods: | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | =head2 recv | ||
| 125 | |||
| 126 | recv() : Maybe[HashRef] | ||
| 127 | |||
| 128 | The recv method receives a single new message from the channel. | ||
| 129 | |||
| 130 | =over 4 | ||
| 131 | |||
| 132 | =item recv example #1 | ||
| 133 | |||
| 134 | my $chan = Zing::Channel->new(name => 'recv-01'); | ||
| 135 | |||
| 136 | $chan->recv; | ||
| 137 | |||
| 138 | =back | ||
| 139 | |||
| 140 | =over 4 | ||
| 141 | |||
| 142 | =item recv example #2 | ||
| 143 | |||
| 144 | my $chan = Zing::Channel->new(name => 'recv-02'); | ||
| 145 | |||
| 146 | $chan->send({ status => 'works' }); | ||
| 147 | |||
| 148 | $chan->recv; | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | =head2 renew | ||
| 155 | |||
| 156 | renew() : Int | ||
| 157 | |||
| 158 | The renew method returns truthy if it resets the internal cursor, otherwise | ||
| 159 | falsy. | ||
| 160 | |||
| 161 | =over 4 | ||
| 162 | |||
| 163 | =item renew example #1 | ||
| 164 | |||
| 165 | my $chan = Zing::Channel->new(name => 'renew-01'); | ||
| 166 | |||
| 167 | $chan->send({ status => 'works' }) for 1..5; | ||
| 168 | |||
| 169 | $chan->renew; | ||
| 170 | |||
| 171 | =back | ||
| 172 | |||
| 173 | =over 4 | ||
| 174 | |||
| 175 | =item renew example #2 | ||
| 176 | |||
| 177 | my $chan = Zing::Channel->new(name => 'renew-02'); | ||
| 178 | |||
| 179 | $chan->send({ status => 'works' }) for 1..5; | ||
| 180 | $chan->recv; | ||
| 181 | $chan->drop; | ||
| 182 | |||
| 183 | $chan->renew; | ||
| 184 | |||
| 185 | =back | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | =head2 reset | ||
| 190 | |||
| 191 | reset() : Int | ||
| 192 | |||
| 193 | The reset method always reset the internal cursor and return truthy. | ||
| 194 | |||
| 195 | =over 4 | ||
| 196 | |||
| 197 | =item reset example #1 | ||
| 198 | |||
| 199 | my $chan = Zing::Channel->new(name => 'reset-01'); | ||
| 200 | |||
| 201 | $chan->send({ status => 'works' }) for 1..5; | ||
| 202 | $chan->recv; | ||
| 203 | $chan->recv; | ||
| 204 | |||
| 205 | $chan->reset; | ||
| 206 | |||
| 207 | =back | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | =head2 send | ||
| 212 | |||
| 213 | send(HashRef $value) : Int | ||
| 214 | |||
| 215 | The send method sends a new message to the channel and return the message | ||
| 216 | count. | ||
| 217 | |||
| 218 | =over 4 | ||
| 219 | |||
| 220 | =item send example #1 | ||
| 221 | |||
| 222 | my $chan = Zing::Channel->new(name => 'send-01'); | ||
| 223 | |||
| 224 | $chan->send({ status => 'works' }); | ||
| 225 | |||
| 226 | =back | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | =head2 size | ||
| 231 | |||
| 232 | size() : Int | ||
| 233 | |||
| 234 | The size method returns the message count of the channel. | ||
| 235 | |||
| 236 | =over 4 | ||
| 237 | |||
| 238 | =item size example #1 | ||
| 239 | |||
| 240 | my $chan = Zing::Channel->new(name => 'size-01'); | ||
| 241 | |||
| 242 | $chan->send({ status => 'works' }) for 1..5; | ||
| 243 | |||
| 244 | $chan->size; | ||
| 245 | |||
| 246 | =back | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | =head2 term | ||
| 251 | |||
| 252 | term() : Str | ||
| 253 | |||
| 254 | The term method returns the name of the channel. | ||
| 255 | |||
| 256 | =over 4 | ||
| 257 | |||
| 258 | =item term example #1 | ||
| 259 | |||
| 260 | my $chan = Zing::Channel->new(name => 'term-01'); | ||
| 261 | |||
| 262 | $chan->term; | ||
| 263 | |||
| 264 | =back | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | =head1 AUTHOR | ||
| 269 | |||
| 270 | Al Newkirk, C<awncorp@cpan.org> | ||
| 271 | |||
| 272 | =head1 LICENSE | ||
| 273 | |||
| 274 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 275 | |||
| 276 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 277 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 278 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 279 | |||
| 280 | =head1 PROJECT | ||
| 281 | |||
| 282 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 283 | |||
| 284 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 285 | |||
| 286 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 287 | |||
| 288 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 289 | |||
| 290 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 291 | |||
| 292 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 293 | |||
| 294 | =cut |