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