1 | #include <stdio.h> |
---|
2 | #include <sys/types.h> |
---|
3 | #include <sys/socket.h> |
---|
4 | #include <netinet/in.h> |
---|
5 | #include <netdb.h> |
---|
6 | #include <iostream> |
---|
7 | |
---|
8 | using namespace std; |
---|
9 | |
---|
10 | struct CE_Info { |
---|
11 | int numUtilities; |
---|
12 | int numParameters; |
---|
13 | int numObservables; |
---|
14 | }; |
---|
15 | |
---|
16 | struct Utility { |
---|
17 | char name[50]; |
---|
18 | char units[50]; |
---|
19 | char goal[50]; |
---|
20 | /* string name; |
---|
21 | string units; |
---|
22 | string goal;*/ |
---|
23 | float target; |
---|
24 | }; |
---|
25 | |
---|
26 | struct Affect { |
---|
27 | Utility u; |
---|
28 | //string relation; |
---|
29 | char relation[50]; |
---|
30 | }; |
---|
31 | |
---|
32 | struct Parameter { |
---|
33 | /*string name; |
---|
34 | string units;*/ |
---|
35 | char name[50]; |
---|
36 | char units[50]; |
---|
37 | float min; |
---|
38 | int numAffects; |
---|
39 | Affect affection_list[10]; |
---|
40 | float max; |
---|
41 | float step; |
---|
42 | }; |
---|
43 | |
---|
44 | struct Observable { |
---|
45 | //string name; |
---|
46 | char name[50]; |
---|
47 | Affect affection_list[10]; |
---|
48 | int numAffects; |
---|
49 | }; |
---|
50 | |
---|
51 | // error handling |
---|
52 | void error(char *msg) |
---|
53 | { |
---|
54 | perror(msg); |
---|
55 | exit(0); |
---|
56 | } |
---|
57 | |
---|
58 | void ReadMessage(int socket,char * buffer) { |
---|
59 | int i,n; |
---|
60 | |
---|
61 | n = recv(socket,buffer,256,MSG_PEEK); |
---|
62 | for(i=0;i<256;i++){ |
---|
63 | if(strcmp(&buffer[i],"\0") == 0) break; |
---|
64 | } |
---|
65 | n = recv(socket,buffer,i+1,0); |
---|
66 | if (n < 0) |
---|
67 | error("ERROR reading from socket"); |
---|
68 | // printf("ReadMessage:%s %d\n",buffer,n); |
---|
69 | } |
---|
70 | |
---|
71 | int GetXMLFromServer(int sockfd, Utility uList[], Parameter pList[], |
---|
72 | Observable oList[], CE_Info *ce_info) |
---|
73 | { |
---|
74 | char buffer[256]; |
---|
75 | int i,j; |
---|
76 | |
---|
77 | // read unitilities |
---|
78 | // numUtilities |
---|
79 | bzero(buffer,256); |
---|
80 | ReadMessage(sockfd,buffer); |
---|
81 | ce_info->numUtilities = atoi(buffer); |
---|
82 | printf("number of utilities: %d\n", ce_info->numUtilities); |
---|
83 | |
---|
84 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
85 | bzero(buffer,256); |
---|
86 | ReadMessage(sockfd,buffer); |
---|
87 | printf("Name: %s\n", buffer); |
---|
88 | strcpy(uList[i].name, buffer); |
---|
89 | |
---|
90 | bzero(buffer,256); |
---|
91 | ReadMessage(sockfd,buffer); |
---|
92 | printf("Units: %s\n", buffer); |
---|
93 | strcpy(uList[i].units, buffer); |
---|
94 | |
---|
95 | bzero(buffer,256); |
---|
96 | ReadMessage(sockfd,buffer); |
---|
97 | printf("Goal: %s\n", buffer); |
---|
98 | strcpy(uList[i].goal, buffer); |
---|
99 | |
---|
100 | bzero(buffer,256); |
---|
101 | ReadMessage(sockfd,buffer); |
---|
102 | printf("Target: %s\n", buffer); |
---|
103 | uList[i].target = atof(buffer); |
---|
104 | } |
---|
105 | |
---|
106 | // read parameters |
---|
107 | // numParameters |
---|
108 | bzero(buffer,256); |
---|
109 | ReadMessage(sockfd,buffer); |
---|
110 | ce_info->numParameters = atoi(buffer); |
---|
111 | printf("number of parameters: %d\n", ce_info->numParameters); |
---|
112 | |
---|
113 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
114 | bzero(buffer,256); |
---|
115 | ReadMessage(sockfd,buffer); |
---|
116 | printf("Name: %s\n", buffer); |
---|
117 | strcpy(pList[i].name, buffer); |
---|
118 | |
---|
119 | bzero(buffer,256); |
---|
120 | ReadMessage(sockfd,buffer); |
---|
121 | printf("Units: %s\n", buffer); |
---|
122 | strcpy(pList[i].units, buffer); |
---|
123 | |
---|
124 | bzero(buffer,256); |
---|
125 | ReadMessage(sockfd,buffer); |
---|
126 | printf("Min: %s\n", buffer); |
---|
127 | pList[i].min = atof(buffer); |
---|
128 | |
---|
129 | bzero(buffer,256); |
---|
130 | ReadMessage(sockfd,buffer); |
---|
131 | printf("Max: %s\n", buffer); |
---|
132 | pList[i].max = atof(buffer); |
---|
133 | |
---|
134 | bzero(buffer,256); |
---|
135 | ReadMessage(sockfd,buffer); |
---|
136 | printf("Step: %s\n", buffer); |
---|
137 | pList[i].step = atof(buffer); |
---|
138 | |
---|
139 | // numAffects |
---|
140 | bzero(buffer,256); |
---|
141 | ReadMessage(sockfd,buffer); |
---|
142 | pList[i].numAffects = atoi(buffer); |
---|
143 | printf("number of affects: %d\n", pList[i].numAffects); |
---|
144 | |
---|
145 | for (j = 0; j < pList[i].numAffects; j++){ |
---|
146 | bzero(buffer,256); |
---|
147 | ReadMessage(sockfd,buffer); |
---|
148 | printf("Utility name: %s\n", buffer); |
---|
149 | strcpy(pList[i].affection_list[j].u.name, buffer); |
---|
150 | |
---|
151 | bzero(buffer,256); |
---|
152 | ReadMessage(sockfd,buffer); |
---|
153 | printf("Relation: %s\n", buffer); |
---|
154 | strcpy(pList[i].affection_list[j].relation, buffer); |
---|
155 | } |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | // read obervables |
---|
160 | // numParameters |
---|
161 | bzero(buffer,256); |
---|
162 | ReadMessage(sockfd,buffer); |
---|
163 | ce_info->numObservables = atoi(buffer); |
---|
164 | printf("number of observables: %d\n", ce_info->numObservables); |
---|
165 | |
---|
166 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
167 | bzero(buffer,256); |
---|
168 | ReadMessage(sockfd,buffer); |
---|
169 | printf("Name: %s\n", buffer); |
---|
170 | strcpy(pList[i].name, buffer); |
---|
171 | |
---|
172 | // numAffects |
---|
173 | bzero(buffer,256); |
---|
174 | ReadMessage(sockfd,buffer); |
---|
175 | oList[i].numAffects = atoi(buffer); |
---|
176 | printf("number of affects: %d\n", oList[i].numAffects); |
---|
177 | |
---|
178 | for (j = 0; j < oList[i].numAffects; j++){ |
---|
179 | bzero(buffer,256); |
---|
180 | ReadMessage(sockfd,buffer); |
---|
181 | printf("Utility name: %s\n", buffer); |
---|
182 | strcpy(oList[i].affection_list[j].u.name, buffer); |
---|
183 | |
---|
184 | bzero(buffer,256); |
---|
185 | ReadMessage(sockfd,buffer); |
---|
186 | printf("Relation: %s\n", buffer); |
---|
187 | strcpy(oList[i].affection_list[j].relation, buffer); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | printf("xml reception done\n"); |
---|
192 | |
---|
193 | return 0; |
---|
194 | } |
---|
195 | |
---|
196 | void print_current_config(Utility * uList[], Parameter * pList[], |
---|
197 | Observable * oList[], CE_Info * ce_info) |
---|
198 | { |
---|
199 | int i = 0; |
---|
200 | int j = 0; |
---|
201 | |
---|
202 | // utilities |
---|
203 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
204 | cout << "Utility: " << uList[i]->name << endl; |
---|
205 | cout << " Units: " << uList[i]->units << endl; |
---|
206 | cout << " Goal: " << uList[i]->goal << endl; |
---|
207 | cout << " Target: " << uList[i]->target << endl; |
---|
208 | } |
---|
209 | |
---|
210 | // parameters |
---|
211 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
212 | cout << "Parameter: " << pList[i]->name << endl; |
---|
213 | cout << " Units: " << pList[i]->units << endl; |
---|
214 | cout << " Min: " << pList[i]->min << endl; |
---|
215 | cout << " Max: " << pList[i]->max << endl; |
---|
216 | cout << " Step: " << pList[i]->step << endl; |
---|
217 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
218 | cout << " Affect: " << pList[i]->affection_list[j].u.name << " -> " << pList[i]->affection_list[j].relation << endl; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | // observables |
---|
223 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
224 | cout << "Observable: " << oList[i]->name << endl; |
---|
225 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
226 | cout << " Affect: " << oList[i]->affection_list[j].u.name << " -> " << oList[i]->affection_list[j].relation << endl; |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | // main client socket |
---|
233 | int main(int argc, char *argv[]) |
---|
234 | { |
---|
235 | Utility uList[10]; |
---|
236 | Parameter pList[10]; |
---|
237 | Observable oList[10]; |
---|
238 | CE_Info ce_info; |
---|
239 | |
---|
240 | int sockfd, portno; |
---|
241 | struct sockaddr_in serv_addr; |
---|
242 | struct hostent *server; |
---|
243 | |
---|
244 | // setup client socket connection |
---|
245 | if (argc < 3) { |
---|
246 | fprintf(stderr,"usage: %s hostname port\n", argv[0]); |
---|
247 | exit(0); |
---|
248 | } |
---|
249 | // server name |
---|
250 | server = gethostbyname(argv[1]); |
---|
251 | if (server == NULL) { |
---|
252 | fprintf(stderr,"ERROR, no such host\n"); |
---|
253 | exit(0); |
---|
254 | } |
---|
255 | // port no. |
---|
256 | portno = atoi(argv[2]); |
---|
257 | // socket file descriptor |
---|
258 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
259 | if (sockfd < 0) |
---|
260 | error("ERROR opening socket"); |
---|
261 | // initiate struct socketaddr_in |
---|
262 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
263 | serv_addr.sin_family = AF_INET; |
---|
264 | serv_addr.sin_port = htons(portno); |
---|
265 | bcopy((char *)server->h_addr, |
---|
266 | (char *)&serv_addr.sin_addr.s_addr, |
---|
267 | server->h_length); |
---|
268 | // cast sockaddr_in to sockaddr |
---|
269 | if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) |
---|
270 | error("ERROR connecting"); |
---|
271 | |
---|
272 | // get xml info from server |
---|
273 | GetXMLFromServer(sockfd, uList, pList, oList, &ce_info); |
---|
274 | |
---|
275 | printf("utility:\n name: %s, units: %s, goal: %s, target: %f\n", |
---|
276 | uList[0].name, uList[0].units, uList[0].goal, uList[0].target); |
---|
277 | |
---|
278 | //print_current_config(&uList, &pList, &oList, &ce_info); |
---|
279 | |
---|
280 | |
---|
281 | return 0; |
---|
282 | } |
---|