]> rtime.felk.cvut.cz Git - frescor/fwp.git/blobdiff - wme_test/wclient.c
wclient now uses error() to print error messages
[frescor/fwp.git] / wme_test / wclient.c
index e1daa8713ea3f28df3a141498de444bdcdee2891..857af5da9afe4065b3f0d03b569b31e2e5bdaef3 100644 (file)
@@ -1,4 +1,5 @@
 #include <errno.h>
+#include <error.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -309,21 +310,21 @@ int create_ac_socket(unsigned int ac)
 
        if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        {
-               perror("Unable to open socket");
+               error(0, errno, "Unable to open socket");
                return -1;
        }
        if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
-               perror("set non-blocking socket");
+               error(0, errno, "set non-blocking socket");
                return -1;
        }
        if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
-               perror("Unable to set socket");
+               error(0, errno, "Unable to set socket");
                return -1;
        }
 
        if (opt_send_buf_size >= 0) {
                if (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&opt_send_buf_size,sizeof(opt_send_buf_size)) == -1) {
-                       perror("Unable to set socket buffer size");
+                       error(0, errno, "Unable to set socket buffer size");
                        return -1;
                }
        }
@@ -332,7 +333,7 @@ int create_ac_socket(unsigned int ac)
        //tos = ((AC_NUM - ac) *2 - 1)*32;
        tos = ac_to_tos[ac];
        if (setsockopt(sockfd, SOL_IP, IP_TOS, &tos, sizeof(tos))) {
-               perror("Unable to set TOS");
+               error(0, errno, "Unable to set TOS");
                close(sockfd);
                return -1;
        }
@@ -376,7 +377,7 @@ int recv_packet_native(unsigned ac, struct msg_t *msg)
                ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
                if (ret < 0) {
                        if (errno == EINTR) continue;
-                       perror("receiver select");
+                       error(0, errno, "receiver select");
                        return -1;
                }
                mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
@@ -418,7 +419,7 @@ void* receiver(void* arg)
                mlen = recv_packet_native(ac, &msg);
 #endif
                if (mlen < 0) {
-                       perror("Chyba pri prijimani pozadavku");
+                       error(0, errno, "receive_packet error");
                        goto out;
                }       
                clock_gettime(CLOCK_REALTIME,&recv_timestamp);
@@ -509,7 +510,7 @@ send_packet_native(struct stream* stream, union msg_buff* buff)
                        ret = 0;
                        break;
                } else {
-                       perror("Error while sending");
+                       error(0, errno, "Error while sending");
                        ret = -1;
                        break;
                }
@@ -664,36 +665,25 @@ static void create_stream_endpoint_fwp(struct stream *stream)
                ret = fwp_send_endpoint_create(a->s_addr, BASE_PORT + stream->ac,
                                               NULL, &stream->endpoint);
                /* stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac); */
-               if (ret < 0) {
-                       perror("fwp_send_endpoint_create");
-                       exit(1);
-               }
+               if (ret < 0) error(1, errno, "fwp_send_endpoint_create");
+               
                ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
-               if (ret != 0) {
-                       perror("fwp_send_endpoint_bind");
-                       exit(1);
-               }
+               if (ret != 0) error(1, errno, "fwp_send_endpoint_bind");
+               
                ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
-               if (ret != 0) {
-                       perror("fwp_receive_endpoint_create");
-                       exit(1);
-               }
+               if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
+               
                unsigned int port;
                fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
                stream->resp_port = port;
 
                ret = pthread_create(&stream->receiver.thread, NULL, receiver, (void*)stream);
-               if (ret) {
-                       perror("Error while creating receiver\n");
-                       exit(1);
-               }
+               if (ret) error(1, ret, "Error while creating receiver");
+               
                stream->receiver.valid = true;
        }
        else {
-               char str[100];
-               snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
-               perror(str);
-               exit(1);
+               error(1, errno, "gethostbyname(%s)", server_addr);
        }
 
 }
@@ -709,10 +699,7 @@ static void create_stream_endpoint_native(struct stream *stream)
        if (ph)
                stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
        else {
-               char str[100];
-               snprintf(str, sizeof(str), "gethostbyname(%s)", server_addr);
-               perror(str);
-               exit(1);
+               error(1, errno, "gethostbyname(%s)", server_addr);
        }
        stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
 }
@@ -747,13 +734,11 @@ calc_stream_params(struct stream *stream)
        } else {
                char buf[200];
                stream_to_text(buf, sizeof(buf), stream, 0);
-               fprintf(stderr, "Neither packet size nor period was specified for a stream %s\n", buf);
-               exit(1);
+               error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
        }
 
        if (packet_size < sizeof(struct msg_t)) {
-               fprintf(stderr, "Packet size too small (min %d)\n", sizeof(struct msg_t));
-               exit(1);
+               error(1, 0, "Packet size too small (min %d)", sizeof(struct msg_t));
        }
 
        stream->packet_size = packet_size;
@@ -938,14 +923,13 @@ int main(int argc, char *argv[])
                                opt_def_bandwidth = atoi(optarg);
                                break;
                        case 'b': {
-                               char *error;
-                               error = parse_bandwidths(optarg);
-                               if (error != NULL) {
-                                       if (*error == '\0')
-                                               fprintf(stderr, "Bandwidth parse error - string to short\n");
+                               char *errpos;
+                               errpos = parse_bandwidths(optarg);
+                               if (errpos != NULL) {
+                                       if (*errpos == '\0')
+                                               error(1, 0, "Bandwidth parse error - string to short");
                                        else
-                                               fprintf(stderr, "Bandwidth parse error at '%s'\n", error);
-                                       exit(1);
+                                               error(1, 0, "Bandwidth parse error at '%s'", errpos);
                                }
                                break;
                        }
@@ -958,8 +942,7 @@ int main(int argc, char *argv[])
                        case 'g':
                                opt_granularity_usec = atoi(optarg);
                                if (opt_granularity_usec < MIN_GRANULARITY) {
-                                       fprintf(stderr, "Granulatiry too small (min %d)!\n", MIN_GRANULARITY);
-                                       exit(1);
+                                       error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
                                }
                                break;
                        case 'I':
@@ -967,8 +950,7 @@ int main(int argc, char *argv[])
                                break;
                        case 'j':
                                #ifdef WITH_FWP
-                               fprintf(stderr, "-j is not allowd when compiled with FWP\n");
-                               exit(1);
+                               error(1, 0, "-j is not allowd when compiled with FWP");
                                #else
                                opt_jitter = atoi(optarg);
                                #endif
@@ -1007,15 +989,13 @@ int main(int argc, char *argv[])
                }
        }
        if (opt_packet_size && opt_def_period_msec) {
-               fprintf(stderr, "Error: Nonzero -T and -s can't be used together!.\n");
-               exit(1);
+               error(1, 0, "Error: Nonzero -T and -s can't be used together!");
        }
 
        if (optind < argc) {
                server_addr = argv[optind];
        } else {
-               fprintf(stderr, "Expected server address argument\n");
-               exit(1);
+               error(1, 0, "Expected server address argument");
        }
 
        if (nr_streams == 0)
@@ -1026,17 +1006,14 @@ int main(int argc, char *argv[])
        snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
 
        if ((logfd = fopen(logfname,"w+")) == NULL) {
-               fprintf(stderr,"Can not open %s\n", logfname);
-               exit(1);
+               error(1, errno ,"Can not open %s", logfname);
        }
        if (signal(SIGTERM, stopper) == SIG_ERR) {
-               perror("Error in signal registration");
-               exit(1);
+               error(1, errno, "Error in signal registration");
        }
                
        if (signal(SIGINT, stopper) == SIG_ERR) {
-               perror("Signal handler registration error");
-               exit(1);
+               error(1, errno, "Signal handler registration error");
        }
 
        struct sigaction sa;
@@ -1044,8 +1021,7 @@ int main(int argc, char *argv[])
        sa.sa_flags = 0;        /* don't restart syscalls */
 
        if (sigaction(SIGUSR1, &sa, NULL) < 0) {
-               perror("sigaction error");
-               exit(1);
+               error(1, errno, "sigaction error");
        }
 
        sem_init(&sem_thread_finished, 0, 0);
@@ -1055,8 +1031,7 @@ int main(int argc, char *argv[])
 #ifdef WITH_FWP
        rc = fwp_init();
        if (rc != 0) {
-               fprintf(stderr, "FWP initialization failed\n");
-               exit(1);
+               error(1, errno, "FWP initialization failed");
        }
 #else
        /* create four receivers each per AC */
@@ -1067,8 +1042,7 @@ int main(int argc, char *argv[])
                }
                rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
                if (rc) {
-                       fprintf(stderr, "Error while creating receiver %d\n",rc);
-                       return 1;
+                       error(1, rc, "Error while creating receiver");
                }
                receivers[ac].valid = true;
        }               
@@ -1081,8 +1055,7 @@ int main(int argc, char *argv[])
                memset(&ifr, 0, sizeof(ifr));
                strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
                if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
-                       fprintf(stderr, "unknown iface %s\n", opt_interface);
-                       exit(1);
+                       error(1, 0, "unknown iface %s", opt_interface);
                }
                cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
                cmsg_len = sizeof(cmsg);
@@ -1093,10 +1066,7 @@ int main(int argc, char *argv[])
                pthread_mutex_init(&s->mutex, NULL);
                calc_stream_params(s);
                rc = pthread_create(&thread, &attr, sender, (void*) s);
-               if (rc) {
-                       fprintf(stderr, "Error while creating sender %d\n",rc);
-                       return 1;
-               }
+               if (rc) error(1, rc, "Error while creating sender");
        }
 
        if (some_contract_not_accepted) {