From d1da19c3bd37f36958fd155f81d42fdc770832e6 Mon Sep 17 00:00:00 2001 From: rudolfkoenig Date: Tue, 5 Feb 2013 08:52:30 +0000 Subject: [PATCH] Modified Patch from markus to create a telnet port if no suitable one is found git-svn-id: https://svn.fhem.de/fhem/trunk@2649 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/Blocking.pm | 55 ++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/fhem/FHEM/Blocking.pm b/fhem/FHEM/Blocking.pm index 981935498..150cf3d20 100644 --- a/fhem/FHEM/Blocking.pm +++ b/fhem/FHEM/Blocking.pm @@ -23,6 +23,43 @@ BlockingCall($$@) { my ($blockingFn, $arg, $finishFn, $timeout) = @_; + # Look for the telnetport + # must be done before forking to be able to create a temporary device + my $telnetDevice; + if($finishFn) { + my $tName = "telnetForBlockingFn"; + $telnetDevice = $tName if($defs{$tName}); + + if(!$telnetDevice) { + foreach my $d (sort keys %defs) { + my $h = $defs{$d}; + next if(!$h->{TYPE} || $h->{TYPE} ne "telnet" || $h->{TEMPORARY}); + next if($attr{$d}{SSL} || $attr{$d}{password}); + next if($h->{DEF} =~ m/IPV6/); + $telnetDevice = $d; + last; + } + } + + # If not suitable telnet device found, create a temporary one + if(!$telnetDevice) { + foreach my $port (7073..7083) { + if(!CommandDefine(undef, "$tName telnet $port")) { + $telnetDevice = $tName; + $defs{$tName}{TEMPORARY} = 1; + last; + } + } + } + + if(!$telnetDevice) { + my $msg = "CallBlockingFn: No telnet port found and cannot create one."; + Log 1, $msg; + return $msg; + } + } + + # do fork my $pid = fork; if(!defined($pid)) { Log 1, "Cannot fork: $!"; @@ -42,24 +79,8 @@ BlockingCall($$@) BlockingExit(undef) if(!$finishFn); - # Look for the telnetport - my $tp; - foreach my $d (sort keys %defs) { - my $h = $defs{$d}; - next if(!$h->{TYPE} || $h->{TYPE} ne "telnet" || $h->{TEMPORARY}); - next if($attr{$d}{SSL} || $attr{$d}{password}); - next if($h->{DEF} =~ m/IPV6/); - $tp = $d; - last; - } - - if(!$tp) { - Log 1, "CallBlockingFn: No telnet port found for sending the data back."; - BlockingExit(undef); - } - # Write the data back, calling the function - my $addr = "localhost:$defs{$tp}{PORT}"; + my $addr = "localhost:$defs{$telnetDevice}{PORT}"; my $client = IO::Socket::INET->new(PeerAddr => $addr); Log 1, "CallBlockingFn: Can't connect to $addr\n" if(!$client); $ret =~ s/'/\\'/g;