//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / SW
//
//File: swconnectid.cpp
//
//Version: $Revision: 1.7 $
//
//State: $State: Exp $
//
//Date: $Date: 1999/03/11 10:30:59 $
//
//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 <autoptr.h>
#include "pf/transp.h"
#include "pf/debug.h"
#include "iface/swif/swfabriccallback.h"
#include "iface/swif/swcallbackmessages.h"
#include "ie/connectionidentifier.h"
#include "swconnectid.h"

swConnectId :: swConnectId(pfUlong inputId_,
                           pfUlong outputId_,
                           ieConnectionIdentifier *input_,
                           ieConnectionIdentifier *output_,
                           bool bidirection_,
                           pfConduit &callback_)
    : _inputId(inputId_),
      _outputId(outputId_),
      _input(input_),
      _output(output_),
      _proxyCallback(callback_),
      _pointerCallback(0),
      _success(!bidirection_)
{
    return;
}

swConnectId :: swConnectId(pfUlong inputId_,
                           pfUlong outputId_,
                           bool bidirection_,
                           swFabricCallback *callback_)
    : _inputId(inputId_),
      _outputId(outputId_),
      _input(0),
      _output(0),
      _proxyCallback(),
      _pointerCallback(callback_),
      _success(!bidirection_)
{
    return;
}

swConnectId :: swConnectId(pfUlong identifier_)
    : _inputId(identifier_),
      _outputId(identifier_),
      _input(0),
      _output(0),
      _proxyCallback(),
      _pointerCallback(0),
      _success(false)
{
    return;
}

swConnectId :: ~swConnectId(void)
{
    _pointerCallback = 0;
    return;
}

swConnectId &swConnectId :: operator=(const swConnectId &other_)
{
    if (&other_ != this)
    {
        _inputId = other_._inputId;
        _outputId = other_._outputId;
        _input = other_._input;
        _output = other_._output;
        _proxyCallback = other_._proxyCallback;
        _pointerCallback = other_._pointerCallback;
        _success = other_._success;        
    }
    return *this; 
}

bool swConnectId :: operator==(const swConnectId &other_) const
{
    bool returnValue = 
        (_inputId == other_._inputId) || (_outputId == other_._outputId);
    return returnValue;
}

bool swConnectId :: operator!=(const swConnectId &other_) const
{
    bool returnValue = !(*this == other_);
    return returnValue;
}

bool swConnectId :: operator<(const swConnectId &other_) const
{
    bool returnValue = ((_inputId <= other_._inputId) &&
                        (_outputId <= other_._outputId) &&
                        (*this != other_));
    return returnValue;
}

bool swConnectId :: processSuccess(void)
{
    bool result = _success;
    if (_success == true)
    {
        if (_pointerCallback != 0)
        {
            _pointerCallback->success(_inputId);
        }
        else
        {
            swSuccessMessage *messenger = 
                new swSuccessMessage(_input, _output);
            pfMsgTransporter *message = 
                pfMsgTransporter::createMsgTransporter(messenger);
            _proxyCallback.accept(message);
        }
    }
    else
    {
        _success = true;
    }
    return result;
}

void swConnectId :: processFailure(pfUlong cause_)
{
    if (_pointerCallback != 0)
    {
        _pointerCallback->failure(_inputId, cause_);
    }
    else
    {
        //++TODO++ use auto_ptr
        ieCause *ie = new ieCause(cause_);
        //ie->setCauseValue(cause_);
    
        swFailureMessage *messenger = new swFailureMessage(ie);
        pfMsgTransporter *message = 
            pfMsgTransporter::createMsgTransporter(messenger);
        _proxyCallback.accept(message);
    }
    return;
}
