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 | LoadPolicies(); |
---|
28 | } |
---|
29 | |
---|
30 | |
---|
31 | PolicyEngine::~PolicyEngine() |
---|
32 | { |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | PolicyEngine::PolicyEngine(const char* serverName, const char* serverPort, \ |
---|
37 | const bool SML) |
---|
38 | { |
---|
39 | LOG("Creating Policy Engine.\n"); |
---|
40 | |
---|
41 | SML_present = SML; |
---|
42 | |
---|
43 | commandSocketFD = ClientSocket(serverName, serverPort); |
---|
44 | |
---|
45 | if(SML_present) { |
---|
46 | RegisterServices(); |
---|
47 | LOG("Policy Engine connected to SML at %s.\n", serverName); |
---|
48 | } |
---|
49 | else { |
---|
50 | RegisterComponent(); |
---|
51 | LOG("Policy Engine connected to shell at %s.\n", serverName); |
---|
52 | } |
---|
53 | |
---|
54 | LoadPolicies(); |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | void |
---|
59 | PolicyEngine::WaitForSignal() |
---|
60 | { |
---|
61 | LOG("Policy Engine:: Waiting for Policy Check Request.\n"); |
---|
62 | |
---|
63 | int32_t decision_array[10]; |
---|
64 | struct Parameter pList[10]; |
---|
65 | struct CE_Info ce_info; |
---|
66 | |
---|
67 | // TODO should this be a GetRequest call or something else? |
---|
68 | if(GetRequest(commandSocketFD, pList, &ce_info)) { |
---|
69 | LOG("Policy Engine:: Validating Transmission Parameters.\n"); |
---|
70 | ValidateParameters(pList, &ce_info, decision_array); |
---|
71 | |
---|
72 | LOG("Policy Engine:: Sending Policy decision to Server.\n"); |
---|
73 | SendPEDecision(pList, &ce_info, decision_array); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | void |
---|
79 | PolicyEngine::Shutdown() |
---|
80 | { |
---|
81 | if(SML_present) |
---|
82 | DeregisterServices(); |
---|
83 | else |
---|
84 | DeregisterComponent(); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | void |
---|
89 | PolicyEngine::Reset() |
---|
90 | { |
---|
91 | LOG("Resetting Policy Engine.\n"); |
---|
92 | SML_present = false; |
---|
93 | commandSocketFD = -1; |
---|
94 | LoadPolicies(); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | void |
---|
99 | PolicyEngine::RegisterComponent() |
---|
100 | { |
---|
101 | SendMessage(commandSocketFD, "register_engine_policy"); |
---|
102 | LOG("Policy Engine:: Registration message sent.\n"); |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | void |
---|
108 | PolicyEngine::DeregisterComponent() |
---|
109 | { |
---|
110 | SendMessage(commandSocketFD, "deregister_engine_policy"); |
---|
111 | LOG("Policy Engine:: Registration message sent.\n"); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | void |
---|
116 | PolicyEngine::RegisterServices() |
---|
117 | { |
---|
118 | LOG("Policy Engine:: Registering services.\n"); |
---|
119 | SendMessage(commandSocketFD, "register_service_pe_geo"); |
---|
120 | SendMessage(commandSocketFD, "register_service_pe_time"); |
---|
121 | SendMessage(commandSocketFD, "register_service_pe_spectrum"); |
---|
122 | SendMessage(commandSocketFD, "register_service_pe_spacial"); |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | void |
---|
127 | PolicyEngine::DeregisterServices() |
---|
128 | { |
---|
129 | LOG("Policy Engine:: Deregistering services.\n"); |
---|
130 | SendMessage(commandSocketFD, "deregister_service_pe_geo"); |
---|
131 | SendMessage(commandSocketFD, "deregister_service_pe_time"); |
---|
132 | SendMessage(commandSocketFD, "deregister_service_pe_spectrum"); |
---|
133 | SendMessage(commandSocketFD, "deregister_service_pe_spacial"); |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | void |
---|
138 | PolicyEngine::LoadPolicies() |
---|
139 | { |
---|
140 | LOG("PolicyEngine:: Loading policies.\n"); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | void |
---|
145 | PolicyEngine::ReloadPolicies() |
---|
146 | { |
---|
147 | LOG("PolicyEngine:: Reloading policies.\n"); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | void |
---|
152 | PolicyEngine::SendPEDecision(struct Parameter pList[], \ |
---|
153 | struct CE_Info *ce_info, int32_t decision_array[]) |
---|
154 | { |
---|
155 | char var[50]; |
---|
156 | |
---|
157 | for (size_t i = 0; i < ce_info->numParameters; i++) { |
---|
158 | sprintf(var, "%i", decision_array[i]); |
---|
159 | SendMessage(commandSocketFD, var); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | void |
---|
165 | PolicyEngine::ValidateParameters(struct Parameter pList[], \ |
---|
166 | struct CE_Info *ce_info, int decision_array[]) |
---|
167 | { |
---|
168 | LOG("Policy Engine:: Policies Validated.\n"); |
---|
169 | for (size_t i = 0; i < ce_info->numParameters; i++) |
---|
170 | decision_array[i] = 1; |
---|
171 | } |
---|
172 | |
---|