#include "UpperTesterLayer.hh"
#include "UpperTesterTypes.hh"

#include "loggers.hh"

UpperTesterLayer::UpperTesterLayer(const std::string & p_type, const std::string & param) : TLayer<LibItsGeoNetworking__TestSystem::UpperTesterPort>(p_type), _params(), _codec() {
  loggers::get_instance().log(">>> UpperTesterLayer::UpperTesterLayer: %s, %s", to_string().c_str(), param.c_str());
  // Setup parameters
  Params::convert(_params, param);
}

void UpperTesterLayer::sendMsg(const LibItsGeoNetworking__TestSystem::UtInitialize& p, Params& params){
  loggers::get_instance().log(">>> UpperTesterLayer::sendMsg");

  // Encode UpperTester PDU
  OCTETSTRING data;
  _codec.encode((const Record_Type &)p, data);
  // Update parameters
  //  Params par(params); // FIXME Review all const Param& in method declarations
  sendData(data, params);
}

void UpperTesterLayer::sendData(OCTETSTRING& data, Params& params) {
  loggers::get_instance().log_msg(">>> UpperTesterLayer::sendData: ", data);
  params.log();  
  sendToAllLayers(data, params);
}

void UpperTesterLayer::receiveData(OCTETSTRING& data, Params& params)
{
  loggers::get_instance().log_msg(">>> UpperTesterLayer::receiveData: ", data);
  // Decode the payload
  //Record_Type *p = new Record_Type();
  //_codec.decode(data, *p);
  
  // Pass it to the ports if amy
  //toAllUpperPorts(p, params);
}

class UpperTesterFactory : public LayerFactory {
  static UpperTesterFactory _f;
public:
  UpperTesterFactory();
  virtual Layer * createLayer(const std::string &  type, const std::string &  param);
};

UpperTesterFactory::UpperTesterFactory(){
  // Register factory
  loggers::get_instance().log(">>> UpperTesterFactory::UpperTesterFactory");
  LayerStackBuilder::RegisterLayerFactory("UT", this);
}

Layer * UpperTesterFactory::createLayer(const std::string & p_type, const std::string & param){
  return new UpperTesterLayer(p_type, param);
}

UpperTesterFactory UpperTesterFactory::_f;
