WSF
WsfCommProtocolStack.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2018 Infoscitex, a DCS Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef WSFCOMMPROTOCOLSTACK_HPP
13#define WSFCOMMPROTOCOLSTACK_HPP
14
15#include "wsf_export.h"
16
17#include <memory>
18#include <vector>
19
20#include "WsfCommLayer.hpp"
22#include "WsfStringId.hpp"
23
24class UtInput;
25class WsfPlatform;
26
27namespace wsf
28{
29namespace comm
30{
31
32class Comm;
33class Message;
34
37{
38public:
40
41 using ProtocolVector = std::vector<std::unique_ptr<Layer>>;
42 using RawProtocolVector = std::vector<Layer*>;
43
45 ProtocolStack() = default;
46 ProtocolStack(const ProtocolStack& aSrc);
47 ProtocolStack& operator=(const ProtocolStack& aRhs) = delete;
48 virtual ~ProtocolStack() = default;
49
52 Layer* AddLayer(std::unique_ptr<Layer> aLayerPtr);
53
55 bool RemoveLayer(size_t aLayerIndex);
56
58 bool RemoveLayerByID(size_t aLayerID);
59
62 std::unique_ptr<Layer> ReplaceLayer(size_t aLayerIndex, std::unique_ptr<Layer> aLayerPtr);
63
66 Layer* InsertLayer(size_t aLayerIndex, std::unique_ptr<Layer> aLayerPtr);
67
69
70
72 Layer* GetLayer(size_t aLayerIndex) const;
73 Layer* GetLayer(LayerType aLayerType) const;
74 RawProtocolVector GetLayers(LayerType aLayerType) const;
75
77 template<typename T>
78 T* GetLayer() const
79 {
80 for (const auto& layer : mProtocolStack)
81 {
82 auto layerTypePtr = dynamic_cast<T*>(layer->GetCommLayerImp());
83 if (layerTypePtr)
84 {
85 return layerTypePtr;
86 }
87 }
88 return nullptr;
89 }
90
92 template<typename T>
93 std::vector<T*> GetLayers() const
94 {
95 std::vector<T*> layers;
96 for (const auto& layer : mProtocolStack)
97 {
98 auto layerTypePtr = dynamic_cast<T*>(layer->GetCommLayerImp());
99 if (layerTypePtr)
100 {
101 layers.push_back(layerTypePtr);
102 }
103 }
104 return layers;
105 }
106
107 Layer* GetLayerByID(size_t aLayerID) const;
109
110
112 size_t Size() const { return mProtocolStack.size(); }
113
115 void ClearLayers() { mProtocolStack.clear(); }
116
118
119 virtual bool Initialize(double aSimTime);
120 virtual bool Initialize2(double aSimTime);
121 virtual bool ProcessInput(UtInput& aInput);
123
124 void SetParent(Comm* aParentCommPtr);
125 Comm* GetParent() const { return mParent; }
126 void SetDebugEnabled(bool aDebugEnabled);
127
129
130 virtual bool Receive(double aSimTime, Comm* aXmtrPtr, Message& aMessage);
131 virtual bool Send(double aSimTime, Message& aMessage);
133
135
136 virtual bool ProcessLayerMessage(double aSimTime, size_t aLayerIndex, layer::Message aLayerMessage, Message* aCommMessagePtr);
138
139protected:
141 void ValidateAdjacentLayers();
142
144
145 ProtocolVector::iterator FindLayerByID(size_t aID);
146 ProtocolVector::const_iterator FindLayerByID(size_t aID) const;
148
151
153 Comm* mParent{nullptr};
154};
155
156} // namespace comm
157} // namespace wsf
158
159#endif
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
Platforms represent a entity within the simulation.
Definition WsfPlatform.hpp:115
Definition WsfComm.hpp:43
Definition WsfCommLayer.hpp:40
LayerType
Definition WsfCommLayer.hpp:45
Definition WsfCommMessage.hpp:51
std::vector< T * > GetLayers() const
Template method for return of all layers of the template type.
Definition WsfCommProtocolStack.hpp:93
bool RemoveLayer(size_t aLayerIndex)
Removes a layer at the indicated position.
Definition WsfCommProtocolStack.cpp:77
std::vector< Layer * > RawProtocolVector
Definition WsfCommProtocolStack.hpp:42
ProtocolStack()=default
Constructors and virtual destructor.
size_t Size() const
Returns the number of layers in the stack.
Definition WsfCommProtocolStack.hpp:112
ProtocolVector mProtocolStack
Protocol stack for communication device.
Definition WsfCommProtocolStack.hpp:150
virtual ~ProtocolStack()=default
void ClearLayers()
Removes all layers from the stack.
Definition WsfCommProtocolStack.hpp:115
ProtocolStack & operator=(const ProtocolStack &aRhs)=delete
std::vector< std::unique_ptr< Layer > > ProtocolVector
Definition WsfCommProtocolStack.hpp:41
Layer * AddLayer(std::unique_ptr< Layer > aLayerPtr)
Definition WsfCommProtocolStack.cpp:46
RawProtocolVector GetLayers(LayerType aLayerType) const
Definition WsfCommProtocolStack.cpp:161
std::unique_ptr< Layer > ReplaceLayer(size_t aLayerIndex, std::unique_ptr< Layer > aLayerPtr)
Definition WsfCommProtocolStack.cpp:109
Comm * GetParent() const
Definition WsfCommProtocolStack.hpp:125
Comm * mParent
Pointer to the stack Parent.
Definition WsfCommProtocolStack.hpp:153
Layer * InsertLayer(size_t aLayerIndex, std::unique_ptr< Layer > aLayerPtr)
Definition WsfCommProtocolStack.cpp:126
bool RemoveLayerByID(size_t aLayerID)
Removes a layer via unique ID.
Definition WsfCommProtocolStack.cpp:91
Layer::LayerType LayerType
Definition WsfCommProtocolStack.hpp:39
Layer * GetLayer(size_t aLayerIndex) const
Returns a pointer to the layer at the indicated position.
Definition WsfCommProtocolStack.cpp:142
T * GetLayer() const
Template method for return of the first layer of the template object type.
Definition WsfCommProtocolStack.hpp:78
Definition WsfCommLayerMessage.hpp:36
Definition WsfCommLayerMessage.hpp:24
Definition WsfComm.cpp:34
Function definitions for those who need run-time access to the version.
Definition WsfComm.cpp:32
Copyrights Multiple, All Rights Reserved