]> rtime.felk.cvut.cz Git - frescor/fwp.git/blobdiff - wme_test/wclient.c
wme_test: Use send_async() which is implemented by FWP
[frescor/fwp.git] / wme_test / wclient.c
index e1daa8713ea3f28df3a141498de444bdcdee2891..592e5f545d8752363d1802237f07f333f33dccdd 100644 (file)
@@ -1,4 +1,5 @@
 #include <errno.h>
+#include <error.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <semaphore.h>
 #include <sys/ioctl.h>
 #include <net/if.h>
+#include <inttypes.h>
+#include <ncurses.h>
 
 #ifdef WITH_FWP
-#include <fwp_confdefs.h>
-#include <fwp.h>
-#include <fwp_vres.h>
-#include <ncurses.h>
+#include <frsh.h>
+#include <fwp_res.h>
+
+/* static UL_LOG_CUST(ulogd); */
+/* static ul_log_domain_t ulogd = {UL_LOGL_MSG, "wclient"}; */
+/* UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_wclient, ulogd); */
+
 #endif
 
 #define MAX_STREAMS  10 
@@ -42,9 +48,10 @@ unsigned opt_def_period_msec = 0;
 int opt_granularity_usec = MIN_GRANULARITY;
 bool opt_wait_for_queue_is_full; /* Don't gather any statistics until any queue is full */
 char *opt_comment = NULL;
+bool opt_gui = false;
 
 bool some_queue_is_full = false;
-struct timespec reset_timestamp;
+uint64_t reset_timestamp; /* [nsec] */
 
 bool some_contract_not_accepted = false;
 
@@ -98,19 +105,17 @@ struct stream {
 #ifndef WITH_FWP
        struct sockaddr_in rem_addr;
 #else
-       fwp_contract_d_t contract_send;
-       fwp_contract_d_t contract_resp;
-       fwp_endpoint_d_t endpoint;
-       fwp_endpoint_d_t resp_endpoint;
-       fwp_vres_d_t vres;
+       frsh_send_endpoint_t endpoint;
+       frsh_receive_endpoint_t resp_endpoint;
+       frsh_vres_id_t vres, vres_rcv;
        uint16_t resp_port;
        struct receiver receiver;
-       long wc_delay;          /* worst-case delay  */
 #endif
 
        /* Statistics */
        pthread_mutex_t mutex;
        unsigned long long sent, really_sent, received;
+       long wc_delay;          /* worst-case delay  */
 };
 
 static struct cmsg_ipi {
@@ -161,8 +166,6 @@ void stopper()
 #ifdef WITH_FWP
        for (i=0; i < nr_streams; i++) {
                if (streams[i].receiver.valid) pthread_kill(streams[i].receiver.thread, SIGUSR1);
-               if (streams[i].contract_send) fwp_contract_cancel(streams[i].contract_send);
-               if (streams[i].contract_resp)fwp_contract_cancel(streams[i].contract_resp);
        }
 #else
        for (i=0; i < AC_NUM; i++) {
@@ -185,7 +188,7 @@ void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long lon
                real[0]=0;
        }
        
-       snprintf(stream_desc, n, "%d: %s %s (%d bytes per %s +-%s, %d packets/s)%s",
+       snprintf(stream_desc, n, "%"PRIdPTR": %s %s (%d bytes per %s +-%s, %d packets/s)%s",
                 stream-streams, ac_to_text[stream->ac], bandwidth_to_text(buf[0], stream->bandwidth_bps),
                 stream->packet_size, usec_to_text(buf[1], stream->period_usec),
                 usec_to_text(buf[2], stream->jitter*stream->period_usec/100),
@@ -301,7 +304,7 @@ void save_results(int argc, char *argv[], int useconds)
        exit(0);
 }
 
-int create_ac_socket(unsigned int ac) 
+int create_ac_socket(intptr_t ac) 
 {
        int sockfd;
        unsigned int yes=1, tos;
@@ -309,21 +312,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 +335,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;
        }
@@ -347,6 +350,7 @@ void empty_handler()
 void reset_statistics()
 {
        int i;
+       struct timespec ts;
        for (i = 0; i < nr_streams; i++) {
                pthread_mutex_lock(&streams[i].mutex);
                streams[i].sent = 0;
@@ -355,13 +359,14 @@ void reset_statistics()
                pthread_mutex_unlock(&streams[i].mutex);
        }
        pthread_mutex_lock(&delay_stats_mutex);
-       clock_gettime(CLOCK_REALTIME, &reset_timestamp);
+       clock_gettime(CLOCK_REALTIME, &ts);
+       reset_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
        memset(delay_stats, 0, sizeof(delay_stats));
        pthread_mutex_unlock(&delay_stats_mutex);
 }
 
 #ifndef WITH_FWP
-int recv_packet_native(unsigned ac, struct msg_t *msg)
+int recv_packet_native(intptr_t ac, struct msg_t *msg)
 {
        int     mlen, ret;
        fd_set fdset;
@@ -376,7 +381,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,
@@ -388,8 +393,9 @@ int recv_packet_native(unsigned ac, struct msg_t *msg)
 #else
 int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
 {
-       int     mlen;
-       mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
+       size_t mlen;
+
+       mlen = frsh_receive_sync(stream->resp_endpoint, msg, sizeof(*msg), &mlen, NULL);
        return mlen;
 }
 #endif
@@ -399,9 +405,10 @@ void* receiver(void* arg)
        struct msg_t    msg;
        long long int trans_time_usec, client_to_server_usec, server_to_client_usec;
        long long int min_trans_time;
-       struct timespec send_timestamp, server_timestamp, recv_timestamp;
-       int     mlen, ret;
-       unsigned ac;
+       struct timespec ts;
+       uint64_t send_timestamp, server_timestamp, recv_timestamp;
+       int     mlen;
+       intptr_t ac;
        
        min_trans_time = ~0;
        
@@ -414,26 +421,28 @@ void* receiver(void* arg)
                ac = stream->ac;
                mlen = recv_packet_fwp(stream, &msg);
 #else
-               ac = (unsigned)arg;
+               ac = (intptr_t)arg;
                mlen = recv_packet_native(ac, &msg);
 #endif
                if (mlen < 0) {
-                       perror("Chyba pri prijimani pozadavku");
+                       if (errno != EINTR)
+                               error(0, errno, "receive_packet error");
                        goto out;
                }       
-               clock_gettime(CLOCK_REALTIME,&recv_timestamp);
+               clock_gettime(CLOCK_REALTIME,&ts);
+               recv_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
                send_timestamp = msg.send_timestamp;
                server_timestamp = msg.sendback_timestamp;
 
                /* Check whether this message was sent after reset_statistics() */
 
-               if ((ret = timespec_sub_usec(&send_timestamp, &reset_timestamp)) < 0) {
+               if (send_timestamp < reset_timestamp) {
                        continue; /* If so, don't count it */
                }
 
-               trans_time_usec = timespec_sub_usec(&recv_timestamp ,&send_timestamp) / 2;
-               client_to_server_usec = timespec_sub_usec(&server_timestamp, &send_timestamp);
-               server_to_client_usec = timespec_sub_usec(&recv_timestamp, &server_timestamp);
+               trans_time_usec = (recv_timestamp - send_timestamp) / 2 / 1000;
+               client_to_server_usec = (server_timestamp - send_timestamp) / 1000;
+               server_to_client_usec = (recv_timestamp - server_timestamp) / 1000;
 
                pthread_mutex_lock(&delay_stats_mutex);
                if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
@@ -447,11 +456,9 @@ void* receiver(void* arg)
                }
                pthread_mutex_unlock(&delay_stats_mutex);
 
-#ifdef WITH_FWP
-               if (trans_time_usec > stream->wc_delay) {
-                       stream->wc_delay = trans_time_usec;
+               if (trans_time_usec > streams[msg.stream].wc_delay) {
+                       streams[msg.stream].wc_delay = trans_time_usec;
                }
-#endif
                receivers[ac].received++;
                
                pthread_mutex_lock(&streams[msg.stream].mutex);
@@ -509,7 +516,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;
                }
@@ -520,12 +527,16 @@ send_packet_native(struct stream* stream, union msg_buff* buff)
 static inline int 
 send_packet_fwp(struct stream* stream, union msg_buff* buff)
 {
-       int ret;
+       int ret = 0;
 
        buff->msg.resp_port = htons(stream->resp_port);
-       ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
-       
-       return ret;
+       ret = frsh_send_async(stream->endpoint, buff, stream->packet_size);
+       if (ret) {
+               char msg[1024];
+               frsh_strerror(ret, msg, sizeof(msg));
+               fprintf(stderr, "frsh_send error: %s\n", msg);
+       }
+       return (ret == 0) ? 0 : -1;
 }
 #endif
 
@@ -557,13 +568,14 @@ void* sender(void* arg)
        union msg_buff buff;
        unsigned long int seqn;
        struct stream* stream = (struct stream*) arg;
+       struct timespec ts;
        int ret;
 
-#ifndef WITH_FWP
-       char stream_desc[100];
-       stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
-       printf("%s\n", stream_desc);
-#endif
+       if (!opt_gui) {
+               char stream_desc[100];
+               stream_to_text(stream_desc, sizeof(stream_desc), stream, 0);
+               printf("%s\n", stream_desc);
+       }
        if (stream->bandwidth_bps == 0)
                goto out;
 
@@ -578,10 +590,12 @@ void* sender(void* arg)
 /*             buff.msg.tos = ac_to_tos[stream->ac]; */
                buff.msg.stream = stream-streams;
                
-               clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
+               clock_gettime(CLOCK_REALTIME,&ts);
+               buff.msg.send_timestamp = ts.tv_sec*1000000000LL + ts.tv_nsec;
 
                ret = send_packet(stream, &buff);
                if (ret < 0) {
+                       stopper();
                        goto out;
                }
 
@@ -596,7 +610,7 @@ void* sender(void* arg)
                fflush(stdout);
 #endif
 
-               wait_for_next_send(stream, &buff.msg.send_timestamp);
+               wait_for_next_send(stream, &ts);
        }
 out:
        sem_post(&sem_thread_finished);
@@ -606,35 +620,42 @@ out:
 #ifdef WITH_FWP
 static int negotiate_contract_for_stream_fwp(struct stream *stream)
 {
-       fwp_contract_t contract;
-       fwp_vres_d_t vres2;
+       frsh_contract_t contract;
        int ret;
+       frsh_rel_time_t budget, period, deadline;
+       frsh_signal_info_t si;
 
        /* Contract for client->server stream */
-       memset(&contract, 0, sizeof(contract));
-       contract.budget = stream->packet_size;
-       contract.period_usec = stream->period_usec;
-       contract.deadline_usec = 3*stream->period_usec;
+       frsh_contract_init(&contract);
+       frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
+       frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
+       period = frsh_usec_to_rel_time(stream->period_usec);
+       frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_REGULAR);
+       deadline = frsh_usec_to_rel_time(3*stream->period_usec);
+       frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
        
-       stream->contract_send = fwp_contract_create(&contract);
-       ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
+       ret = frsh_contract_negotiate(&contract, &stream->vres);
+       frsh_contract_destroy(&contract);
        if (ret != 0) {
-               stream->contract_send = NULL;
-/*             fprintf(stderr, "Send contract was not accepted\n\n\n"); */
+               stream->vres = NULL;
+               fprintf(stderr, "Send contract was not accepted\n");
                return ret;
        }
 
        /* Contract for server->client stream */
-       memset(&contract, 0, sizeof(contract));
-       contract.budget = stream->packet_size;
-       contract.period_usec = stream->period_usec;
-       contract.deadline_usec = 3*stream->period_usec;
-
-       stream->contract_resp = fwp_contract_create(&contract);
-       ret = fwp_contract_negotiate(stream->contract_resp, &vres2);
+       /* TODO: Use group negotiation for these two contracts */
+       frsh_contract_init(&contract);
+       frsh_contract_set_resource_and_label(&contract, FRSH_RT_NETWORK, FRSH_NETPF_FWP, NULL);
+       frsh_network_bytes_to_budget(FRSH_NETPF_FWP, stream->packet_size, &budget);
+       period = frsh_usec_to_rel_time(stream->period_usec);
+       frsh_contract_set_basic_params(&contract, &budget, &period, FRSH_WT_BOUNDED, FRSH_CT_DUMMY);
+       deadline = frsh_usec_to_rel_time(3*stream->period_usec);
+       frsh_contract_set_timing_reqs(&contract, false, &deadline, 0, si, 0, si);
+
+       ret = frsh_contract_negotiate(&contract, &stream->vres_rcv);
+       frsh_contract_destroy(&contract);
        if (ret != 0) {
-               stream->contract_resp = NULL;
-/*             fprintf(stderr, "Receive contract was not accepted\n\n\n"); */
+               fprintf(stderr, "Receive contract was not accepted\n");
                return ret;
        }
 
@@ -652,8 +673,13 @@ static void create_stream_endpoint_fwp(struct stream *stream)
 /*     fwp_endpoint_attr_t  attr; */
        int ret;
        struct hostent* ph;
+       frsh_contract_t c;
+       fres_block_fwp_sched *fwp_sched;
+
+       frsh_vres_get_contract(stream->vres, &c);
+       fwp_sched = fres_contract_get_fwp_sched(c);
 
-       stream->ac = fwp_vres_get_ac(stream->vres);
+       stream->ac = fwp_sched->ac_id;
 
 /*     fwp_endpoint_attr_init(&attr); */
 /*     fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
@@ -661,39 +687,31 @@ static void create_stream_endpoint_fwp(struct stream *stream)
        ph = gethostbyname(server_addr);
        if (ph && ph->h_addr_list[0]) {
                struct in_addr *a = (struct in_addr *)(ph->h_addr_list[0]);
-               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);
-               }
-               ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
-               if (ret != 0) {
-                       perror("fwp_send_endpoint_bind");
-                       exit(1);
-               }
-               ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
-               if (ret != 0) {
-                       perror("fwp_receive_endpoint_create");
-                       exit(1);
-               }
+               frsh_send_endpoint_protocol_info_t    spi = { NULL, 0 };
+               frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
+               frsh_endpoint_queueing_info_t qi = { .queue_size=0, .queue_policy=FRSH_QRP_OLDEST };
+               ret = frsh_send_endpoint_create(FRSH_NETPF_FWP, a->s_addr, BASE_PORT + stream->ac, 
+                                               spi, &stream->endpoint);
+               if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
+               
+               ret = frsh_send_endpoint_bind(stream->vres, stream->endpoint);
+               if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
+
+               ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
+                                                  &stream->resp_endpoint);
+               if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
+               
                unsigned int port;
-               fwp_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL);
+               frsh_receive_endpoint_get_params(stream->resp_endpoint, NULL, &port, NULL, 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 +727,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 +762,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 %zd)", sizeof(struct msg_t));
        }
 
        stream->packet_size = packet_size;
@@ -854,11 +867,10 @@ char* parse_bandwidths(char *params)
 void wait_for_all_threads_to_finish_fwp(void)
 {
        int i;
-       /* Wait for all threads to finish */
-       /* FIXME: */
-/*     for (i=0; i < 2*nr_streams; i++) { */
-/*             sem_wait(&sem_thread_finished); */
-/*     } */
+       /* wait for all threads to finish */
+       for (i=0; i < 2*nr_streams; i++) {
+               sem_wait(&sem_thread_finished);
+       }
 }
 #else
 void wait_for_all_threads_to_finish_native(void)
@@ -871,14 +883,78 @@ void wait_for_all_threads_to_finish_native(void)
 }
 #endif
 
-#ifdef WITH_FWP
+WINDOW *logwin;
+
+#if 0
+struct log_params {
+       ul_log_domain_t *domain;
+       int level;
+       const char *format;
+       va_list ap;
+};
+
+
+int locked_log(WINDOW *logwin, void *arg)
+{
+       struct log_params *p = arg;
+       if(!(p->level&UL_LOGL_CONT)) {
+               p->level&=UL_LOGL_MASK;
+               if(p->level)
+                       wprintw(logwin,"<%d>", p->level);
+               if(p->domain && p->domain->name)
+                       wprintw(logwin,"%s: ",p->domain->name);
+       }
+       vwprintw(logwin, p->format, p->ap);
+       wnoutrefresh(logwin);
+       return 0;
+}
+
+void
+wclient_log_fnc(ul_log_domain_t *domain, int level,
+               const char *format, va_list ap)
+{
+       struct log_params p = {
+               .domain = domain,
+               .level = level,
+               .format = format,
+       };
+       va_copy(p.ap, ap);
+       va_end(ap);
+
+       use_window(logwin, locked_log, (void*)&p);
+}
+#endif
+
+
 void init_gui()
 {
-       initscr();
-       cbreak();
-       noecho();
+       if (opt_gui) {
+               initscr();
+               cbreak();
+               noecho();
+/*             nonl(); */
+/*             intrflush(stdscr, FALSE); */
+/*             keypad(stdscr, TRUE); */
+
+               logwin = newwin(0, 0, LINES/2, 0);
+               if (logwin) {
+                       scrollok(logwin, TRUE);
+/*             ul_log_redir(wclient_log_fnc, 0); */
+               }
+       }
 }
 
+void end_gui()
+{
+       if (opt_gui) {
+               endwin();
+               if (logwin) {
+/*             ul_log_redir(NULL, 0); */
+               }
+       }
+}
+
+
 #define addfield(title, format, ...)                                   \
        move(y, x);                                                     \
        x+=strlen(title)+1;                                             \
@@ -888,7 +964,7 @@ void init_gui()
                addstr(str);                                            \
        }
 
-void print_status(int seconds)
+void print_status_gui(int seconds)
 {
        int i;
        char str[200], s1[20];
@@ -907,11 +983,11 @@ void print_status(int seconds)
                addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
                addfield("Received responses", "%lld", s->received);
        }
-       refresh();
+       wnoutrefresh(stdscr);
+       doupdate();
 }
-#else
-void init_gui() {}
-void print_status(int seconds)
+
+void print_status_nogui(int seconds)
 {
        int ac;
        fprintf(stderr, "\r%3ds", seconds);
@@ -922,7 +998,6 @@ void print_status(int seconds)
        }
        fflush(stderr);
 }
-#endif
 
 int main(int argc, char *argv[])
 {
@@ -932,20 +1007,19 @@ int main(int argc, char *argv[])
        char opt;
 
 
-       while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
+       while ((opt = getopt(argc, argv, "B:b:C:c:Gg:I:j:o:qQ:s:T:")) != -1) {
                switch (opt) {
                        case 'B':
                                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;
                        }
@@ -955,11 +1029,13 @@ int main(int argc, char *argv[])
                        case 'c':
                                opt_count_sec = atoi(optarg);
                                break;
+                       case 'G':
+                               opt_gui = true;
+                               break;
                        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 +1043,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
@@ -996,7 +1071,7 @@ int main(int argc, char *argv[])
                                fprintf(stderr, "    -C  comment (added to header)\n");
                                fprintf(stderr, "    -c  count (number of seconds to run)\n");
                                fprintf(stderr, "    -g  histogram granularity [usec]\n");
-                               fprintf(stderr, "    -I  <interface> send packets from this interface");
+                               fprintf(stderr, "    -I  <interface> send packets from this interface\n");
                                fprintf(stderr, "    -j  send jitter (0-100) [%%]\n");
                                fprintf(stderr, "    -o  output filename (.dat will be appended)\n");
                                fprintf(stderr, "    -q  gather statistics only after some queue becomes full\n");
@@ -1007,15 +1082,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 +1099,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 +1114,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);
@@ -1053,12 +1122,13 @@ int main(int argc, char *argv[])
        reset_statistics();
 
 #ifdef WITH_FWP
-       rc = fwp_init();
+       //ul_log_domain_arg2levels("6");
+       rc = frsh_init();
        if (rc != 0) {
-               fprintf(stderr, "FWP initialization failed\n");
-               exit(1);
+               error(1, errno, "FWP initialization failed");
        }
 #else
+       intptr_t ac;
        /* create four receivers each per AC */
        for (ac = AC_NUM - 1; ac >= 0; ac--) {
                ac_sockfd[ac] = create_ac_socket(ac);
@@ -1067,8 +1137,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 +1150,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 +1161,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) {
@@ -1107,31 +1172,45 @@ int main(int argc, char *argv[])
                seconds = 1;
                frames=0;
                while (!exit_flag) {
-                       usleep(40000);
-                       frames++;
-                       if (frames>=25) {
+                       if (opt_gui) {
+                               usleep(40000);
+                               frames++;
+                               if (frames>=25) {
+                                       seconds++;
+                                       frames = 0;
+                               }
+                               print_status_gui(seconds);
+                       } else {
+                               sleep(1);
                                seconds++;
-                               frames = 0;
+                               print_status_nogui(seconds);
                        }
-                       print_status(seconds);
+
                        if (seconds == opt_count_sec)
                                stopper();
                }
        }
 
+       end_gui();
 #ifdef WITH_FWP
-       endwin();
-       fwp_done();
-#else
+       for (i=0; i < nr_streams; i++) {
+               if (streams[i].vres)
+                       frsh_contract_cancel(streams[i].vres);
+               if (streams[i].vres_rcv)
+                       frsh_contract_cancel(streams[i].vres_rcv);
+       }
+#endif
+
        fprintf(stderr, "\nWaiting for threads to finish\n");
        wait_for_all_threads_to_finish();
 
-       struct timespec end_timestamp, measure_length;
-       clock_gettime(CLOCK_REALTIME,&end_timestamp);
-       timespec_sub(&measure_length, &end_timestamp, &reset_timestamp);
+       struct timespec ts;
+       uint64_t end_timestamp, measure_length;
+       clock_gettime(CLOCK_REALTIME,&ts);
+       end_timestamp = ts.tv_sec*1000000000LL+ts.tv_nsec;
+       measure_length = end_timestamp - reset_timestamp;
 
-       save_results(argc, argv, timespec2usec(&measure_length));
-#endif
+       save_results(argc, argv, measure_length/1000);
 
        return 0;
 }