// This Test Port skeleton source file was generated by the
// TTCN-3 Compiler of the TTCN-3 Test Executor version CRL 113 200/5 R3A
// for U-ERICSSON\ethgry (ethgry@HU00078339) on Fri Aug 14 16:32:05 2015

// Copyright Ericsson Telecom AB 2000-2014

// You may modify this file. Complete the body of empty functions and
// add your member functions here.

#include "MapemSpatemPort.hh"
#include "MapemSpatemLayerFactory.hh"
#include "loggers.hh"

namespace LibItsMapemSpatem__TestSystem {

  MapemSpatemPort::MapemSpatemPort(const char *par_port_name) : MapemSpatemPort_BASE(par_port_name), _cfg_params(), _layer_params(), _layer(NULL), _time_key("MapemSpatemPort::outgoing_send") {

  }

  MapemSpatemPort::~MapemSpatemPort()
  {
    if (_layer != NULL) {
      delete _layer;
    }
  }

  void MapemSpatemPort::set_parameter(const char * parameter_name, const char * parameter_value)
  {
    loggers::get_instance().log("MapemSpatemPort::set_parameter: %s=%s", parameter_name, parameter_value);
    _cfg_params.insert(std::pair<std::string, std::string>(std::string(parameter_name), std::string(parameter_value)));
  }

  /*void MapemSpatemPort::Handle_Fd_Event(int fd, boolean is_readable,
    boolean is_writable, boolean is_error) {}*/

  void MapemSpatemPort::Handle_Fd_Event_Error(int /*fd*/)
  {

  }

  void MapemSpatemPort::Handle_Fd_Event_Writable(int /*fd*/)
  {

  }

  void MapemSpatemPort::Handle_Fd_Event_Readable(int /*fd*/)
  {

  }

  /*void MapemSpatemPort::Handle_Timeout(double time_since_last_call) {}*/

  void MapemSpatemPort::user_map(const char * system_port)
  {
    loggers::get_instance().log(">>> MapemSpatemPort::user_map: %s", system_port);
    // Build layer stack
    params::iterator it = _cfg_params.find(std::string("params"));
    if (it != _cfg_params.end()) {
      loggers::get_instance().log("MapemSpatemPort::user_map: %s", it->second.c_str());
      _layer = layer_stack_builder::get_instance()->create_layer_stack(it->second.c_str());
      if (static_cast<MapemSpatemLayer *>(_layer) == NULL) {
	loggers::get_instance().error("MapemSpatemPort::user_map: Invalid stack configuration: %s", it->second.c_str());
      }
      static_cast<MapemSpatemLayer *>(_layer)->add_upper_port(this);
    } else {
      loggers::get_instance().error("MapemSpatemPort::user_map: No layers defined in configuration file");
    }
  }

  void MapemSpatemPort::user_unmap(const char * system_port)
  {
    loggers::get_instance().log(">>> MapemSpatemPort::user_unmap: %s", system_port);
    if (_layer != NULL) {
      delete _layer;
      _layer = NULL;
    }
  }

  void MapemSpatemPort::user_start()
  {

  }

  void MapemSpatemPort::user_stop()
  {

  }

  void MapemSpatemPort::outgoing_send(const MapemReq& send_par)
  {
    loggers::get_instance().log_msg(">>> MapemSpatemPort::outgoing_send: payload=", send_par);
    
    float duration;
    loggers::get_instance().set_start_time(_time_key);
    params params;
    static_cast<MapemSpatemLayer *>(_layer)->sendMsg(send_par, params);
    loggers::get_instance().set_stop_time(_time_key, duration);
  }

  void MapemSpatemPort::outgoing_send(const SpatemReq& send_par)
  {
    loggers::get_instance().log_msg(">>> MapemSpatemPort::outgoing_send: payload=", send_par);
    
    float duration;
    loggers::get_instance().set_start_time(_time_key);
    params params;
    static_cast<MapemSpatemLayer *>(_layer)->sendMsg(send_par, params);
    loggers::get_instance().set_stop_time(_time_key, duration);
  }

  void MapemSpatemPort::receiveMsg(const LibItsMapemSpatem__TestSystem::MapemInd& p_ind, const params& p_params) {
    loggers::get_instance().log_msg(">>> MapemSpatemPort::receive_msg: ", p_ind);

    incoming_message(p_ind);
  }

  void MapemSpatemPort::receiveMsg(const LibItsMapemSpatem__TestSystem::SpatemInd& p_ind, const params& p_params) {
    loggers::get_instance().log_msg(">>> MapemSpatemPort::receive_msg: ", p_ind);

    incoming_message(p_ind);
  }

} /* end of namespace */

