2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-08 13:24:56 +00:00

ccurpcd: Multi RPC process support

git-svn-id: https://svn.fhem.de/fhem/trunk@10505 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
fhemzap 2016-01-14 19:33:51 +00:00
parent b38225a9e7
commit 68a86c24c1

View File

@ -3,7 +3,7 @@
######################################################### #########################################################
# ccurpcd.pl # ccurpcd.pl
# #
# Version 1.4 # Version 1.5
# #
# RPC server for Homematic CCU. # RPC server for Homematic CCU.
# #
@ -27,7 +27,6 @@ use warnings;
# use File::Queue; # use File::Queue;
use RPC::XML::Server; use RPC::XML::Server;
use RPC::XML::Client; use RPC::XML::Client;
# use XML::Simple qw(:strict);
use IO::Socket::INET; use IO::Socket::INET;
use FindBin qw($Bin); use FindBin qw($Bin);
use lib "$Bin"; use lib "$Bin";
@ -41,7 +40,7 @@ my $logfile;
my $shutdown = 0; my $shutdown = 0;
my $eventcount = 0; my $eventcount = 0;
sub CheckProcess ($); sub CheckProcess ($$);
sub Log ($); sub Log ($);
@ -49,18 +48,21 @@ sub Log ($);
# Get PID of running RPC server or 0 # Get PID of running RPC server or 0
##################################### #####################################
sub CheckProcess ($) sub CheckProcess ($$)
{ {
my ($prcname) = @_; my ($prcname, $port) = @_;
my $filename = $prcname;
my $pdump = `ps -ef | grep $prcname | grep -v grep`; my $pdump = `ps -ef | grep $prcname | grep -v grep`;
my @plist = split "\n", $pdump; my @plist = split "\n", $pdump;
foreach my $proc (@plist) { foreach my $proc (@plist) {
# Remove leading blanks, fix for MacOS. Thanks to mcdeck # Remove leading blanks, fix for MacOS. Thanks to mcdeck
$proc =~ s/^\s+//; $proc =~ s/^\s+//;
my @procattr = split /\s+/, $proc; my @procattr = split /\s+/, $proc;
if ($procattr[1] != $$ && $procattr[7] =~ /perl$/ && $procattr[8] eq $prcname) { if ($procattr[1] != $$ && $procattr[7] =~ /perl$/ &&
Log "Process $proc is running"; ($procattr[8] eq $prcname || $procattr[8] =~ /\/ccurpcd\.pl$/) &&
$procattr[10] eq "$port") {
Log "Process $proc is running connected to CCU port $port";
return $procattr[1]; return $procattr[1];
} }
} }
@ -131,22 +133,24 @@ sub CCURPC_Initialize ($$)
# Detect local IP # Detect local IP
my $socket = IO::Socket::INET->new (PeerAddr => $serveraddr, PeerPort => $serverport); my $socket = IO::Socket::INET->new (PeerAddr => $serveraddr, PeerPort => $serverport);
if (!$socket) { if (!$socket) {
Log "Can't connect to CCU"; Log "Can't connect to CCU port $serverport";
return undef; return undef;
} }
my $localaddr = $socket->sockhost (); my $localaddr = $socket->sockhost ();
close ($socket); close ($socket);
# Create RPC client
$client = RPC::XML::Client->new ("http://$serveraddr:$serverport/"); $client = RPC::XML::Client->new ("http://$serveraddr:$serverport/");
# Check if RPC daemon on CCU is running # Check if RPC daemon on CCU is running
my $resp = $client->send_request ('system.listMethods'); my $resp = $client->send_request ('system.listMethods');
if (!ref($resp)) { if (!ref($resp)) {
Log "No response from CCU. Error message follows in next line"; Log "No response from CCU on port $serverport. Error message follows in next line";
Log $resp; Log $resp;
return undef; return undef;
} }
# Create RPC server
$server = RPC::XML::Server->new (port=>$callbackport); $server = RPC::XML::Server->new (port=>$callbackport);
if (!ref($server)) if (!ref($server))
{ {
@ -154,7 +158,7 @@ sub CCURPC_Initialize ($$)
return undef; return undef;
} }
else { else {
Log "callback server created"; Log "callback server created listening on port $callbackport";
} }
# Callback for events # Callback for events
@ -201,15 +205,13 @@ sub CCURPC_Initialize ($$)
} }
); );
# $CCURPC_SERVERSOCKET = $server->{__daemon};
# $CCURPC_FD = $CCURPC_SERVERSOCKET->fileno();
my $ccurpcport = $server->{__daemon}->sockport(); my $ccurpcport = $server->{__daemon}->sockport();
# Register callback # Register callback
my $callbackurl = "http://".$localaddr.":".$ccurpcport."/fh"; my $callbackurl = "http://".$localaddr.":".$ccurpcport."/fh".$serverport;
Log "Registering callback"; Log "Registering callback $callbackurl with ID CB".$serverport;
$client->send_request ("init",$callbackurl,"CB1"); $client->send_request ("init",$callbackurl,"CB".$serverport);
Log "RPC callback with URL ".$callbackurl." initialized"; Log "RPC callback with URL ".$callbackurl." initialized";
return $callbackurl; return $callbackurl;
@ -310,10 +312,10 @@ my $ccuport = $ARGV[1];
my $queuefile = $ARGV[2]; my $queuefile = $ARGV[2];
$logfile = $ARGV[3]; $logfile = $ARGV[3];
my $pid = CheckProcess ($name); my $pid = CheckProcess ($name, $ccuport);
if ($pid > 0) { if ($pid > 0) {
Log "Error: ccurpcd.pl is already running (PID=$pid)"; Log "Error: ccurpcd.pl is already running (PID=$pid) for CCU port $ccuport";
die "Error: ccurpcd.pl is already running (PID=$pid)\n"; die "Error: ccurpcd.pl is already running (PID=$pid) for CCU port $ccuport\n";
} }
# Create or open queue # Create or open queue