httpgw mostly consists of bugs at this point
authorVictor Grishchenko (Debian) <victor.grishchenko@gmail.com>
Fri, 12 Mar 2010 19:16:21 +0000 (20:16 +0100)
committerVictor Grishchenko (Debian) <victor.grishchenko@gmail.com>
Fri, 12 Mar 2010 19:16:21 +0000 (20:16 +0100)
bins.cpp
datagram.cpp
httpgw.cpp
send_control.cpp
sendrecv.cpp
swift.h
transfer.cpp

index 184e678..510b4a0 100644 (file)
--- a/bins.cpp
+++ b/bins.cpp
@@ -439,7 +439,7 @@ void        binmap_t::copy_range (binmap_t& origin, bin64_t range) {
 }
 
 uint64_t    binmap_t::seq_length () {
-    iterator i(this);
+    iterator i(this,bin64_t(height,0));
     if (!i.deep() && *i==FILLED)
         return i.pos.width();
     while (!i.pos.is_base()) {
index 97391b3..9904a7c 100644 (file)
@@ -137,12 +137,13 @@ void Datagram::Wait (int sockcnt, socket_callbacks_t* sockets, tint usec) {
     Time();
     if (sel>0) {
         for (int i=0; i<=sockcnt; i++) {
-            if (FD_ISSET(sockets[i].sock,&rdfd))
-                (*(sockets[i].may_read))(sockets[i].sock);
-            if (FD_ISSET(sockets[i].sock,&wrfd))
-                (*(sockets[i].may_write))(sockets[i].sock);
-            if (FD_ISSET(sockets[i].sock,&errfd))
-                (*(sockets[i].on_error))(sockets[i].sock);
+            socket_callbacks_t& sct = sockets[i];
+            if (sct.may_read && FD_ISSET(sct.sock,&rdfd))
+                (*(sct.may_read))(sct.sock);
+            if (sct.may_write && FD_ISSET(sockets[i].sock,&wrfd))
+                (*(sct.may_write))(sct.sock);
+            if (sct.on_error && FD_ISSET(sockets[i].sock,&errfd))
+                (*(sct.on_error))(sct.sock);
         }
     } else if (sel<0) {
         print_error("select fails");
index 2f90271..b79b080 100644 (file)
@@ -40,6 +40,7 @@ http_gw_t* HttpGwFindRequest (SOCKET sock) {
 void HttpGwCloseConnection (SOCKET sock) {
     http_gw_t* req = HttpGwFindRequest(sock);
     if (req) {
+        dprintf("%s @%i closed http connection %i\n",tintstr(),req->id,sock);
         for(int i=0; i<HTTPGW_MAX_HEADER; i++)
             if (req->headers[i]) {
                 free(req->headers[i]);
@@ -50,7 +51,7 @@ void HttpGwCloseConnection (SOCKET sock) {
     swift::close_socket(sock);
     swift::Listen3rdPartySocket(socket_callbacks_t(sock));
 }
+
 
 void HttpGwMayWriteCallback (SOCKET sink) {
     http_gw_t* req = HttpGwFindRequest(sink);
@@ -63,18 +64,22 @@ void HttpGwMayWriteCallback (SOCKET sink) {
             HttpGwCloseConnection(sink);
             return;
         }
-        size_t wn = send(sink, buf, rd, 0);
+        int wn = send(sink, buf, rd, 0);
         if (wn<0) {
+            print_error("send fails");
             HttpGwCloseConnection(sink);
             return;
         }
+        dprintf("%s @%i sent %ib\n",tintstr(),req->id,(int)wn);
         req->offset += wn;
         req->tosend -= wn;
     } else {
-        if (swift::IsComplete(req->transfer)) { // done; wait for new request
+        if (req->tosend==0) { // done; wait for new request
+            dprintf("%s @%i done\n",tintstr(),req->id);
             socket_callbacks_t wait_new_req(req->sink,HttpGwNewRequestCallback,NULL,HttpGwCloseConnection);
             swift::Listen3rdPartySocket (wait_new_req);
         } else { // wait for data
+            dprintf("%s @%i waiting for data\n",tintstr(),req->id);
             socket_callbacks_t wait_swift_data(req->sink,NULL,NULL,HttpGwCloseConnection);
             swift::Listen3rdPartySocket(wait_swift_data);
         }
@@ -86,6 +91,7 @@ void HttpGwSwiftProgressCallback (int transfer, bin64_t bin) {
     for (int httpc=0; httpc<http_gw_reqs_open; httpc++)
         if (http_requests[httpc].transfer==transfer)
             if ( (bin.offset()<<10) == http_requests[httpc].offset ) {
+                dprintf("%s @%i progress: %s\n",tintstr(),http_requests[httpc].id,bin.str());
                 socket_callbacks_t maywrite_callbacks
                         (http_requests[httpc].sink,NULL,HttpGwMayWriteCallback,HttpGwCloseConnection);
                 Listen3rdPartySocket (maywrite_callbacks);
@@ -100,18 +106,21 @@ void HttpGwFirstProgressCallback (int transfer, bin64_t bin) {
     swift::AddProgressCallback(transfer,&HttpGwSwiftProgressCallback);
     for (int httpc=0; httpc<http_gw_reqs_open; httpc++) {
         http_gw_t * req = http_requests + httpc;
-        if (req->transfer==transfer) {
+        if (req->transfer==transfer && req->tosend==0) { // FIXME states
             uint64_t file_size = swift::Size(transfer);
             char response[1024];
             sprintf(response,
                 "HTTP/1.1 200 OK\r\n"\
-                "Connection: close\r\n"\
-                "Content-Type: text/plain; charset=iso-8859-1\r\n"\
+                "Connection: keep-alive\r\n"\
+                "Content-Type: video/ogg\r\n"\
+                "X-Content-Duration: 32\r\n"\
                 "Content-Length: %lli\r\n"\
+                "Accept-Ranges: bytes\r\n"\
                 "\r\n",
                 file_size);
             send(req->sink,response,strlen(response),0);
             req->tosend = file_size;
+            dprintf("%s @%i headers_sent size %lli\n",tintstr(),req->id,file_size);
         }
     }
     HttpGwSwiftProgressCallback(transfer,bin);
@@ -124,6 +133,7 @@ void HttpGwNewRequestCallback (SOCKET http_conn){
     req->sink = http_conn;
     req->offset = 0;
     req->tosend = 0;
+    dprintf("%s @%i new http request\n",tintstr(),req->id);
     // read headers - the thrilling part
     // we surely do not support pipelining => one request at a time
     #define HTTPGW_MAX_REQ_SIZE 1024
@@ -161,14 +171,18 @@ void HttpGwNewRequestCallback (SOCKET http_conn){
         HttpGwCloseConnection(http_conn);
         return;
     }
+    dprintf("%s @%i demands %s\n",tintstr(),req->id,hash);
     // initiate transmission
-    int file = swift::Open(hash,Sha1Hash(true,hash));
+    Sha1Hash root_hash = Sha1Hash(true,hash);
+    int file = swift::Find(root_hash);
+    if (file==-1)
+        file = swift::Open(hash,root_hash);
     req->transfer = file;
     if (swift::Size(file)) {
         HttpGwFirstProgressCallback(file,bin64_t(0,0));
     } else {
         swift::AddProgressCallback(file,&HttpGwFirstProgressCallback);
-        socket_callbacks_t install (http_conn,NULL,NULL,HttpGwCloseConnection); // FIXME: if conn is closed / no data arrives
+        socket_callbacks_t install (http_conn,NULL,NULL,HttpGwCloseConnection);
         swift::Listen3rdPartySocket(install);
     }
 }
@@ -199,6 +213,7 @@ void HttpGwError (SOCKET s) {
 }
 
 
+#include <signal.h>
 SOCKET InstallHTTPGateway (Address bind_to) {
     SOCKET fd;
     #define gw_ensure(x) { if (!(x)) { \
@@ -207,6 +222,12 @@ SOCKET InstallHTTPGateway (Address bind_to) {
     gw_ensure ( (fd=socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET );
     int enable = true;
     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (setsockoptptr_t)&enable, sizeof(int));
+    //setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (setsockoptptr_t)&enable, sizeof(int));
+    //struct sigaction act;
+    //memset(&act,0,sizeof(struct sigaction));
+    //act.sa_handler = SIG_IGN;
+    //sigaction (SIGPIPE, &act, NULL); // FIXME
+    signal( SIGPIPE, SIG_IGN );
     gw_ensure ( 0==bind(fd, (sockaddr*)&(bind_to.addr), sizeof(struct sockaddr_in)) );
     gw_ensure (make_socket_nonblocking(fd));
     gw_ensure ( 0==listen(fd,8) );
index 6f76bc4..005b70c 100644 (file)
@@ -13,7 +13,7 @@ using namespace swift;
 using namespace std;
 
 tint Channel::MIN_DEV = 50*TINT_MSEC;
-tint Channel::MAX_SEND_INTERVAL = TINT_SEC; //*58
+tint Channel::MAX_SEND_INTERVAL = TINT_SEC*58;
 tint Channel::LEDBAT_TARGET = TINT_MSEC*25;
 float Channel::LEDBAT_GAIN = 1.0/LEDBAT_TARGET;
 tint Channel::LEDBAT_DELAY_BIN = TINT_SEC*30;
index 29c15c5..3ff3d63 100644 (file)
@@ -480,7 +480,7 @@ void    Channel::RecvDatagram (SOCKET socket) {
     Datagram data(socket);
     data.Recv();
     const Address& addr = data.address();
-#define return_log(...) { printf(__VA_ARGS__); }
+#define return_log(...) { fprintf(stderr,__VA_ARGS__); return; }
     if (data.size()<4)
         return_log("datagram shorter than 4 bytes %s\n",addr.str());
     uint32_t mych = data.Pull32();
diff --git a/swift.h b/swift.h
index 12bf058..40dada3 100644 (file)
--- a/swift.h
+++ b/swift.h
@@ -456,6 +456,8 @@ namespace swift {
     /** Returns the number of bytes that are complete sequentially, starting from the
         beginning, till the first not-yet-retrieved packet. */
     uint64_t  SeqComplete (int fdes);
+    /***/
+    int       Find (Sha1Hash hash);
 
     void AddProgressCallback (int transfer,TransferProgressCallback cb);
     void RemoveProgressCallback (int transfer,TransferProgressCallback cb);
index c885846..a67a61b 100644 (file)
@@ -74,6 +74,15 @@ FileTransfer* FileTransfer::Find (const Sha1Hash& root_hash) {
 }
 
 
+int       swift:: Find (Sha1Hash hash) {
+    FileTransfer* t = FileTransfer::Find(hash);
+    if (t)
+        return t->fd();
+    return -1;
+}
+
+
+
 void            FileTransfer::OnPexIn (const Address& addr) {
     for(int i=0; i<hs_in_.size(); i++) {
         Channel* c = Channel::channel(hs_in_[i]);