Merge pull request #5 from intothevoid/master
[KDIS.git] / KDIS / KDIS / Extras / PDU_Factory.h
blob42aaf0a396d694ddc1abf3bcd87cac9495c5d063
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
30 /********************************************************************
31 class: PDU_Factory
32 created: 06/12/2008
33 author: Karl Jones
35 purpose: Using a factory design pattern to decode a data stream to
36 the correct PDU type.
37 *********************************************************************/
39 #pragma once
41 #include <memory>
42 #include <vector>
43 #include "./../PDU/Header.h"
44 #include "./PDU_Factory_Filters.h"
46 namespace KDIS {
47 namespace UTILS {
49 class KDIS_EXPORT PDU_Factory
51 protected:
53 std::vector<PDU_Factory_Filter*> m_vFilters;
55 //************************************
56 // FullName: KDIS::UTILS::PDU_Factory::applyFiltersBeforeDecodingPDUBody
57 // Description: Applies the filter/s to the PDU header before the body is decoded.
58 // Parameter: const Header * H
59 //************************************
60 KBOOL applyFiltersBeforeDecodingPDUBody( const KDIS::PDU::Header * H );
62 //************************************
63 // FullName: KDIS::UTILS::PDU_Factory::applyFilters
64 // Description: Applies the filter/s to the PDU and returns a NULL
65 // pointer if the PDU does not pass all filters.
66 // Parameter: Header * H
67 //************************************
68 std::unique_ptr<KDIS::PDU::Header> applyFilters( KDIS::PDU::Header * H );
70 public:
72 PDU_Factory();
74 ~PDU_Factory();
76 //************************************
77 // FullName: KDIS::UTILS::PDU_Factory::AddFilter
78 // Description: Add a filter that will be applied to all PDU's.
79 // Note: All filters will be automatically deleted when the PDU factory is deleted.
80 // Parameter: PDU_Factory_Filter * F
81 //************************************
82 void AddFilter( PDU_Factory_Filter * F );
84 //************************************
85 // FullName: KDIS::UTILS::PDU_Factory::RemoveFilter
86 // Description: Remove a filter.
87 // Parameter: PDU_Factory_Filters * F
88 //************************************
89 void RemoveFilter( PDU_Factory_Filter * F );
91 //************************************
92 // FullName: KDIS::UTILS::PDU_Factory::Decode
93 // Description: Converts a stream of OCTETS into the correct PDU type.
94 // If the PDU type is unknown or not currently
95 // implemented in KDIS a NULL unique_ptr is returned.
96 // Parameter: KOCTET * Buffer
97 // Parameter: KUINT16 BufferSize
98 //************************************
99 virtual std::unique_ptr<KDIS::PDU::Header> Decode( KOCTET * Buffer, KUINT16 BufferSize );
101 //************************************
102 // FullName: KDIS::UTILS::PDU_Factory::Decode
103 // Description: Converts data stream into the correct PDU type.
104 // If the PDU type is unknown or not currently
105 // implemented a NULL unique_ptr is returned.
106 // The full PDU data can be retrieved by casting the header to the relevant PDU.
107 // For example:
108 // unique_ptr<Header> pHeader = Factory.Decode(cBuffer, ui32Recv);
109 // if (pHeader.get())
110 // {
111 // if (pHeader->GetPDUType() == Entity_State_PDU_Type)
112 // {
113 // Entity_State_PDU* entityState = dynamic_cast<Entity_State_PDU*>(pHeader.get());
114 // cout << "Entity Force: " << entityState->GetForceID() << endl;
115 // }
116 // }
117 // Parameter: KDataStream & Stream
118 //************************************
119 virtual std::unique_ptr<KDIS::PDU::Header> Decode( KDataStream & Stream );
121 //************************************
122 // FullName: KDIS::UTILS::PDU_Factory::Decode
123 // Description: Converts data stream into the correct PDU.
124 // This version takes a known PDU and decodes just the body.
125 // Note: If you wanted to add support for your own PDU this would be a great
126 // place to add it, just override this function and check for your PDU first,
127 // if the PDU is not yours then call the parent function.
128 // The full PDU data can be retrieved by casting the header to the relevant PDU.
129 // For example:
130 // unique_ptr<Header> pHeader = Factory.Decode(cBuffer, ui32Recv);
131 // if (pHeader.get())
132 // {
133 // if (pHeader->GetPDUType() == Entity_State_PDU_Type)
134 // {
135 // Entity_State_PDU* entityState = dynamic_cast<Entity_State_PDU*>(pHeader.get());
136 // cout << "Entity Force: " << entityState->GetForceID() << endl;
137 // }
138 // }
139 // Parameter: const Header & H
140 // Parameter: KDataStream & Stream
141 //************************************
142 virtual std::unique_ptr<KDIS::PDU::Header> Decode( const KDIS::PDU::Header & H, KDataStream & Stream );
145 } // END namespace UTILS
146 } // END namespace KDIS