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

Revision 411, 7.7 KB (checked in by trnewman, 15 years ago)

Adding Apache license information.

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