1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
#!/usr/bin/perl
#
# $Id: StateMachine.pm,v 1.3 2001/10/06 22:19:14 levine Exp $
#
# Copyright (C) 2001 James D. Levine (jdl@vinecorp.com)
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
####################################################################
####################################################################
#
# Implements a rather simple state machine. Each state is a perl
# object. The input "symbols" are NWatch::Packet types. The
# equivalence function for matching input to transitions is a perl
# expression that is evaluated as each packet is received. Each
# state also has a perl expression which executes when the state
# is entered.
#
# The state machine exposes arbitrary storage to the expressions
# using the storage hash, addressable within expressions
# via %s:FIELD_NAME%. The state machine performs a substitution
# at the appropriate time.
#
# Fields of the input packet are addressed via %p:FIELD_NAME%. The
# specific field names are available in the class definitions
# that implement the different packet types.
#
# Eventually the state machine will also expose certain parts of
# the runtime environment such as user preferences, i/o handles,
# etc. to allow the perl expressions to be more functional.
#
#
#
#
####################################################################
use strict;
use NWatch::Packet;
package NWatch::sm_transition;
sub new
{
my( $class, $expr, $state ) = @_;
my $self =
{
expr => $expr,
state => $state,
};
bless $self, $class;
$self;
}
sub set_or_get
{
my $field = shift;
my $self = shift;
my $val = shift;
$self->{$field} = $val if defined $val;
return $self->{$field};
}
sub expr {set_or_get('expr', @_);}
sub state {set_or_get('state', @_);}
####################################################################
#
# sm_state
#
# each state is a 3-tuple ( name, entry_expression, arcs )
# entry_expression = a perl expression evaluated when the state is entered
#
####################################################################
package NWatch::sm_state;
sub set_or_get
{
my $field = shift;
my $self = shift;
my $val = shift;
$self->{$field} = $val if defined $val;
return $self->{$field};
}
sub name {set_or_get('name', @_);}
sub entry_expr {set_or_get('entry_expr', @_);}
sub transitions {set_or_get('transitions', @_);}
sub new
{
my( $class, $name, $entry_expr, $transitions ) = @_;
my $self =
{
name => $name, # a text label, for initial machine assembly and logging
entry_expr => $entry_expr, # some expression to eval when the state is entered
transitions => undef, # a listref of sm_transition instances
};
bless $self, $class;
$self;
}
sub entry_expr_subst
{
my( $self, $packet ) = @_;
return $self->subst( $self->entry_expr, $packet );
}
####################################################################
#
# state_machine
#
#
# states is a list of sm_state instances;
# states[0] is the start state
#
####################################################################
# add persistent state storage hash
# formalize access to nwatch environment ( i/o channels, user options, port/host observations )
package NWatch::state_machine;
sub new
{
my( $class, $states, $handler ) = @_;
my $self =
{
states => $states, # compiled definition of state machine
current => $states->[0], # start state is first defined in list
handler => $handler, # the PacketHandler which owns this machine
storage => {}, # arbitrary storage hash for state machine expressions
};
bless $self, $class;
$self->{'time'} = time;
$self;
}
sub current {set_or_get('current', @_);}
sub handler {set_or_get('handler', @_);}
# return age in seconds
sub age
{
time - $_[0]->{'time'};
}
sub set_or_get
{
my $field = shift;
my $self = shift;
my $val = shift;
$self->{$field} = $val if defined $val;
return $self->{$field};
}
#
# each state specified as [ name, entry-expr [ edges: [eval-expr, transition-to-name)], .. ]
#
#
#
sub compile
{
my( $states ) = shift;
# keep both a hash and a list of the states
my $states_list = [];
my $states_hash = {};
# gather up all states, store in an ordered list and a hash
# mapping the state name to the sm_state instance
foreach my $state ( @$states )
{
my( $name, $entry_expr, $transitions ) = @$state;
$entry_expr = full_subst( $entry_expr );
# print "compile: name: $name entry_expr: $entry_expr transitions: $transitions \n"
# if( NWatch::state_machine::verbose );
my $s = new NWatch::sm_state( $name, $entry_expr, 0 ); # temporary
push @$states_list, $s;
$states_hash->{$name} = $s;
}
# now pass over each state, resolving the transitions
# match each next_state name to the actual sm_state instance
# mapped in states_hash
foreach my $state ( @$states )
{
my( $name, $entry_expr, $transitions ) = @$state;
$entry_expr = full_subst( $entry_expr );
my $transition_list = [];
foreach my $transition ( @$transitions )
{
my( $expr, $next_state ) = @$transition;
$expr = '( ' . full_subst( $expr ) . ' )';
# print "compile: transition: $expr -> $next_state \n"
# if( NWatch::state_machine::verbose );
push @$transition_list,
new NWatch::sm_transition( $expr,
$states_hash->{$next_state} );
}
$states_hash->{$name}->transitions( $transition_list );
}
$states_list;
}
#
# Instead of a stack or a tape, the state machine has a hash
# for storage in which the state machine can store and reference
# arbitrary key/value pairs
#
# %s:FIELD% - substitute with the value of FIELD from storage
# %s:FIELD%=expr; - assign a value to FIELD
#
sub storage
{
my $self = shift;
return $self->{storage};
}
sub process_packet
{
my( $self, $packet ) = @_;
my $foo = $self->{current}->input( $packet, $self );
$self->{current} = $foo;
my $e = $foo->entry_expr_subst( $packet );
# my $e = $self->field_expr_subst( $self->storage );
eval $e;
}
#
# full_subst
#
# a static method which replaces instances of %p:FIELD% and %s:FIELD%
#
# %p:FIELD% maps to $packet->get( "FIELD" )
# %s:FIELD% maps to $self->storage{ "FIELD" }
#
#
sub full_subst
{
my( $string, $packet ) = @_;
$string =~ s/\%p:([\:\w]*)\%/\$p->field_path( "$1" )/g;
$string =~ s/\%s:(\w*)\%/\$self->storage()->{"$1"}/g;
$string;
}
sub input
{
my( $self, $p ) = @_;
# print "StateMachine::input entry \n";
my $transitions = $self->current->transitions; # get current state's transitions
my $i = 0; # index into list of transitions
while( $i <= $#$transitions )
{
my $t = $transitions->[$i];
if( eval $t->expr ) # state matches
{
$self->current( $t->state );
#print "StateMachine::input $p evaluating " . $t->state->entry_expr . "\n";
eval $t->state->entry_expr;
return $t->state;
}
$i++;
}
# print( "NWatch::state_machine::input: no matching state found\n" )
# if( NWatch::state_machine::verbose );
# fall through - let state machine designer insert a default transition
# if they want to
}
#
# Simply a packet type which signifies end of transmission.
#
package NWatch::EOT_packet;
@NWatch::EOT_packet::ISA = qw( NWatch::packet );
sub protocol_name { "EOT"; }
sub new
{
my( $type, $data ) = @_;
# init from base class
my $self = NWatch::packet::new( $type, $data );
$self->set( "eot", 1 );
$self;
}
1;
|