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.4 by pajs, Sun May 12 12:00:33 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 83 | Line 85 | int ihost_configure(ihost_state_t *ihost_state){
85                  errf("Server error (%m)");
86                  return -1;
87          }
88 <        ihost_state->last_modified=atol(reply);
88 >        if((ihost_state->last_modified=strdup(reply)) == NULL){
89 >                errf("strdup failed (%m)");
90 >                return -1;
91 >        }
92          
93          reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n");
94          if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){
# Line 175 | Line 180 | int ihost_configure(ihost_state_t *ihost_state){
180          }
181  
182          return 0;
183 + }
184  
185 + int heartbeat(ihost_state_t *ihost_state){
186 +        struct sockaddr_in addr;
187 +        struct in_addr haddr;
188 +        int sd;
189 +        FILE *fm_fd_r, *fm_fd_w;
190 +        char *reply;
191 +        int exitcode=0;
192 +
193 +        if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) {
194 +                errf("Can't create AF_INET socket (%m)");
195 +                return -1;
196 +        }
197 +
198 +        if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){
199 +                errf("Failed to resolve address %s (%m)", ihost_state->fm_host);
200 +                return -1;
201 +        }
202 +
203 +        memset((char *)&addr, 0, sizeof addr);
204 +        addr.sin_family = AF_INET;
205 +        memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr);
206 +        addr.sin_port =  htons(ihost_state->server_tcp_port);
207 +
208 +        if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) {
209 +                errf("Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port);
210 +                return -1;
211 +        }
212 +
213 +        /* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */
214 +        if ((fm_fd_r=fdopen(sd,"r")) == NULL){
215 +                errf("Failed to open stream (%m)");
216 +                return -1;
217 +        }
218 +
219 +        if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){
220 +                errf("Failed to open stream (%m)");
221 +                return -1;
222 +        }
223 +
224 +        reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n");
225 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
226 +                errf("Server error");
227 +                return -1;
228 +        }
229 +
230 +        reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n");
231 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
232 +                errf("Server error");
233 +                return -1;
234 +        }
235 +
236 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list);
237 +        if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) {
238 +                errf("Server error");
239 +                return -1;
240 +        }
241 +
242 +        reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified);
243 +        if (reply==NULL) {
244 +                errf("Server error");
245 +                return -1;
246 +        }
247 +        if (strncasecmp(reply, "ERROR", 2) == 0){
248 +        /* Means the config has changed */
249 +                exitcode=RECONFIGURE_RETURN_CODE;
250 +        }
251 +        reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n");
252 +        if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) {
253 +                errf("Server error");
254 +                return -1;
255 +        }
256 +        
257 +        if((ihost_state->key=strdup(reply)) == NULL){
258 +                errf("strdup failed (%m)");
259 +                return -1;
260 +        }
261 +
262 +        reply=sock_comm(fm_fd_r, fm_fd_w, "END\n");
263 +        if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){
264 +                errf("Server error (%m)");
265 +                return -1;
266 +        }
267 +
268 +        return exitcode;                
269   }
270  
271 < int main(){
271 >
272 > int main(int argc, char **argv){
273          ihost_state_t ihost_state;
274 +        errf_set_progname(argv[0]);
275 +        if(argc!=3){
276 +                errf_usage("<host> <port>");    
277 +                exit(1);
278 +        }
279  
280 <        ihost_state.fm_host=strdup("kernow.ukc.ac.uk");
281 <        ihost_state.fm_port=4567;
280 >        ihost_state.fm_host=argv[1];
281 >        ihost_state.fm_port=atoi(argv[2]);
282  
283          if(ihost_configure(&ihost_state)!=0){
284                  errf("configure failed");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines