#include "NamingVideoSupplier.h"

NamingVideoSupplier::NamingVideoSupplier (const char *name, CORBA::Boolean store)
  : VideoSupplier (name, store)
{
  // Note: the base class constructor will add this video supplier to the service provider's collection.
  VideoSupplier::pServiceProvider_->getObjectAdapter ()->addServantToAdapter (this, name, store);
}

CORBA::Boolean
NamingVideoSupplier::bind (const CosNaming::NamingContext_ptr inc)
{
  // Declare an arbitary name variable.
  CosNaming::Name name;
  name.length (1);

  try
    {
      CosNaming::NamingContext_var typeVar = inc->new_context ();
      name[0].id = CORBA::string_dup (getType ());
      CosNaming::NamingContext_var tmpVar = inc->bind_new_context (name);
    }
  catch (const CORBA::Exception &)
    {
      // OK continue.
    }

  StringArray *array;
  // Create contexts - ignoring those already existing.
  for (ASA_Iter itb = ASA_Iter (videos_); itb.next (array) != 0; itb.advance ())
    {
      name.length (2);
      try
        {
          CosNaming::NamingContext_var typeVar = inc->new_context ();
          name[1].id = CORBA::string_dup ((*array)[0]);
          CosNaming::NamingContext_var tmpVar = inc->bind_new_context (name);
        }
      catch (const CORBA::Exception &)
        {
          // OK continue.
        }
    }

  // Bind this company into context.
  name.length (3);

  for (ASA_Iter itb2 = ASA_Iter (videos_); itb2.next (array) != 0; itb2.advance ())
    {
      CORBA::String_var nameVar = getName ();
      name[1].id = CORBA::string_dup ((*array)[0]);
      name[2].id = CORBA::string_dup (nameVar.in ());
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%s: %s: %s\n"), getType (), (*array)[0], nameVar.in ()));

      CORBA::Object_var objVar = _this ();
      inc->rebind (name, objVar.in ());
    }

  return 1;
}
