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 | |
---|
23 | PolicyEngine::PolicyEngine() |
---|
24 | { |
---|
25 | LOG("Creating Policy Engine.\n"); |
---|
26 | SML_present = false; |
---|
27 | commandSocketFD = -1; |
---|
28 | LoadPolicies(); |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | PolicyEngine::~PolicyEngine() |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | PolicyEngine::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 | |
---|
48 | void |
---|
49 | PolicyEngine::SendComponentType() |
---|
50 | { |
---|
51 | SendMessage(commandSocketFD, "response_engine_policy"); |
---|
52 | LOG("Policy Engine responded to GetRemoteComponentType query.\n"); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | void |
---|
57 | PolicyEngine::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 | |
---|
75 | void |
---|
76 | PolicyEngine::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 | |
---|
156 | void |
---|
157 | PolicyEngine::Shutdown() |
---|
158 | { |
---|
159 | if(SML_present) |
---|
160 | DeregisterServices(); |
---|
161 | else |
---|
162 | DeregisterComponent(); |
---|
163 | |
---|
164 | // TODO should something else be happening here? |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | void |
---|
169 | PolicyEngine::Reset() |
---|
170 | { |
---|
171 | LOG("Resetting Policy Engine.\n"); |
---|
172 | |
---|
173 | if(SML_present) |
---|
174 | DeregisterServices(); |
---|
175 | else |
---|
176 | DeregisterComponent(); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | void |
---|
181 | PolicyEngine::RegisterComponent() |
---|
182 | { |
---|
183 | SendMessage(commandSocketFD, "register_engine_policy"); |
---|
184 | LOG("Policy Engine:: Registration message sent to shell.\n"); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | void |
---|
189 | PolicyEngine::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 | |
---|
201 | void |
---|
202 | PolicyEngine::RegisterServices() |
---|
203 | { |
---|
204 | LOG("Policy Engine:: Registering services.\n"); |
---|
205 | |
---|
206 | SendMessage(commandSocketFD, "register_service"); |
---|
207 | SendMessage(commandSocketFD, "policy_geo"); |
---|
208 | |
---|
209 | SendMessage(commandSocketFD, "register_service"); |
---|
210 | SendMessage(commandSocketFD, "policy_time"); |
---|
211 | |
---|
212 | SendMessage(commandSocketFD, "register_service"); |
---|
213 | SendMessage(commandSocketFD, "policy_spectrum"); |
---|
214 | |
---|
215 | SendMessage(commandSocketFD, "register_service"); |
---|
216 | SendMessage(commandSocketFD, "policy_spacial"); |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | void |
---|
221 | PolicyEngine::DeregisterServices() |
---|
222 | { |
---|
223 | LOG("Policy Engine:: Deregistering services.\n"); |
---|
224 | |
---|
225 | SendMessage(commandSocketFD, "deregister_service"); |
---|
226 | SendMessage(commandSocketFD, "policy_geo"); |
---|
227 | |
---|
228 | SendMessage(commandSocketFD, "deregister_service"); |
---|
229 | SendMessage(commandSocketFD, "policy_time"); |
---|
230 | |
---|
231 | SendMessage(commandSocketFD, "deregister_service"); |
---|
232 | SendMessage(commandSocketFD, "policy_spectrum"); |
---|
233 | |
---|
234 | SendMessage(commandSocketFD, "deregister_service"); |
---|
235 | SendMessage(commandSocketFD, "policy_spacial"); |
---|
236 | |
---|
237 | shutdown(commandSocketFD, 2); |
---|
238 | close(commandSocketFD); |
---|
239 | commandSocketFD = -1; |
---|
240 | LOG("Policy Engine:: SML socket closed.\n"); |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | void |
---|
245 | PolicyEngine::LoadPolicies() |
---|
246 | { |
---|
247 | LOG("PolicyEngine:: Loading policies.\n"); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | void |
---|
252 | PolicyEngine::ReloadPolicies() |
---|
253 | { |
---|
254 | LOG("PolicyEngine:: Reloading policies.\n"); |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | void |
---|
259 | PolicyEngine::SendPEDecision(struct Parameter pList[], \ |
---|
260 | struct Radio_Info *radio_info, int32_t decision_array[]) |
---|
261 | { |
---|
262 | char var[50]; |
---|
263 | |
---|
264 | for (size_t i = 0; i < radio_info->numParameters; i++) { |
---|
265 | sprintf(var, "%i", decision_array[i]); |
---|
266 | SendMessage(commandSocketFD, var); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | void |
---|
272 | PolicyEngine::ValidateParameters() |
---|
273 | { |
---|
274 | LOG("Policy Engine:: Preparing to receive parameters for validation....\n"); |
---|
275 | |
---|
276 | int32_t decision_array[10]; |
---|
277 | struct Parameter pList[10]; |
---|
278 | struct Radio_Info radio_info; |
---|
279 | |
---|
280 | if(GetRequest(commandSocketFD, pList, &radio_info)) { |
---|
281 | LOG("Policy Engine:: Validating Transmission Parameters.\n"); |
---|
282 | for (size_t i = 0; i < radio_info.numParameters; i++) |
---|
283 | decision_array[i] = 1; |
---|
284 | |
---|
285 | LOG("Policy Engine:: Sending Policy decision to Server.\n"); |
---|
286 | SendPEDecision(pList, &radio_info, decision_array); |
---|
287 | |
---|
288 | LOG("Policy Engine:: Policies Validated.\n"); |
---|
289 | } |
---|
290 | else |
---|
291 | ERROR(1, "Call to GetRequest in ValidateParameters failed.\n"); |
---|
292 | |
---|
293 | } |
---|
294 | |
---|