root/vtcross/trunk/src/policy_engines/PolicyEngine.cpp @ 194

Revision 194, 6.4 KB (checked in by bhilburn, 15 years ago)

Finished implementing basic structure of WaitForSignal?, added new
functions to components to allow proper response to component type
queries, modularized the connect & register step for engines, and added
a few TODOs in areas that need review. This is an abnormally large
commit.

Line 
1/* Virginia Tech Cognitive Radio Open Source Systems
2 * Virginia Tech, 2009
3 *
4 * LICENSE INFORMATION GOES HERE
5 */
6
7/* DESCRIPTION OF FILE.
8 */
9
10
11#include <cstdlib>
12#include <cstring>
13#include <stdint.h>
14
15#include "vtcross/common.h"
16#include "vtcross/components.h"
17#include "vtcross/containers.h"
18#include "vtcross/debug.h"
19#include "vtcross/error.h"
20#include "vtcross/socketcomm.h"
21
22
23PolicyEngine::PolicyEngine()
24{
25    LOG("Creating Policy Engine.\n");
26    SML_present = false;
27    LoadPolicies();
28}
29
30
31PolicyEngine::~PolicyEngine()
32{
33}
34
35
36PolicyEngine::PolicyEngine(const char* serverName, const char* serverPort, \
37        const bool SML)
38{
39    LOG("Creating Policy Engine.\n");
40
41    SML_present = SML;
42   
43    ConnectToRemoteComponent(serverName, serverPort);
44
45    LoadPolicies();
46}
47
48
49void
50PolicyEngine::SendComponentType()
51{
52    SendMessage(commandSocketFD, "response_engine_policy");
53    LOG("Policy Engine responded to GetRemoteComponentType query.\n");
54}
55
56
57void
58PolicyEngine::ConnectToRemoteComponent(const char* serverName, \
59        const char* serverPort)
60{
61    commandSocketFD = ClientSocket(serverName, serverPort);
62
63    if(SML_present) {
64        RegisterServices();
65        LOG("Policy Engine connected to SML at %s.\n", serverName);
66    }
67    else {
68        RegisterComponent();
69        LOG("Policy Engine connected to shell at %s.\n", serverName);
70    }
71}
72
73
74void
75PolicyEngine::WaitForSignal()
76{
77    char buffer[256];
78
79    while(true) {
80        memset(buffer, 0, 256);
81       
82        ReadMessage(commandSocketFD, buffer);
83
84        // TODO this is ugly... is there a better way? Doesn't strcmp compare the
85        // whole string?  We only need to compare until we find a single different
86        // byte...
87        if(strcmp(buffer, "validate_parameters") == 0) {
88            ValidateParameters();
89        }
90        else if(strcmp(buffer, "query_component_type") == 0) {
91            SendComponentType();
92        }
93        else if(strcmp(buffer, "connect_sml") == 0) {
94            /* This command implies that we are disconnecting from the shell and
95             * connecting to a SML component. */
96            char serverName[256];
97            char serverPort[256];
98            // TODO is this going to end up being too slow?
99            memset(serverName, 0, 256);
100            memset(serverPort, 0, 256);
101
102            ReadMessage(commandSocketFD, serverName);
103            ReadMessage(commandSocketFD, serverPort);
104
105            /* Only continue if we are currently connected to a shell. */
106            if(!SML_present) {
107                DeregisterComponent();
108
109                shutdown(commandSocketFD, 2);
110                close(commandSocketFD);
111
112                SML_present = true;
113
114                ConnectToRemoteComponent(serverName, serverPort);
115            }
116        }
117        else if(strcmp(buffer, "disconnect_sml") == 0) {
118            /* This command implies that we are disconnecting from the SML and
119             * connecting to a shell component. */
120            char serverName[256];
121            char serverPort[256];
122            // TODO is this going to end up being too slow?
123            memset(serverName, 0, 256);
124            memset(serverPort, 0, 256);
125
126            ReadMessage(commandSocketFD, serverName);
127            ReadMessage(commandSocketFD, serverPort);
128
129            /* We only want to do this if we are actually connected to an SML
130             * currently. */
131            if(SML_present) {
132                DeregisterServices();
133
134                shutdown(commandSocketFD, 2);
135                close(commandSocketFD);
136
137                SML_present = false;
138
139                ConnectToRemoteComponent(serverName, serverPort);
140            }
141        }
142        else if(strcmp(buffer, "relead_engine_configs") == 0) {
143            ReloadPolicies();
144        }
145        else if(strcmp(buffer, "reset_engine_policy") == 0) {
146            Reset();
147        }
148        else if(strcmp(buffer, "shutdown_engine_policy") == 0) {
149            Shutdown();
150        }
151    }
152}
153
154
155void
156PolicyEngine::Shutdown()
157{
158    if(SML_present)
159        DeregisterServices();
160    else
161        DeregisterComponent();
162
163    // TODO should something else be happening here?
164}
165
166
167void
168PolicyEngine::Reset()
169{
170    LOG("Resetting Policy Engine.\n");
171    SML_present = false;
172    commandSocketFD = -1;
173    LoadPolicies();
174}
175
176
177void
178PolicyEngine::RegisterComponent()
179{
180    SendMessage(commandSocketFD, "register_engine_policy");
181    LOG("Policy Engine:: Registration message sent to shell.\n");
182
183}
184
185
186void
187PolicyEngine::DeregisterComponent()
188{
189    SendMessage(commandSocketFD, "deregister_engine_policy");
190    LOG("Policy Engine:: Deregistration message sent to shell.\n");
191}
192
193
194void
195PolicyEngine::RegisterServices()
196{
197    LOG("Policy Engine:: Registering services.\n");
198    SendMessage(commandSocketFD, "register_service_pe_geo");
199    SendMessage(commandSocketFD, "register_service_pe_time");
200    SendMessage(commandSocketFD, "register_service_pe_spectrum");
201    SendMessage(commandSocketFD, "register_service_pe_spacial");
202}
203
204
205void
206PolicyEngine::DeregisterServices()
207{
208    LOG("Policy Engine:: Deregistering services.\n");
209    SendMessage(commandSocketFD, "deregister_service_pe_geo");
210    SendMessage(commandSocketFD, "deregister_service_pe_time");
211    SendMessage(commandSocketFD, "deregister_service_pe_spectrum");
212    SendMessage(commandSocketFD, "deregister_service_pe_spacial");
213}
214
215
216void
217PolicyEngine::LoadPolicies()
218{
219    LOG("PolicyEngine:: Loading policies.\n");
220}
221
222
223void
224PolicyEngine::ReloadPolicies()
225{
226    LOG("PolicyEngine:: Reloading policies.\n");
227}
228
229
230void
231PolicyEngine::SendPEDecision(struct Parameter pList[], \
232        struct CE_Info *ce_info, int32_t decision_array[])
233{
234    char var[50];
235 
236    for (size_t i = 0; i < ce_info->numParameters; i++) {
237        sprintf(var, "%i", decision_array[i]);
238        SendMessage(commandSocketFD, var);
239    }
240}
241
242
243void
244PolicyEngine::ValidateParameters()
245{
246    LOG("Policy Engine:: Preparing to receive parameters for validation....\n");
247
248    int32_t decision_array[10];
249    struct Parameter pList[10];
250    struct CE_Info ce_info;
251
252    if(GetRequest(commandSocketFD, pList, &ce_info)) {
253        LOG("Policy Engine:: Validating Transmission Parameters.\n");
254        for (size_t i = 0; i < ce_info.numParameters; i++)
255            decision_array[i] = 1;
256
257        LOG("Policy Engine:: Sending Policy decision to Server.\n");
258        SendPEDecision(pList, &ce_info, decision_array);
259
260        LOG("Policy Engine:: Policies Validated.\n");
261    }
262    else
263        ERROR(1, "Call to GetRequest in ValidateParameters failed.\n");
264
265}
266
Note: See TracBrowser for help on using the browser.