2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 18:59:33 +00:00

PRESENCE/collectord: allow spaces for room names in config file (Forum: #19169), code make-up

git-svn-id: https://svn.fhem.de/fhem/trunk@8558 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
markusbloch 2015-05-10 14:13:06 +00:00
parent 76db86933e
commit e7ff7770f0

View File

@ -65,18 +65,11 @@ my %queues;
my $thread_counter = 0;
my %state;
my %handle;
my %socket_to_handle;
my $uuid;
Getopt::Long::Configure('bundling');
GetOptions(
"d" => \$opt_d, "daemon" => \$opt_d,
@ -85,19 +78,12 @@ GetOptions(
"c=s" => \$opt_c, "configfile=s" => \$opt_c,
"p=i" => \$opt_p, "port=i" => \$opt_p,
"P=s" => \$opt_P, "pid-file=s" => \$opt_P,
"h" => \$opt_h, "help" => \$opt_h);
"h" => \$opt_h, "help" => \$opt_h
);
Log 0, "=================================================" if($opt_l);
sub print_usage () {
print "Usage:\n";
print " collectord -c <configfile> [-d] [-p <port>] [-P <pidfile>] \n";
@ -117,11 +103,8 @@ sub print_usage () {
print " log to the given logfile\n";
print " -h, --help\n";
print " Print detailed help screen\n";
}
if($opt_h)
{
print_usage();
@ -135,7 +118,6 @@ if(-e "$opt_P")
exit 1;
}
if(not $opt_c)
{
print STDERR "no config file provided\n\n";
@ -153,7 +135,6 @@ if(not -e "$opt_c" or not -r "$opt_c")
}
Log 0, "started with PID $$";
readConfig($opt_c);
@ -163,13 +144,11 @@ if($opt_d)
daemonize();
}
# Write PID file
open(PIDFILE, ">$opt_P");
print PIDFILE $$."\n";
close PIDFILE;
$server = new IO::Socket::INET (
LocalPort => $opt_p,
Proto => 'tcp',
@ -472,7 +451,6 @@ while(1)
# now close the socket, that's it
close $client;
}
}
}
@ -487,9 +465,6 @@ while(1)
Log 1, "server shutdown";
exit;
}
}
Log 2, "leaving main loop";
@ -504,7 +479,6 @@ Log 2, "leaving main loop";
# to fork the process from the terminal
sub daemonize
{
POSIX::setsid or die "setsid $!";
my $pid = fork();
@ -529,16 +503,12 @@ sub daemonize
open (STDIN, "</dev/null");
open (STDOUT, ">/dev/null");
open (STDERR, ">&STDOUT");
}
# the thread subroutine which performs a request for a specific room
sub doQuery($$$)
{
my ($do_config, $do_room, $do_address, $do_uuid) = @_;
my $return;
my $socket;
@ -548,8 +518,6 @@ sub doQuery($$$)
my @client_handle;
my $reconnect_count = 0;
my $client_socket = undef;
# if the thread gets a termination signal, the thread must be shutdown by itself
my $last_contact = gettimeofday();
@ -570,7 +538,6 @@ sub doQuery($$$)
if(defined($client_socket))
{
# send the given address to the presence daemon
$client_socket->send($do_address."|".$values{absence_timeout}."\n");
}
@ -578,10 +545,8 @@ sub doQuery($$$)
{
$selector->remove($client_socket);
$client_socket = undef;
}
# thread main loop
THREADLOOP: while($run)
{
@ -737,10 +702,7 @@ sub doQuery($$$)
# set the previous state to the current state
($previous_state, undef) = split(";", lc($return));
}
}
else # the socket is EOF which means the connection was closed
{
@ -750,14 +712,9 @@ sub doQuery($$$)
shutdown($local_client, 2);
close($local_client);
$client_socket = undef;
}
}
}
}
}
$log_queue->enqueue(threads->tid()."|exiting thread");
@ -776,7 +733,6 @@ my $value;
my $errorcount = 0;
Log 1, "reading configuration file";
%config = ();
@ -784,11 +740,11 @@ my $errorcount = 0;
open (INI, "$ini") or (print STDERR timestamp()."Can't open $ini: $!\n" and exit(1));
while (<INI>) {
chomp;
if (/^\s*?\[(\w+?)\]/) {
if (/^\s*?\[([^\]\n\r]+?)\]/) {
$section = $1;
}
if (/^\s*(\w+?)=(.+?)\s*(#.*)?$/) {
if (/^\s*(\w+?)=(.+?)\s*(#.*)?$/ and defined($section)) {
$keyword = $1;
$value = $2 ;
# put them into hash
@ -817,8 +773,6 @@ my $errorcount = 0;
}
if(not exists($config{$room}{port}))
{
Log 0, "room >>$room<< has no value for >>port<< configured";
@ -885,7 +839,6 @@ my $errorcount = 0;
sub aggregateRooms
{
my ($hash) = @_;
@ -896,12 +849,13 @@ my @rooms;
my $key;
my $value;
my $temp_name;
foreach $key (keys %$hash)
{
if($hash->{$key} ne "")
{
($value, $name) = split(";", $hash->{$key});
if($value eq "present")
{
push @rooms, $key;
@ -914,18 +868,14 @@ foreach $key (keys %$hash)
}
}
if(@rooms > 0)
{
return "present;$temp_name;".join(",",sort @rooms);
}
else
{
return "absence";
}
}
@ -941,10 +891,8 @@ sub generateUUID
return $uuid;
}
sub timestamp
{
return POSIX::strftime("%Y-%m-%d %H:%M:%S",localtime);
}
@ -958,6 +906,7 @@ sub Log($$)
{
($thread, $message) = split("\\|", $message);
}
if($loglevel <= $opt_v)
{
if($opt_l)
@ -973,6 +922,5 @@ sub Log($$)
close(LOGFILE);
}
}