--- SlimTrayorg.pl Wed Feb 28 08:41:06 2007 +++ SlimTray.pl Wed Feb 28 22:55:34 2007 @@ -23,7 +23,7 @@ use File::Spec; use Getopt::Long; use Socket; -use Symbol; + use Win32; use Win32::Daemon; use Win32::Process qw(DETACHED_PROCESS CREATE_NO_WINDOW NORMAL_PRIORITY_CLASS); @@ -36,6 +36,7 @@ my $starting = 0; my $processObj = 0; my $checkHTTP = 0; +my $lasthttpport= 0; # Passed on the command line by Getopt::Long my $cliStart = 0; @@ -44,18 +45,21 @@ my $registryKey = 'CUser/Software/SlimDevices/SlimServer'; my $serviceName = 'slimsvc'; +my $sqlserviceName = 'SlimServerMySQL'; my $appExe = File::Spec->catdir(baseDir(), 'server', 'slim.exe'); my $errString = 'SlimServer Failed. Please see the Event Viewer & Contact Support'; +my $stopMySqld = 0; + # Dynamically create the popup menu based on Slimserver state sub PopupMenu { my @menu = (); if ($ssActive) { - push @menu, ["*Open SlimServer", "Execute 'SlimServer Web Interface.url'"]; + push @menu, ["*Open SlimServer",\&openSlimServer]; push @menu, ["--------"]; - push @menu, ["Stop SlimServer", \&stopSlimServer]; + push @menu, ["Stop SlimServer", \&stopSlimServerMySQL]; } elsif ($starting) { push @menu, ["Starting SlimServer...", ""]; @@ -167,6 +171,36 @@ Execute("SlimServer Web Interface.url"); } +# +# Check if user has requested to stop SLimserver And MySqld +# Only try to stop MySql service when slimserver has stopped. +# + if (!$ssActive && $stopMySqld) { + + my %status = (); + Win32::Service::GetStatus('', $sqlserviceName, \%status); + + if (scalar keys %status != 0) { + if ($status{CurrentState} == 1) { # Service already stopped + $stopMySqld = 0; + return; + }; + + if (Win32::Service::StopService('', $sqlserviceName)) { + $stopMySqld = 0; + return; + } else { + my $t = "GetStatus Failed"; + Win32::Service::GetStatus('', $sqlserviceName, \%status); + if (scalar keys %status != 0) { + $t = "GetStatus CurrentState=" . $status{CurrentState}; + }; + showErrorMessage("Running StopService on MySql failed! $t" ); + } + } +# MySQL service is not running - perhaps slimserver is started at system level. + $stopMySqld = 0; + } } # The one-time startup timer, since there are things we can't do @@ -340,6 +374,25 @@ } } +# +# Called from menu when SS is active +# +sub openSlimServer { + +# Check HTTP first in case slimserver has changed the HTTP port while running + checkForHTTP (); + Execute("SlimServer Web Interface.url"); +} + +sub stopSlimServerMySQL { + + stopSlimServer(); + $stopMySqld = 1; +} + + + + sub showErrorMessage { my $message = shift; @@ -465,28 +518,23 @@ close(PREF); } } - + if ($lasthttpport ne $httpPort) { + updateSlimServerWebInterface ($httpPort); + $lasthttpport = $httpPort + } # Use low-level socket code. IO::Socket returns a 'Invalid Descriptor' # erorr. It also sucks more memory than it should. my $raddr = '127.0.0.1'; - my $rport = 9000; - - my $proto = (getprotobyname('tcp'))[2]; - my $pname = (getprotobynumber($proto))[0]; - my $sock = Symbol::gensym(); + my $rport = $httpPort; my $iaddr = inet_aton($raddr); my $paddr = sockaddr_in($rport, $iaddr); - socket($sock, PF_INET, SOCK_STREAM, $proto); - connect($sock, $paddr); - - if (defined $sock && fileno($sock)) { - - close($sock); + socket(SSERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')); + if (connect(SSERVER, $paddr)) { + close(SSERVER); return $httpPort; } - return 0; } @@ -541,11 +589,34 @@ sub processID { my $p = Win32::Process::List->new; - my $pid = ($p->GetProcessPid(qr/^slim\.exe$/))[0]; + if($p->IsError == 1) { + showErrorMessage("ProcessID: an error occured: " . $p->GetErrorText . " "); + } - return $pid; + my $pid = ($p->GetProcessPid(qr/^slim\.exe$/))[1]; + return $pid if ( defined ($pid) ) ; + return -1; } + +# update SlimServer Web Interface.url +# +# One parameter the new port number +# + +sub updateSlimServerWebInterface { + my $portno = shift; + my $urlfile = File::Spec->catfile(baseDir(), "SlimServer Web Interface.url"); + + if ( open(URLFILE, ">:crlf", $urlfile)) { + print URLFILE "[InternetShortcut]\nURL=http://127.0.0.1:$portno\n"; + close URLFILE; + } else { + showErrorMessage( "Can't open to write to $urlfile: $!"); + } +} + + *PerlTray::ToolTip = \&ToolTip; GetOptions(