[411] | 1 | /* |
---|
| 2 | Copyright 2009 Virginia Polytechnic Institute and State University |
---|
[161] | 3 | |
---|
[411] | 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 | |
---|
[161] | 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/containers.h" |
---|
| 27 | #include "vtcross/debug.h" |
---|
| 28 | #include "vtcross/error.h" |
---|
[465] | 29 | #include "vtcross/policy_engine.h" |
---|
[161] | 30 | #include "vtcross/socketcomm.h" |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | PolicyEngine::PolicyEngine() |
---|
| 34 | { |
---|
| 35 | LOG("Creating Policy Engine.\n"); |
---|
[185] | 36 | SML_present = false; |
---|
[202] | 37 | commandSocketFD = -1; |
---|
[161] | 38 | LoadPolicies(); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | PolicyEngine::~PolicyEngine() |
---|
| 43 | { |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
[193] | 47 | PolicyEngine::PolicyEngine(const char* serverName, const char* serverPort, \ |
---|
[186] | 48 | const bool SML) |
---|
[181] | 49 | { |
---|
| 50 | LOG("Creating Policy Engine.\n"); |
---|
| 51 | |
---|
[203] | 52 | ConnectToRemoteComponent(serverName, serverPort, SML); |
---|
[181] | 53 | |
---|
[194] | 54 | LoadPolicies(); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | void |
---|
| 59 | PolicyEngine::SendComponentType() |
---|
| 60 | { |
---|
| 61 | SendMessage(commandSocketFD, "response_engine_policy"); |
---|
| 62 | LOG("Policy Engine responded to GetRemoteComponentType query.\n"); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | void |
---|
| 67 | PolicyEngine::ConnectToRemoteComponent(const char* serverName, \ |
---|
[203] | 68 | const char* serverPort, const bool SML) |
---|
[194] | 69 | { |
---|
[193] | 70 | commandSocketFD = ClientSocket(serverName, serverPort); |
---|
[181] | 71 | |
---|
[203] | 72 | SML_present = SML; |
---|
| 73 | |
---|
| 74 | if(SML) { |
---|
[188] | 75 | RegisterServices(); |
---|
[186] | 76 | LOG("Policy Engine connected to SML at %s.\n", serverName); |
---|
| 77 | } |
---|
| 78 | else { |
---|
[188] | 79 | RegisterComponent(); |
---|
[186] | 80 | LOG("Policy Engine connected to shell at %s.\n", serverName); |
---|
| 81 | } |
---|
[181] | 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
[161] | 85 | void |
---|
[188] | 86 | PolicyEngine::WaitForSignal() |
---|
[161] | 87 | { |
---|
[194] | 88 | char buffer[256]; |
---|
[161] | 89 | |
---|
[194] | 90 | while(true) { |
---|
| 91 | memset(buffer, 0, 256); |
---|
| 92 | |
---|
| 93 | ReadMessage(commandSocketFD, buffer); |
---|
[164] | 94 | |
---|
[194] | 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... |
---|
[198] | 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... |
---|
[194] | 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); |
---|
[161] | 116 | |
---|
[194] | 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 | |
---|
[203] | 127 | ConnectToRemoteComponent(serverName, serverPort, true); |
---|
[194] | 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 | |
---|
[203] | 150 | ConnectToRemoteComponent(serverName, serverPort, false); |
---|
[194] | 151 | } |
---|
| 152 | } |
---|
[197] | 153 | else if(strcmp(buffer, "reload_engine_configs") == 0) { |
---|
[194] | 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 | } |
---|
[161] | 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | void |
---|
| 167 | PolicyEngine::Shutdown() |
---|
| 168 | { |
---|
[185] | 169 | if(SML_present) |
---|
[188] | 170 | DeregisterServices(); |
---|
[186] | 171 | else |
---|
[188] | 172 | DeregisterComponent(); |
---|
[194] | 173 | |
---|
| 174 | // TODO should something else be happening here? |
---|
[161] | 175 | } |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | void |
---|
| 179 | PolicyEngine::Reset() |
---|
| 180 | { |
---|
[186] | 181 | LOG("Resetting Policy Engine.\n"); |
---|
[202] | 182 | |
---|
| 183 | if(SML_present) |
---|
| 184 | DeregisterServices(); |
---|
| 185 | else |
---|
| 186 | DeregisterComponent(); |
---|
[161] | 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | void |
---|
[188] | 191 | PolicyEngine::RegisterComponent() |
---|
[161] | 192 | { |
---|
[191] | 193 | SendMessage(commandSocketFD, "register_engine_policy"); |
---|
[194] | 194 | LOG("Policy Engine:: Registration message sent to shell.\n"); |
---|
[161] | 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | void |
---|
[188] | 199 | PolicyEngine::DeregisterComponent() |
---|
[161] | 200 | { |
---|
[191] | 201 | SendMessage(commandSocketFD, "deregister_engine_policy"); |
---|
[194] | 202 | LOG("Policy Engine:: Deregistration message sent to shell.\n"); |
---|
[201] | 203 | |
---|
| 204 | shutdown(commandSocketFD, 2); |
---|
| 205 | close(commandSocketFD); |
---|
[203] | 206 | commandSocketFD = -1; |
---|
[201] | 207 | LOG("Policy Engine:: Shell socket closed.\n"); |
---|
[161] | 208 | } |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | void |
---|
[188] | 212 | PolicyEngine::RegisterServices() |
---|
[161] | 213 | { |
---|
[181] | 214 | LOG("Policy Engine:: Registering services.\n"); |
---|
[205] | 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"); |
---|
[161] | 227 | } |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | void |
---|
[188] | 231 | PolicyEngine::DeregisterServices() |
---|
[161] | 232 | { |
---|
[181] | 233 | LOG("Policy Engine:: Deregistering services.\n"); |
---|
[201] | 234 | |
---|
[205] | 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 | |
---|
[201] | 247 | shutdown(commandSocketFD, 2); |
---|
| 248 | close(commandSocketFD); |
---|
[203] | 249 | commandSocketFD = -1; |
---|
[201] | 250 | LOG("Policy Engine:: SML socket closed.\n"); |
---|
[161] | 251 | } |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | void |
---|
| 255 | PolicyEngine::LoadPolicies() |
---|
| 256 | { |
---|
| 257 | LOG("PolicyEngine:: Loading policies.\n"); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | void |
---|
| 262 | PolicyEngine::ReloadPolicies() |
---|
| 263 | { |
---|
[182] | 264 | LOG("PolicyEngine:: Reloading policies.\n"); |
---|
[161] | 265 | } |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | void |
---|
[188] | 269 | PolicyEngine::SendPEDecision(struct Parameter pList[], \ |
---|
[195] | 270 | struct Radio_Info *radio_info, int32_t decision_array[]) |
---|
[161] | 271 | { |
---|
| 272 | char var[50]; |
---|
| 273 | |
---|
[195] | 274 | for (size_t i = 0; i < radio_info->numParameters; i++) { |
---|
[161] | 275 | sprintf(var, "%i", decision_array[i]); |
---|
[191] | 276 | SendMessage(commandSocketFD, var); |
---|
[161] | 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | void |
---|
[194] | 282 | PolicyEngine::ValidateParameters() |
---|
[161] | 283 | { |
---|
[194] | 284 | LOG("Policy Engine:: Preparing to receive parameters for validation....\n"); |
---|
| 285 | |
---|
| 286 | int32_t decision_array[10]; |
---|
| 287 | struct Parameter pList[10]; |
---|
[195] | 288 | struct Radio_Info radio_info; |
---|
[194] | 289 | |
---|
[195] | 290 | if(GetRequest(commandSocketFD, pList, &radio_info)) { |
---|
[194] | 291 | LOG("Policy Engine:: Validating Transmission Parameters.\n"); |
---|
[195] | 292 | for (size_t i = 0; i < radio_info.numParameters; i++) |
---|
[194] | 293 | decision_array[i] = 1; |
---|
| 294 | |
---|
| 295 | LOG("Policy Engine:: Sending Policy decision to Server.\n"); |
---|
[195] | 296 | SendPEDecision(pList, &radio_info, decision_array); |
---|
[194] | 297 | |
---|
| 298 | LOG("Policy Engine:: Policies Validated.\n"); |
---|
| 299 | } |
---|
| 300 | else |
---|
| 301 | ERROR(1, "Call to GetRequest in ValidateParameters failed.\n"); |
---|
| 302 | |
---|
[161] | 303 | } |
---|
| 304 | |
---|