Skip to content

Commit 229516f

Browse files
committed
Error handling taken care
1 parent 091081d commit 229516f

File tree

7 files changed

+69
-32
lines changed

7 files changed

+69
-32
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Use this Makefile to compile the code
1+
# Use this Makefile to compile tutor program
22
# Collaborators: Avinash Joshi <[email protected]>
33
# Sandeep Shenoy <[email protected]>
44

README

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ Collaborators: Avinash Joshi <[email protected]>, Sandeep Shenoy <sxs115220
99
-----------------------
1010

1111
0) Compiling the code
12-
make all
12+
`make all`
1313
1) Removing object files
14-
make clean
14+
`make clean`
1515
2) Removing all compiled files
16-
make cleanall
16+
`make cleanall`
1717

1818
Note: You may want to have a look at the file Makefile
1919

@@ -73,7 +73,7 @@ Joining the tree (join):
7373

7474
If No, then ROOT sends a list of its child nodes. The requesting node now sends a UDP Join Request to each of this nodes. This will continue until the requesting node joins the tree.
7575

76-
Once the requesting node is accept as a child by one of the node, a UDP Socket and TCP Socket will be created on this child's port. A persistance TCP Connection will also be established with the node that accepted the request which becomes the parent node.
76+
Once the requesting node is accepted as a child by one of the node, a UDP Socket and TCP Socket will be created on this child's port. A persistance TCP Connection will also be established with the node that accepted the request which becomes the parent node.
7777

7878

7979
Computing the path till the ROOT (computepath):
@@ -82,12 +82,12 @@ Computing the path till the ROOT (computepath):
8282
Any node in the tree, to compute its path to ROOT, will send a TCP request to its parent node asking for the path from itself to ROOT. This request would flow from requesting node to ROOT node and then back to requesting node. Finally, we will be having the path from the requesting node to ROOT node.
8383

8484

85-
File in the application
86-
------------------------------
85+
Source File in the application
86+
-------------------------------
8787

8888
0) tutor.c
8989
1) udp.c
9090
2) tcp.c
9191
3) join.c
9292
4) services.c
93-
93+
5) myHeader.h

join.c

+38-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
*/
1111

1212
#include "myheader.h"
13+
#include <signal.h>
14+
#include <setjmp.h>
15+
16+
#define RECV_TIMEOUT 5 /* timeout in seconds */
17+
18+
static sigjmp_buf recv_timed_out;
19+
20+
/* timeout handler */
21+
void
22+
timeout_handler (int signum) {
23+
signal(SIGALRM, SIG_DFL);
24+
siglongjmp(recv_timed_out, 1);
25+
}
1326

1427
struct list_details {
1528
char node_name[50];
@@ -23,7 +36,7 @@ struct list_details {
2336
* with the node that accepted the request
2437
*
2538
*/
26-
void
39+
int
2740
join_tree(int uport,int tport,int r_uport,int r_tport,char* host) {
2841

2942
char *udp_buffer;
@@ -35,14 +48,14 @@ join_tree(int uport,int tport,int r_uport,int r_tport,char* host) {
3548
char **list, *t_list[5], *temp_str;
3649

3750
if ((temp_udp_sockfd = socket (AF_INET,SOCK_DGRAM,0)) < 0) {
38-
fprintf (stderr,"udp: Socket couldn't be opened\n");
39-
exit (EXIT_FAILURE);
51+
perror ("socket() failed");
52+
return -1;
4053
}
4154

4255
/* create hostent structure from user entered host name*/
4356
if ((he = gethostbyname(host)) == NULL) {
44-
printf("\n%s: error in gethostbyname()", "tutor");
45-
exit(0);
57+
fprintf(stdout, "Unable to resolve host %s", host);
58+
return -1;
4659
}
4760

4861
while (1) {
@@ -70,11 +83,27 @@ join_tree(int uport,int tport,int r_uport,int r_tport,char* host) {
7083
strcat (sendbuff, port_ch);
7184

7285
if ((bytes_sent = sendto (temp_udp_sockfd, sendbuff, strlen(sendbuff), 0, (struct sockaddr *) &server_addr, server_addr_size)) < 0) {
73-
exit (EXIT_FAILURE);
86+
perror ("sendto() error");
87+
return -1;
7488
}
7589

76-
if ((bytes_received = recvfrom (temp_udp_sockfd, udp_buffer, STRLEN, 0, (struct sockaddr *) &server_addr, (socklen_t *) &server_addr_size)) < 0) {
77-
exit (EXIT_FAILURE);
90+
if (sigsetjmp(recv_timed_out, 1)) {
91+
printf("recvfrom() timed out\n\n");
92+
return -1;
93+
}
94+
95+
/* set timer and handler */
96+
signal(SIGALRM, timeout_handler);
97+
alarm(RECV_TIMEOUT);
98+
99+
bytes_received = recvfrom (temp_udp_sockfd, udp_buffer, STRLEN, 0, (struct sockaddr *) &server_addr, (socklen_t *) &server_addr_size);
100+
101+
alarm(0);
102+
signal(SIGALRM, SIG_DFL);
103+
104+
if (bytes_received < 0) {
105+
perror ("recvfrom() error");
106+
return -1;
78107
}
79108

80109
if (udp_buffer[0] == 'a') {
@@ -128,4 +157,5 @@ join_tree(int uport,int tport,int r_uport,int r_tport,char* host) {
128157
udp_buffer = NULL;
129158
}
130159
close(temp_udp_sockfd);
160+
return 1;
131161
}

myheader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ create_tcp(size_t,int);
4848
void
4949
make_persistance(char*,int);
5050

51-
void
51+
int
5252
join_tree (int,int,int,int,char*);
5353

5454
void

tcp.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ handle_persistance (void *p) {
157157
int cstatus = connect(clientSockid,(struct sockaddr *)&servaddr,sizeof(servaddr));
158158

159159
if (cstatus < 0) {
160-
perror ("connect() error");
161-
return NULL;
160+
perror ("connect() to parent error");
161+
exit (EXIT_FAILURE);
162162
}
163163

164164
DBG (("Persistant connection with %s:%d", t_det.thost, t_det.tport));
@@ -181,6 +181,7 @@ handle_tcp (void* tport) {
181181
sock = socket(AF_INET,SOCK_STREAM,0);
182182
if (sock < 0) {
183183
perror ("socket() error");
184+
exit (EXIT_FAILURE);
184185
}
185186

186187
/* So that we can re-bind to it without TIME_WAIT problems */
@@ -201,14 +202,14 @@ handle_tcp (void* tport) {
201202
int bstatus = bind(sock, (struct sockaddr *)&servaddr,sizeof(servaddr));
202203
if (bstatus < 0) {
203204
perror ("bind() error");
204-
return NULL;
205+
exit (EXIT_FAILURE);
205206
}
206207

207208
//Convert the socket to listening socket
208209
int lstatus = listen(sock,5);
209210
if (lstatus < 0) {
210211
perror ("listen() error");
211-
return NULL;
212+
exit (EXIT_FAILURE);
212213
}
213214
else {
214215
DBG (("Listening..."));

tutor.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ main (int argc, char **argv) {
122122
char *tport_arg, *uport_arg;
123123
char *in_line = (char *) malloc (sizeof (char) * 100);
124124
char *command, *com_arg, *host;
125-
int join_uport;
125+
int join_uport, join_status;
126126
// Flags for the commands
127127
int makeserver_f = 0, join_f = 0, computepath_f = 0;
128128

@@ -254,8 +254,11 @@ main (int argc, char **argv) {
254254
if ((join_uport = check_value (com_arg, "port")) == -1)
255255
continue;
256256

257+
join_status = join_tree (uport, tport, join_uport, 1234, host);
258+
sleep (2);
259+
if (join_status == -1)
260+
continue;
257261
join_f = 1;
258-
join_tree (uport, tport, join_uport, 1234, host);
259262
fprintf (stdout, "You are now a tutor with ID:%d. Reach your instructor @ %s:%s", node_number, parent.ip, parent.tport);
260263
continue;
261264
}

udp.c

+12-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* Contributors: Avinash Joshi <[email protected]>, Sandeep Shenoy <[email protected]>
1010
*/
11+
1112
#include "myheader.h"
1213

1314
int child_count=0;
@@ -62,8 +63,8 @@ handle_udp (void* uport) {
6263

6364
//Creates a UDP socket
6465
if ((udp_sockfd = socket (AF_INET,SOCK_DGRAM,0)) < 0) {
65-
fprintf (stderr,"udp: Socket couldn't be opened\n");
66-
exit (EXIT_FAILURE);
66+
perror ("socket() failed");
67+
return NULL;
6768
}
6869

6970
//Creates a local server structure
@@ -77,15 +78,15 @@ handle_udp (void* uport) {
7778
gethostname (server_host, 49);
7879
if ((he = gethostbyname(server_host)) == NULL) {
7980
herror(server_host);
80-
puts("error resolving hostname..");
81-
return NULL;
81+
fprintf (stdout, "Unable to resolve host %s", server_host);
82+
exit (EXIT_FAILURE);
8283
}
8384
struct in_addr **tmp = (struct in_addr **) he->h_addr_list;
8485
DBG (("Hostname:IP = %s:%s", server_host, inet_ntoa(*tmp[0])));
8586

8687
/*Binds the socket*/
8788
if (bind (udp_sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) {
88-
fprintf (stderr,"udp: Socket couldn't be bind\n");
89+
perror ("bind() failed");
8990
exit (EXIT_FAILURE);
9091
}
9192
int next_node = (node_number * k_child) + 1;
@@ -99,7 +100,8 @@ handle_udp (void* uport) {
99100
bzero (&udp_buffer,sizeof(udp_buffer));
100101
char temp[STRLEN];
101102
if ((bytes_received = recvfrom(udp_sockfd, udp_buffer, STRLEN, 0, (struct sockaddr *) &client_addr, (socklen_t *) &client_addr_size)) < 0) {
102-
exit(EXIT_FAILURE);
103+
perror ("recvfrom() failed");
104+
exit (EXIT_FAILURE);
103105
}
104106

105107
char temp_buff[50], *temp_port, *temp_command;
@@ -132,7 +134,8 @@ handle_udp (void* uport) {
132134
next_node++;
133135
DBG (("Sending %s", temp));
134136
if ((bytes_sent = sendto(udp_sockfd,temp,strlen(temp),0,(struct sockaddr *)&client_addr,client_addr_size)) < 0) {
135-
exit(EXIT_FAILURE);
137+
perror ("sendto() failed");
138+
exit (EXIT_FAILURE);
136139
}
137140
child_count++;
138141
}
@@ -155,12 +158,12 @@ handle_udp (void* uport) {
155158
}
156159
DBG (("Sending %s",temp));
157160
if ((bytes_sent = sendto(udp_sockfd,temp,strlen(temp),0,(struct sockaddr *)&client_addr,client_addr_size)) < 0) {
158-
exit(EXIT_FAILURE);
161+
perror ("sendto() failed");
162+
exit (EXIT_FAILURE);
159163
}
160164
}
161165
}
162166

163167
}
164-
165168
return NULL;
166169
}

0 commit comments

Comments
 (0)