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

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

CE_Info didn't make sense anymore since it was really describing the
entire radio; fixed.

RevLine 
[161]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");
[185]26    SML_present = false;
[161]27    LoadPolicies();
28}
29
30
31PolicyEngine::~PolicyEngine()
32{
33}
34
35
[193]36PolicyEngine::PolicyEngine(const char* serverName, const char* serverPort, \
[186]37        const bool SML)
[181]38{
39    LOG("Creating Policy Engine.\n");
40
[186]41    SML_present = SML;
[194]42   
43    ConnectToRemoteComponent(serverName, serverPort);
[181]44
[194]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{
[193]61    commandSocketFD = ClientSocket(serverName, serverPort);
[181]62
[186]63    if(SML_present) {
[188]64        RegisterServices();
[186]65        LOG("Policy Engine connected to SML at %s.\n", serverName);
66    }
67    else {
[188]68        RegisterComponent();
[186]69        LOG("Policy Engine connected to shell at %s.\n", serverName);
70    }
[181]71}
72
73
[161]74void
[188]75PolicyEngine::WaitForSignal()
[161]76{
[194]77    char buffer[256];
[161]78
[194]79    while(true) {
80        memset(buffer, 0, 256);
81       
82        ReadMessage(commandSocketFD, buffer);
[164]83
[194]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);
[161]101
[194]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        }
[161]151    }
152}
153
154
155void
156PolicyEngine::Shutdown()
157{
[185]158    if(SML_present)
[188]159        DeregisterServices();
[186]160    else
[188]161        DeregisterComponent();
[194]162
163    // TODO should something else be happening here?
[161]164}
165
166
167void
168PolicyEngine::Reset()
169{
[186]170    LOG("Resetting Policy Engine.\n");
171    SML_present = false;
[191]172    commandSocketFD = -1;
[186]173    LoadPolicies();
[161]174}
175
176
177void
[188]178PolicyEngine::RegisterComponent()
[161]179{
[191]180    SendMessage(commandSocketFD, "register_engine_policy");
[194]181    LOG("Policy Engine:: Registration message sent to shell.\n");
[181]182
[161]183}
184
185
186void
[188]187PolicyEngine::DeregisterComponent()
[161]188{
[191]189    SendMessage(commandSocketFD, "deregister_engine_policy");
[194]190    LOG("Policy Engine:: Deregistration message sent to shell.\n");
[161]191}
192
193
194void
[188]195PolicyEngine::RegisterServices()
[161]196{
[181]197    LOG("Policy Engine:: Registering services.\n");
[191]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");
[161]202}
203
204
205void
[188]206PolicyEngine::DeregisterServices()
[161]207{
[181]208    LOG("Policy Engine:: Deregistering services.\n");
[191]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");
[161]213}
214
215
216void
217PolicyEngine::LoadPolicies()
218{
219    LOG("PolicyEngine:: Loading policies.\n");
220}
221
222
223void
224PolicyEngine::ReloadPolicies()
225{
[182]226    LOG("PolicyEngine:: Reloading policies.\n");
[161]227}
228
229
230void
[188]231PolicyEngine::SendPEDecision(struct Parameter pList[], \
[195]232        struct Radio_Info *radio_info, int32_t decision_array[])
[161]233{
234    char var[50];
235 
[195]236    for (size_t i = 0; i < radio_info->numParameters; i++) {
[161]237        sprintf(var, "%i", decision_array[i]);
[191]238        SendMessage(commandSocketFD, var);
[161]239    }
240}
241
242
243void
[194]244PolicyEngine::ValidateParameters()
[161]245{
[194]246    LOG("Policy Engine:: Preparing to receive parameters for validation....\n");
247
248    int32_t decision_array[10];
249    struct Parameter pList[10];
[195]250    struct Radio_Info radio_info;
[194]251
[195]252    if(GetRequest(commandSocketFD, pList, &radio_info)) {
[194]253        LOG("Policy Engine:: Validating Transmission Parameters.\n");
[195]254        for (size_t i = 0; i < radio_info.numParameters; i++)
[194]255            decision_array[i] = 1;
256
257        LOG("Policy Engine:: Sending Policy decision to Server.\n");
[195]258        SendPEDecision(pList, &radio_info, decision_array);
[194]259
260        LOG("Policy Engine:: Policies Validated.\n");
261    }
262    else
263        ERROR(1, "Call to GetRequest in ValidateParameters failed.\n");
264
[161]265}
266
Note: See TracBrowser for help on using the browser.