]> rtime.felk.cvut.cz Git - frescor/fwp.git/blobdiff - wme_test/wclient.c
wme_test: update status every second when compiled without FWP
[frescor/fwp.git] / wme_test / wclient.c
index 2876c64cb9687188b435e987fb4626dfa31b13ca..30fb5f162f684a41086a75e923ee0b7a5a36b40d 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 <pthread.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdbool.h>
+#include "common.h"
+#include <semaphore.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
 
-/*#define AC_VO 0
-#define AC_VI 1
-#define AC_BE 2
-#define AC_BK 3
-*/
+#ifdef WITH_FWP
+#include <fwp_confdefs.h>
+#include <fwp.h>
+#include <fwp_vres.h>
+#include <ncurses.h>
+#endif
+
+#define MAX_STREAMS  10 
+#define MIN_GRANULARITY 100
+
+unsigned opt_packet_size = 800;
+int opt_send_buf_size = -1;
+unsigned opt_period_usec = 10*MSEC_TO_USEC;
+char   *opt_interface;
+unsigned opt_jitter = 0;
+char    *opt_output = "delay_stats";
+unsigned opt_count_sec = 0;
+unsigned opt_def_bandwidth = 50;
+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 some_queue_is_full = false;
+struct timespec reset_timestamp;
 
-#define PARAM_SERVERADDR 1
-#define BASE_PORT       5100
-#define AC_QUEUES       4
-#define MAX_SENDENDPOINTS  10
-#define MTU             800 
+bool some_contract_not_accepted = false;
 
+/* Locked when some queue is full to prevent multiple resets of
+   statstics. */
+pthread_mutex_t queue_full_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-enum {  AC_VO = 0,
-       AC_VI = 1,
-       AC_BE = 2,
-       AC_BK = 3
+int ac_sockfd[AC_NUM];
+
+struct receiver {
+       bool valid;
+       pthread_t thread;
+       unsigned received, last_received;
 };
 
-const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
-const unsigned int ac_to_tos[4] = {224,160,96,64};
+struct receiver        receivers[AC_NUM];
 
-int ac_sockfd[AC_QUEUES];
 FILE* logfd;
 char* server_addr; 
+char logfname[100];
 
-const char logfname[] = "delay_stats.dat";
+/* maximal traffic delay in ms - 10 s*/
+#define MAX_DELAY_US 10000000
 
-struct msg_t {
-       unsigned int tos;
-       struct timespec send_timestamp;
-       unsigned long int seqn;
-       unsigned char padding[MTU];
+struct delay_stat {
+       unsigned csc;           /* Client-server-client delay divided by 2 */
+       unsigned cs;            /* Client-server delay */
+       unsigned sc;            /* Server-client delay */
 };
 
-/* maximal traffic delay in ms */
-#define MAX_DELAY 10000
-#define GRANULARITY 10
-
-unsigned short int delay_stats[AC_QUEUES][MAX_DELAY/GRANULARITY];
+struct delay_stat delay_stats[AC_NUM][MAX_DELAY_US/MIN_GRANULARITY];
+pthread_mutex_t delay_stats_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-/*struct ac_stats[AC_QUEUES] {
+/*struct ac_stats[AC_NUM] {
    unsigned long int min_trans_time;
    unsigned long int sum_trans_time;
    struct timespec   recv_timestamp;
    struct timespec   send_timestamp; 
 };*/
 
-struct send_endpoint {
-       int ac;
-       long period_nsec;
+struct stream {
+       /* Input parameters */
+       enum ac ac;             /*  */
        int bandwidth_bps;      /* bits per second */
-};
+       int jitter;             /* percent */
+       /* Mulualy exclusive input parameters */
+       int packet_size;
+       long period_usec;       /* all time units are in microseconds */
 
-#define MSEC (1000*1000)
-#define Mbit (1000*1000)
-#define Kbit 1000
+       /* Internal fields */
+#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;
+       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;
+};
 
+static struct cmsg_ipi {
+       struct cmsghdr cm;
+       struct in_pktinfo ipi;
+} cmsg = { {sizeof(struct cmsg_ipi), SOL_IP, IP_PKTINFO},
+          {0, }};
+int cmsg_len = 0;
+
+/*
 struct send_endpoint sepoint[] = {
-       { .ac = AC_VO, .period_nsec=200*MSEC, .bandwidth_bps = 34*Kbit },
-       { .ac = AC_VI, .period_nsec=25*MSEC, .bandwidth_bps =  480*Kbit },
-       { .ac = AC_BE, .period_nsec=40*MSEC, .bandwidth_bps =  300*Kbit },
-       { .ac = AC_BK, .period_nsec=40*MSEC, .bandwidth_bps =  300*Kbit },
-//     { .ac = AC_VI, .period_nsec=17*MSEC, .bandwidth_bps =  675*Kbit },
+       { .ac = AC_VO, .period_usec=200*MSEC_TO_USEC, .bandwidth_bps = 34*Kbit },
+       { .ac = AC_VI, .period_usec=25*MSEC_TO_USEC, .bandwidth_bps =  480*Kbit },
+       { .ac = AC_BE, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
+       { .ac = AC_BK, .period_usec=40*MSEC_TO_USEC, .bandwidth_bps =  300*Kbit },
+//     { .ac = AC_VI, .period_usec=17*MSEC_TO_USEC, .bandwidth_bps =  675*Kbit },
 };
+*/
+
+struct stream streams[MAX_STREAMS];
 
-unsigned int nr_sepoints = sizeof(sepoint)/sizeof(*sepoint);
+unsigned int nr_streams = 0;
+
+sem_t sem_thread_finished;
+
+bool exit_flag = false;
+
+#ifdef WITH_FWP
+#define negotiate_contract_for_stream(s) negotiate_contract_for_stream_fwp(s)
+#define create_stream_endpoint(s) create_stream_endpoint_fwp(s)
+#define send_packet(s, b) send_packet_fwp(s, b)
+#define recv_packet(s, b) recv_packet_fwp(s, b)
+#define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_fwp()
+#else
+#define negotiate_contract_for_stream(s) 0
+#define create_stream_endpoint(s) create_stream_endpoint_native(s)
+#define send_packet(s, b) send_packet_native(s, b)
+#define recv_packet(s, b) recv_packet_native(s, b)
+#define wait_for_all_threads_to_finish() wait_for_all_threads_to_finish_native()
+#endif
 
 void stopper()
 {
-       int ac,i;
-
-       printf("Writing data to log file...\n");
-       for (ac = 0; ac < AC_QUEUES; ac++) { 
-               for ( i = 0 ; i < MAX_DELAY/GRANULARITY; i++) 
-                       if (delay_stats[ac][i])
-                               fprintf(logfd,"%d %d\n", i*GRANULARITY,\
-                                        delay_stats[ac][i]);
-               
-               fprintf(logfd, "\n\n");
-       }
-       
-       printf("Finished.\n");
-       fclose(logfd);
+       int i;
+       exit_flag = true;
 
-       exit(0);
+       /* Interrupt all receivers */
+#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++) {
+               pthread_kill(receivers[i].thread, SIGUSR1);
+       }       
+#endif
 }
 
-static inline 
-void timespec_add (struct timespec *sum, const struct timespec *left,
-             const struct timespec *right)
+void stream_to_text(char *stream_desc, size_t n, struct stream *stream, long long useconds)
 {
-       sum->tv_sec = left->tv_sec + right->tv_sec;
-       sum->tv_nsec = left->tv_nsec + right->tv_nsec;
+       char buf[3][12];
+       char real[100];
 
-       if (sum->tv_nsec >= 1000000000){
-               ++sum->tv_sec;
-               sum->tv_nsec -= 1000000000;
+       if (useconds) {
+               snprintf(real, sizeof(real), "; real: %s sent %lld (%lld/s), received %lld (%lld/s)",
+                        bandwidth_to_text(buf[0], (long long)stream->really_sent*stream->packet_size*8*SEC_TO_USEC/useconds),
+                        stream->sent, stream->sent*SEC_TO_USEC/useconds,
+                        stream->received, stream->received*SEC_TO_USEC/useconds);
+       } else {
+               real[0]=0;
        }
+       
+       snprintf(stream_desc, n, "%d: %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),
+                (int)(SEC_TO_USEC/stream->period_usec), real);
 }
 
-static inline 
-void timespec_sub (struct timespec *diff, const struct timespec *left,
-             const struct timespec *right)
+void save_results(int argc, char *argv[], int useconds)
 {
-       diff->tv_sec = left->tv_sec - right->tv_sec;
-       diff->tv_nsec = left->tv_nsec - right->tv_nsec;
+       int ac, i, maxi;
+       const int mini = 3000/opt_granularity_usec;
+       bool allzeros;
+       unsigned send_count[AC_NUM];
+
+       fprintf(stderr, "Writing data to %s... ", logfname);
+       fflush(stderr);
+
+       fprintf(logfd, "# Invoked as: ");
+       for (i=0; i<argc; i++) fprintf(logfd, "%s ", argv[i]);
+       if (opt_comment) {
+               fprintf(logfd, "(%s)", opt_comment);
+       }
+       fprintf(logfd, "\n");
+
+       if (useconds/SEC_TO_USEC != opt_count_sec) {
+               char buf[20];
+               usec_to_text(buf, useconds);
+               fprintf(logfd, "# Data gathered for %s.\n", buf);
+       }
+               
+       for (i = 0; i < nr_streams; i++) {
+               char stream_desc[200];
+               stream_to_text(stream_desc, sizeof(stream_desc), &streams[i], useconds);
+               fprintf(logfd, "# Stream %s\n", stream_desc);
+       }
+
+       /* Find maximal delay */
+       allzeros = true;
+       for (maxi = MAX_DELAY_US/opt_granularity_usec - 1; maxi >= 0; maxi--) {
+               for (ac = 0; ac < AC_NUM; ac++) {
+                       if ((delay_stats[ac][maxi].csc != 0) ||
+                           (delay_stats[ac][maxi].cs != 0) ||
+                           (delay_stats[ac][maxi].sc != 0))
+                               allzeros = false;
+               }
+               if (!allzeros) break;
+       }
+       maxi++;
+       if (maxi < mini) maxi = mini;
+
+       /* Calculate total number of sent packets per AC */
+       memset(send_count, 0, sizeof(send_count));
+       for (i = 0; i < nr_streams; i++) {
+               ac = streams[i].ac;
+               send_count[ac] += streams[i].sent;
+       }
+
+#if 0
+       /* Write pdf */
+       for ( i = 0 ; i < maxi; i++) {
+               fprintf(logfd,"\n%f", i*opt_granularity_usec/1000.0);
+               for (ac = 0; ac < AC_NUM; ac++) { 
+                       if (sum[ac])
+                               val = (double)delay_stats[ac][i]*100.0 / sum[ac];
+                       else val = -1; /* Don't display this ac */
+                       fprintf(logfd," %lf", val);
+               }
+       }
+       
+       fprintf(logfd,"\n\n");
+#endif
+       
+       fprintf(logfd,"## Format: msec csc%% cs%% sc%%\n");
+
+       /* Write PDF */
+       for (ac = 0; ac < AC_NUM; ac++) {
+               struct delay_stat integral = {0,0,0}, last = {-1,-1,-1};
+
+               fprintf(logfd,"%f %f %f %f\n", 0.0, 0.0, 0.0, 0.0);
 
-       if (diff->tv_nsec < 0){
-                 --diff->tv_sec;
-                 diff->tv_nsec += 1000000000;
+               if (send_count[ac] != 0) {
+                       i=0;
+                       while ((delay_stats[ac][i].csc == 0) &&
+                              (delay_stats[ac][i].cs == 0) &&
+                              (delay_stats[ac][i].sc == 0)) i++;
+               
+                       for (i++; i < maxi+1; i++) {
+                               if (memcmp(&last, &integral, sizeof(last))) {
+                                       char buf[3][20];
+                                       snprintf(buf[0], sizeof(buf[0]), "%f", (double)integral.csc*100.0 / send_count[ac]);
+                                       snprintf(buf[1], sizeof(buf[1]), "%f", (double)integral.cs *100.0 / send_count[ac]);
+                                       snprintf(buf[2], sizeof(buf[2]), "%f", (double)integral.sc *100.0 / send_count[ac]);
+                                                
+                                       fprintf(logfd,"%f %s %s %s\n", i*opt_granularity_usec/1000.0,
+                                               integral.csc != last.csc ? buf[0] : "-",
+                                               integral.cs  != last.cs  ? buf[1] : "-",
+                                               integral.sc  != last.sc  ? buf[2] : "-"
+                                               );
+                                       last = integral;
+                               }
+                               if (i>0) {
+                                       integral.csc += delay_stats[ac][i-1].csc;
+                                       integral.sc  += delay_stats[ac][i-1].sc;
+                                       integral.cs  += delay_stats[ac][i-1].cs;
+                               }
+                       }
+               }
+               fprintf(logfd,"\n\n");
        }
+       
+       fprintf(stderr, "finished.\n");
+       fclose(logfd);
+
+       exit(0);
 }
 
 int create_ac_socket(unsigned int ac) 
@@ -138,20 +310,30 @@ int create_ac_socket(unsigned int ac)
 
        if ((sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        {
-               perror("Unable to open socket\n");
+               error(0, errno, "Unable to open socket");
+               return -1;
+       }
+       if (fcntl(sockfd, F_SETFL, O_NONBLOCK) != 0) {
+               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\n");
+               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) {
+                       error(0, errno, "Unable to set socket buffer size");
+                       return -1;
+               }
+       }
+
        
-       //tos = ((AC_QUEUES - ac) *2 - 1)*32;
+       //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\n");
+               error(0, errno, "Unable to set TOS");
                close(sockfd);
                return -1;
        }
@@ -159,36 +341,124 @@ int create_ac_socket(unsigned int ac)
        return sockfd;
 }
 
+void empty_handler()
+{
+}
 
-void* receiver(void* queue)
+void reset_statistics()
 {
-       struct msg_t    msg;
+       int i;
+       for (i = 0; i < nr_streams; i++) {
+               pthread_mutex_lock(&streams[i].mutex);
+               streams[i].sent = 0;
+               streams[i].really_sent = 0;
+               streams[i].received = 0;
+               pthread_mutex_unlock(&streams[i].mutex);
+       }
+       pthread_mutex_lock(&delay_stats_mutex);
+       clock_gettime(CLOCK_REALTIME, &reset_timestamp);
+       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     mlen, ret;
+       fd_set fdset;
        struct  sockaddr_in rem_addr;
+       unsigned int rem_addr_length; 
+
+       FD_ZERO(&fdset);
+       FD_SET(ac_sockfd[ac], &fdset);
+        rem_addr_length = sizeof(rem_addr);
+       mlen = -1;
+       while (!exit_flag) {
+               ret = select(ac_sockfd[ac]+1, &fdset, NULL, NULL, NULL);
+               if (ret < 0) {
+                       if (errno == EINTR) continue;
+                       error(0, errno, "receiver select");
+                       return -1;
+               }
+               mlen = recvfrom(ac_sockfd[ac], msg, sizeof(*msg), 0,
+                               (struct sockaddr*)&rem_addr, &rem_addr_length);
+               break;
+       }
+       return mlen;
+}
+#else
+int recv_packet_fwp(struct stream *stream, struct msg_t *msg)
+{
        int     mlen;
-       unsigned int ac, rem_addr_length; 
-       unsigned long int trans_time_msec;
-       unsigned long int min_trans_time;
-       struct timespec   send_timestamp,recv_timestamp, trans_time; 
+       mlen = fwp_recv(stream->resp_endpoint, msg, sizeof(*msg), 0);
+       return mlen;
+}
+#endif
+
+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;
        
        min_trans_time = ~0;
        
-       ac = (int)queue;
-        rem_addr_length = sizeof(rem_addr);
-       while (1) {
-               while ((mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,\
-                       (struct sockaddr*)&rem_addr, &rem_addr_length)) < 0) {
-                           if (errno == EINTR) continue;
-                           perror("Chyba pri prijimani pozadavku\n");
-                           return NULL;
+       block_signals();
+       set_rt_prio(99);
+
+       while (!exit_flag) {
+#ifdef WITH_FWP
+               struct stream *stream = arg;
+               ac = stream->ac;
+               mlen = recv_packet_fwp(stream, &msg);
+#else
+               ac = (unsigned)arg;
+               mlen = recv_packet_native(ac, &msg);
+#endif
+               if (mlen < 0) {
+                       if (errno != EINTR)
+                               error(0, errno, "receive_packet error");
+                       goto out;
                }       
-               clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
+               clock_gettime(CLOCK_REALTIME,&recv_timestamp);
                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) {
+                       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);
+
+               pthread_mutex_lock(&delay_stats_mutex);
+               if (trans_time_usec < MAX_DELAY_US && trans_time_usec >= 0) {
+                       delay_stats[ac][trans_time_usec/opt_granularity_usec].csc++;
+               }
+               if (client_to_server_usec < MAX_DELAY_US && client_to_server_usec >= 0) {
+                       delay_stats[ac][client_to_server_usec/opt_granularity_usec].cs++;
+               }
+               if (server_to_client_usec < MAX_DELAY_US && server_to_client_usec >= 0) {
+                       delay_stats[ac][server_to_client_usec/opt_granularity_usec].sc++;
+               }
+               pthread_mutex_unlock(&delay_stats_mutex);
+
+#ifdef WITH_FWP
+               if (trans_time_usec > stream->wc_delay) {
+                       stream->wc_delay = trans_time_usec;
+               }
+#endif
+               receivers[ac].received++;
                
-               timespec_sub(&trans_time,&recv_timestamp ,&send_timestamp);
-               trans_time_msec = (trans_time.tv_sec * 1000 + \
-                                        trans_time.tv_nsec / 1000000) /2;
-         
-               delay_stats[ac][trans_time_msec/GRANULARITY]++;
+               pthread_mutex_lock(&streams[msg.stream].mutex);
+               streams[msg.stream].received++;
+               pthread_mutex_unlock(&streams[msg.stream].mutex);
        
                /*if (trans_time_nsec < min_trans_time) 
                        min_trans_time = trans_time_nsec;*/
@@ -198,134 +468,646 @@ void* receiver(void* queue)
                         send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
                         recv_timestamp.tv_nsec, trans_time_msec); */
        }
+out:
+       sem_post(&sem_thread_finished);
+       return NULL;
 }
 
-void* sender(void* endpoint)
+/** 
+ * Send a packet.
+ * 
+ * @return -1 in case of error, 1 in case of sucessfull send and 0
+ *        when all buffers are full.
+ */
+#ifndef WITH_FWP
+static inline int 
+send_packet_native(struct stream* stream, union msg_buff* buff)
+{
+       struct iovec  iov;
+       struct msghdr msg;
+
+       iov.iov_base = buff;
+       iov.iov_len = stream->packet_size;
+       msg.msg_name = (void*)&stream->rem_addr;
+       msg.msg_namelen = sizeof(stream->rem_addr);
+       msg.msg_iov = &iov;
+       msg.msg_iovlen = 1;
+       msg.msg_flags = 0;
+       msg.msg_control = &cmsg;
+       msg.msg_controllen = cmsg_len;
+
+       int ret = 1;
+       
+       while (sendmsg(ac_sockfd[stream->ac], &msg, 0) < 0) {
+               if (errno == EINTR) continue;
+               if (errno == EAGAIN) {
+                       if (opt_wait_for_queue_is_full && 
+                           !some_queue_is_full && 
+                           /* We use mutex as atomic test and set */
+                           (pthread_mutex_trylock(&queue_full_mutex) != EBUSY)) {
+                               some_queue_is_full = true;
+                               reset_statistics();
+                       }
+                       ret = 0;
+                       break;
+               } else {
+                       error(0, errno, "Error while sending");
+                       ret = -1;
+                       break;
+               }
+       }
+       return ret;
+}
+#else
+static inline int 
+send_packet_fwp(struct stream* stream, union msg_buff* buff)
+{
+       int ret;
+
+       buff->msg.resp_port = htons(stream->resp_port);
+       ret = fwp_send(stream->endpoint, buff, stream->packet_size, 0);
+       
+       return ret;
+}
+#endif
+
+static inline void
+wait_for_next_send(struct stream* stream, struct timespec *last_send_time)
 {
-       struct sockaddr_in rem_addr;
-       struct msg_t msg;
-       unsigned long int seqn;
        struct timespec time_to_wait, current_time, period, interval;
-       struct hostent* ph;
-       int ac;
-       struct send_endpoint* spoint;
+       unsigned period_usec = stream->period_usec;
+
+       /*           |~~~+~~~| jitter interval (width = 2*stream->jitter percentage from period)*/
+       /* |-------------|     nominal period*/
+       if (stream->jitter) {
+               period.tv_nsec = USEC_TO_NSEC*(period_usec*(100-stream->jitter)/100
+                                              + rand() % (2*period_usec*stream->jitter/100));
+       } else {
+               period.tv_nsec = USEC_TO_NSEC*(period_usec);
+       }
+       period.tv_sec = 0;
        
-       spoint = (struct send_endpoint*) endpoint;
-       memset(&rem_addr,0, sizeof(rem_addr));
-       //-------------------------------------------------------------------
-       // TODO: not functioning - check it
+       timespec_add(&time_to_wait, last_send_time, &period);
+       clock_gettime(CLOCK_REALTIME,&current_time);
+       timespec_sub(&interval,&time_to_wait,&current_time);
+       nanosleep(&interval,NULL);
+}
+
+
+void* sender(void* arg)
+{
+       union msg_buff buff;
+       unsigned long int seqn;
+       struct stream* stream = (struct stream*) arg;
+       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 (stream->bandwidth_bps == 0)
+               goto out;
+
+       seqn = 0;
+       
+       block_signals();
+       set_rt_prio(90-stream->ac);
+
+       while (!exit_flag) {
+
+/*             buff.msg.seqn = seqn++; */
+/*             buff.msg.tos = ac_to_tos[stream->ac]; */
+               buff.msg.stream = stream-streams;
                
-       if ((rem_addr.sin_addr.s_addr = inet_addr(server_addr)) == INADDR_NONE){
-          ph = gethostbyname(server_addr);
-          if (ph) 
-                  rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
-          else {
-                  perror("Unknown server\n"); 
-          }
+               clock_gettime(CLOCK_REALTIME,&buff.msg.send_timestamp);
+
+               ret = send_packet(stream, &buff);
+               if (ret < 0) {
+                       goto out;
+               }
+
+               pthread_mutex_lock(&stream->mutex);
+               stream->sent++;
+               if (ret > 0)
+                       stream->really_sent++;
+               pthread_mutex_unlock(&stream->mutex);
+
+#ifdef DEBUG
+               printf("%d", stream->ac);
+               fflush(stdout);
+#endif
+
+               wait_for_next_send(stream, &buff.msg.send_timestamp);
        }
+out:
+       sem_post(&sem_thread_finished);
+       return NULL;
+}
+
+#ifdef WITH_FWP
+static int negotiate_contract_for_stream_fwp(struct stream *stream)
+{
+       fwp_contract_t contract;
+       fwp_vres_d_t vres2;
+       int ret;
+
+       /* 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;
        
-       //------------------------------------------------------------------
-       rem_addr.sin_family = AF_INET;
-       rem_addr.sin_addr.s_addr =  inet_addr(server_addr);
-       rem_addr.sin_port = htons(BASE_PORT + ac);
-       seqn = 0;
+       stream->contract_send = fwp_contract_create(&contract);
+       ret = fwp_contract_negotiate(stream->contract_send, &stream->vres);
+       if (ret != 0) {
+               stream->contract_send = NULL;
+/*             fprintf(stderr, "Send contract was not accepted\n\n\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);
+       if (ret != 0) {
+               stream->contract_resp = NULL;
+/*             fprintf(stderr, "Receive contract was not accepted\n\n\n"); */
+               return ret;
+       }
+
+       /* We don't use the vres at server, since the server doesn't
+        * know the parameters. Instread, server uses plain
+        * sockets. */
        
-       period.tv_nsec = spoint->period_nsec;
-       period.tv_sec = 0;
+       return ret;
+}
+#endif
+
+#ifdef WITH_FWP
+static void create_stream_endpoint_fwp(struct stream *stream)
+{
+/*     fwp_endpoint_attr_t  attr; */
+       int ret;
+       struct hostent* ph;
+
+       stream->ac = fwp_vres_get_ac(stream->vres);
 
-       ac = spoint->ac;
-       while (1) {
+/*     fwp_endpoint_attr_init(&attr); */
+/*     fwp_endpoint_attr_setreliability(&attr, FWP_EPOINT_BESTEFFORT); */
 
-               msg.seqn = seqn;
-               msg.tos = ac_to_tos[ac];
+       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) error(1, errno, "fwp_send_endpoint_create");
                
-               clock_gettime(CLOCK_MONOTONIC,&msg.send_timestamp);
+               ret = fwp_send_endpoint_bind(stream->endpoint, stream->vres);
+               if (ret != 0) error(1, errno, "fwp_send_endpoint_bind");
                
-               while (sendto(ac_sockfd[ac], &msg, sizeof(msg), 0,\
-                               (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
-                               if (errno == EINTR) continue;
-                               perror("Error while sending.\n");
-                               return NULL;
-               }               
-
-               seqn++;
+               ret = fwp_receive_endpoint_create(0, NULL, &stream->resp_endpoint);
+               if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
                
-               timespec_add(&time_to_wait,&msg.send_timestamp,&period);
-               clock_gettime(CLOCK_MONOTONIC,&current_time);
-               timespec_sub(&interval,&time_to_wait,&current_time);
-               nanosleep(&interval,NULL);
-       }       
+               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) error(1, ret, "Error while creating receiver");
+               
+               stream->receiver.valid = true;
+       }
+       else {
+               error(1, errno, "gethostbyname(%s)", server_addr);
+       }
+
+}
+#else
+static void create_stream_endpoint_native(struct stream *stream)
+{
+       struct hostent* ph;
+
+       memset(&stream->rem_addr,0, sizeof(stream->rem_addr));
+
+       stream->rem_addr.sin_family = AF_INET;
+       ph = gethostbyname(server_addr);
+       if (ph)
+               stream->rem_addr.sin_addr = *((struct in_addr *)ph->h_addr);
+       else {
+               error(1, errno, "gethostbyname(%s)", server_addr);
+       }
+       stream->rem_addr.sin_port = htons(BASE_PORT + stream->ac);
+}
+#endif
+
+static inline void
+calc_stream_params(struct stream *stream)
+{
+       int packet_size;
+       unsigned period_usec;
+       int bandwidth;
+       int ret;
+
+       /* If some parameters are not set explicitely, use default values. */
+       if (stream->bandwidth_bps < 0) stream->bandwidth_bps = opt_def_bandwidth * Kbit;
+       if (stream->packet_size < 0) stream->packet_size = opt_packet_size;
+       if (stream->period_usec < 0) stream->period_usec = opt_def_period_msec * MSEC_TO_USEC;
+
+       bandwidth = stream->bandwidth_bps;
+
+       /* Avoid arithmetic exception. Server thread will exit if
+          stream->bandwidth_bps == 0. */
+       if (bandwidth == 0) bandwidth = 1;
+
+       if (stream->packet_size) {
+               packet_size = stream->packet_size;
+               period_usec = SEC_TO_USEC*packet_size*8/bandwidth;
+               if (period_usec == 0) period_usec = 1;
+       } else if (stream->period_usec) {
+               period_usec = stream->period_usec;
+               packet_size = (long long)bandwidth/8 * period_usec/SEC_TO_USEC;
+       } else {
+               char buf[200];
+               stream_to_text(buf, sizeof(buf), stream, 0);
+               error(1, 0, "Neither packet size nor period was specified for a stream %s", buf);
+       }
+
+       if (packet_size < sizeof(struct msg_t)) {
+               error(1, 0, "Packet size too small (min %d)", sizeof(struct msg_t));
+       }
+
+       stream->packet_size = packet_size;
+       stream->period_usec = period_usec;
+       stream->jitter = opt_jitter;
+
+       ret = negotiate_contract_for_stream(stream);
+       if (ret == 0) {
+               create_stream_endpoint(stream);
+       } else {
+               char buf[200];
+               stream_to_text(buf, sizeof(buf), stream, 0);
+               fprintf(stderr, "Contract hasn't been accepted:\n%s\n", buf);
+               stream->bandwidth_bps = 0;
+               some_contract_not_accepted = true;
+       }
+}
+
+/** 
+ * Parse -b parameter.
+ * 
+ * @param params String to parse
+ * 
+ * @return NULL in case of success, pointer to a problematic character
+ *        on error.
+ */
+char* parse_bandwidths(char *params)
+{
+       struct stream *sp = &streams[nr_streams];
+
+       while (*params && nr_streams < MAX_STREAMS) {
+               char *ac_ids[AC_NUM] = { [AC_VO]="VO", [AC_VI]="VI", [AC_BE]="BE", [AC_BK]="BK" };
+               int i;
+               char *next_char;
+
+               if (strlen(params) < 2)
+                       return params;
+               for (i=0; i<AC_NUM; i++) {
+                       if (strncmp(params, ac_ids[i], 2) == 0) {
+                               sp->ac = i;
+                               params+=strlen(ac_ids[i]);
+                               break;
+                       }
+               }
+               if (i==AC_NUM)
+                       return params;
+
+               long bw;
+               if (*params == ':') {
+                       params++;
+
+                       bw = strtol(params, &next_char, 10);
+                       if (next_char == params)
+                               return params;
+                       params = next_char;
+               } else
+                       bw = -1;
+               
+               sp->bandwidth_bps = bw*Kbit;
+
+               long period = 0;
+               long packet_size = 0;
+               if (*params == '@') {
+                       params++;
+                       period = strtol(params, &next_char, 10);
+                       if (period == 0)
+                               return params;
+                       params = next_char;
+               }
+               else {
+                       if (*params == '/') {
+                               params++;
+                               packet_size = strtol(params, &next_char, 10);
+                               if (packet_size == 0)
+                                       return params;
+                               params = next_char;
+                       } else {
+                               packet_size = -1; 
+                               period = -1;
+                       }
+               }
+               sp->period_usec = period*MSEC_TO_USEC;
+               sp->packet_size = packet_size;
+
+               
+
+               if (*params != '\0' && *params != ',')
+                       return params;
+               nr_streams++;
+               sp++;
+               if (*params == ',')
+                       params++;
+       }
+       return NULL;
+}
+
+#ifdef WITH_FWP
+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); */
+/*     } */
+}
+#else
+void wait_for_all_threads_to_finish_native(void)
+{
+       int i;
+       /* Wait for all threads to finish */
+       for (i=0; i < nr_streams + AC_NUM; i++) {
+               sem_wait(&sem_thread_finished);
+       }
+}
+#endif
+
+#ifdef WITH_FWP
+void init_gui()
+{
+       initscr();
+       cbreak();
+       noecho();
+}
+
+#define addfield(title, format, ...)                                   \
+       move(y, x);                                                     \
+       x+=strlen(title)+1;                                             \
+       if (i == 0) addstr(title);                                      \
+       else {                                                          \
+               snprintf(str, sizeof(str), format, __VA_ARGS__);        \
+               addstr(str);                                            \
+       }
+
+void print_status(int seconds)
+{
+       int i;
+       char str[200], s1[20];
+       int x = 0, y;
+       struct stream *s = NULL;
+       
+       for (i = 0; i <= nr_streams; i++) {
+               if (i>0) s = &streams[i-1];
+               y=i;
+               x=0;
+               addfield("Stream", "%d", i);
+               addfield("Bandwidth", "%s", bandwidth_to_text(s1, s->bandwidth_bps));
+               addfield("Packet size", "%d bytes", s->packet_size);
+               addfield("Period   ", "%s", usec_to_text(s1, s->period_usec));
+               addfield("AC   ", "%s", ac_to_text[s->ac]);
+               addfield("Worst-case delay", "%s", usec_to_text(s1, s->wc_delay));
+               addfield("Received responses", "%lld", s->received);
+       }
+       refresh();
+}
+#else
+void init_gui() {}
+void print_status(int seconds)
+{
+       int ac;
+       fprintf(stderr, "\r%3ds", seconds);
+       for (ac = 0; ac < AC_NUM; ac++) {
+               int delta = receivers[ac].received - receivers[ac].last_received;
+               receivers[ac].last_received = receivers[ac].received;
+               fprintf(stderr, " %s %5d %4d/s", ac_to_text[ac], receivers[ac].received, delta);
+       }
+       fflush(stderr);
 }
+#endif
 
 int main(int argc, char *argv[])
 {
-       int ac, i, rc;
+       int i, rc, frames, seconds;
        pthread_attr_t attr;
        pthread_t thread;
+       char opt;
 
-       server_addr = argv[1];          
-               
-       memset(delay_stats,0, sizeof(delay_stats));     
-       pthread_attr_init(&attr);
 
-       if (argc < 2) {
-               printf("Usage: %s server_ip_addr \n", argv[0]);
-               return 0;
+       while ((opt = getopt(argc, argv, "B:b:C:c:g:I:j:o:qQ:s:T:")) != -1) {
+               switch (opt) {
+                       case 'B':
+                               opt_def_bandwidth = atoi(optarg);
+                               break;
+                       case 'b': {
+                               char *errpos;
+                               errpos = parse_bandwidths(optarg);
+                               if (errpos != NULL) {
+                                       if (*errpos == '\0')
+                                               error(1, 0, "Bandwidth parse error - string to short");
+                                       else
+                                               error(1, 0, "Bandwidth parse error at '%s'", errpos);
+                               }
+                               break;
+                       }
+                       case 'C':
+                               opt_comment = optarg;
+                               break;
+                       case 'c':
+                               opt_count_sec = atoi(optarg);
+                               break;
+                       case 'g':
+                               opt_granularity_usec = atoi(optarg);
+                               if (opt_granularity_usec < MIN_GRANULARITY) {
+                                       error(1, 0, "Granulatiry too small (min %d)", MIN_GRANULARITY);
+                               }
+                               break;
+                       case 'I':
+                               opt_interface = optarg;
+                               break;
+                       case 'j':
+                               #ifdef WITH_FWP
+                               error(1, 0, "-j is not allowd when compiled with FWP");
+                               #else
+                               opt_jitter = atoi(optarg);
+                               #endif
+                               break;
+                       case 'o':
+                               opt_output = optarg;
+                               break;
+                       case 'Q':
+                               opt_send_buf_size = atoi(optarg);
+                               break;
+                       case 'q':
+                               opt_wait_for_queue_is_full = true;
+                               break;
+                       case 's':
+                               opt_packet_size = atoi(optarg);
+                               break;
+                       case 'T':
+                               opt_def_period_msec = atoi(optarg);
+                               break;
+                       default:
+                               fprintf(stderr, "Usage: %s [ options ] server_addr\n\n", argv[0]);
+                               fprintf(stderr, "Options:\n");
+                               fprintf(stderr, "    -B  default bandwidth for -b option [kbit]\n");
+                               fprintf(stderr, "    -b  bandwidth of streams (VO|VI|BE|BK)[:<kbit>][@<msec> or /<bytes>][,...]\n");
+                               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, "    -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");
+                               fprintf(stderr, "    -Q  <bytes> set size for socket send buffers\n");
+                               fprintf(stderr, "    -s  size of data payload in packets [bytes] (default: %d)\n", opt_packet_size);
+                               fprintf(stderr, "    -T  default period for -b option [msec]\n");
+                               exit(1);
+               }
        }
-       
-       if ((logfd = fopen(logfname,"w+")) == NULL) {
-               fprintf(stderr,"Can not open %s\n", logfname);
-               exit(1);
+       if (opt_packet_size && opt_def_period_msec) {
+               error(1, 0, "Error: Nonzero -T and -s can't be used together!");
        }
-       
-       /*
-       sendpoints.epoint[AC_BE].ac = AC_BE;
-       sendpoints.epoint[AC_BE].period_nsec = 76000;
 
-       sendpoints.epoint[AC_BK].ac = AC_BK;
-       sendpoints.epoint[AC_BK].period_nsec = 75750;
-       
-       sendpoints.epoint[AC_VI].ac = AC_VI;
-       sendpoints.epoint[AC_VI].period_nsec = 19080;
-       
-       sendpoints.epoint[AC_VO].ac = AC_VO;
-       sendpoints.epoint[AC_VO].period_nsec = 300000;
-       */
+       if (optind < argc) {
+               server_addr = argv[optind];
+       } else {
+               error(1, 0, "Expected server address argument");
+       }
+
+       if (nr_streams == 0)
+               parse_bandwidths("BE");
                
+       pthread_attr_init(&attr);
+
+       snprintf(logfname, sizeof(logfname), "%s.dat", opt_output);
 
+       if ((logfd = fopen(logfname,"w+")) == NULL) {
+               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\n");
-               exit(1);
+               error(1, errno, "Signal handler registration error");
        }
-       
+
+       struct sigaction sa;
+       sa.sa_handler = empty_handler;
+       sa.sa_flags = 0;        /* don't restart syscalls */
+
+       if (sigaction(SIGUSR1, &sa, NULL) < 0) {
+               error(1, errno, "sigaction error");
+       }
+
+       sem_init(&sem_thread_finished, 0, 0);
+
+       reset_statistics();
+
+#ifdef WITH_FWP
+       rc = fwp_init();
+       if (rc != 0) {
+               error(1, errno, "FWP initialization failed");
+       }
+#else
+       int ac;
        /* create four receivers each per AC */
-       for (ac = AC_QUEUES - 1; ac >= 0; ac--) {
+       for (ac = AC_NUM - 1; ac >= 0; ac--) {
                ac_sockfd[ac] = create_ac_socket(ac);
-               rc = pthread_create(&thread, &attr, receiver, (void*) ac);
-               if (rc) {
-                       printf("Error while creating receiver %d\n",rc);
+               if (ac_sockfd[ac] < 0) {
                        return 1;
                }
+               rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
+               if (rc) {
+                       error(1, rc, "Error while creating receiver");
+               }
+               receivers[ac].valid = true;
        }               
+#endif
+
                        
-       /* create sendpoints */
-       for (i = 0; i < nr_sepoints; i++) {
-               rc = pthread_create(&thread, &attr, sender, (void*) &sepoint[i]);
-               if (rc) {
-                       printf("Error while creating sender %d\n",rc);
-                       return 1;
+       if (opt_interface) {
+               struct ifreq ifr;
+
+               memset(&ifr, 0, sizeof(ifr));
+               strncpy(ifr.ifr_name, opt_interface, IFNAMSIZ-1);
+               if (ioctl(ac_sockfd[AC_VO], SIOCGIFINDEX, &ifr) < 0) {
+                       error(1, 0, "unknown iface %s", opt_interface);
                }
+               cmsg.ipi.ipi_ifindex = ifr.ifr_ifindex;
+               cmsg_len = sizeof(cmsg);
        }
-       
-       while (1) {
-               sleep(100000);
+       /* create sendpoints */
+       for (i = 0; i < nr_streams; i++) {
+               struct stream *s = &streams[i];
+               pthread_mutex_init(&s->mutex, NULL);
+               calc_stream_params(s);
+               rc = pthread_create(&thread, &attr, sender, (void*) s);
+               if (rc) error(1, rc, "Error while creating sender");
        }
-       
+
+       if (some_contract_not_accepted) {
+               stopper();
+       } else {
+               //init_gui();
+
+               seconds = 1;
+               frames=0;
+               while (!exit_flag) {
+#ifdef WITH_FWP
+                       usleep(40000);
+#else
+                       sleep(1);
+#endif
+                       frames++;
+                       if (frames>=25) {
+                               seconds++;
+                               frames = 0;
+                       }
+                       print_status(seconds);
+                       if (seconds == opt_count_sec)
+                               stopper();
+               }
+       }
+
+#ifdef WITH_FWP
+       //endwin();
+       fwp_done();
+#else
+       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);
+
+       save_results(argc, argv, timespec2usec(&measure_length));
+#endif
+
        return 0;
 }