Layer.hh 3.45 KB
Newer Older
filatov's avatar
filatov committed
1
2
#ifndef LAYER_HH
#define LAYER_HH
filatov's avatar
filatov committed
3
4
5
6
#include <string>
#include <map>
#include <vector>

7
#include "Params.hh"
garciay's avatar
garciay committed
8
#include "loggers.hh"
9

filatov's avatar
filatov committed
10
class OCTETSTRING;
garciay's avatar
garciay committed
11
class CHARSTRING;
filatov's avatar
filatov committed
12
13
14
15

class Layer {
  std::vector<Layer*> upperLayers;
  std::vector<Layer*> lowerLayers;
garciay's avatar
garciay committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
protected:
  std::string type;
public:
  Layer(const std::string& p_type) : upperLayers(), lowerLayers(), type(std::string(p_type.begin(), p_type.end())) {};
  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
30
    };
garciay's avatar
garciay committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  };
  void removeUpperLayer(Layer* p_layer) {  };

  virtual void sendData(const OCTETSTRING& data, const Params& params) {};
  virtual void receiveData(OCTETSTRING& data, Params& info) {};

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

protected:
  inline void toAllLayers(std::vector<Layer*>&layers, OCTETSTRING& data, Params& info) {
    //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;
      p->receiveData(data, info);
    } // End of 'for' statement
  };
  inline void toAllUpperLayers(OCTETSTRING& data, Params& info) { toAllLayers(upperLayers, data, info); }
  inline void toAllLowerLayers(OCTETSTRING& data, Params& info) { toAllLayers(lowerLayers, data, info); }
  inline void receiveToAllLayers(OCTETSTRING& data, Params& info) { toAllLayers(upperLayers, data, info); }
  inline void sendToAllLayers(OCTETSTRING& data, Params& info)  { toAllLayers(lowerLayers, data, info); }
filatov's avatar
filatov committed
52
53
};

filatov's avatar
filatov committed
54

filatov's avatar
filatov committed
55
56
template <typename TPort> class TLayer : public Layer {
  typedef std::vector<TPort*> TPortList;
57
  typedef typename std::vector<TPort*>::iterator TPortListIterator;
filatov's avatar
filatov committed
58

filatov's avatar
filatov committed
59
  TPortList upperPorts;
60
  
garciay's avatar
garciay committed
61
62
63
64
65
66
67
68
69
70
public:
  TLayer(const std::string& p_type) : Layer(p_type), upperPorts() {};
  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
71
    }
garciay's avatar
garciay committed
72
  }
filatov's avatar
filatov committed
73
74
};

filatov's avatar
filatov committed
75

filatov's avatar
filatov committed
76
class LayerFactory {
garciay's avatar
garciay committed
77
78
79
public:
  LayerFactory(){}
  virtual Layer * createLayer(const std::string & type, const std::string & param) = 0;
filatov's avatar
filatov committed
80
81
};

filatov's avatar
filatov committed
82
class LayerStackBuilder {
garciay's avatar
garciay committed
83
84
85
86
87
88
89
90
91
92
93
94
95
  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*);
filatov's avatar
filatov committed
96
97
};

filatov's avatar
filatov committed
98
#endif