//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / ILMI protocol
//
//File: ilmiarstate_address.cpp
//
//Version: $Revision: 1.4 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/07/09 13:09:07 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Pärnänen
//
//Description:
//      See corresponding header file.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History:

#include "pf/debug.h"
#include "ilmiarstate_address.h"
#include "ilmiarstate_idle.h"
#include "ilmiarstate_null.h"
#include "mib/mibexception.h"

// Implementation of the singleton pattern

ilmiARState_Address *ilmiARState_Address::_only = 0;

ilmiARState_Address *ilmiARState_Address :: instance(void)
{
    if (_only == 0)
    {
        _only = new ilmiARState_Address;
    }
    return _only;
}

ilmiARState_Address :: ilmiARState_Address(void)
{
    return;
}

ilmiARState_Address :: ~ilmiARState_Address(void)
{
    _only = 0;
    return;
}

void ilmiARState_Address :: setValue(ilmiAddressRegistration *protocol_,
                                     const string &name_,
                                     const string &value_)
{
    debugUser("ilmiARState_Address::setValue");

    if (value_.compare(VALID) == 0)
    {
        protocol_->addAddress(name_);
        try
        {
            protocol_->sendListAddresses();
            protocol_->startPollTimer();
            protocol_->changeState(ilmiARState_Idle::instance());
        }
        catch (pfException &exception)
        {
            debugUser("listAddresses failed");
            // ++TODO++
            // Error handling
            //  * re-register to nameservice ?
            //  * colstart ?
            //  * changeState ?
            exit(0);
        }
    }
    else if (value_.compare(INVALID) == 0)
    {
        protocol_->removeAddress(name_);
    }
    else
    {
        debugUser("ilmiARState_Address::setValue / BadValue");
        throw mibException(PDUInt::badValue);
    }
    return;
}
