ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/ihost.c
(Generate patch)

Comparing projects/cms/source/ihost/ihost.c (file contents):
Revision 1.2 by pajs, Fri May 10 20:05:05 2002 UTC vs.
Revision 1.6 by pajs, Sun May 12 17:33:21 2002 UTC

# Line 8 | Line 8
8   #include <netdb.h>
9   #include <strings.h>
10  
11 + #define RECONFIGURE_RETURN_CODE 2
12 +
13   typedef struct{
14          int fm_port;
15          char *fm_host;
# Line 16 | Line 18 | typedef struct{
18          char *server_fqdn;
19          int server_udp_port;
20          int server_tcp_port;
21 <        long last_modified;
21 >        char *last_modified;
22          char *files_list;
23          char *key;
24          int udp_update_time;
# Line 41 | Line 43 | int ihost_configure(ihost_state_t *ihost_state){
43          char *reply;
44          char *reply_ptr;
45  
46 +        /* Check to see if anything needs to be free'd */
47 +        if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn);
48 +        if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
49 +        if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
50 +        if (ihost_state->files_list!=NULL) free(ihost_state->files_list);
51 +
52          if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
53                  errf("Can't create AF_INET socket (%m)");
54                  return -1;
# Line 83 | Line 91 | int ihost_configure(ihost_state_t *ihost_state){
91                  errf("Server error (%m)");
92                  return -1;
93          }
94 <        ihost_state->last_modified=atol(reply);
94 >        if((ihost_state->last_modified=malloc((strlen(reply))+1)) == NULL){
95 >                errf("malloc failed (%m)");
96 >                return -1;
97 >        }
98 >        strcpy(ihost_state->last_modified, reply);
99 >        ihost_state->last_modified[(strlen(ihost_state->last_modified))]='\n';
100          
101          reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n");
102          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
103                  errf("Server error (%m)");
104                  return -1;
105          }
106 <        if((ihost_state->files_list=strdup(reply)) == NULL){
107 <                errf("strdup failed (%m)");
108 <                return -1;
109 <        }
106 >        if((ihost_state->files_list=malloc((strlen(reply))+1)) == NULL){
107 >                errf("malloc failed (%m)");
108 >                return -1;
109 >        }
110 >        strcpy(ihost_state->files_list, reply);
111 >        ihost_state->files_list[(strlen(ihost_state->files_list))]='\n';
112  
113          reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n");
114          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){
# Line 175 | Line 190 | int ihost_configure(ihost_state_t *ihost_state){
190          }
191  
192          return 0;
193 + }
194  
195 + int heartbeat(ihost_state_t *ihost_state){
196 +        struct sockaddr_in addr;
197 +        struct in_addr haddr;
198 +        int sd;
199 +        FILE *fm_fd_r, *fm_fd_w;
200 +        char *reply;
201 +        int exitcode=0;
202 +
203 +        if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
204 +                errf("Can't create AF_INET socket (%m)");
205 +                return -1;
206 +        }
207 +
208 +        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
209 +                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
210 +                return -1;
211 +        }
212 +
213 +        memset((char *)&addr, 0, sizeof addr);
214 +        addr.sin_family = AF_INET;
215 +        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
216 +        addr.sin_port =  htons(ihost_state->server_tcp_port);
217 +
218 +        if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
219 +                errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
220 +                return -1;
221 +        }
222 +
223 +        /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
224 +        if ((fm_fd_r=fdopen(sd,"r")) == NULL){
225 +                errf("Failed to open stream (%m)");
226 +                return -1;
227 +        }
228 +
229 +        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
230 +                errf("Failed to open stream (%m)");
231 +                return -1;
232 +        }
233 +
234 +        reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n");
235 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
236 +                errf("Server error");
237 +                return -1;
238 +        }
239 +        if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host);
240 +        reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n");
241 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
242 +                errf("Server error");
243 +                return -1;
244 +        }
245 +
246 +        printf("filelist %s\n",ihost_state->files_list);
247 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
248 +        if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
249 +                errf("Server error");
250 +                return -1;
251 +        }
252 +
253 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
254 +        if (reply==NULL) {
255 +                errf("Server error");
256 +                return -1;
257 +        }
258 +        if (strncasecmp(reply, "ERROR", 5) == 0){
259 +        /* Means the config has changed */
260 +                exitcode=RECONFIGURE_RETURN_CODE;
261 +        }
262 +        reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n");
263 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) {
264 +                errf("Server error");
265 +                return -1;
266 +        }
267 +        if (ihost_state->key!=NULL) free(ihost_state->key);
268 +
269 +        if((ihost_state->key=malloc(strlen(reply)+1)) == NULL){
270 +                errf("malloc failed (%m)");
271 +                return -1;
272 +        }
273 +        strcpy(ihost_state->key, reply);
274 +        ihost_state->key[(strlen(ihost_state->key))]='\n';
275 +
276 +        reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT\n");
277 +        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
278 +                errf("Server error (%m)");
279 +                return -1;
280 +        }
281 +
282 +        if(fclose(fm_fd_r) !=0){
283 +                errf("Failed to close FD (%m)");
284 +                return -1;
285 +        }
286 +        if(fclose(fm_fd_w) !=0){
287 +                errf("Failed to close FD (%m)");
288 +                return -1;
289 +        }
290 +
291 +        return exitcode;                
292   }
293  
294 < int main(){
294 >
295 > int main(int argc, char **argv){
296          ihost_state_t ihost_state;
297 +        int heartbeat_exit;
298 +        int counter=0;
299  
184        ihost_state.fm_host=strdup("kernow.ukc.ac.uk");
185        ihost_state.fm_port=4567;
300  
301 +        /* NULL'ify so i can tell if i need to free it or not */
302 +        ihost_state.fm_host=NULL;
303 +        ihost_state.my_fqdn=NULL;
304 +        ihost_state.server_fqdn=NULL;
305 +        ihost_state.last_modified=NULL;
306 +        ihost_state.files_list=NULL;
307 +        ihost_state.key=NULL;
308 +
309 +        errf_set_progname(argv[0]);
310 +        if(argc!=3){
311 +                errf_usage("<host> <port>");    
312 +                exit(1);
313 +        }
314 +
315 +        ihost_state.fm_host=argv[1];
316 +        ihost_state.fm_port=atoi(argv[2]);
317 +
318          if(ihost_configure(&ihost_state)!=0){
319                  errf("configure failed");
320 +                /* Ok, ideally we prob should have 2 copies of the structure and carry on if this
321 +                happens.. But we dont :) (at the moment) */
322 +                exit(1);
323          }
324  
325 +        while(TRUE){
326 +
327 +                heartbeat_exit=heartbeat(&ihost_state);
328 +                if(heartbeat_exit==RECONFIGURE_RETURN_CODE){
329 +                        errf("heartbeat needs to be reconfigured");
330 +                        ihost_configure(&ihost_state);
331 +                }
332 +                if(heartbeat_exit==-1){
333 +                        errf("ah crap");
334 +                        exit(1);
335 +                }
336 +                printf("Count : %d\n",counter++);
337 +                printf("waiting %d\n",ihost_state.tcp_update_time);
338 +                sleep(ihost_state.tcp_update_time);
339 +        }
340          return 0;
341   }
342  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines