// ---------------------------------------------------------------------------
// - TcpClient.cpp                                                           -
// - afnix:net module - tcp client socket 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-2019 amaury darsch                                   -
// ---------------------------------------------------------------------------

#include "Vector.hpp"
#include "TcpClient.hpp"
#include "Exception.hpp"

namespace afnix {

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

  // create a tcp server by parameters

  TcpClient::TcpClient (const SockParams& prms) : TcpSocket (false) {
    // get the server parameters
    Address addr = prms.getaddr ();
    t_word  port = prms.getport ();
    // create the client by address
    create (addr);
    setopt (prms);
    // connect with the server
    if (connect (port, addr, true) == false) {
      throw Exception ("client-error", "cannot connect socket");
    }
  }
  
  // create a tcp client by host and port

  TcpClient::TcpClient (const String& host, t_word port) : TcpSocket (false) {
    // get the host address
    Address addr (host);
    // create the client by address
    create (addr);
    // connect with the server
    if (connect (port, addr, true) == false)
      throw Exception ("client-error", "cannot connect socket");
  }

  // create a tcp client by ip address and port

  TcpClient::TcpClient (const Address& addr, t_word port) : TcpSocket (false) {
    // create the socket
    create (addr);
    // connect with the server
    if (connect (port, addr, true) == false) {
      throw Exception ("client-error", "cannot connect socket");
    }
  }

  // return the class name

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

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

  // create a new object in a generic way

  Object* TcpClient::mknew (Vector* argv) {
    long argc = (argv == nullptr) ? 0 : argv->length ();
    // check for 1 argument
    if (argc == 1) {
      Object* obj = argv->get (0);
      // check for a socket parameters
      SockParams* prms = dynamic_cast <SockParams*> (obj);
      if (prms != nullptr) {
	return new TcpClient (*prms);
      }
    }
    // check for 2 arguments
    if (argc == 2) {
      // get the tcp client arguments
      Object* obj = argv->get (0);
      if (dynamic_cast <String*> (obj) != nullptr) {
	String host = argv->getstring (0);
	long   port = argv->getlong (1);
	return new TcpClient (host, port);
      }
      Address* addr = dynamic_cast <Address*> (obj);
      if (addr != nullptr) {
	long   port = argv->getlong (1);
	return new TcpClient (*addr, port);
      }
    }
    throw Exception ("argument-error", "invalid arguments with tcp client");

  }
}
