// ---------------------------------------------------------------------------
// - Address.cpp                                                             -
// - afnix:net module - address class implementation                         -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  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.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2006 amaury darsch                                   -
// ---------------------------------------------------------------------------

#include "Vector.hpp"
#include "Address.hpp"
#include "Integer.hpp"
#include "QuarkZone.hpp"
#include "Exception.hpp"
#include "cnet.hpp"

namespace afnix {

  // -------------------------------------------------------------------------
  // - class section                                                         -
  // -------------------------------------------------------------------------

  // create an empty address

  Address::Address (void) {
    p_addr = nilp;
    d_size = 0;
    p_aals = nilp;
  }

  // create a new address by name

  Address::Address (const String& name) {
    // get the ip address info
    char* host = name.tochar ();
    s_ipaddr* ipaddr = c_getipa (host);
    delete [] host;
    // check for good address
    if ((ipaddr == nilp) || (ipaddr->d_size == 0))
      throw Exception ("address-error", "cannot resolve address", name);
    // save canonical info
    d_name = name;
    d_cnam = ipaddr->p_name[0];
    p_addr = c_cpaddr (ipaddr->p_addr[0]);
    // save aliases
    d_size = ipaddr->d_size;
    p_aals = new s_alias[d_size];
    for (long i = 0; i < d_size; i++) {
      p_aals[i].d_anam = ipaddr->p_name[i];
      p_aals[i].p_aadr = c_cpaddr (ipaddr->p_addr[i]);
    }
    delete ipaddr;
  }

  // create a new address by bytes

  Address::Address (const t_byte* addr) {
    // get the ip address info
    char* host = c_iprepr (addr);
    d_name =  host;
    s_ipaddr* ipaddr = c_getipa (host);
    delete [] host;
    // check for good address
    if ((ipaddr == nilp) || (ipaddr->d_size == 0))
      throw Exception ("address-error", "cannot resolve address", d_name);
    // save canonical info
    d_cnam = ipaddr->p_name[0];
    p_addr = c_cpaddr (ipaddr->p_addr[0]);
    // save aliases
    d_size = ipaddr->d_size;
    p_aals = new s_alias[d_size];
    for (long i = 0; i < d_size; i++) {
      p_aals[i].d_anam = ipaddr->p_name[i];
      p_aals[i].p_aadr = c_cpaddr (ipaddr->p_addr[i]);
    }
    delete ipaddr;
  }

  // copy construct an address

  Address::Address (const Address& that) {
    that.rdlock ();
    // save canonical info
    d_name = that.d_name;
    d_cnam = that.d_cnam;
    p_addr = c_cpaddr (that.p_addr);
    // save aliases
    d_size = that.d_size;
    p_aals = new s_alias[d_size];
    for (long i = 0; i < d_size; i++) {
      p_aals[i].d_anam = that.p_aals[i].d_anam;
      p_aals[i].p_aadr = c_cpaddr (that.p_aals[i].p_aadr);
    }
    that.unlock ();
  }

  // destroy this address instance

  Address::~Address (void) {
    delete [] p_addr;
    delete [] p_aals;
  }

  // return the class name

  String Address::repr (void) const {
    return "Address";
  }

  // assign an address to this one

  Address& Address::operator = (const Address& that) {
    // check for equality
    if (this == &that) return *this;
    // clean the old address first
    wrlock ();
    delete [] p_addr;
    // lock and copy
    that.rdlock ();
    // save canonical info
    d_name = that.d_name;
    d_cnam = that.d_cnam;
    p_addr = c_cpaddr (that.p_addr);
    // save aliases
    d_size = that.d_size;
    p_aals = new s_alias[d_size];
    for (long i = 0; i < d_size; i++) {
      p_aals[i].d_anam = that.p_aals[i].d_anam;
      p_aals[i].p_aadr = c_cpaddr (that.p_aals[i].p_aadr);
    }
    // unlock eveything
    that.unlock ();
    unlock ();
    return *this;
  }

  // return the address canonical name

  String Address::getcanon (void) const {
    rdlock ();
    String result = d_cnam;
    unlock ();
    return result;
  }

  // return the address representation

  String Address::getaddr (void) const {
    rdlock ();
    char* data = c_iprepr (p_addr);
    String result = data;
    delete [] data;
    unlock ();
    return result;
  }

  // get an integer vector representation
  Vector* Address::getvmap (void) const {
    rdlock ();
    if (p_addr == nilp) {
      unlock ();
      return nilp;
    }
    long vlen = (long) p_addr[0] + 1;
    Vector* result = new Vector;
    for (long i = 1; i < vlen; i++) result->append (new Integer (p_addr[i]));
    unlock ();
    return result;
  }

  // return the alias size

  long Address::getsize (void) const {
    rdlock ();
    long result = d_size;
    unlock ();
    return result;
  }

  // return the address canonical name by index

  String Address::getcanon (const long index) const {
    rdlock ();
    if ((index < 0) || (index >= d_size)) {
      unlock ();
      throw Exception ("index-error", "out-of-bound address alias index");
    }
    String result = p_aals[index].d_anam;
    unlock ();
    return result;
  }

  // return the address representation

  String Address::getaddr (const long index) const {
    rdlock ();
    if ((index < 0) || (index >= d_size)) {
      unlock ();
      throw Exception ("index-error", "out-of-bound address alias index");
    }
    char* data = c_iprepr (p_aals[index].p_aadr);
    String result = data;
    delete [] data;
    unlock ();
    return result;
  }

  // get an integer vector representation
  Vector* Address::getvmap (const long index) const {
    rdlock ();
    if ((index < 0) || (index >= d_size)) {
      unlock ();
      throw Exception ("index-error", "out-of-bound address alias index");
    }
    const t_byte* aadr = p_aals[index].p_aadr;
    if (aadr == nilp) {
      unlock ();
      return nilp;
    }
    long vlen = (long) aadr[0] + 1;
    Vector* result = new Vector;
    for (long i = 1; i < vlen; i++) {
      result->append (new Integer (aadr[i]));
    }
    unlock ();
    return result;
  }

  // -------------------------------------------------------------------------
  // - object section                                                        -
  // -------------------------------------------------------------------------

  // the quark zone
  static const long QUARK_ZONE_LENGTH = 8;
  static QuarkZone  zone (QUARK_ZONE_LENGTH);

  // the object supported quarks
  static const long QUARK_GETNAME  = zone.intern ("get-name");
  static const long QUARK_GETADDR  = zone.intern ("get-address");
  static const long QUARK_GETVMAP  = zone.intern ("get-vector");
  static const long QUARK_GETSIZE  = zone.intern ("get-alias-size");
  static const long QUARK_GETANAM  = zone.intern ("get-alias-name");
  static const long QUARK_GETAADR  = zone.intern ("get-alias-address");
  static const long QUARK_GETAMAP  = zone.intern ("get-alias-vector");
  static const long QUARK_GETCANON = zone.intern ("get-canonical-name");

  // create a new object in a generic way
 
  Object* Address::mknew (Vector* argv) {
    long argc = (argv == nilp) ? 0 : argv->length ();
    // check for 1 argument
    if (argc == 1) {
      String name = argv->getstring (0);
      return new Address (name);
    }
    throw Exception ("argument-error",
                     "too many argument with address constructor");
  }
 
  // return true if the given quark is defined

  bool Address::isquark (const long quark, const bool hflg) const {
    rdlock ();
    if (zone.exists (quark) == true) {
      unlock ();
      return true;
    }
    bool result = hflg ? Object::isquark (quark, hflg) : false;
    unlock ();
    return result;
  }

  // apply this object with a set of arguments and a quark
 
  Object* Address::apply (Runnable* robj, Nameset* nset, const long quark,
			  Vector* argv) {

    // get the number of arguments
    long argc = (argv == nilp) ? 0 : argv->length ();
 
    // check for 0 argument
    if (argc == 0) {
      if (quark == QUARK_GETNAME)  return new String  (d_name);
      if (quark == QUARK_GETCANON) return new String  (getcanon ());
      if (quark == QUARK_GETADDR)  return new String  (getaddr  ());
      if (quark == QUARK_GETSIZE)  return new Integer (getsize ());
      if (quark == QUARK_GETVMAP)  return getvmap ();
    }
    // check for 1 argument
    if (argc == 1) {
      if (quark == QUARK_GETANAM) {
	long index = argv->getint (0);
	return new String (getcanon (index));
      }
      if (quark == QUARK_GETAADR) {
	long index = argv->getint (0);
	return new String (getaddr (index));
      }
      if (quark == QUARK_GETAMAP) {
	long index = argv->getint (0);
	return getvmap (index);
      }
    }
    // call the object method
    return Object::apply (robj, nset, quark, argv);
  }
}
