#////////////////////////////////////////////////////////////////////////////// #// #// Callback3.pl #// Win32::Daemon Perl extension test script employing Callbacks #// #// Copyright (c) 1998-2008 Dave Roth #// Courtesy of Roth Consulting #// http://www.roth.net/ #// #// This file may be copied or modified only under the terms of either #// the Artistic License or the GNU General Public License, which may #// be found in the Perl 5.0 source kit. #// #// 2008.03.24 :Date #// 20080324 :Version #////////////////////////////////////////////////////////////////////////////// # Demonstration of an AutoStart Service using the Win32::Daemon # Perl extension. This service will auto launch applications # when it starts. Effectively enabling Windows to auto start # applications at boot time, before a user ever logs on. BEGIN { my ( $SCRIPT_DIR, $SCRIPT_FILE_NAME ) = ( Win32::GetFullPathName( $0 ) =~ /^(.*)\\([^\\]*)$/ ); push( @INC, $SCRIPT_DIR ); } use Win32::Daemon; use TestFramework; my %List; my $START_TIME = time(); my $gPaused = 0; # For DEBUG version ONLY!! if( Win32::Daemon::IsDebugBuild() ) { # Win32::Daemon::DebugOutputPath( "\\\\.\\pipe\\syslog" ); } my $gTestFramework = new TestFramework; $gTestFramework->LogStart(); Win32::Daemon::RegisterCallbacks( { start => \&Callback_Starting, stop => \&Callback_Stop, pause => \&Callback_Pause, continue => \&Callback_Continue, running => \&Callback_Running, timer => \&Callback_Timer, net_bind_disable => \&Callback_NetBindingDisable, net_bind_enable => \&Callback_NetBindingEnable, } ); Log( "Starting service" ); %Context = ( last_state => SERVICE_STOPPED, count => 0, start_time => time(), ); Win32::Daemon::StartService( \%Context, 2000 ); Log( "Shutting down the service." ); Log( "Start time: " . localtime( $Context{start_time} ) ); Log( "End time: " . localtime() ); Log( "Total running callback count: $Context{count}" ); $gTestFramework->LogClose(); # # Define the callback routines # sub Callback_Starting { my( $State, $Context ) = @_; Log( "Starting: '$State'\n" ); Log( " We are Starting!!!" ); return( SERVICE_RUNNING ); } sub Callback_Running { my( $State, $Context ) = @_; Log( "Running: '$State'\n" ); $Context->{count}++; Log( " Count=$Context->{count}\n" ); Log( " Callback timer: " . Win32::Daemon::CallbackTimer() ); return; } sub Callback_Timer { my( $State, $Context ) = @_; Log( "Timer: '$State'\n" ); $Context->{count}++; Log( " Count=$Context->{count}\n" ); Log( " Callback timer: " . Win32::Daemon::CallbackTimer() ); return; } sub Callback_Pause { my( $State, $Context ) = @_; Log( "Pause: '$State'\n" ); if( $gPaused ) { Log( "Already paused!" ); } else { Log( "Pausing." ); $gPaused = 1; Win32::Daemon::CallbackTimer( 0 ); $Context->{last_state} = SERVICE_PAUSED; } return( SERVICE_PAUSED ); } sub Callback_Continue { my( $State, $Context ) = @_; Log( "Continue: '$State'\n" ); if( $gPaused ) { Log( "Resuming from paused state." ); $gPaused = 0; Win32::Daemon::CallbackTimer( 2000 ); $Context->{last_state} = SERVICE_RUNNING; } else { Log( "Not already paused." ); } return( SERVICE_RUNNING ); } sub Callback_Stop { my( $State, $Context ) = @_; Log( "Stop: '$State'\n" ); $Context->{last_state} = SERVICE_STOPPED; # Win32::Daemon::State( [ state => SERVICE_STOPPED, error => 1234 ] ); Log( "Stopping service." ); # We need to notify the Daemon that we want to stop callbacks and the service. Win32::Daemon::StopService(); return( SERVICE_STOPPED ); } sub Callback_NetBindingDisable { my( $State, $Context ) = @_; Log( "Net Binding Disable: '$State'\n" ); return; } sub Callback_NetBindingEnable { my( $State, $Context ) = @_; Log( "Net Binding Enable: '$State'\n" ); return; } sub Log { my( $Message ) = @_; $gTestFramework->LogMessage( $Message ); }