// ---------------------------------------------------------------------------
// - 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-2011 amaury darsch                                   -
// ---------------------------------------------------------------------------

#include "Vector.hpp"
#include "Boolean.hpp"
#include "Address.hpp"
#include "Integer.hpp"
#include "Runnable.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) {
    p_addr = nilp;
    d_size = 0;
    p_aals = nilp;
    resolve (name, false);
  }

  // create a new address by name and resolve lookup

  Address::Address (const String& name, const bool rflg) {
    p_addr = nilp;
    d_size = 0;
    p_aals = nilp;
    resolve (name, rflg);
  }

  // create a new address by bytes

  Address::Address (const t_byte* addr) {
    p_addr = nilp;
    d_size = 0;
    p_aals = nilp;
    resolve (addr, false);
  }

  // create a new address by bytes and resolve flag

  Address::Address (const t_byte* addr, const bool rflg) {
    p_addr = nilp;
    d_size = 0;
    p_aals = nilp;
    resolve (addr, rflg);
  }

  // copy construct an address

  Address::Address (const Address& that) {
    that.rdlock ();
    try {
      // save canonical info
      d_name = that.d_name;
      d_cnam = that.d_cnam;
      p_addr = c_cpaddr (that.p_addr);
      // save aliases
      if (that.d_size > 0) {
	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);
	}
      } else {
	d_size = 0;
	p_aals = nilp;
      }
      that.unlock ();
    } catch (...) {
      that.unlock ();
      throw;
    }
  }

  // destroy this address instance

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

  // return the class name

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

  // return a clone of this address

  Object* Address::clone (void) const {
    return new Address (*this);
  }

  // assign an address to this one

  Address& Address::operator = (const Address& that) {
    // check for equality
    if (this == &that) return *this;
    // lock and copy
    wrlock ();
    that.rdlock ();
    try {
      // delete old address
      delete [] p_addr;
      // save canonical info
      d_name = that.d_name;
      d_cnam = that.d_cnam;
      p_addr = c_cpaddr (that.p_addr);
      // save aliases
      if (that.d_size > 0) {
	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);
	}
      } else {
	d_size = 0;
	p_aals = nilp;
      }
      // unlock eveything
      that.unlock ();
      unlock ();
      return *this;
    } catch (...) {
      that.unlock ();
      unlock ();
      throw;
    }
  }

  // prefix increase this address by one position

  Address& Address::operator ++ (void) {
    wrlock ();
    t_byte* addr = nilp;
    // try to resolv the new address
    try {
      addr = c_nxaddr (p_addr);
      resolve (addr, true);
      delete [] addr;
      // here is the new address
      unlock ();
      return *this;
    } catch (...) {
      delete [] addr;
      unlock ();
      throw;
    }
  }

  // postfix increase this address by one position

  Address Address::operator ++ (int) {
    wrlock ();
    t_byte* addr = nilp;
    try {
      Address result = *this;
      // get the next address
      addr = c_nxaddr (p_addr);
      // try to resolv the new address
      resolve (addr, true);
      delete [] addr;
      // here is the new address
      unlock ();
      return result;
    } catch (...) {
      delete [] addr;
      unlock ();
      throw;
    }
  }

  // compare two addresses

  bool Address::operator == (const Address& addr) const {
    rdlock ();
    addr.rdlock ();
    try {
      bool result = c_eqaddr (p_addr, addr.p_addr);
      addr.unlock ();
      unlock ();
      return result;
    } catch (...) {
      addr.unlock ();
      unlock ();
      throw;
    }
  }

  // compare two addresses

  bool Address::operator != (const Address& addr) const {
    rdlock ();
    addr.rdlock ();
    try {
      bool result = !c_eqaddr (p_addr, addr.p_addr);
      addr.unlock ();
      unlock ();
      return result;
    } catch (...) {
      addr.unlock ();
      unlock ();
      throw;
    }
  }

  // compare two addresses

  bool Address::operator < (const Address& addr) const {
    rdlock ();
    addr.rdlock ();
    try {
      bool result = c_ltaddr (p_addr, addr.p_addr);
      addr.unlock ();
      unlock ();
      return result;
    } catch (...) {
      addr.unlock ();
      unlock ();
      throw;
    }
  }

  // compare two addresses

  bool Address::operator <= (const Address& addr) const {
    rdlock ();
    addr.rdlock ();
    try {
      bool result = c_leaddr (p_addr, addr.p_addr);
      addr.unlock ();
      unlock ();
      return result;
    } catch (...) {
      addr.unlock ();
      unlock ();
      throw;
    }
  }

  // compare two addresses

  bool Address::operator > (const Address& addr) const {
    rdlock ();
    addr.rdlock ();
    try {
      bool result = ! (*this <= addr);
      addr.unlock ();
      unlock ();
      return result;
    } catch (...) {
      addr.unlock ();
      unlock ();
      throw;
    }
  }


  bool Address::operator >= (const Address& addr) const {
    rdlock ();
    addr.rdlock ();
    try {
      bool result = ! (*this < addr);
      addr.unlock ();
      unlock ();
      return result;
    } catch (...) {
      addr.unlock ();
      unlock ();
      throw;
    }
  }

  // get the next available address but do not resolve

  void Address::next (void) {
    wrlock ();
    t_byte* addr = nilp;
    try {
      // get the next address
      addr = c_nxaddr (p_addr);
      // resolve the address
      resolve (addr, false);
      delete [] addr;
      unlock ();
    } catch (...) {
      delete [] addr;
      throw;
    }
  }

  // resolve an existing address

  void Address::resolve (const bool rflg) {
    wrlock ();
    try {
      if ((rflg == true) && (p_addr != nilp)) {
	char* cnam = c_getipn (p_addr);
	if (cnam != nilp) {
	  d_cnam = cnam;
	  delete [] cnam;
	}
      }
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // set the address by name and reverse lookup flag

  void Address::resolve (const String& name, const bool rflg) {
    wrlock ();
    char*     host = nilp;
    s_ipaddr* addr = nilp;
    try {
      // clean old address
      d_size = 0;
      delete [] p_addr; p_addr = nilp;
      delete [] p_aals; p_aals = nilp;
      // get the ip address info
      host = name.tochar ();
      addr = c_getipa (host);
      delete [] host; host = nilp;
      // check for good address
      if ((addr == nilp) || (addr->d_size == 0)) {
	throw Exception ("address-error", "cannot resolve address", name);
      }
      // save canonical info
      d_name = name;
      d_cnam = addr->p_name[0];
      p_addr = c_cpaddr (addr->p_addr[0]);
      // eventually do a reverse lookup
      resolve (rflg);
      // save aliases
      if (addr->d_size > 1) {
	d_size = addr->d_size - 1;
	p_aals = new s_alias[d_size];
	for (long i = 0; i < d_size; i++) {
	  p_aals[i].d_anam = addr->p_name[i+1];
	  p_aals[i].p_aadr = c_cpaddr (addr->p_addr[i+1]);
	}
      } else {
	d_size = 0;
	p_aals = nilp;
      }
      delete addr;
      unlock ();
    } catch (...) {
      delete [] host;
      delete addr;
      unlock ();
      throw;
    }
  }

  // resolve an address by bytes and resolve flag

  void Address::resolve (const t_byte* addr, const bool rflg) {
    // do nothing if nil
    if (addr == nilp) return;
    wrlock ();
    char* host = nilp;
    try {
      // clean old address
      d_size = 0;
      delete [] p_addr; p_addr = nilp;
      delete [] p_aals; p_aals = nilp;
      // get the host name representation
      host   = c_iprepr (addr);
      d_name = host;
      d_cnam = host;
      delete [] host; host = nilp;
      // save the byte address form
      p_addr = c_cpaddr (addr);
      // eventually do a reverse lookup
      resolve (rflg);
      unlock ();
    } catch (...) {
      delete [] host;
      unlock ();
      throw;
    }
  }

  // return the address canonical name

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

  // return the address representation

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

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

  // return the alias size

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

  // return the address canonical name by index

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

  // return the address representation

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

  // get an integer vector representation
  Vector* Address::getvmap (const long index) const {
    rdlock ();
    Vector* result = nilp;
    try {
      if ((index < 0) || (index >= d_size)) {
	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;
      result = new Vector;
      for (long i = 1; i < vlen; i++) {
	result->add (new Integer (aadr[i]));
      }
      unlock ();
      return result;
    } catch (...) {
      delete result;
      unlock ();
      throw;
    }
  }

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

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

  // the object supported quarks
  static const long QUARK_OPP      = zone.intern ("++");
  static const long QUARK_EQL      = zone.intern ("==");
  static const long QUARK_NEQ      = zone.intern ("!=");
  static const long QUARK_LTH      = zone.intern ("<");
  static const long QUARK_GEQ      = zone.intern ("<=");
  static const long QUARK_GTH      = zone.intern ("<");
  static const long QUARK_LEQ      = zone.intern ("<=");
  static const long QUARK_NEXT     = zone.intern ("next");
  static const long QUARK_RESOLVE  = zone.intern ("resolve");
  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);
    }
    // check for 2 arguments
    if (argc == 2) {
      String name = argv->getstring (0);
      bool   rflg = argv->getbool   (1);
      return new Address (name, rflg);
    }
    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;
  }

  // operate this object with another object

  Object* Address::oper (t_oper type, Object* object) {
    Address* addr = dynamic_cast <Address*> (object);
    switch (type) {
    case Object::EQL:
      if (addr != nilp) return new Boolean (*this == *addr);
      break;
    case Object::NEQ:
      if (addr != nilp) return new Boolean (*this != *addr);
      break;
    case Object::LEQ:
      if (addr != nilp) return new Boolean (*this <= *addr);
      break;
    case Object::LTH:
      if (addr != nilp) return new Boolean (*this < *addr);
      break;
    case Object::GTH:
      if (addr != nilp) return new Boolean (*this > *addr);
      break;
    case Object::GEQ:
      if (addr != nilp) return new Boolean (*this >= *addr);
      break;
    default:
      throw Exception ("operator-error", "unsupported address operator");
    }
    throw Exception ("type-error", "invalid operand with address",
                     Object::repr (object));
  }

  // 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 ();
      if (quark == QUARK_OPP) {
        ++(*this);
        robj->post (this);
        return this;
      }
      if (quark == QUARK_NEXT) {
	next ();
	return nilp;
      }
      if (quark == QUARK_RESOLVE) {
	resolve (true);
	return nilp;
      }
    }
    // check for 1 argument
    if (argc == 1) {
      if (quark == QUARK_EQL) return oper (Object::EQL, argv->get (0));
      if (quark == QUARK_NEQ) return oper (Object::NEQ, argv->get (0));
      if (quark == QUARK_LTH) return oper (Object::LTH, argv->get (0));
      if (quark == QUARK_LEQ) return oper (Object::LEQ, argv->get (0));
      if (quark == QUARK_GTH) return oper (Object::GTH, argv->get (0));
      if (quark == QUARK_GEQ) return oper (Object::GEQ, argv->get (0));

      if (quark == QUARK_RESOLVE) {
	String name = argv->getstring (0);
	resolve (name, false);
	return nilp;
      }
      if (quark == QUARK_GETANAM) {
	long index = argv->getlong (0);
	return new String (getcanon (index));
      }
      if (quark == QUARK_GETAADR) {
	long index = argv->getlong (0);
	return new String (getaddr (index));
      }
      if (quark == QUARK_GETAMAP) {
	long index = argv->getlong (0);
	return getvmap (index);
      }
    }
    if (argc == 2) {
      if (quark == QUARK_RESOLVE) {
	String name = argv->getstring (0);
	bool   rflg = argv->getbool   (1);
	resolve (name, rflg);
	return nilp;
      }
    }
    // call the object method
    return Object::apply (robj, nset, quark, argv);
  }
}
