xstab


#!/usr/bin/perl
#
# XStab
#
# This program is licensed under the GPL
# Documentation at:
#   http://xstab.sourceforge.net
# Author: Dan Hart
# Email: dranok@users.sourceforge.net
##################################################


NAME

xstab


DESCRIPTION

This is the skeleton perl script which provides the glue to make the modules work together as one


INITIALIZATION

use strict;
# Game module
use XSTAB::RTCW::XSmokeHerb;

# Voice module
use XSTAB::Voice::XRcon;

# Sensor modules
use XSTAB::Sensor::XUdp;
use XSTAB::Sensor::XTail;

# Storage module
use XSTAB::Storage::XDbiMySQL;

# Debug module
use XSTAB::XDebug;

# Data module
use XSTAB::XData;

# Other
use IO::File;
use POSIX;

# This points to the XStab configuration file, can be full path
my $cfile = "xstab.conf";

# Declaration of variables
my $VERSION = "0.9";
my @sensors = ();

# Entry point
print "XStab $VERSION starting...\n";

# Load config file
load_config($cfile);

# Open log file
$Global{logfh} = new IO::File ">> $Global{Log_file}"
  or die "Could not open log file $Global{Log_file}!!";

# Create game object
# Global so Debug has access
our $game = XSTAB::RTCW::XCore->create;

# Create the voice object
my $voice = XSTAB::Voice::XRcon->create;

# Create and push sensors
push(@sensors, XSTAB::Sensor::XUdp->create);
push(@sensors, XSTAB::Sensor::XTail->create);

# Create the storage object
# Global so game module can use it
our $storage = XSTAB::Storage::XDbiMySQL->create;

# Create debug object
# Global so game module can use it
our $debug = XSTAB::XDebug->create;

#################################
### END OF USER CONFIGURATION ###
#################################


PRE-PROCESS

# Daemonize code
# First install the needed signal handler
$SIG{CHLD} = sub { while ( waitpid(-1,WNOHANG)>0 ) {} };

# Now become a daemon
die "Can't fork" unless defined (my $child = fork); # Fork so we can turn
                                                    # control over to child
exit 0 if $child; # Commit patricide
setsid(); # Me the leader!
open(STDIN, "</dev/null");
open(STDOUT, ">/dev/null"); # Close standard filehandles
open(STDERR, ">&STDOUT");
chdir '/';
umask(0);
$ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin';


PROCESS

# Start of the primary loop
my $cur_loops = 0;
my $avg_loops = 0;
my $main_loop_start = time;
while(1) {
  # Pre-loop processing hook
  $game->pre_loop_hook();
 
  # Process and Gather data from sensors
  foreach my $sensor (@sensors)
  {
    $game->process_data($sensor->gather_data($game->sensor_command($sensor->whoami())));
  }


  # Take any actions needed (says, etc);
  $voice->act();

  # Deal with admin queue
  $storage->do_admin();

  # Deal with any debug sessions
  $debug->do_one_loop($avg_loops);

  # Write debug log
  write_log($Global{MAX_WRITE});

  # Post-loop processing hoook
  $game->post_loop_hook();

  # Post-loop processing
  my $main_loop_end = time;
  if ($main_loop_end - $main_loop_start > 5) {
    my $new_avg_loops = $cur_loops / 5;
    $avg_loops = floor(($avg_loops + $new_avg_loops) / 2);
    $cur_loops = 0;
    $main_loop_start = $main_loop_end;
  } else {
    $cur_loops++;
  }
} 
# End of event loop

exit(0);


AUTHOR

This module was coded by DranoK and is part of the core XStab modules. You may directly contact DranoK at dranok@users.sourceforge.net, or by posting to the forums at:

        http://www.oltl.net/forums/forumdisplay.php?s=&forumid=25