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.5 by pajs, Sun May 12 14:10:45 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->fm_host!=NULL) free(ihost_state->fm_host);
48 +        if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn);
49 +        if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn);
50 +        if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified);
51 +        if (ihost_state->files_list!=NULL) free(ihost_state->files_list);
52 +
53          if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
54                  errf("Can't create AF_INET socket (%m)");
55                  return -1;
# Line 83 | Line 92 | int ihost_configure(ihost_state_t *ihost_state){
92                  errf("Server error (%m)");
93                  return -1;
94          }
95 <        ihost_state->last_modified=atol(reply);
95 >        if((ihost_state->last_modified=strdup(reply)) == NULL){
96 >                errf("strdup failed (%m)");
97 >                return -1;
98 >        }
99          
100          reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n");
101          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
# Line 175 | Line 187 | int ihost_configure(ihost_state_t *ihost_state){
187          }
188  
189          return 0;
190 + }
191  
192 + int heartbeat(ihost_state_t *ihost_state){
193 +        struct sockaddr_in addr;
194 +        struct in_addr haddr;
195 +        int sd;
196 +        FILE *fm_fd_r, *fm_fd_w;
197 +        char *reply;
198 +        int exitcode=0;
199 +
200 +        if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
201 +                errf("Can't create AF_INET socket (%m)");
202 +                return -1;
203 +        }
204 +
205 +        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
206 +                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
207 +                return -1;
208 +        }
209 +
210 +        memset((char *)&addr, 0, sizeof addr);
211 +        addr.sin_family = AF_INET;
212 +        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
213 +        addr.sin_port =  htons(ihost_state->server_tcp_port);
214 +
215 +        if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
216 +                errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
217 +                return -1;
218 +        }
219 +
220 +        /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
221 +        if ((fm_fd_r=fdopen(sd,"r")) == NULL){
222 +                errf("Failed to open stream (%m)");
223 +                return -1;
224 +        }
225 +
226 +        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
227 +                errf("Failed to open stream (%m)");
228 +                return -1;
229 +        }
230 +
231 +        reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n");
232 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
233 +                errf("Server error");
234 +                return -1;
235 +        }
236 +
237 +        reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n");
238 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
239 +                errf("Server error");
240 +                return -1;
241 +        }
242 +
243 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
244 +        if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
245 +                errf("Server error");
246 +                return -1;
247 +        }
248 +
249 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
250 +        if (reply==NULL) {
251 +                errf("Server error");
252 +                return -1;
253 +        }
254 +        if (strncasecmp(reply, "ERROR", 2) == 0){
255 +        /* Means the config has changed */
256 +                exitcode=RECONFIGURE_RETURN_CODE;
257 +        }
258 +        reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n");
259 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
260 +                errf("Server error");
261 +                return -1;
262 +        }
263 +        if (ihost_state->key!=NULL) free(ihost_state->key);
264 +
265 +        if((ihost_state->key=strdup(reply)) == NULL){
266 +                errf("strdup failed (%m)");
267 +                return -1;
268 +        }
269 +
270 +        reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
271 +        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
272 +                errf("Server error (%m)");
273 +                return -1;
274 +        }
275 +
276 +        return exitcode;                
277   }
278  
279 < int main(){
279 >
280 > int main(int argc, char **argv){
281          ihost_state_t ihost_state;
282  
283 <        ihost_state.fm_host=strdup("kernow.ukc.ac.uk");
284 <        ihost_state.fm_port=4567;
283 >        /* NULL'ify so i can tell if i need to free it or not */
284 >        ihost_state.fm_host=NULL;
285 >        ihost_state.my_fqdn=NULL;
286 >        ihost_state.server_fqdn=NULL;
287 >        ihost_state.last_modified=NULL;
288 >        ihost_state.files_list=NULL;
289 >        ihost_state.key=NULL;
290  
291 +        errf_set_progname(argv[0]);
292 +        if(argc!=3){
293 +                errf_usage("<host> <port>");    
294 +                exit(1);
295 +        }
296 +
297 +        ihost_state.fm_host=argv[1];
298 +        ihost_state.fm_port=atoi(argv[2]);
299 +
300          if(ihost_configure(&ihost_state)!=0){
301                  errf("configure failed");
302          }
303 +
304  
305          return 0;
306   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines