| 1 | package BoardStreams::Registry; | ||
| 2 | |||
| 3 | use Mojo::Base -base, -signatures; | ||
| 4 | |||
| 5 | use BoardStreams::Util 'belongs_to'; | ||
| 6 | use BoardStreams::REs; | ||
| 7 | |||
| 8 | use List::AllUtils 'pairs'; | ||
| 9 | use Hash::Util 'fieldhash'; | ||
| 10 | use Carp 'croak'; | ||
| 11 | |||
| 12 | no autovivification; | ||
| 13 | |||
| 14 | our $VERSION = "v0.0.36"; | ||
| 15 | |||
| 16 | has _streams_to_conns => sub { +{} }; # ends in connection object | ||
| 17 | has _conns_to_streams => sub { +{} }; # ends in number of joins of that connection | ||
| 18 | has _conns => sub { +{} }; # $int_c -> $c | ||
| 19 | has conn_subscriptions => sub { fieldhash my %foo }; # stuff to unsubscribe from on leave/finish | ||
| 20 | has pending_joins => sub { fieldhash my %foo }; # $c -> behavior subject ($num) | ||
| 21 | |||
| 22 | has _actions_requests => sub { +{} }; # actions and requests | ||
| 23 | |||
| 24 | sub register_conn ($self, $c) { | ||
| 25 | $self->_conns->{int $c} = $c; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub unregister_conn ($self, $c) { | ||
| 29 | delete $self->_conns->{int $c}; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub is_conn_registered ($self, $c) { | ||
| 33 | return exists $self->_conns->{int $c}; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub add_membership ($self, $c, $stream_name) { | ||
| 37 | my $int_c = int $c; | ||
| 38 | $self->_streams_to_conns->{$stream_name}{$int_c} //= $c; | ||
| 39 | $self->_conns_to_streams->{$int_c}{$stream_name}++; | ||
| 40 | |||
| 41 | return $self->_conns_to_streams->{$int_c}{$stream_name} == 1; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub remove_membership ($self, $c, $stream_name) { | ||
| 45 | my $int_c = int $c; | ||
| 46 | my $count_ref = \($self->_conns_to_streams->{$int_c}{$stream_name}); | ||
| 47 | |||
| 48 | die "not a member of '$stream_name'\n" | ||
| 49 | unless defined $$count_ref and $$count_ref > 0; | ||
| 50 | |||
| 51 | if (--$$count_ref == 0) { | ||
| 52 | delete $self->_streams_to_conns->{$stream_name}{$int_c}; | ||
| 53 | delete $self->_streams_to_conns->{$stream_name} if ! $self->_streams_to_conns->{$stream_name}->%*; | ||
| 54 | delete $self->_conns_to_streams->{$int_c}{$stream_name}; | ||
| 55 | delete $self->_conns_to_streams->{$int_c} if ! $self->_conns_to_streams->{$int_c}->%*; | ||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub is_member_of ($self, $c, $stream_name) { | ||
| 63 | return exists $self->_streams_to_conns->{$stream_name}{int $c}; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub get_conns_of_stream ($self, $stream_name) { | ||
| 67 | return [ | ||
| 68 | values $self->_streams_to_conns->{$stream_name}->%* | ||
| 69 | ]; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub get_streams_and_counts_of_conn ($self, $c) { | ||
| 73 | return $self->_conns_to_streams->{int $c}; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub get_all_conns ($self) { | ||
| 77 | return [ | ||
| 78 | values $self->_conns->%* | ||
| 79 | ]; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub inc_pending_joins_by ($self, $c, $n) { | ||
| 83 | my $pj_o = $self->pending_joins->{$c}; | ||
| 84 | my $pj_value = $pj_o->get_value; | ||
| 85 | $pj_o->next($pj_value + $n); | ||
| 86 | } | ||
| 87 | |||
| 88 | ### ACTIONS AND REQUESTS | ||
| 89 | |||
| 90 | sub set_action_request ($self, $type, $stream_def, $thing_name, $sub) { | ||
| 91 | # validate | ||
| 92 | belongs_to($type, [qw/ action request join_leave /]) | ||
| 93 | or die "invalid type '$type'"; | ||
| 94 | |||
| 95 | # pre-process | ||
| 96 | $stream_def = [$stream_def] if ref $stream_def ne 'ARRAY'; | ||
| 97 | @$stream_def = map { | ||
| 98 | my $thing = $_; | ||
| 99 | if (! length ref $thing) { | ||
| 100 | $thing =~ $BoardStreams::REs::ANY_STREAM_NAME or croak 'invalid stream definition'; | ||
| 101 | split /\:/, $thing; | ||
| 102 | } else { | ||
| 103 | $thing; | ||
| 104 | } | ||
| 105 | } @$stream_def; | ||
| 106 | |||
| 107 | my $start = $self->_actions_requests; | ||
| 108 | my $cursor_ref = \$start; | ||
| 109 | SEGMENT: | ||
| 110 | foreach my $segment (@$stream_def) { | ||
| 111 | if (! length ref $segment) { | ||
| 112 | $cursor_ref = \($$cursor_ref->{strings}{$segment} //= {}); | ||
| 113 | } else { | ||
| 114 | foreach my $pair (pairs $$cursor_ref->{regexes}->@*) { | ||
| 115 | my ($preexisting_regex, $hashref) = @$pair; | ||
| 116 | if ("$segment" eq "$preexisting_regex") { | ||
| 117 | $cursor_ref = \$hashref; | ||
| 118 | next SEGMENT; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | push $$cursor_ref->{regexes}->@*, ($segment => {}); | ||
| 122 | $cursor_ref = \$$cursor_ref->{regexes}[-1]; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | $$cursor_ref->{$type}{$thing_name} = $sub; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub get_action_request ($self, $type, $stream_name, $thing_name) { | ||
| 129 | # validate | ||
| 130 | belongs_to($type, [qw/ action request join_leave /]) | ||
| 131 | or die "invalid type '$type'"; | ||
| 132 | |||
| 133 | my @segments = split /\:/, $stream_name; | ||
| 134 | my @cursors = $self->_actions_requests; | ||
| 135 | foreach my $segment (@segments) { | ||
| 136 | my @new_cursors = grep defined, map $_->{strings}{$segment}, @cursors; | ||
| 137 | foreach my $cursor (@cursors) { | ||
| 138 | if ($cursor->{regexes}) { | ||
| 139 | foreach my $pair (pairs $cursor->{regexes}->@*) { | ||
| 140 | my ($regex, $new_candidate_cursor) = @$pair; | ||
| 141 | if ($segment =~ $regex) { | ||
| 142 | push @new_cursors, $new_candidate_cursor; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
| 147 | @cursors = @new_cursors; | ||
| 148 | } | ||
| 149 | return (grep defined, map $_->{$type}{$thing_name}, @cursors)[0]; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub get_action ($self, $stream_name, $action_name) { | ||
| 153 | return $self->get_action_request(action => $stream_name, $action_name); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub get_request ($self, $stream_name, $request_name) { | ||
| 157 | return $self->get_action_request(request => $stream_name, $request_name); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub get_join ($self, $stream_name) { | ||
| 161 | return $self->get_action_request(join_leave => $stream_name, 'join'); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub get_leave ($self, $stream_name) { | ||
| 165 | return $self->get_action_request(join_leave => $stream_name, 'leave'); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub get_repair ($self, $stream_name) { | ||
| 169 | return $self->get_action_request(join_leave => $stream_name, 'repair'); | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; |