Commit 2948d1dd authored by filatov's avatar filatov
Browse files

initial commit of Framework

parent 063a0281
#ifndef CODEC_H
#define CODEC_H
class OCTETSTRING;
template<typename TPDU> class Codec {
public:
Codec(){}
virtual int encode (const TPDU& msg, OCTETSTRING& data) = 0;
virtual int decode (const OCTETSTRING& data, TPDU& msg) = 0;
};
#endif
#ifndef LAYER_H
#define LAYER_H
#include <string>
#include <map>
#include <vector>
typedef std::map<std::string, std::string> Params;
class OCTETSTRING;
class Layer {
std::vector<Layer*> upperLayers;
std::vector<Layer*> lowerLayers;
public:
Layer(){}
public:
void addUpperLayer(Layer*);
void removeUpperLayer(Layer*);
virtual void sendData(const OCTETSTRING& data, const Params& params);
virtual void receiveData(const OCTETSTRING& data, const Params& info);
protected:
void toAllLayers(std::vector<Layer*>&layers, const OCTETSTRING& data, const Params& info);
inline void receiveToAllLayers(const OCTETSTRING& data, const Params& info) { toAllLayers(upperLayers, data, info); }
inline void sendToAllLayers(const OCTETSTRING& data, const Params& info) { toAllLayers(lowerLayers, data, info); }
};
template <typename TPort> class TLayer : public Layer {
typedef std::vector<TPort*> TPortList;
TPortList upperPorts;
public:
TLayer(){}
void addUpperPort(TPort*);
void removeUpperPort(TPort*);
/*
template <typename Msg> void receiveToAllPorts(Msg* m, const Param& param) {
for(TPortList::iterator it=upperPorts.begin(); it<upperPorts.end(); ++it){
(*it)->receiveMsg(m, param);
}
}
*/
};
class LayerFactory {
public:
LayerFactory(){}
virtual Layer * createLayer(const std::string & type, const std::string & param) = 0;
};
class StackFactory {
static StackFactory * _instance;
std::map<std::string,LayerFactory*> _fs;
public:
static StackFactory * getInstance();
void registerLayerFactory(const std::string & type, LayerFactory * f);
Layer* createLayerStack(const char*);
};
#endif
\ No newline at end of file
#include "Layer.h"
StackFactory * StackFactory::_instance = NULL;
StackFactory * StackFactory::getInstance(){
return _instance ? _instance : _instance=new StackFactory();
}
void StackFactory::registerLayerFactory(const std::string & type, LayerFactory * f)
{
_fs[type] = f;
}
Layer* StackFactory::createLayerStack(const char* str)
{
std::string type;
std::string param;
// parse str
LayerFactory * f = _fs[type];
if(f){
return f->createLayer(type, param);
}
return NULL;
}
#include "BTPCodec.h"
int BTPACodec::encode (const BTPA& msg, OCTETSTRING& data)
{
return 0;
}
int BTPACodec::decode (const OCTETSTRING& data, BTPA& msg)
{
return 0;
}
int BTPBCodec::encode (const BTPB& msg, OCTETSTRING& data)
{
return 0;
}
int BTPBCodec::decode (const OCTETSTRING& data, BTPB& msg)
{
return 0;
}
#ifndef BTPCODEC_H
#define BTPCODEC_H
#include "BTPTypes.h"
#include "Codec.h"
class BTPACodec : public Codec<BTPA> {
virtual int encode (const BTPA& msg, OCTETSTRING& data);
virtual int decode (const OCTETSTRING& data, BTPA& msg);
};
class BTPBCodec : public Codec<BTPB> {
virtual int encode (const BTPB& msg, OCTETSTRING& data);
virtual int decode (const OCTETSTRING& data, BTPB& msg);
};
#endif
#include "BTPLayer.h"
#include "BTPPort.h"
void BTPLayer::sendMsg(const BTPA& m, const Params& params){
OCTETSTRING data;
_a.encode(m, data);
sendData(data, Params());
}
void BTPLayer::sendMsg(const BTPB& m, const Params& params){
OCTETSTRING data;
_b.encode(m, data);
sendData(data, Params());
}
void BTPLayer::sendData(const OCTETSTRING& data, const Params& params){
sendToAllLayers(data, params);
}
void BTPLayer::receiveData(const OCTETSTRING& data, const Params& info)
{
BTPB b;
BTPA a;
receiveToAllPorts(a, Params());
receiveToAllPorts(b, Params());
receiveToAllLayers(data, Params()); //a.payload shall be sent!
}
class BTPFactory : public LayerFactory {
static BTPFactory _f;
public:
BTPFactory();
virtual Layer * createLayer(const std::string & type, const std::string & param);
};
BTPFactory::BTPFactory()
{
StackFactory::getInstance()->registerLayerFactory("BTP", this);
// register factory
}
Layer * BTPFactory::createLayer(const std::string & type, const std::string & param)
{
return new BTPLayer();
}
BTPFactory BTPFactory::_f;
#ifndef BTP_LAYER_H
#define BTP_LAYER_H
#include "Layer.h"
#include "BTPTypes.h"
#include "BTPCodec.h"
class BTPPort;
class BTPLayer : public TLayer<BTPPort> {
BTPACodec *_a;
BTPBCodec *_b;
public:
BTPLayer():TLayer<BTPPort>(){};
void sendMsg(const BTPA&, const Params& param);
void sendMsg(const BTPB&, const Params& param);
void receiveToAllPorts(const BTPA& m, const Params& param);
void receiveToAllPorts(const BTPB& m, const Params& param);
virtual void sendData(const OCTETSTRING& data, const Params& params);
virtual void receiveData(const OCTETSTRING& data, const Params& info);
};
#endif
\ No newline at end of file
#include "BTPPort.h"
#include "BTPLayer.h"
void BTPPort::receiveMsg(const BTPA& m) {
}
void BTPPort::receiveMsg(const BTPB& m) {
}
void BTPPort::outgoingMsg(const BTPA& a) {
layer->sendMsg(a);
}
void BTPPort::outgoingMsg(const BTPB& b) {
layer->sendMsg(b);
}
#ifndef BTPPORT_H
#define BTPPORT_H
#include "BTPTypes.h"
class BTPLayer;
class BTPPort {
BTPLayer * layer;
public:
BTPPort(){};
void receiveMsg (const BTPA&);
void receiveMsg (const BTPB&);
void outgoingMsg(const BTPA&);
void outgoingMsg(const BTPB&);
};
#endif
\ No newline at end of file
#ifndef BTPTYPES_H
#define BTPTYPES_H
class BTPA {};
class BTPB {};
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment