3 # This file is part of Stem.
4 # Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
6 # Stem is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # Stem is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Stem; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # For a license to use the Stem under conditions other than those
21 # described here, to purchase support for this software, or to purchase a
22 # commercial warranty contract, please contact Stem Systems at:
24 # Stem Systems, Inc. 781-643-7504
25 # 79 Everett St. info@stemsystems.com
29 package Stem::Switch ;
31 use Stem::Trace 'log' => 'stem_status', 'sub' => 'TraceStatus' ;
32 use Stem::Trace 'log' => 'stem_error' , 'sub' => 'TraceError' ;
38 Stem::Switch has several functions:
49 my $this_package = __PACKAGE__ ;
58 This is a unique name used to register this instance of a Switch.
67 This field contains the incoming address map.
68 Any message coming in to one of these addresses will be resent out
69 to every address in out_map.
78 This contains the outgoing addresses for this Switch.
85 new creates a new Stem::Switch object, parsing $attr_spec and any arguments
88 It returns the new object.
94 my( $class ) = shift ;
96 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
97 return $self unless ref $self ;
100 # to be replaced with Stem::Class supporting 'hash' attribute types
103 if ( ref $self->{'in_map'} eq 'ARRAY' ) {
105 $self->{'in_map'} = { @{$self->{'in_map'}} } ;
108 if ( ref $self->{'out_map'} eq 'ARRAY' ) {
110 $self->{'out_map'} = { @{$self->{'out_map'}} } ;
121 my( $self, $msg ) = @_ ;
123 my $in_target = $msg->to_target() ;
125 my $in_map = $self->{'in_map'}{$in_target} ;
127 return unless $in_map ;
129 my @out_keys = ref $in_map ? @{$in_map} : ($in_map) ;
131 # loop over all the output keys for this in_map entry
133 foreach my $out_key ( @out_keys ) {
135 my $out_addr = $self->{'out_map'}{$out_key} ;
137 next unless $out_addr ;
139 my @out_addrs = ref $out_addr ? @{$out_addr} : ($out_addr) ;
141 # loop over all the output address for this out_map entry
143 foreach my $out_addr ( @out_addrs ) {
145 # now we clone the message with the new address
147 my $switched_msg = $msg->clone(
152 $switched_msg->dispatch() ;
160 my( $self, $msg ) = @_ ;
162 my @tokens = split( ' ', ${$msg->data()} ) ;
164 my $target = shift @tokens ;
166 $self->{'in_map'}{$target} = \@tokens ;
173 my( $self, $msg ) = @_ ;
175 my @tokens = split( ' ', ${$msg->data()} ) ;
177 my $key = shift @tokens ;
179 $self->{'out_map'}{$key} = \@tokens ;
187 my( $self, $msg ) = @_ ;
195 This cell is a message multiplex or switch. Any message addressed to a
196 target in the cell, can be resent to any subset of the output map
206 my( $self, $msg ) = @_ ;
210 $status_text = <<TEXT ;
212 Status of switch: $self->{'reg_name'}
218 foreach my $target ( sort keys %{$self->{'in_map'}} ) {
220 my $targets_ref = $self->{'in_map'}{$target} ;
221 my @targets = ref $targets_ref ?
222 @{$targets_ref} : ($targets_ref) ;
224 $status_text .= "\t$target -> @targets\n" ;
227 $status_text .= "\nOut Map:\n\n" ;
229 my $out_ref = $self->{'out_map'} ;
231 foreach my $key ( sort keys %{$out_ref} ) {
233 my $out_addr = $out_ref->{$key} ;
235 my @out_addrs = ref $out_addr ? @{$out_addr} : ($out_addr) ;
237 $status_text .= "\t$key -> @out_addrs\n" ;
240 return $status_text ;