sendfile: added server implementation
authorRăzvan Crainea <razvan.crainea@gmail.com>
Sat, 2 Jun 2012 11:43:12 +0000 (14:43 +0300)
committerRăzvan Crainea <razvan.crainea@gmail.com>
Sat, 2 Jun 2012 11:43:12 +0000 (14:43 +0300)
module/p2pkp_sock.h
sendfile/Makefile [new file with mode: 0644]
sendfile/client/.gitignore [new file with mode: 0644]
sendfile/client/Makefile
sendfile/client/client.c
sendfile/server/.gitignore [new file with mode: 0644]
sendfile/server/Makefile
sendfile/server/server.c
sendfile/test.sh [new file with mode: 0755]
sendfile/utils/utils.h [new file with mode: 0644]

index ae5e78a..152d029 100644 (file)
@@ -3,7 +3,7 @@
 
 #include <net/sock.h>
 
-#define P2PKP_DEFAULT_BUF_LEN  256
+#define P2PKP_DEFAULT_BUF_LEN  8196
 #define P2PKP_DEFAULT_MODE             0644
 
 struct p2pkp_conn_info {
diff --git a/sendfile/Makefile b/sendfile/Makefile
new file mode 100644 (file)
index 0000000..22d865b
--- /dev/null
@@ -0,0 +1,12 @@
+all: client server
+
+.PHONY: client server clean
+client server:
+       make -C $@
+
+run: client server
+       @./test.sh
+
+clean:
+       -make -C client clean
+       -make -C server clean
diff --git a/sendfile/client/.gitignore b/sendfile/client/.gitignore
new file mode 100644 (file)
index 0000000..b051c6c
--- /dev/null
@@ -0,0 +1 @@
+client
index 8d5c7ad..8864d3e 100644 (file)
@@ -1,4 +1,5 @@
 FILE=client
+CFLAGS=-Wall -g
 
 $(FILE):$(FILE).o
 
index 1e5b59e..572f9cd 100644 (file)
@@ -10,8 +10,9 @@
 #include <errno.h>
 #include <netdb.h>
 
+#include "../utils/utils.h"
+
 #define MAX_NUMBER_OF_PEERS            256
-#define CHUNK_SIZE                     256
 
 int main(int argc, char **argv) {
        int sock, err;
@@ -76,6 +77,7 @@ int main(int argc, char **argv) {
                        printf("Opening file error: %s(%d)\n", strerror(errno), errno);
                        i--;
                }
+               argv += 2;
        }
 
        total = i;
@@ -84,11 +86,12 @@ int main(int argc, char **argv) {
                sent_size = 0;
 
                for (i = 0; i < total; i++) {
-                       if ((err = sendfile(fd[i], peers[i], NULL, CHUNK_SIZE)) < 0) {
+                       if ((err = sendfile(peers[i], fd[i], NULL, CHUNK_SIZE)) < 0) {
                                printf("Sending file error: %s(%d)\n", strerror(errno), errno);
-                       } else {
-                               sent_size += err;
+                               continue;
                        }
+                       sent_size += err;
+                       usleep(20);
                }
        } while(sent_size);
 
diff --git a/sendfile/server/.gitignore b/sendfile/server/.gitignore
new file mode 100644 (file)
index 0000000..6adea68
--- /dev/null
@@ -0,0 +1,2 @@
+
+server
index b52084b..b0da358 100644 (file)
@@ -1,4 +1,5 @@
 FILE=server
+CFLAGS=-Wall -g
 
 $(FILE):$(FILE).o
 
index 8bc0ff0..603a1c0 100644 (file)
@@ -2,21 +2,22 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <string.h>
+#include <unistd.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <netdb.h>
 #include <stdio.h>
 
-#define BUFFER_SIZE            256
+#include "../utils/utils.h"
 
 int main(int argc, char **argv)
 {
-       int sock, err, file_fd;
+       int sock, file_fd;
        ssize_t ret, len, offset, bytes_to_write;
        struct sockaddr_in psin;
        struct hostent *host;
-       char buffer[BUFFER_SIZE];
+       char buffer[CHUNK_SIZE];
 
        if (argc != 4) {
                fprintf(stderr, "Usage: %s <peer_ip> <peer_port> <file_name>\n", argv[0]);
@@ -57,11 +58,11 @@ int main(int argc, char **argv)
        printf("Bound to: %s:%s\n", argv[1], argv[2]);
        
        for (;;) {
-               ret = read(sock, buffer, BUFFER_SIZE);
+               ret = read(sock, buffer, CHUNK_SIZE);
                if (ret < 0) {
                        printf("Error while receiving file %s: %s(%d)\n", argv[0],
                                        strerror(errno), errno);
-                       return err;
+                       return ret;
                } else if (ret == 0) {
                        break;
                }
@@ -76,12 +77,10 @@ int main(int argc, char **argv)
                        }
                        bytes_to_write -= len;
                        offset += len;
-               } while (bytes_to_write > 0);
-               if (ret < BUFFER_SIZE)
-                       break;
+               } while (bytes_to_write);
        }
 
-       printf("Successfully read file %s\n", argv[0]);
+       printf("Successfully read file %s\n", argv[3]);
 
        return 0;
 }
diff --git a/sendfile/test.sh b/sendfile/test.sh
new file mode 100755 (executable)
index 0000000..3cba976
--- /dev/null
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+MODULE_NAME=af_p2pkp
+MODULE_DIR=../module
+CLIENT=client/client
+SERVER=server/server
+FILE=/tmp/testfile1
+IP="127.0.0.1"
+WAIT_TIME=5
+
+LISTENERS_PORTS=(
+       60000
+)
+
+
+#use nc.traditional
+echo 2 | update-alternatives --config nc &> /dev/null
+
+[ -e $FILE ] || exit
+
+#increase tests debugging
+set -x
+
+# listen for UDP packets on localhost, port 60000 (run in background)
+for port in "${LISTENERS_PORTS[@]}"; do
+       $SERVER $IP $port testfile.$port &
+       pids="$pids $!"
+       ARGUMENTS="$IP $port $ARGUMENTS"
+done
+
+# wait for netcat to start listening
+sleep 1
+
+$CLIENT $FILE $ARGUMENTS
+
+sleep $WAIT_TIME
+# kill netcat
+for pid in $pids; do
+       kill -9 $pid &> /dev/null
+done
diff --git a/sendfile/utils/utils.h b/sendfile/utils/utils.h
new file mode 100644 (file)
index 0000000..5dda0b2
--- /dev/null
@@ -0,0 +1,5 @@
+#ifndef _SENDFILE_UTILS_H_
+#define _SENDFILE_UTILS_H_
+
+#define CHUNK_SIZE 8196
+#endif /* _SENDFILE_UTILS_H_ */