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

Revision 203, 6.9 KB (checked in by bhilburn, 15 years ago)

Making ConnectToRemoteComponent? an engine function since the SML doesn't
need it, adding a bool representing presence of SML, updating
PolicyEngine? to reflect changes.

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    ConnectToRemoteComponent(serverName, serverPort, SML);
43
44    LoadPolicies();
45}
46
47
48void
49PolicyEngine::SendComponentType()
50{
51    SendMessage(commandSocketFD, "response_engine_policy");
52    LOG("Policy Engine responded to GetRemoteComponentType query.\n");
53}
54
55
56void
57PolicyEngine::ConnectToRemoteComponent(const char* serverName, \
58        const char* serverPort, const bool SML)
59{
60    commandSocketFD = ClientSocket(serverName, serverPort);
61
62    SML_present = SML;
63
64    if(SML) {
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                ConnectToRemoteComponent(serverName, serverPort, true);
118            }
119        }
120        else if(strcmp(buffer, "disconnect_sml") == 0) {
121            /* This command implies that we are disconnecting from the SML and
122             * connecting to a shell component. */
123            char serverName[256];
124            char serverPort[256];
125            // TODO is this going to end up being too slow?
126            memset(serverName, 0, 256);
127            memset(serverPort, 0, 256);
128
129            ReadMessage(commandSocketFD, serverName);
130            ReadMessage(commandSocketFD, serverPort);
131
132            /* We only want to do this if we are actually connected to an SML
133             * currently. */
134            if(SML_present) {
135                DeregisterServices();
136
137                shutdown(commandSocketFD, 2);
138                close(commandSocketFD);
139
140                ConnectToRemoteComponent(serverName, serverPort, false);
141            }
142        }
143        else if(strcmp(buffer, "reload_engine_configs") == 0) {
144            ReloadPolicies();
145        }
146        else if(strcmp(buffer, "reset_engine_policy") == 0) {
147            Reset();
148        }
149        else if(strcmp(buffer, "shutdown_engine_policy") == 0) {
150            Shutdown();
151        }
152    }
153}
154
155
156void
157PolicyEngine::Shutdown()
158{
159    if(SML_present)
160        DeregisterServices();
161    else
162        DeregisterComponent();
163
164    // TODO should something else be happening here?
165}
166
167
168void
169PolicyEngine::Reset()
170{
171    LOG("Resetting Policy Engine.\n");
172
173    if(SML_present)
174        DeregisterServices();
175    else
176        DeregisterComponent();
177}
178
179
180void
181PolicyEngine::RegisterComponent()
182{
183    SendMessage(commandSocketFD, "register_engine_policy");
184    LOG("Policy Engine:: Registration message sent to shell.\n");
185}
186
187
188void
189PolicyEngine::DeregisterComponent()
190{
191    SendMessage(commandSocketFD, "deregister_engine_policy");
192    LOG("Policy Engine:: Deregistration message sent to shell.\n");
193
194    shutdown(commandSocketFD, 2);
195    close(commandSocketFD);
196    commandSocketFD = -1;
197    LOG("Policy Engine:: Shell socket closed.\n");
198}
199
200
201void
202PolicyEngine::RegisterServices()
203{
204    LOG("Policy Engine:: Registering services.\n");
205    SendMessage(commandSocketFD, "register_service_pe_geo");
206    SendMessage(commandSocketFD, "register_service_pe_time");
207    SendMessage(commandSocketFD, "register_service_pe_spectrum");
208    SendMessage(commandSocketFD, "register_service_pe_spacial");
209}
210
211
212void
213PolicyEngine::DeregisterServices()
214{
215    LOG("Policy Engine:: Deregistering services.\n");
216    SendMessage(commandSocketFD, "deregister_service_pe_geo");
217    SendMessage(commandSocketFD, "deregister_service_pe_time");
218    SendMessage(commandSocketFD, "deregister_service_pe_spectrum");
219    SendMessage(commandSocketFD, "deregister_service_pe_spacial");
220
221    shutdown(commandSocketFD, 2);
222    close(commandSocketFD);
223    commandSocketFD = -1;
224    LOG("Policy Engine:: SML socket closed.\n");
225}
226
227
228void
229PolicyEngine::LoadPolicies()
230{
231    LOG("PolicyEngine:: Loading policies.\n");
232}
233
234
235void
236PolicyEngine::ReloadPolicies()
237{
238    LOG("PolicyEngine:: Reloading policies.\n");
239}
240
241
242void
243PolicyEngine::SendPEDecision(struct Parameter pList[], \
244        struct Radio_Info *radio_info, int32_t decision_array[])
245{
246    char var[50];
247 
248    for (size_t i = 0; i < radio_info->numParameters; i++) {
249        sprintf(var, "%i", decision_array[i]);
250        SendMessage(commandSocketFD, var);
251    }
252}
253
254
255void
256PolicyEngine::ValidateParameters()
257{
258    LOG("Policy Engine:: Preparing to receive parameters for validation....\n");
259
260    int32_t decision_array[10];
261    struct Parameter pList[10];
262    struct Radio_Info radio_info;
263
264    if(GetRequest(commandSocketFD, pList, &radio_info)) {
265        LOG("Policy Engine:: Validating Transmission Parameters.\n");
266        for (size_t i = 0; i < radio_info.numParameters; i++)
267            decision_array[i] = 1;
268
269        LOG("Policy Engine:: Sending Policy decision to Server.\n");
270        SendPEDecision(pList, &radio_info, decision_array);
271
272        LOG("Policy Engine:: Policies Validated.\n");
273    }
274    else
275        ERROR(1, "Call to GetRequest in ValidateParameters failed.\n");
276
277}
278
Note: See TracBrowser for help on using the browser.