#
# Copyright (c) 2004-2007 - Consultas, PKG.fr
#
# This file is part of A2P.
#
# A2P is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# A2P is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with A2P; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# $Id: EOC.pm 3 2007-10-18 16:20:19Z g $
#
# Class to export End Object Container members
#
# cf ref #2, p. 164
#

package AFPDS::MODCA::EOC ;

use strict ;
use A2P::Globals ;
use A2P::Syslog ;
use AFPDS::MODCA::Common ;

BEGIN {
    our $VERSION = sprintf "%s", q$Rev: 1007 $ =~ /([0-9.]+)\s+/ ;
}
our $VERSION ;
our @ISA = ("AFPDS::MODCA");
our $IDENTS ;

sub _ID { 0xD3A992 }

sub _supported_triplets {
    return [], [
            # 0x02 # Only X'02' FQN triplet should be supported
        ];
}

sub new {
    my $class = shift ;
    &Debug("new " . __PACKAGE__ . " v$VERSION object");

    my $self = {
        FLUX     => 0,        # Associated flux
        IDENT    => _ID,      # MO:DCA identity
        FLAG     => 0,        # MO:DCA flags
        RESERVED => 0,        # MO:DCA reserved
        LONG     => 0,        # MO:DCA buffer length
        BUFFER   => ''        # MO:DCA content buffer
    };

    return bless $self , $class ;
}

sub validate {
    my $self = shift ;

    &Debug("Found EOC to close integrated resource");
    &UPSTAT('GET-COMPLETED-RESOURCE');

    # Check FLUX is a Flux object
    return ( 251, "EOC needs to be used with Flux object" )
        unless ( ref($self->{FLUX}) =~ /^AFPDS::Flux$/ );

    return () ;
}

sub create {
    my $self = shift ;
    my $resname = "" ;
    my @err = () ;

    if ( $self->{LONG} ) {
        # Set resource name
        $resname = substr( $self->{BUFFER}, 0, 8 );
        $resname = $self->convert( $resname );

        # Analyse authorized triplets if needed
        @err = $self->check_triplets(substr( $self->{BUFFER}, 8 ))
            if ( $self->{LONG} > 8 );

        return @err if @err ;
    }

    # Check container is opened in flux
    my $flux = $self->{FLUX} ;
    my $container = $flux->get_resource($resname);
    return ( 252, $resname ?
        "Resource '$resname' not available" : "No resource available to close" )
        unless ( $container and ref($container) =~ /^AFPDS::MODCA/ );

    # Validate the expected structure, see BOC
    my $struct = $container->{STRUCT} || 0 ;
    return ( 253, "Unexpected '$struct' structure found for BOC object" )
        unless ( $struct == 0xDC00 );

    # At that time, just initialize a resource using counter
    $container->{USED} = 0 ;

    # Unselect resource in flux
    $flux->set_resource(undef);

    return @err ;
}

&Debug("Module " . __PACKAGE__ . " v$VERSION loaded");

( $IDENTS->{&_ID} ) = __PACKAGE__ =~ /(\w+)$/ ;
