add .gitignore
[swift-upb.git] / transfer.cpp
index 97979b8..ad121f3 100644 (file)
@@ -1,25 +1,19 @@
 /*
  *  transfer.cpp
- *  p2tp
+ *  some transfer-scope code
  *
  *  Created by Victor Grishchenko on 10/6/09.
  *  Copyright 2009 Delft University of Technology. All rights reserved.
  *
  */
-#ifdef _WIN32
-#include "compat.h"
-#else
-#include <sys/mman.h>
-#endif
 #include <errno.h>
 #include <string>
 #include <sstream>
-#include "p2tp.h"
-#include "compat/util.h"
+#include "swift.h"
 
 #include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME 
 
-using namespace p2tp;
+using namespace swift;
 
 std::vector<FileTransfer*> FileTransfer::files(20);
 
@@ -28,7 +22,7 @@ std::vector<FileTransfer*> FileTransfer::files(20);
 // FIXME: separate Bootstrap() and Download(), then Size(), Progress(), SeqProgress()
 
 FileTransfer::FileTransfer (const char* filename, const Sha1Hash& _root_hash) :
-    file_(filename,_root_hash), hs_in_offset_(0)
+    file_(filename,_root_hash), hs_in_offset_(0), cb_installed(0)
 {
     if (files.size()<fd()+1)
         files.resize(fd()+1);
@@ -39,11 +33,46 @@ FileTransfer::FileTransfer (const char* filename, const Sha1Hash& _root_hash) :
 }
 
 
+void    Channel::CloseTransfer (FileTransfer* trans) {
+    for(int i=0; i<Channel::channels.size(); i++) 
+        if (Channel::channels[i] && Channel::channels[i]->transfer_==trans) 
+            delete Channel::channels[i];
+}
+
+
+void swift::AddProgressCallback (int transfer,ProgressCallback cb,uint8_t agg) {
+    FileTransfer* trans = FileTransfer::file(transfer);
+    if (!trans)
+        return;
+    trans->cb_agg[trans->cb_installed] = agg;
+    trans->callbacks[trans->cb_installed] = cb;
+    trans->cb_installed++;
+}
+
+
+void swift::ExternallyRetrieved (int transfer,bin64_t piece) {
+    FileTransfer* trans = FileTransfer::file(transfer);
+    if (!trans)
+        return;
+    trans->ack_out().set(piece); // that easy
+}
+
+
+void swift::RemoveProgressCallback (int transfer, ProgressCallback cb) {
+    FileTransfer* trans = FileTransfer::file(transfer);
+    if (!trans)
+        return;
+    for(int i=0; i<trans->cb_installed; i++)
+        if (trans->callbacks[i]==cb)
+            trans->callbacks[i]=trans->callbacks[--trans->cb_installed];
+}
+
 
 FileTransfer::~FileTransfer ()
 {
-
+    Channel::CloseTransfer(this);
     files[fd()] = NULL;
+    delete picker_;
 }
 
 
@@ -55,14 +84,23 @@ 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::channels[hs_in_[i]];
-        if (c && c->transfer().fd()==this->fd() && c->peer_==addr)
+        Channel* c = Channel::channel(hs_in_[i]);
+        if (c && c->transfer().fd()==this->fd() && c->peer()==addr)
             return; // already connected
     }
     if (hs_in_.size()<20) {
-        new Channel(this,Channel::sockets[0],addr);
+        new Channel(this,Datagram::default_socket(),addr);
     } else {
         pex_in_.push_back(addr);
         if (pex_in_.size()>1000)
@@ -71,15 +109,18 @@ void            FileTransfer::OnPexIn (const Address& addr) {
 }
 
 
-int        FileTransfer::RevealChannel (int& pex_out_) {
+int        FileTransfer::RevealChannel (int& pex_out_) { // FIXME brainfuck
     pex_out_ -= hs_in_offset_;
     if (pex_out_<0)
         pex_out_ = 0;
     while (pex_out_<hs_in_.size()) {
-        Channel* c = Channel::channels[hs_in_[pex_out_]];
+        Channel* c = Channel::channel(hs_in_[pex_out_]);
         if (c && c->transfer().fd()==this->fd()) {
-            pex_out_ += hs_in_offset_ + 1;
-            return c->id;
+            if (c->is_established()) {
+                pex_out_ += hs_in_offset_ + 1;
+                return c->id();
+            } else
+                pex_out_++;
         } else {
             hs_in_[pex_out_] = hs_in_[0];
             hs_in_.pop_front();