root/vtcross/trunk/src/include/vtcross/components.h @ 411

Revision 411, 13.8 KB (checked in by trnewman, 15 years ago)

Adding Apache license information.

Line 
1/*
2 Copyright 2009 Virginia Polytechnic Institute and State University 
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/* This header exports the declarations for all VT-CROSS radio components.  It
18 * contains two pure abstract base classes, Component and Engine; Engine derives
19 * from Component.  All functions contained within the abstract base classes are
20 * dynamically linked and pure, and all child non-abstract classes derive using
21 * private inheritence.  Class functions of the abstract base classes are public
22 * for two reasons: (1) To allow for public/protected inheritence in other
23 * implementations, (2) So that symbolic debuggers can navigate the call tree
24 * for typecasted objects of derivative classes.
25 */
26
27#ifndef COMPONENTS_H
28#define COMPONENTS_H
29
30
31#include <cstring>
32#include <stdint.h>
33#include <string>
34
35#include "containers.h"
36#include "socketcomm.h"
37
38
39/* Component abstract base class that all component classes should inherit from,
40 * including cognitive and policy engines, and the service management layer.
41 * Defines only functions required by all component types.
42 */
43class Component
44{
45    public:
46        /* Asks the component at the passed socket FD for its component type
47         * string.  Note that this implementation is global for all component
48         * types, so is implemented here.  Should a component need to override
49         * it, that is possible via dynamic binding or overloading.
50         */
51        virtual std::string GetRemoteComponentType(int32_t componentSocketFD)
52        {
53            SendMessage(componentSocketFD, "request_component_type");
54
55            char buffer[256];
56            memset(buffer, 0, 256);
57            ReadMessage(componentSocketFD, buffer);
58
59            return std::string(buffer);
60        }
61
62        /* Send an indentfying string for this object's component type in
63         * response to a GetRemoteComponentType query.
64         */
65        virtual void SendComponentType() = 0;
66
67        /* Wait for a command signal containing task instructions.
68         */
69        //virtual void WaitForSignal() = 0;
70
71        /* Completely shutdown the radio and all operations.
72         */
73        virtual void Shutdown() = 0;
74
75        /* Reset the radio and reload all configuration files.
76         *
77         * TODO are we remembering experiences in CEs?
78         */
79        virtual void Reset() = 0;
80
81        /* Register or deregister a component with the primary radio shell.
82         */
83        virtual void RegisterComponent() = 0;
84        virtual void DeregisterComponent() = 0;
85};
86
87
88/* Engine abstract base class from which all engine component types should
89 * inherit (e.g. cognitive and policy engines). Inherits all functions from the
90 * ABC Component publically.
91 */
92class Engine : public Component
93{
94    public:
95        /* Connect to the remote control component, which will always be either
96         * the VTCROSS shell or SML.  Based on the status of the SML_present
97         * bool, this function will also register the component or services.
98         *
99         * TODO I feel like the name of this function could be changed to be a
100         * little more descriptive?
101         */
102        virtual void ConnectToRemoteComponent(const char* serverName, \
103                const char* serverPort, const bool SML) = 0;
104
105        /* Register or deregister services that this engine provides with the
106         * service management layer.
107         */
108        virtual void RegisterServices() = 0;
109        virtual void DeregisterServices() = 0;
110};
111
112
113/* Service Management Layer (SML) class declaration.  The functions listed here
114 * are required by the VTCROSS API for service-oriented VTCROSS radio
115 * architectures.
116 */
117class ServiceManagementLayer : public Component
118{
119    public:
120        ServiceManagementLayer();
121        ~ServiceManagementLayer();
122
123        /* Overloaded constructor that creates an SML and connects it to the
124         * shell with the passed hostname and port.
125         */
126        ServiceManagementLayer(const char* SML_Config, const char* serverName, const char* serverPort, int16_t clientPort);
127
128        /* Connect and register with the shell component at the passed hostname
129         * and port.
130         */
131        void ConnectToShell(const char* serverName, \
132                const char* serverPort);
133        void SendComponentType();
134        void MessageHandler(int32_t ID);
135        void Shutdown();
136        void Reset();
137        void RegisterComponent();
138        void DeregisterComponent();
139
140        /* Starts the SML Server and watches it for incoming messages
141         */
142        void StartSMLServer();
143
144    private:
145        /* Receive the radio configuration settings from the shell and pass them
146         * on to another component.
147         */
148        void TransferRadioConfiguration(int32_t ID);
149
150        /* Receive information regarding a completed 'experience' and pass it on
151         * to the appropriate cognitive engine.
152         */
153        void TransferExperience(int32_t ID);
154       
155        /* Listen for other components registering their available services with
156         * the SML.
157         */
158        void ReceiveServices(int32_t ID);
159        void DeregisterServices(int32_t ID);
160
161        /* Change the active mission of the radio to a new one and adjust radio
162         * behavoir appropriately.
163         */
164        void SetActiveMission();
165
166        void RegisterCognitiveEngine(int32_t ID);
167        void DeregisterCognitiveEngine(int32_t ID);
168
169        /* List all services provided to the radio by registered components.
170         */
171        void ListServices();
172
173        /* Load/Relead the XML configuration file.
174         */
175        void ReloadConfiguration();
176        void LoadConfiguration(const char *SML_Config, Mission* &mList);
177
178        /* Create and initialize the DB to hold the services
179         */
180        void CreateServicesDB();
181        void CreateDataDB();
182
183        void PerformActiveMission();
184        void TransactData(int32_t sourceID);
185
186
187        /* The socket file descriptor information for the shell which this SML
188         * is connected to.
189         */
190        int32_t shellSocketFD;
191        CE_Reg *CE_List;
192        int32_t cogEngSrv;
193        int16_t CEPort;
194        uint16_t numberOfCognitiveEngines;
195        uint32_t Current_ID;
196        Mission *miss;
197        bool CE_Present;
198        int32_t activeMission;
199
200        int16_t SMLport;
201};
202
203
204/* Policy Engine class declaration.  All public functions are inherited from
205 * parent classes.
206 */
207class PolicyEngine : public Engine
208{
209    public:
210        PolicyEngine();
211        ~PolicyEngine();
212
213        /* Overloaded constructor that creates a policy engine object and
214         * connects it to either the shell or an SML, depening on the SML bool.
215         */
216        PolicyEngine(const char* serverName, const char* serverPort, \
217                const bool SML);
218
219        void SendComponentType();
220        void ConnectToRemoteComponent(const char* serverName, \
221                const char* serverPort, const bool SML);
222        void WaitForSignal();
223        void Shutdown();
224        void Reset();
225        void RegisterComponent();
226        void DeregisterComponent();
227
228        void RegisterServices();
229        void DeregisterServices();
230
231    private:
232        /* Parse and load/reload policies into the policy engine.
233         */
234        void LoadPolicies();
235        void ReloadPolicies();
236
237        /* Return a decision made by the policy engine regarding a certain set
238         * of transmission parameters.
239         */
240        void SendPEDecision(struct Parameter pList[], struct Radio_Info *radio_info, \
241                int32_t decision_array[]);
242
243        /* Validate a set of transmission parameters received from the radio.
244         */
245        void ValidateParameters();
246
247        /* The SML_present bool reflects whether or not the remote component
248         * this object is connected to is an SML.  If it isn't, then it must be
249         * a shell.  The socketFD stores the socket file descriptor for this
250         * connection.
251         */
252        bool SML_present;
253        int32_t commandSocketFD;
254};
255
256
257/* Cognitive Engine class declaration.  All public functions are inherited from
258 * parent classes.
259 */
260class CognitiveEngine : public Engine
261{
262    public:
263        CognitiveEngine();
264        ~CognitiveEngine();
265
266        /* Overloaded constructor that creates a cognitive engine object and
267         * connects it to either the shell or an SML, depening on the SML bool.
268         */
269        CognitiveEngine(const char* serverName, const char* serverPort, \
270                const bool SML);
271       
272        void SendComponentType();
273        void ConnectToRemoteComponent(const char* serverName, \
274                const char* serverPort, const bool SML);
275        void WaitForSignal();
276        void Shutdown();
277        void Reset();
278        void RegisterComponent();
279        void DeregisterComponent();
280
281        void RegisterServices();
282        void DeregisterServices();
283
284    private:
285        /* Receive the transmitted radio configuration from the radio itself
286         * (the CE will not always be local to the radio).
287         */
288        void ReceiveRadioConfiguration();
289
290        /* Receive an 'experience' report from the radio.
291         */
292        void ReceiveExperience();
293
294        /* Find the most optimal set of transmission parameters given certain
295         * observables and possibly a service if the SML component is present
296         * and active.
297         */
298        Parameter *GetSolution(Observable *observables, Parameter *currentParameters);
299        Parameter *GetSolution(Observable *observables, Parameter *currentParameters, std::string service);
300
301        /* Receive a feedback from the radio regarding the performance of a
302         * certain set of parameters, possibly associated with a service.
303         *
304         * Feedback is a single set of performance statistics that is achieved
305         * corresponding to a specific set of transmission parameters.  Feedback
306         * helps a Cognitive Engine make better future decisions based upon
307         * more accurate performance statistics.
308         */
309        void ReceiveFeedback(Observable *observables,\
310                Parameter *parameters);
311        void ReceiveFeedback(Observable *observables, \
312                Parameter *parameters, std::string service);
313
314
315                /* BuildCognitiveEngine performs the CE implementation specific work
316                 * that defines the internals of a CE.  For example, a CBR CE engine
317                 * would build the case-base reasoner or create the database, a neural
318                 * network based CE may perform the initial training, a GA based CE
319                 * may build the chromosome structure.
320                 */
321                void BuildCognitiveEngine();
322
323        /* The SML_present bool reflects whether or not the remote component
324         * this object is connected to is an SML.  If it isn't, then it must be
325         * a shell.  The socketFD stores the socket file descriptor for this
326         * connection.
327         */
328        bool SML_present;
329        int32_t commandSocketFD;
330       
331        // TODO Need a description for these fields.  Are these radio utilites,
332        // parameters, and observables global to the whole system?
333        Utility *uList;
334        Parameter *pList;
335        Observable *oList;
336        struct Radio_Info *radioInfo;
337};
338
339/* Cognitive Radio Shell class declaration.
340 */
341class CognitiveRadioShell
342{
343    public:
344        CognitiveRadioShell();
345        ~CognitiveRadioShell();
346
347        /* Overloaded constructor that creates a CR Shell object and loads the
348         * passed radio configuration XML file.
349         */
350        CognitiveRadioShell(const char* radioConfig, int16_t primaryPort, \
351            int16_t policyPort, int16_t commandPort);
352
353        /* Ask for the component type of a remote component via sockets, or
354         * respond to such a query sent to the shell itself.
355         */
356        std::string GetRemoteComponentType(int32_t socketFD);
357        void SendComponentType(int32_t socketFD);
358
359        void Shutdown();
360        void Reset();
361       
362        /* Start all the socket servers */
363        void StartShellServer();
364
365        int32_t LoadRadioConfiguration(const char* radioConfig, Parameter* &pList, \
366            Utility* &uList, Observable* &oList, Radio_Info* radioInfo);
367    private:
368        /* Parse and load/reload policies into the policy engine.
369         */
370        void LoadPolicies();
371        void ReloadPolicies();
372
373        /* Register and Deregister the different components.
374         */
375        void RegisterCognitiveEngine(int32_t socketFD);
376        void DeregisterCognitiveEngine(int32_t socketFD);
377        void RegisterPolicyEngine(int32_t socketFD);
378        void DeregisterPolicyEngine(int32_t socketFD);
379        void RegisterSML(int32_t socketFD);
380        void DeregisterSML(int32_t socketFD);
381       
382        void SetActiveMission(int32_t socketFD);
383
384        /* Handle a message that is received from a component.
385         */
386        int HandleMessage(int32_t socketFD);
387       
388        /* Send optimization request to primary port FD.
389         */
390        void GetOptimalParameters(int32_t socketFD);
391
392        bool SendRadioConfiguration(int32_t socketFD);
393        bool SendRadioExperience(int32_t socketFD);
394
395                bool UpdateParameterPerformance(int32_t socketFD);
396
397        bool SML_present;
398        bool PE_present;
399        bool CE_present;
400       
401        int32_t numberOfCognitiveEngines;
402        int16_t primaryPort;
403        int16_t policyPort;
404        int16_t commandPort;
405
406        int32_t ceSocketFD;
407        int32_t commandSocketFD;
408        int32_t policySocketFD;
409
410        Utility *utils;
411        Parameter *params;
412        Observable *observables;
413        struct Radio_Info *radio_info;
414};
415
416#endif
Note: See TracBrowser for help on using the browser.