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

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

Properly closing sockets when the PolicyEngine? shuts down.

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        //
88        // If we send integer op codes rather than strings, this process will be
89        // MUCH faster since instead of donig string compares we can simply
90        // switch on the integer value...
91        if(strcmp(buffer, "validate_parameters") == 0) {
92            ValidateParameters();
93        }
94        else if(strcmp(buffer, "query_component_type") == 0) {
95            SendComponentType();
96        }
97        else if(strcmp(buffer, "connect_sml") == 0) {
98            /* This command implies that we are disconnecting from the shell and
99             * connecting to a SML component. */
100            char serverName[256];
101            char serverPort[256];
102            // TODO is this going to end up being too slow?
103            memset(serverName, 0, 256);
104            memset(serverPort, 0, 256);
105
106            ReadMessage(commandSocketFD, serverName);
107            ReadMessage(commandSocketFD, serverPort);
108
109            /* Only continue if we are currently connected to a shell. */
110            if(!SML_present) {
111                DeregisterComponent();
112
113                shutdown(commandSocketFD, 2);
114                close(commandSocketFD);
115
116                SML_present = true;
117
118                ConnectToRemoteComponent(serverName, serverPort);
119            }
120        }
121        else if(strcmp(buffer, "disconnect_sml") == 0) {
122            /* This command implies that we are disconnecting from the SML and
123             * connecting to a shell component. */
124            char serverName[256];
125            char serverPort[256];
126            // TODO is this going to end up being too slow?
127            memset(serverName, 0, 256);
128            memset(serverPort, 0, 256);
129
130            ReadMessage(commandSocketFD, serverName);
131            ReadMessage(commandSocketFD, serverPort);
132
133            /* We only want to do this if we are actually connected to an SML
134             * currently. */
135            if(SML_present) {
136                DeregisterServices();
137
138                shutdown(commandSocketFD, 2);
139                close(commandSocketFD);
140
141                SML_present = false;
142
143                ConnectToRemoteComponent(serverName, serverPort);
144            }
145        }
146        else if(strcmp(buffer, "reload_engine_configs") == 0) {
147            ReloadPolicies();
148        }
149        else if(strcmp(buffer, "reset_engine_policy") == 0) {
150            Reset();
151        }
152        else if(strcmp(buffer, "shutdown_engine_policy") == 0) {
153            Shutdown();
154        }
155    }
156}
157
158
159void
160PolicyEngine::Shutdown()
161{
162    if(SML_present)
163        DeregisterServices();
164    else
165        DeregisterComponent();
166
167    // TODO should something else be happening here?
168}
169
170
171void
172PolicyEngine::Reset()
173{
174    LOG("Resetting Policy Engine.\n");
175    SML_present = false;
176    commandSocketFD = -1;
177    LoadPolicies();
178}
179
180
181void
182PolicyEngine::RegisterComponent()
183{
184    SendMessage(commandSocketFD, "register_engine_policy");
185    LOG("Policy Engine:: Registration message sent to shell.\n");
186}
187
188
189void
190PolicyEngine::DeregisterComponent()
191{
192    SendMessage(commandSocketFD, "deregister_engine_policy");
193    LOG("Policy Engine:: Deregistration message sent to shell.\n");
194
195    shutdown(commandSocketFD, 2);
196    close(commandSocketFD);
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    LOG("Policy Engine:: SML socket closed.\n");
224}
225
226
227void
228PolicyEngine::LoadPolicies()
229{
230    LOG("PolicyEngine:: Loading policies.\n");
231}
232
233
234void
235PolicyEngine::ReloadPolicies()
236{
237    LOG("PolicyEngine:: Reloading policies.\n");
238}
239
240
241void
242PolicyEngine::SendPEDecision(struct Parameter pList[], \
243        struct Radio_Info *radio_info, int32_t decision_array[])
244{
245    char var[50];
246 
247    for (size_t i = 0; i < radio_info->numParameters; i++) {
248        sprintf(var, "%i", decision_array[i]);
249        SendMessage(commandSocketFD, var);
250    }
251}
252
253
254void
255PolicyEngine::ValidateParameters()
256{
257    LOG("Policy Engine:: Preparing to receive parameters for validation....\n");
258
259    int32_t decision_array[10];
260    struct Parameter pList[10];
261    struct Radio_Info radio_info;
262
263    if(GetRequest(commandSocketFD, pList, &radio_info)) {
264        LOG("Policy Engine:: Validating Transmission Parameters.\n");
265        for (size_t i = 0; i < radio_info.numParameters; i++)
266            decision_array[i] = 1;
267
268        LOG("Policy Engine:: Sending Policy decision to Server.\n");
269        SendPEDecision(pList, &radio_info, decision_array);
270
271        LOG("Policy Engine:: Policies Validated.\n");
272    }
273    else
274        ERROR(1, "Call to GetRequest in ValidateParameters failed.\n");
275
276}
277
Note: See TracBrowser for help on using the browser.