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

Revision 521, 7.8 KB (checked in by bhilburn, 15 years ago)

Added some more doxygen docs at the top of files.

Line 
1/*
2 Copyright 2009 Virginia Polytechnic Institute and State University 
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/*! This is a reference implementation for the CROSS Policy Engine.
18 *
19 * Note that this implementation is NOT complete.
20 */
21
22
23#include <cstdlib>
24#include <cstring>
25#include <stdint.h>
26
27#include "vtcross/common.h"
28#include "vtcross/containers.h"
29#include "vtcross/debug.h"
30#include "vtcross/error.h"
31#include "vtcross/policy_engine.h"
32#include "vtcross/socketcomm.h"
33
34
35PolicyEngine::PolicyEngine()
36{
37    LOG("Creating Policy Engine.\n");
38    SML_present = false;
39    commandSocketFD = -1;
40    LoadPolicies();
41}
42
43
44PolicyEngine::~PolicyEngine()
45{
46}
47
48
49PolicyEngine::PolicyEngine(const char* serverName, const char* serverPort, \
50        const bool SML)
51{
52    LOG("Creating Policy Engine.\n");
53
54    ConnectToRemoteComponent(serverName, serverPort, SML);
55
56    LoadPolicies();
57}
58
59
60void
61PolicyEngine::SendComponentType()
62{
63    SendMessage(commandSocketFD, "response_engine_policy");
64    LOG("Policy Engine responded to GetRemoteComponentType query.\n");
65}
66
67
68void
69PolicyEngine::ConnectToRemoteComponent(const char* serverName, \
70        const char* serverPort, const bool SML)
71{
72    commandSocketFD = ClientSocket(serverName, serverPort);
73
74    SML_present = SML;
75
76    if(SML) {
77        RegisterServices();
78        LOG("Policy Engine connected to SML at %s.\n", serverName);
79    }
80    else {
81        RegisterComponent();
82        LOG("Policy Engine connected to shell at %s.\n", serverName);
83    }
84}
85
86
87void
88PolicyEngine::WaitForSignal()
89{
90    char buffer[256];
91
92    while(true) {
93        memset(buffer, 0, 256);
94       
95        ReadMessage(commandSocketFD, buffer);
96
97        // TODO this is ugly... is there a better way? Doesn't strcmp compare the
98        // whole string?  We only need to compare until we find a single different
99        // byte...
100        //
101        // If we send integer op codes rather than strings, this process will be
102        // MUCH faster since instead of donig string compares we can simply
103        // switch on the integer value...
104        if(strcmp(buffer, "validate_parameters") == 0) {
105            ValidateParameters();
106        }
107        else if(strcmp(buffer, "query_component_type") == 0) {
108            SendComponentType();
109        }
110        else if(strcmp(buffer, "connect_sml") == 0) {
111            /* This command implies that we are disconnecting from the shell and
112             * connecting to a SML component. */
113            char serverName[256];
114            char serverPort[256];
115            // TODO is this going to end up being too slow?
116            memset(serverName, 0, 256);
117            memset(serverPort, 0, 256);
118
119            ReadMessage(commandSocketFD, serverName);
120            ReadMessage(commandSocketFD, serverPort);
121
122            /* Only continue if we are currently connected to a shell. */
123            if(!SML_present) {
124                DeregisterComponent();
125
126                shutdown(commandSocketFD, 2);
127                close(commandSocketFD);
128
129                ConnectToRemoteComponent(serverName, serverPort, true);
130            }
131        }
132        else if(strcmp(buffer, "disconnect_sml") == 0) {
133            /* This command implies that we are disconnecting from the SML and
134             * connecting to a shell component. */
135            char serverName[256];
136            char serverPort[256];
137            // TODO is this going to end up being too slow?
138            memset(serverName, 0, 256);
139            memset(serverPort, 0, 256);
140
141            ReadMessage(commandSocketFD, serverName);
142            ReadMessage(commandSocketFD, serverPort);
143
144            /* We only want to do this if we are actually connected to an SML
145             * currently. */
146            if(SML_present) {
147                DeregisterServices();
148
149                shutdown(commandSocketFD, 2);
150                close(commandSocketFD);
151
152                ConnectToRemoteComponent(serverName, serverPort, false);
153            }
154        }
155        else if(strcmp(buffer, "reload_engine_configs") == 0) {
156            ReloadPolicies();
157        }
158        else if(strcmp(buffer, "reset_engine_policy") == 0) {
159            Reset();
160        }
161        else if(strcmp(buffer, "shutdown_engine_policy") == 0) {
162            Shutdown();
163        }
164    }
165}
166
167
168void
169PolicyEngine::Shutdown()
170{
171    if(SML_present)
172        DeregisterServices();
173    else
174        DeregisterComponent();
175
176    // TODO should something else be happening here?
177}
178
179
180void
181PolicyEngine::Reset()
182{
183    LOG("Resetting Policy Engine.\n");
184
185    if(SML_present)
186        DeregisterServices();
187    else
188        DeregisterComponent();
189}
190
191
192void
193PolicyEngine::RegisterComponent()
194{
195    SendMessage(commandSocketFD, "register_engine_policy");
196    LOG("Policy Engine:: Registration message sent to shell.\n");
197}
198
199
200void
201PolicyEngine::DeregisterComponent()
202{
203    SendMessage(commandSocketFD, "deregister_engine_policy");
204    LOG("Policy Engine:: Deregistration message sent to shell.\n");
205
206    shutdown(commandSocketFD, 2);
207    close(commandSocketFD);
208    commandSocketFD = -1;
209    LOG("Policy Engine:: Shell socket closed.\n");
210}
211
212
213void
214PolicyEngine::RegisterServices()
215{
216    LOG("Policy Engine:: Registering services.\n");
217
218    SendMessage(commandSocketFD, "register_service");
219    SendMessage(commandSocketFD, "policy_geo");
220
221    SendMessage(commandSocketFD, "register_service");
222    SendMessage(commandSocketFD, "policy_time");
223
224    SendMessage(commandSocketFD, "register_service");
225    SendMessage(commandSocketFD, "policy_spectrum");
226
227    SendMessage(commandSocketFD, "register_service");
228    SendMessage(commandSocketFD, "policy_spacial");
229}
230
231
232void
233PolicyEngine::DeregisterServices()
234{
235    LOG("Policy Engine:: Deregistering services.\n");
236
237    SendMessage(commandSocketFD, "deregister_service");
238    SendMessage(commandSocketFD, "policy_geo");
239
240    SendMessage(commandSocketFD, "deregister_service");
241    SendMessage(commandSocketFD, "policy_time");
242
243    SendMessage(commandSocketFD, "deregister_service");
244    SendMessage(commandSocketFD, "policy_spectrum");
245
246    SendMessage(commandSocketFD, "deregister_service");
247    SendMessage(commandSocketFD, "policy_spacial");
248
249    shutdown(commandSocketFD, 2);
250    close(commandSocketFD);
251    commandSocketFD = -1;
252    LOG("Policy Engine:: SML socket closed.\n");
253}
254
255
256void
257PolicyEngine::LoadPolicies()
258{
259    LOG("PolicyEngine:: Loading policies.\n");
260}
261
262
263void
264PolicyEngine::ReloadPolicies()
265{
266    LOG("PolicyEngine:: Reloading policies.\n");
267}
268
269
270void
271PolicyEngine::SendPEDecision(struct Parameter pList[], \
272        struct Radio_Info *radio_info, int32_t decision_array[])
273{
274    char var[50];
275 
276    for (size_t i = 0; i < radio_info->numParameters; i++) {
277        sprintf(var, "%i", decision_array[i]);
278        SendMessage(commandSocketFD, var);
279    }
280}
281
282
283void
284PolicyEngine::ValidateParameters()
285{
286    LOG("Policy Engine:: Preparing to receive parameters for validation....\n");
287
288    int32_t decision_array[10];
289    struct Parameter pList[10];
290    struct Radio_Info radio_info;
291
292    if(GetRequest(commandSocketFD, pList, &radio_info)) {
293        LOG("Policy Engine:: Validating Transmission Parameters.\n");
294        for (size_t i = 0; i < radio_info.numParameters; i++)
295            decision_array[i] = 1;
296
297        LOG("Policy Engine:: Sending Policy decision to Server.\n");
298        SendPEDecision(pList, &radio_info, decision_array);
299
300        LOG("Policy Engine:: Policies Validated.\n");
301    }
302    else
303        ERROR(1, "Call to GetRequest in ValidateParameters failed.\n");
304
305}
306
Note: See TracBrowser for help on using the browser.