1 | #include <cstdlib> |
---|
2 | #include <cstdio> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <sys/socket.h> |
---|
5 | #include <netinet/in.h> |
---|
6 | #include <netdb.h> |
---|
7 | #include <string> |
---|
8 | #include <string.h> |
---|
9 | |
---|
10 | #include "vtcross/cbr.h" |
---|
11 | #include "vtcross/containers.h" |
---|
12 | |
---|
13 | |
---|
14 | #define CR_PORT 30002 |
---|
15 | |
---|
16 | // error handling |
---|
17 | void error(char *msg) |
---|
18 | { |
---|
19 | perror(msg); |
---|
20 | exit(0); |
---|
21 | } |
---|
22 | |
---|
23 | void ReadMessage(int socket,char * buffer) { |
---|
24 | int i,n; |
---|
25 | |
---|
26 | n = recv(socket,buffer,256,MSG_PEEK); |
---|
27 | for(i=0;i<256;i++){ |
---|
28 | if(strcmp(&buffer[i],"\0") == 0) break; |
---|
29 | } |
---|
30 | n = recv(socket,buffer,i+1,0); |
---|
31 | if (n < 0) |
---|
32 | error("ERROR reading from socket"); |
---|
33 | //printf("ReadMessage:%s %d\n",buffer,n); |
---|
34 | } |
---|
35 | |
---|
36 | // setup client socket connection |
---|
37 | int ClientSocket(int argc, char** argv) |
---|
38 | { |
---|
39 | int sockfd; |
---|
40 | int portno; |
---|
41 | struct sockaddr_in serv_addr; |
---|
42 | struct hostent *server; |
---|
43 | |
---|
44 | // server name |
---|
45 | server = gethostbyname(argv[1]); |
---|
46 | if (server == NULL) { |
---|
47 | fprintf(stderr,"ERROR, no such host\n"); |
---|
48 | exit(0); |
---|
49 | } |
---|
50 | // port no. |
---|
51 | portno = atoi(argv[2]); |
---|
52 | // socket file descriptor |
---|
53 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
54 | if (sockfd < 0) |
---|
55 | error("ERROR opening socket"); |
---|
56 | // initiate struct socketaddr_in |
---|
57 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
58 | serv_addr.sin_family = AF_INET; |
---|
59 | serv_addr.sin_port = htons(portno); |
---|
60 | bcopy((char *)server->h_addr, |
---|
61 | (char *)&serv_addr.sin_addr.s_addr, |
---|
62 | server->h_length); |
---|
63 | // cast sockaddr_in to sockaddr |
---|
64 | if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) |
---|
65 | error("ERROR connecting"); |
---|
66 | |
---|
67 | return sockfd; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | int SendMessage(int socketfd, char *message) |
---|
72 | { |
---|
73 | int n; |
---|
74 | |
---|
75 | strcat(message, "\0000"); |
---|
76 | n = send(socketfd,message,(strlen(message)+1),0); |
---|
77 | if (n<0) |
---|
78 | error("Error sending to server\n"); |
---|
79 | else if(n == 0) |
---|
80 | printf("Server closed the socket.\n"); |
---|
81 | |
---|
82 | return n; |
---|
83 | } |
---|
84 | |
---|
85 | int GetParameter(int sockfd, struct Parameter pList[], struct CE_Info *ce_info) |
---|
86 | { |
---|
87 | char buffer[256]; |
---|
88 | int i; |
---|
89 | |
---|
90 | // numParameters |
---|
91 | bzero(buffer,256); |
---|
92 | ReadMessage(sockfd,buffer); |
---|
93 | ce_info->numParameters = atoi(buffer); |
---|
94 | printf("number of parameters: %d\n", ce_info->numParameters); |
---|
95 | |
---|
96 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
97 | bzero(buffer,256); |
---|
98 | ReadMessage(sockfd,buffer); |
---|
99 | printf("Name: %s\n", buffer); |
---|
100 | pList[i].name = std::string(buffer); |
---|
101 | // strcpy(pList[i].name.c_str(), buffer); |
---|
102 | |
---|
103 | bzero(buffer,256); |
---|
104 | ReadMessage(sockfd,buffer); |
---|
105 | printf("Units: %s\n", buffer); |
---|
106 | pList[i].units = std::string(buffer); |
---|
107 | // strcpy(pList[i].units.c_str(), buffer); |
---|
108 | |
---|
109 | bzero(buffer,256); |
---|
110 | ReadMessage(sockfd,buffer); |
---|
111 | printf("Min: %s\n", buffer); |
---|
112 | pList[i].min = atof(buffer); |
---|
113 | |
---|
114 | bzero(buffer,256); |
---|
115 | ReadMessage(sockfd,buffer); |
---|
116 | printf("Max: %s\n", buffer); |
---|
117 | pList[i].max = atof(buffer); |
---|
118 | |
---|
119 | bzero(buffer,256); |
---|
120 | ReadMessage(sockfd,buffer); |
---|
121 | printf("Step: %s\n", buffer); |
---|
122 | pList[i].step = atof(buffer); |
---|
123 | |
---|
124 | bzero(buffer,256); |
---|
125 | ReadMessage(sockfd,buffer); |
---|
126 | printf("Value: %s\n", buffer); |
---|
127 | pList[i].value = atof(buffer); |
---|
128 | |
---|
129 | } |
---|
130 | return 1; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | int GetRequest(int sockfd, struct Parameter pList[], struct CE_Info *ce_info) |
---|
135 | { |
---|
136 | char buffer[256]; |
---|
137 | |
---|
138 | // Get control identifier |
---|
139 | // Possibly could implement some sort of authentication here |
---|
140 | bzero(buffer,256); |
---|
141 | ReadMessage(sockfd,buffer); |
---|
142 | |
---|
143 | if(strcmp(buffer,"val") != 0) { |
---|
144 | printf("Policy Engine:: Unexpected control data received.\n\n"); |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | printf("Policy Engine:: Getting parameters.\n\n"); |
---|
149 | // Get potenial transmission parameter values |
---|
150 | GetParameter(sockfd, pList, ce_info); |
---|
151 | |
---|
152 | return 1; |
---|
153 | } |
---|
154 | |
---|
155 | int ValidateTransmissionParameters(struct Parameter pList[], struct CE_Info *ce_info, int decision_array[]) |
---|
156 | { |
---|
157 | int i = 0; |
---|
158 | // Decision array holds parameter decision |
---|
159 | // 1 = OK |
---|
160 | // 0 = FAIL |
---|
161 | |
---|
162 | printf("Policy Engine:: Policies Validated.\n\n"); |
---|
163 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
164 | decision_array[i] = 1; |
---|
165 | } |
---|
166 | |
---|
167 | return 1; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | int RegisterPE(int sockfd) |
---|
172 | { |
---|
173 | // Send register message to cognitive radio |
---|
174 | SendMessage(sockfd, "p_register"); |
---|
175 | printf("Policy Engine:: Registration message sent.\n\n"); |
---|
176 | return 1; |
---|
177 | } |
---|
178 | |
---|
179 | int SendCEDecision(int sockfd, struct Parameter pList[], struct CE_Info *ce_info, int decision_array[]) |
---|
180 | { |
---|
181 | // send results back to server |
---|
182 | char var[50]; |
---|
183 | int i; |
---|
184 | |
---|
185 | // parameter |
---|
186 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
187 | sprintf(var, "%i", decision_array[i]); |
---|
188 | SendMessage(sockfd, var); |
---|
189 | } |
---|
190 | |
---|
191 | return 1; |
---|
192 | } |
---|
193 | |
---|
194 | int ParsePolicies() { |
---|
195 | |
---|
196 | printf("Policy Engine:: Policies parsed.\n\n"); |
---|
197 | |
---|
198 | return 1; |
---|
199 | } |
---|
200 | |
---|
201 | // main client socket |
---|
202 | int main(int argc, char *argv[]) |
---|
203 | { |
---|
204 | int decision_array[10]; |
---|
205 | struct Parameter pList[10]; |
---|
206 | struct CE_Info ce_info; |
---|
207 | int sockfd; |
---|
208 | int ret = 0; |
---|
209 | |
---|
210 | // setup client socket connection |
---|
211 | if (argc < 3) { |
---|
212 | fprintf(stderr,"usage: %s hostname port\n", argv[0]); |
---|
213 | exit(0); |
---|
214 | } |
---|
215 | |
---|
216 | // setup client socket connection with server |
---|
217 | sockfd = ClientSocket(argc, argv); |
---|
218 | |
---|
219 | // Parse initial Policies |
---|
220 | printf("Policy Engine:: Parsing local policies.\n\n"); |
---|
221 | ret = ParsePolicies(); |
---|
222 | |
---|
223 | ret = RegisterPE(sockfd); |
---|
224 | |
---|
225 | while(1) { |
---|
226 | |
---|
227 | printf("Policy Engine:: Waiting for Policy Check Request.\n\n"); |
---|
228 | ret = GetRequest(sockfd, pList, &ce_info); |
---|
229 | |
---|
230 | if(ret == 1) { |
---|
231 | printf("Policy Engine:: Validating Transmission Parameters.\n\n"); |
---|
232 | ValidateTransmissionParameters(pList, &ce_info, decision_array); |
---|
233 | |
---|
234 | printf("Policy Engine:: Sending Policy decision to Server.\n\n"); |
---|
235 | SendCEDecision(sockfd, pList, &ce_info, decision_array); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | return 0; |
---|
240 | } |
---|