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

Revision 202, 7.0 KB (checked in by bhilburn, 15 years ago)

Wrote the policy engine's reset function and properly initialized the
socket in the default constructor.

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