清单 7. 读取敲打文件
sub readKnockFile {
open(KNCKFILE,"$ENV{HOME}/.knockFile") or die "no knock file: $!";
while(<KNCKFILE>){
if( !/^#/ ){
my @arrLine = split "_#_";
$knockHash{ $arrLine[0] }{ cmd } = $arrLine[1];
$knockHash{ $arrLine[0] }{ comment } = $arrLine[2];
}#if not a comment line
}#for each line in file
close(KNCKFILE);
}#readKnockFile
|
当 knockListen
获得敲打模式时,它会将该模式与从 readKnockFile
中读取的敲打模式进行比较。下面的 compareKnockSequences
子程序会对敲打之间的时间进行简单的区别检查。注意,敲打之间的差别并不是简单混合在一起的:很多次敲打时的少量时间差别并不会累积成总体的匹配失效。
第一个要比较的是敲打的次数,因为我们没有必要将一个七次敲打的序列与一个两次敲打的序列进行比较。如果敲打的次数与 ~/.knockFile 中现有的敲打序列匹配,每次敲打之间的差别也少于最大敲打偏差,那么这次敲打就可以认为是完全匹配的。在允许敲打序列进行不精确匹配时,最大敲打偏差非常关键。我们可以增大最大敲打偏差来使敲打节奏更加自由,但是要注意,这可能会导致敲打模式匹配不正确。例如,我们可以在所期望的时间之前或之后半秒钟允许自己的敲打模式发生偏离,但这仍然可以匹配。这样就可以有效地说明 “刮脸和理发” 可以与 “Mary 姓 Little Lamb” 匹配,因此在修改这个参数时一定要小心。
如果完整的模式可以匹配,就会运行 ~/.knockFile 中指定的命令,如果启用了冗余模式,则会打印结果。下一个步骤是如果没有找到匹配项,就退出这个子程序;如果找到了匹配项,就重置所记录的敲打序列。这个步骤会执行 compareKnockSequences
子程序:
清单 8. 比较敲打序列
sub compareKnockSequences {
my $countMatch = 0; # record how many knocks matched
# for each knock sequence in the config file
for( keys %knockHash ){
# get the timings between knocks
my @confKnocks = split;
# if the count of knocks match
if( $knockCount eq @confKnocks ){
my $knockDiff = 0;
my $counter = 0;
for( $counter=0; $counter<$knockCount; $counter++ ){
$knockDiff = abs($confKnocks[$counter] - $baseKnocks[$counter]);
my $knkStr = "k $counter b $baseKnocks[$counter] ".
"c $confKnocks[$counter] d $knockDiff\n";
# if it's an exact match, increment the matching counter
if( $knockDiff < $MAX_KNOCK_DEV ){
if( $option ){ print "MATCH $knkStr" }
$countMatch++;
# if the knocks don't match, move on to the next pattern in the list
}else{
if( $option ){ print "DISSONANCE $knkStr" }
last;
}# deviation check
}#for each knock
}#if number of knocks matches
# if the count of knocks is an exact match, run the command
if( $countMatch eq @confKnocks ){
my $cmd = system( $knockHash{"@confKnocks "}{ cmd } );
if( $option ){ print "$cmd\n" }
last;
# otherwise, make the count of matches zero, in order to not reset
}else{
$countMatch = 0;
}
}#for keys
# if the match count is zero, exit and don't reset variables so a longer
# knock sequence can be entered and checked
if( $countMatch == 0 ){ return() }
# if a match occurred, reset the variables so it won't match another pattern
$knockCount = 0;
@baseKnocks = ();
}#compareKnockSequences
|
本新闻共
6页,当前在第
5页
1 2 3 4 5 6