Messaging.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Messaging/Exception.pm ↑ Summary Messaging/Content.pm »
1 package Spread::Messaging;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 require Exporter;
8 0 ▶use Spread::Messaging::Content;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Spread::Messaging::Exception;
10  
11 our @ISA = qw(Exporter Spread::Messaging::Content);
12  
13 our $VERSION = '0.03';
14  
15 our @EXPORT = qw(
16 UNRELIABLE_MESS
17 RELIABLE_MESS
18 FIFO_MESS
19 CAUSAL_MESS
20 AGREED_MESS
21 SAFE_MESS
22 );
23  
24 # ------------------------------------------------------------------------
25 # public methods
26 # ------------------------------------------------------------------------
27  
28 sub new {
29 my $class = shift;
30 my $self = $class->SUPER::new(@_);
31  
32 $self->{entity} = "";
33 $self->{joined_groups} = undef;
34 $self->{callback_private} = undef;
35 $self->{callback_group} = undef;
36 $self->{callback_join} = undef;
37 $self->{callback_leave} = undef;
38 $self->{callback_disconnect} = undef;
39 $self->{callback_network} = undef;
40 $self->{callback_transition} = undef;
41 $self->{callback_other} = undef;
42  
43 return $self;
44  
45 }
46  
47 sub callbacks {
48 my $self = shift;
49 my %params = @_;
50  
51 my ($k, $v);
52 local $_;
53  
54 if (defined($v = delete $params{'-private'})) {
55  
56 $self->{callback_private} = $v;
57  
58 }
59  
60 if (defined($v = delete $params{'-group'})) {
61  
62 $self->{callback_group} = $v;
63  
64 }
65  
66 if (defined($v = delete $params{'-join'})) {
67  
68 $self->{callback_join} = $v;
69  
70 }
71  
72 if (defined($v = delete $params{'-leave'})) {
73  
74 $self->{callback_leave} = $v;
75  
76 }
77  
78 if (defined($v = delete $params{'-disconnect'})) {
79  
80 $self->{callback_disconnect} = $v;
81  
82 }
83  
84 if (defined($v = delete $params{'-network'})) {
85  
86 $self->{callback_network} = $v;
87  
88 }
89  
90 if (defined($v = delete $params{'-transition'})) {
91  
92 $self->{callback_transition} = $v;
93  
94 }
95  
96 if (defined($v = delete $params{'-other'})) {
97  
98 $self->{callback_other} = $v;
99  
100 }
101  
102 }
103  
104 sub process {
105 my ($self) = @_;
106  
107 $self->SUPER::recv();
108  
109 if ($self->is_regular_mess) {
110  
111 if ($self->is_private_mess) {
112  
113 $self->entity($self->sender);
114 return $self->{callback_private}->($self)
115 if defined $self->{callback_private};
116  
117 } else {
118  
119 $self->entity($self->sender);
120 return $self->{callback_group}->($self)
121 if defined $self->{callback_group};
122  
123 }
124  
125 } elsif ($self->is_membership_mess) {
126  
127 if ($self->is_reg_memb_mess) {
128  
129 if ($self->is_caused_by_join) {
130  
131 $self->entity(@{$self->message}[3]);
132 _do_groups($self);
133 return $self->{callback_join}->($self)
134 if defined $self->{callback_join};
135  
136 } elsif ($self->is_caused_by_leave) {
137  
138 $self->entity(@{$self->message}[3]);
139 _do_groups($self);
140 return $self->{callback_leave}->($self)
141 if defined $self->{callback_leave};
142  
143 } elsif ($self->is_caused_by_disconnect) {
144  
145 $self->entity(@{$self->message}[3]);
146 _do_groups($self);
147 return $self->{callback_disconnect}->($self)
148 if defined $self->{callback_disconnect};
149  
150 } elsif ($self->is_caused_by_network) {
151  
152 $self->entity("");
153 return $self->{callback_network}->($self)
154 if defined $self->{callback_network};
155  
156 }
157  
158 } elsif ($self->is_transition_mess) {
159  
160 $self->entity("");
161 return $self->{callback_transition}->($self)
162 if defined $self->{callback_transition};
163  
164 }
165  
166 } else {
167  
168 $self->entity("");
169 return $self->{callback_other}->($self)
170 if defined $self->{callback_other};
171  
172 }
173  
174 }
175  
176 # ------------------------------------------------------------------------
177 # public accessors
178 # ------------------------------------------------------------------------
179  
180 sub entity {
181 my ($self, $p) = @_;
182  
183 $self->{entity} = $p if defined $p;
184 return($self->{entity});
185  
186 }
187  
188 sub groups_joined {
189 my ($self) = @_;
190  
191 my @groups;
192  
193 foreach my $group (@{$self->{joined_groups}}) {
194  
195 push(@groups, $group->{name});
196  
197 }
198  
199 return @groups;
200  
201 }
202  
203 sub group_members {
204 my ($self, $name) = @_;
205  
206 my @groups;
207  
208 foreach my $group (@{$self->{joined_groups}}) {
209  
210 if ($group->{name} =~ /$name/i) {
211  
212 @groups = split(',', $group->{members});
213 return @groups;
214  
215 }
216  
217 }
218  
219 return undef;
220  
221 }
222  
223 # ------------------------------------------------------------------------
224 # private functions
225 # ------------------------------------------------------------------------
226  
227 sub _do_groups {
228 my ($self) = @_;
229  
230 my ($groups, $x);
231  
232 $x = 0;
233 $groups->{name} = $self->sender;
234 $groups->{members} = join(',', @{$self->group});
235  
236 foreach my $group (@{$self->{joined_groups}}) {
237  
238 if ($group->{name} eq $groups->{name}) {
239  
240 @{$self->{joined_groups}}[$x] = $groups;
241 return;
242  
243 }
244  
245 $x++;
246  
247 }
248  
249 push(@{$self->{joined_groups}}, $groups);
250  
251 }
252  
253 1;
254  
255 __END__
« Messaging/Exception.pm ↑ Summary Messaging/Content.pm »