Skip to content
Layer.hh 4.45 KiB
Newer Older
#ifndef LAYER_HH
#define LAYER_HH
garciay's avatar
garciay committed

filatov's avatar
filatov committed
#include <string>
#include <map>
#include <vector>

#include "Params.hh"
garciay's avatar
garciay committed
#include "loggers.hh"
filatov's avatar
filatov committed
class OCTETSTRING;
garciay's avatar
garciay committed
class CHARSTRING;
filatov's avatar
filatov committed

class Layer {
  std::vector<Layer*> upperLayers;
  std::vector<Layer*> lowerLayers;
garciay's avatar
garciay committed
protected:
  std::string type;
public:
garciay's avatar
garciay committed
  Layer() : upperLayers(), lowerLayers(), type(std::string("")) { loggers::loggers::log("Layer::Layer (D)"); };
  Layer(const std::string& p_type) : upperLayers(), lowerLayers(), type(std::string(p_type.begin(), p_type.end())) { loggers::loggers::log("Layer::Layer"); };
garciay's avatar
garciay committed
  virtual ~Layer() { upperLayers.clear(); lowerLayers.clear(); };
  void deleteLayer() { };
public:
  inline void addUpperLayer(Layer* p_layer) { 
    //loggers::loggers::log(">>> Layer::addUpperLayer");
    if (p_layer != NULL) {
      loggers::loggers::log("Layer::addUpperLayer: %s is upper layer of %s", p_layer->to_string().c_str(), to_string().c_str());
      upperLayers.push_back(p_layer);
      loggers::loggers::log(" Layer::addUpperLayer: %s is loweer layer of %s", to_string().c_str(), p_layer->to_string().c_str());
      p_layer->lowerLayers.push_back(this);
garciay's avatar
garciay committed
    };
garciay's avatar
garciay committed
  };
  void removeUpperLayer(Layer* p_layer) {  };

  virtual void sendData(const OCTETSTRING& data, const Params& params) {};
garciay's avatar
garciay committed
  virtual void receiveData(OCTETSTRING& data, Params& params) {};
garciay's avatar
garciay committed

  inline const std::string& to_string() const { return type; };

protected:
garciay's avatar
garciay committed
  inline void toAllLayers(std::vector<Layer*>&layers, OCTETSTRING& data, Params& params) {
garciay's avatar
garciay committed
    //loggers::loggers::log_msg(">>> Layer::toAllLayer: ", data);
    loggers::loggers::log(">>> Layer::toAllLayer: %d", layers.size());
    for (std::vector<Layer*>::const_iterator it = layers.cbegin(); it != layers.cend(); ++it) {
      Layer * p = *it;
garciay's avatar
garciay committed
      p->receiveData(data, params); // FIXME BUG I 
garciay's avatar
garciay committed
    } // End of 'for' statement
  };
garciay's avatar
garciay committed
  //inline void toAllUpperLayers(OCTETSTRING& data, Params& params) { toAllLayers(upperLayers, data, params); } // TODO Useless??
  //inline void toAllLowerLayers(OCTETSTRING& data, Params& params) { toAllLayers(lowerLayers, data, params); } // TODO Useless??
  inline void receiveToAllLayers(OCTETSTRING& data, Params& params) {
    //loggers::loggers::log_msg(">>> Layer::receiveToAllLayers: ", data);
    loggers::loggers::log(">>> Layer::receiveToAllLayers: %d", upperLayers.size());
    for (std::vector<Layer*>::const_iterator it = upperLayers.cbegin(); it != upperLayers.cend(); ++it) {
      Layer * p = *it;
      p->receiveData(data, params);
    } // End of 'for' statement
  };
  inline void sendToAllLayers(OCTETSTRING& data, Params& params)  {
    //loggers::loggers::log_msg(">>> Layer::sendToAllLayers: ", data);
    loggers::loggers::log(">>> Layer::sendToAllLayers: %d", lowerLayers.size());
    for (std::vector<Layer*>::const_iterator it = lowerLayers.cbegin(); it != lowerLayers.cend(); ++it) {
      Layer * p = *it;
      p->sendData(data, params);
    } // End of 'for' statement
  };
}; // End of class Layer
filatov's avatar
filatov committed
template <typename TPort> class TLayer : public Layer {
  typedef std::vector<TPort*> TPortList;
  typedef typename std::vector<TPort*>::iterator TPortListIterator;
filatov's avatar
filatov committed
  TPortList upperPorts;
garciay's avatar
garciay committed
public:
garciay's avatar
garciay committed
  TLayer() : Layer(), upperPorts() { loggers::loggers::log("TLayer::TLayer (D)"); };
  TLayer(const std::string& p_type) : Layer(p_type), upperPorts() { loggers::loggers::log("TLayer::TLayer"); };
garciay's avatar
garciay committed
  void addUpperPort(TPort * p_port) { upperPorts.push_back(p_port); };
  void removeUpperPort(TPort*);

protected:
  template <typename TMessage> void toAllUpperPorts(const TMessage& m, const Params& param) {
    loggers::loggers::log(">>> TLayer::toAllUpperPorts: %d", upperPorts.size());
    for(TPortListIterator it=upperPorts.begin(); it<upperPorts.end(); ++it){
      (*it)->receiveMsg(m, param);
filatov's avatar
filatov committed
    }
garciay's avatar
garciay committed
  }
filatov's avatar
filatov committed
class LayerFactory {
garciay's avatar
garciay committed
public:
garciay's avatar
garciay committed
  LayerFactory() {};
garciay's avatar
garciay committed
  virtual Layer * createLayer(const std::string & type, const std::string & param) = 0;
class LayerStackBuilder {
garciay's avatar
garciay committed
  typedef std::map<std::string, LayerFactory*> LayerFactoryMap;

  static LayerStackBuilder * _instance;
  std::map<std::string, LayerFactory*> _fs;
private:
  LayerStackBuilder(); // can not be created manually
public:
  static LayerStackBuilder * GetInstance();
  static void RegisterLayerFactory(const std::string & type, LayerFactory * f);

public:
  void registerLayerFactory(const std::string & type, LayerFactory * f);
  Layer* createLayerStack(const char*);
#endif