PSYC/State.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 124 CC 21 SCAR 32.2
CC
21
Cov
0%
outstate
56.0
msg
30.0
sendmsg
20.0
CRAP 24.9
1 package Net::PSYC::State;
2  
3 use strict;
4 our $VERSION = '0.1';
5  
6 use strict;
7  
8 # module implementing psyc-state-maintainance for objects...
9 #
10 # the state of mmp-vars is maintained by the connection
11 # object!
12 #
13 # right know I am not quite happy to have 2 objects for state. the wrapper and
14  
15 sub new {
16 my $class = shift;
17 my $unl = shift;
18 my $self = {
19 # 'co' => {},
20 'o' => {},
21 # this is temporary out-state. just to keep keep in mind variables which
22 # have been set manually by the api. these variables are really assigned
23 # when a tcp packet is send.
24 'to' => {},
25 # ci is the in
26 'ci' => {},
27 'i' => {},
28 };
29 return bless $self, $class;
30 }
31  
32 # sendmsg ( target, mc, data, vars[, source || mmp-vars ] )
33 sub sendmsg {
34 my $self = shift;
35 my $s = $self->{'o'};
36 my $vars = $_[3];
37 if (exists $s->{$_[0]}) {
38 foreach (keys %{$s->{$_[0]}}) {
39 if (!exists $vars->{$_}) {
40 $vars->{$_} = '';
41 } elsif ($vars->{$_} eq $s->{$_}) {
42 delete $vars->{$_};
43 }
44 }
45 }
46 }
47  
48 # msg ( source, mc, data, vars )
49 sub msg {
50 my $self = shift;
51 my ($source, $vars) = ($_[0], $_[3]);
52 my $s;
53  
54 if (exists $vars->{'_context'}) {
55 return unless (exists $self->{'ci'}->{$vars->{'_context'}});
56 $s = $self->{'ci'}->{$vars->{'_context'}};
57 } else {
58 return unless (exists $self->{'i'}->{$source});
59 $s = $self->{'i'}->{$source};
60 }
61  
62 foreach (keys %$s) {
63 $vars->{$_} = $s->{$_} unless (exists $vars->{$_});
64 }
65 }
66  
67 # ( source, key, value )
68 sub diminish {
69 my $self = shift;
70 my ($source, $key, $value, $iscontext) = @_;
71 my $s = ($iscontext) ? $self->{'ci'} : $self->{'i'};
72  
73 return unless (exists $s->{$source});
74 Net::PSYC::_diminish($s->{$source}, $key, $value);
75 }
76  
77 sub augment {
78 my $self = shift;
79 my ($source, $key, $value, $iscontext) = @_;
80 my $s = ($iscontext) ? $self->{'ci'} : $self->{'i'};
81  
82 $s->{$source} = {} unless (exists $s->{$source});
83 Net::PSYC::_augment($s->{$source}, $key, $value);
84 }
85  
86 sub assign {
87 my $self = shift;
88 my ($source, $key, $value, $iscontext) = @_;
89 my $s = ($iscontext) ? $self->{'ci'} : $self->{'i'};
90  
91 $s->{$source} = {} unless (exists $s->{$source});
92 $s->{$source}->{$key} = $value;
93 }
94  
95 # new API ( we will see how good it works )
96 sub outstate {
97 my $self = shift;
98 my ($mod, $key, $value, $t, $iscontext) = @_;
99  
100 $self->{'o'}->{$t} = {} unless (exists $self->{'o'}->{$t});
101 my $o = $self->{'o'}->{$t};
102
103 if ($mod eq ':') {
104 return 0 if (exists $o->{$key} && $o->{$key} eq $value);
105 return 1;
106 }
107  
108 # to implement automated state here we would need to
109 # return 0 if the var is not to be send and fill everything else
110 # into $to. TODO
111
112 if ($mod eq '=') {
113 $o->{$key} = $value;
114 } elsif ($mod eq '+') {
115 Net::PSYC::_augment($o, $key, $value);
116 } elsif ($mod eq '-') {
117 Net::PSYC::_diminish($o, $key, $value);
118 }
119 return 1; # 1 means: render it..
120 }
121  
122 # i dont like that name. returns vars currently set.
123 #
124 sub state {
125 my $self = shift;
126 my ($target, $iscontext) = @_;
127  
128 return (exists $self->{'to'}->{$target} ? delete $self->{'to'}->{$target}
129 : {});
130 }
131  
132 1;