lib/RDF/Service/Dispatcher.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 243 CC 34 SCAR 64.1
CC
34
Cov
0%
go
870.0
create_jumptable
42.0
BEGIN
2.0
CRAP 607.0
1 # $Id: Dispatcher.pm,v 1.24 2000/12/21 20:06:19 aigan Exp $ -*-perl-*-
2  
3 package RDF::Service::Dispatcher;
4  
5 #=====================================================================
6 #
7 # DESCRIPTION
8 # Forwards Resource actions to the appropriate interface
9 #
10 # AUTHOR
11 # Jonas Liljegren <jonas@paranormal.se>
12 #
13 # COPYRIGHT
14 # Copyright (C) 2000 Jonas Liljegren. All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 #=====================================================================
20  
21 use strict;
22 use base 'Exporter';
23 use vars qw( %JumpJumpTable @EXPORT_OK %EXPORT_TAGS );
24 use RDF::Service::Constants qw( :all );
25 use RDF::Service::Cache qw( interfaces uri2id debug $DEBUG
26 debug_start debug_end ); #);
27 use Data::Dumper;
28 use Carp;
29  
30 # Every resource can only belong to one dispatcher. This should change
31 # in a future version.
32  
33 {
34 @EXPORT_OK = ( 'go' );
35 }
36  
37 sub go
38 {
39 my( $self, $call, @args ) = @_;
40  
41 my $node = $self->[NODE];
42  
43  
44  
45 if( $DEBUG )
46 {
47 unless( ref( $self ) eq 'RDF::Service::Context' )
48 {
49 confess "Called with invalid object: $self";
50 }
51 if( $node->[MODEL] )
52 {
53 ref $node->[MODEL] eq 'RDF::Service::Resource'
54 or confess "Bad model ($node->[MODEL])";
55 }
56 if( $node->[VALUE] )
57 {
58 unless( ref( $node->[VALUE]) eq 'SCALAR')
59 {
60 confess "Bad value for $node->[URISTR] ( ".
61 ref($node->[VALUE])." ne 'SCALAR' )";
62 }
63 }
64 unless( length( $node->[IDS] ) )
65 {
66 confess "No IDS" if $node->[RUNLEVEL];
67 }
68 unless( ref($self->[HISTORY]) eq 'ARRAY' )
69 {
70 confess "Malformed HISTORY: $self->[HISTORY]";
71 }
72 }
73  
74  
75 my $uri = $node->[URISTR] or
76 confess "Call to $call from anonymous obj";
77  
78 # Todo: optimize for common case
79 #
80 if( not defined $node->[JUMPTABLE] )
81 {
82 debug_start( "select_jumptable", ' ', $self );
83  
84 my $prefix_key = $node->[IDS].'/'.$node->find_prefix_id;
85  
86 unless( $node->[TYPE_ALL] )
87 {
88 # Create a temporary JUMPTABLE in order to call the
89 # init_types() function in the correct interfaces
90  
91 # Set the first type: Resource
92 # TODO: Determine the model for this type statement
93 # TODO: Make all things type resource implicitly!
94 #
95 my $c_resource = $self->get( NS_RDFS.'Resource' )
96 or die "Oh no!!!";
97 my $model_id = uri2id(NS_RDFS);
98 $node->[TYPE]{$c_resource->[NODE][ID]}{$model_id}=2;
99  
100 if(not defined $JumpJumpTable{$prefix_key})
101 {
102 # Create the jumptable
103 &create_jumptable($self, $prefix_key);
104 }
105 $node->[JUMPTABLE] = $JumpJumpTable{$prefix_key};
106  
107 # This will call go() the second time. This time with the
108 # temporary JUMPTABLE. Since JUMPTABLE is defined, this
109 # part will not be called a second time. The interfaces
110 # init_types functions will be called. After this, the
111 # real JUMPTABLE will be defined and the original function
112 # called.
113  
114  
115 &go($self, 'init_types');
116  
117 # There will allways be at least one type for a node
118 # die "No types found for $node->[URISTR]\n "
119 # unless defined $node->[TYPE][0];
120 }
121  
122 if( $DEBUG > 1 )
123 {
124 debug "D Types for $uri:\n";
125 # This lists all types not thinking about what their
126 # models are
127 foreach my $type_id ( keys %{$node->[TYPE]} )
128 {
129 my $type = $self->get_context_by_id( $type_id );
130 # TODO: Check that at least one node declared this
131 # type
132 debug "..$type->[NODE][ID] : $type->[NODE][URISTR]\n";
133 }
134 debug "\n";
135 }
136  
137 # Defines the TYPES list
138 #
139 my $key = $prefix_key.'/'.join('-', map $_->[NODE][ID],
140 @{$self->type_orderd_list});
141 debug "Jumptable for $uri is defined to $key\n", 2;
142  
143 if(not defined $JumpJumpTable{$key})
144 {
145 # Create the jumptable
146 &create_jumptable($self, $key);
147 }
148  
149 $node->[JUMPTABLE] = $JumpJumpTable{$key};
150 $node->[JTK] = $key;
151  
152 debug_end("select_jumptable", ' ', $self);
153 }
154  
155  
156  
157 ### Dispatch to the handling interfaces
158 ###
159  
160 if( defined(my $coderef = $node->[JUMPTABLE]{$call}) )
161 {
162 # TODO: If call not found: treat this as a property and maby
163 # as an dynamic property
164  
165  
166 my $call_key = "$call $self->[NODE] @args";
167 foreach( @{$self->[HISTORY]} )
168 {
169 if( $_ eq $call_key )
170 {
171 debug "Recursive call '$call_key' skipped", 2;
172 return 0;
173 }
174 }
175 # warn "---<<< called $call_key >>>---\n";
176 push @{$self->[HISTORY]}, $call_key;
177  
178  
179 debug "Dispatching $call...\n", 2;
180  
181 # Return a object or a list ref.
182 # Arg 1: the return value
183 # Arg 2: Action
184 # 0 = Ignore this result; call next
185 # 1 = Final; Return result
186 # 2 = Part; Append and call next
187 # 3 = Successful. Call next. Return 1
188  
189 my $success = 0;
190 my $result = [];
191 my $result_type = 0;
192  
193 for( my $i=0; $i<= $#$coderef; $i++ )
194 {
195 next if $result_type == 1;
196  
197 debug_start( $call, $i, $self );
198 debug "..Calling $coderef->[$i][1][URISTR]\n", 2;
199  
200 # The second parameter is the interface object
201 my( $res, $action ) = &{$coderef->[$i][0]}($self,
202 $coderef->[$i][1],
203 @args);
204  
205 if( not defined $action )
206 {
207 die "Malformed return value from $call ".
208 "in $coderef->[$i][1][URISTR]\n".
209 " ($res, $action)\n";
210 }
211  
212 $result_type ||= $action;
213 if( $action and $result_type != $action )
214 {
215 die "Mismatch in returned result types";
216 }
217  
218 if( $action == 0 )
219 {
220 # Do nothing
221 }
222 elsif( $action == 1 )
223 {
224 $result = $res;
225 }
226 elsif( $action == 2 )
227 {
228 push @$result, @$res;
229 }
230 elsif( $action == 3)
231 {
232 confess "Result undefined for call $call " unless defined $res;
233 $success += $res;
234 }
235 else
236 {
237 confess "Action ($action) not implemented";
238 }
239 debug_end( $call, $i, $self );
240 }
241  
242 # Falling back one step (not one LEVEL)
243 pop @{$self->[HISTORY]};
244  
245  
246 if( $result_type == 0 )
247 {
248 return 0;
249 }
250 elsif( $result_type == 1 )
251 {
252 return $result;
253 }
254 elsif( $result_type == 2 )
255 {
256 return $result;
257 }
258 elsif( $result_type == 3 )
259 {
260 return 1 if $success;
261 return 0;
262 }
263 else
264 {
265 die "Oh nooo!!!!\n";
266 }
267 }
268  
269  
270 my $types_str = $self->types_as_string;
271 $node->[JTK] ||= "--no JTK--";
272 die("\nNo function named '$call' defined for $uri ".
273 "($node->[JTK])\n$types_str\n");
274 }
275  
276 sub create_jumptable
277 {
278 my( $self, $key ) = @_;
279  
280 my $node = $self->[NODE];
281 my $entry = {};
282  
283 # TODO: Make filters part of signature. Especially model and
284 # language filters.
285  
286 # TODO: Make sure to create the jumptable so that Resource
287 # functions comes last!
288  
289 # Remember if the codref already has been added for the function
290 my %func_count;
291  
292 debug_start( "create_jumptable", ' ', $self );
293  
294 if( $DEBUG )
295 {
296 unless( $node->[IDS] )
297 {
298 die "No IDS found for $node->[URISTR]\n";
299 }
300 }
301  
302 # Iterate through every interface and type.
303 foreach my $interface ( @{interfaces( $node->[IDS] )} )
304 {
305 debug "..I ".$interface->[URISTR]."\n", 5;
306 foreach my $domain ( sort {length($b) <=> length($a)}
307 keys %{$interface->[MODULE_REG]} )
308 {
309 next if $node->[URISTR] !~ /^\Q$domain/;
310  
311 debug "....D $domain\n", 5;
312  
313 my $domain_reg = $interface->[MODULE_REG]{$domain};
314 foreach my $type ( @{$self->type_orderd_list} )
315 {
316 # warn "checking $type->[URISTR]...\n";
317 if( defined( my $jt = $domain_reg->{ $type->[NODE][URISTR]} ))
318 {
319 debug "......T $type->[NODE][URISTR]\n", 5;
320 foreach my $func ( keys %$jt )
321 {
322 debug "........F $func()\n", 5;
323 # Add The coderefs for this type
324 foreach my $coderef ( @{$jt->{$func}} )
325 {
326 next if defined $func_count{$func}{$coderef}{$interface};
327 push @{$entry->{$func}}, [$coderef,$interface];
328 $func_count{$func}{$coderef}{$interface}++;
329 }
330 }
331 }
332 }
333 }
334 }
335  
336 debug_end( "create_jumptable", ' ', $self );
337  
338 # Insert the jumptable in shared memory
339 $JumpJumpTable{$key}=$entry;
340 }
341  
342  
343  
344 1;
345