#!/usr/bin/perl 
# xDash - Asynchronous Messaging and Instant Messaging reunited

#===
package EventLogger;
use base xDash::Logger::File;
# Check the correct file path for logger (absolute path if daemon!) and
# Uncomment 1.line and comment out 2.line below after debugging.
#sub Open { shift->SUPER::Open( '/home/Ex1S1R/r.rachel@xdash_PC/event.log' ) }
sub Open { shift->SUPER::Open( STDOUT ) }

package ErrorLogger;
use base xDash::Logger::File;
# Check the correct file path for logger (absolute path if daemon!) and
# Uncomment 1.line and comment out 2.line below after debugging.
#sub Open { shift->SUPER::Open( '/home/Ex1S1R/r.rachel@xdash_PC/error.log' ) }
sub Open { shift->SUPER::Open( STDERR ) }

package MessageLogger;
# Uncomment the 1.line and comment out 2.line & 3.line below after debugging.
#use base xDash::Logger::Dumb;
use base xDash::Logger::File;
sub Open { shift->SUPER::Open( STDOUT ) }
#===

package main;
use strict;
use warnings;
use xDash::Receiver;

# Establish first local communication to the application receiving jobs
# (die, if not possible) and then...

my $receiver = new xDash::Receiver;

# After debugging change:
# daemon => 1, for running as daemon (daemon => O, console usage)
# delay => 10, for waiting 10 seconds before becomming a daemon
# timeout => 100, for waiting 100 seconds to try to reconnect
# Test settings:
$receiver->SetMode( daemon => 0, delay => 0, timeout=> 100 );

# Parameters from receiver.xml and default connection parameters 
# from package Net::Jabber::Client (::Connect()) can be overriden here:
#	hostname => string, port => integer,
# 	connectiontype => tcpip|http, ssl => 0|1
# Uncomment if needed, method is optional.
# $receiver->SetConnectionParameters( ... => ... , );

# Set Subject to everything, what helps to track better jobs
# (below alias name from the archiv database).
$receiver->SetJobSubject( 'receiver1' );

# Initiate Receiver's and Archivist's JIDs (absolute path if daemon!)
$receiver->SetParticipants( '/home/Ex1S1R/r.rachel@xdash_PC/receiver.xml' );

# Set job callback function for incomming jobs
# (You habe to implement you own job handling - see some lines below...).
$receiver->SetJobCallback( \&job_execution );

# Go on ...
$receiver->Process();

#===========
#  CUSTOM: This should be implemented as the script doing integration
#===========
    
sub job_execution {
    
    # Unique ID of the transported data
    my $thread = shift;  
    
    # Data transported inside of the message from Sender
    my $job = shift; 
            
    # Use for critical part of the internal script: eval{...}; if($!){...} 
    eval { 
     print "\n THIS JOB EXECUTION SCRIPT NEEDS STILL TO BE IMPLEMENTED !!!\n\n"
    };
    if ($!) { print "Ups, some error...!\n"};
    
    # If everything OK make return without any parameters or
    # only optional response of your choice:
     
    return { response => '  ~{:-)  ' };
        
    # if the were some troubles:
    return { 

	    # Beware of jabber internal error codes: 400-409, 500-510
	    # carried also by the coresponding jabber message tag    
	    error_code => '1001',

	    # Your optional error description
	    error => 'hocus pocus',

	    # Your optional response
	    response => '  ~{:-(  '
	    }
}
