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