blib/lib/Games/Axmud/Obj/Loop.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 162 CC 12 SCAR 27.1
CC
12
Cov
0%
startLoop
42.0
stopLoop
20.0
spinLoop
12.0
CRAP 15.0
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # General Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Obj::Loop
16 # Loop objects, handling the client loop (in GA::Client) and the session loop (in GA::Session).
17 # Implemented using a Glib::Timeout. Not suitable for use with other parts of the code (write your
18 # own Glib::Timeout functions!)
19  
20 { package Games::Axmud::Obj::Loop;
21  
22 use strict;
23 use warnings;
24 # use diagnostics;
25  
26 use Glib qw(TRUE FALSE);
27  
28 our @ISA = qw(Games::Axmud);
29  
30 ##################
31 # Constructors
32  
33 sub new {
34  
35 # Called by GA::Client->startClientLoop and GA::Session->startSessionLoop
36 # Creates the GA::Obj::Loop, handling a Glib::Timeout loop on behalf of a GA::Client or
37 # GA::Session
38 #
39 # Expected arguments
40 # $owner - The GA::Client or GA::Session on whose behalf the loop is handled
41 # $func - The function to call when the loop spins (i.e. $owner->$func() )
42 # $type - The loop type: (GA::Client) 'client', (GA::Session) 'session'
43 #
44 # Return values
45 # 'undef' on improper arguments
46 # Blessed reference to the newly-created object on success
47  
48 my ($class, $owner, $func, $type, $check) = @_;
49  
50 # Check for improper arguments
51 if (
52 ! defined $class || ! defined $owner || ! defined $func || ! defined $type
53 || defined $check
54 ) {
55 return $axmud::CLIENT->writeImproper($class . '->new', @_);
56 }
57  
58 # Setup
59 my $self = {
60 _objName => 'loop_' . $type,
61 _objClass => $class,
62 _parentFile => undef, # No parent file object
63 _parentWorld => undef, # No parent file object
64 _privFlag => TRUE, # All IVs are private
65  
66 # IVs
67 # ---
68  
69 # The GA::Client or GA::Session on whose behalf the loop is handled
70 owner => $owner,
71 # The function to call when the loop spins (i.e. $owner->$func() )
72 func => $func,
73 # The loop type: (GA::Client) 'client', (GA::Session) 'session'
74 type => $type,
75  
76 # The Glib::Timeout which spins the loop
77 id => undef,
78 # The loop delay, in seconds (set by $self->startLoop); absolute minimum value of 0.01
79 delay => undef,
80 # The time at which the loop first spun (system time, in seconds)
81 startTime => undef,
82 # The time at which the loop first spun, or which it spun after a restart (having been
83 # suspended due to a Perl error), whichever is later
84 restartTime => undef,
85 # The time at which the loop halted (system time, in seconds)
86 stopTime => undef,
87 # The time at which the loop last spun (seconds after ->startTime)
88 spinTime => undef,
89 };
90  
91 # Bless the object into existence
92 bless $self, $class;
93  
94 return $self;
95 }
96  
97 ##################
98 # Methods
99  
100 sub startLoop {
101  
102 # Called by GA::Client->startClientLoop and GA::Session->startSessionLoop
103 # Sets up the loop by creating a new Glib::Timeout
104 #
105 # Expected arguments
106 # $delay - The loop delay, in seconds. If less than 0.01 is specified, then 0.01 is
107 # used instead
108 #
109 # Return values
110 # 'undef' on improper arguments or if the loop can't be started
111 # 1 on success
112  
113 my ($self, $delay, $check) = @_;
114  
115 # Local variables
116 my $id;
117  
118 # Check for improper arguments
119 if (! defined $delay || defined $check) {
120  
121 return $axmud::CLIENT->writeImproper($self->_objClass . '->startLoop', @_);
122 }
123  
124 # Set absolute minimum delay
125 if ($delay < 0.01) {
126  
127 $delay = 0.01;
128 }
129  
130 # If the loop had already been started (and was suspended after a Perl error), remove the
131 # previous Glib::Timeout
132 if ($self->id) {
133  
134 # If the old Glib::Timeout had not expired, we'll get a 'GLib-Critical' error; trap it
135 # using an 'eval' statement
136 eval { Glib::Source->remove($self->id); };
137 }
138  
139 # Start the loop. The Glib::Timeout calls $self->func every $self->delay seconds. In the
140 # call to Glib::Timeout->add, we must convert the delay to milliseconds
141 $id = Glib::Timeout->add(($delay * 1000), sub{ $self->spinLoop() });
142 if (! $id) {
143  
144 # The task loop can't be started
145 return undef;
146  
147 } else {
148  
149 # Loop started. If the loop is being re-started after a suspension, we should
150 # continue using the previous loop start time (for the benefit of any code which is
151 # executing its own timeout, for example)
152 if (! defined $self->startTime) {
153  
154 # Loop started for first time
155 $self->ivPoke('startTime', $axmud::CLIENT->getTime());
156 $self->ivPoke('restartTime', $self->startTime);
157  
158 } else {
159  
160 # Loop re-started after a suspension
161 $self->ivPoke('restartTime', $axmud::CLIENT->getTime());
162 }
163  
164 # Update remaining IVs
165 $self->ivPoke('id', $id);
166 $self->ivPoke('delay', $delay);
167 $self->ivPoke('spinTime', 0);
168  
169 return 1;
170 }
171 }
172  
173 sub stopLoop {
174  
175 # Called by GA::Client->stopClientLoop and GA::Session->stopSessionLoop
176 # Stops the loop by terminating the Glib::Timeout
177 #
178 # Expected arguments
179 # (none besides $self)
180 #
181 # Return values
182 # 'undef' on improper arguments, if the loop isn't running or if it can't be stopped
183 # 1 on success
184  
185 my ($self, $check) = @_;
186  
187 # Check for improper arguments
188 if (defined $check) {
189  
190 return $axmud::CLIENT->writeImproper($self->_objClass . '->stopLoop', @_);
191 }
192  
193 if (! $self->id) {
194  
195 # The loop isn't running
196 return undef;
197 }
198  
199 # Stop the loop
200 if (! Glib::Source->remove($self->id)) {
201  
202 # Loop wasn't stopped
203 return undef;
204  
205 } else {
206  
207 # Loop stopped. Update IVs
208 $self->ivUndef('id');
209 $self->ivUndef('delay');
210  
211 $self->ivPoke('stopTime', $axmud::CLIENT->getTime());
212 $self->ivPoke(
213 'spinTime',
214 Math::Round::nearest(0.001, ($self->stopTime() - $self->startTime)),
215 );
216  
217 return 1;
218 }
219 }
220  
221 sub spinLoop {
222  
223 # Callback, called by a Glib::Timeout created by $self->startLoop whenever the loop spins
224 # Updates IVs and calls the function specified by $self->owner and $self->func
225 #
226 # Expected arguments
227 # (none besides $self)
228 #
229 # Return values
230 # 'undef' on improper arguments
231 # 1 otherwise
232  
233 my ($self, $check) = @_;
234  
235 # Local variables
236 my ($owner, $func);
237  
238 # Check for improper arguments
239 if (defined $check) {
240  
241 return $axmud::CLIENT->writeImproper($self->_objClass . '->spin', @_);
242 }
243  
244 # Import IVs
245 $owner = $self->owner;
246 $func = $self->func;
247  
248 # Do nothing if the owner is currently processing another spin (either from this loop, or
249 # from any other loop with the same owner)
250 if (
251 ($self->type eq 'client' && $owner->clientLoopSpinFlag)
252 || ($self->type eq 'session' && $owner->sessionLoopSpinFlag)
253 ) {
254 # Do nothing
255 return 1;
256  
257 } else {
258  
259 # Forbid other loop spins with the same owner until this one is finished)
260 $owner->set_loopSpinFlag(TRUE);
261 }
262  
263 # Update IVs. The subtraction produces a system rounding error, so we need to round the
264 # value to 3dp
265 $self->ivPoke(
266 'spinTime',
267 Math::Round::nearest(0.001, ($axmud::CLIENT->getTime() - $self->startTime)),
268 );
269  
270 # Call the loop's owner
271 $owner->$func($self);
272  
273 # Spin complete
274 $owner->set_loopSpinFlag(FALSE);
275  
276 return 1;
277 }
278  
279 ##################
280 # Accessors - set
281  
282 ##################
283 # Accessors - get
284  
285 sub owner
286 { $_[0]->{owner} }
287 sub func
288 { $_[0]->{func} }
289 sub type
290 { $_[0]->{type} }
291  
292 sub id
293 { $_[0]->{id} }
294 sub delay
295 { $_[0]->{delay} }
296 sub startTime
297 { $_[0]->{startTime} }
298 sub restartTime
299 { $_[0]->{restartTime} }
300 sub stopTime
301 { $_[0]->{stopTime} }
302 sub spinTime
303 { $_[0]->{spinTime} }
304 }
305  
306 # Package must return a true value
307 1