Windows: compiles with MinGW and runs.
authorunknown <Victor Grishchenko@.(none)>
Wed, 24 Feb 2010 20:12:59 +0000 (21:12 +0100)
committerunknown <Victor Grishchenko@.(none)>
Wed, 24 Feb 2010 20:12:59 +0000 (21:12 +0100)
15 files changed:
bin64.h
compat.cpp
compat.h
compat/hirestimeofday.cpp [deleted file]
compat/hirestimeofday.h [deleted file]
compat/stdint.h [deleted file]
compat/unixio.cpp [deleted file]
compat/unixio.h [deleted file]
compat/util.cpp [deleted file]
compat/util.h [deleted file]
datagram.h
sendrecv.cpp
swift.cpp
swift.h
transfer.cpp

diff --git a/bin64.h b/bin64.h
index 3550f22..6ddce3f 100644 (file)
--- a/bin64.h
+++ b/bin64.h
@@ -9,11 +9,7 @@
 #ifndef BIN64_H
 #define BIN64_H
 #include <assert.h>
-#ifdef _MSC_VER
-    #include "compat/stdint.h"
-#else
-    #include <stdint.h>
-#endif
+#include "compat.h"
 
 
 /** Numbering for (aligned) logarithmical bins.
index c81bdbf..30b0607 100644 (file)
@@ -15,7 +15,6 @@
 #include <Tchar.h>
 #include <io.h>
 #include <sys/timeb.h>
-#include "compat/hirestimeofday.h"
 #else
 #include <unistd.h>
 #include <sys/time.h>
@@ -121,10 +120,28 @@ int inet_aton(const char *cp, struct in_addr *inp)
     return 1;
 }
 
+#endif
+
+#ifdef _WIN32
+
+LARGE_INTEGER get_freq() {
+    LARGE_INTEGER proc_freq;
+    if (!::QueryPerformanceFrequency(&proc_freq))
+       print_error("HiResTimeOfDay: QueryPerformanceFrequency() failed");
+    return proc_freq;
+}
+
 tint usec_time(void)
 {
-    HiResTimeOfDay* tod = HiResTimeOfDay::Instance();
-    return tod->getTimeUSec();
+       static LARGE_INTEGER last_time;
+       LARGE_INTEGER cur_time;
+       QueryPerformanceCounter(&cur_time);
+       if (cur_time.QuadPart<last_time.QuadPart)
+               print_error("QueryPerformanceCounter wrapped"); // does this happen?
+       last_time = cur_time;
+       static float freq = 1000000.0/get_freq().QuadPart;
+       tint usec = cur_time.QuadPart * freq;
+       return usec;
 }
 
 
@@ -143,4 +160,16 @@ tint usec_time(void)
 
 #endif
 
+void LibraryInit(void)
+{
+#ifdef _WIN32
+       static WSADATA _WSAData;
+       // win32 requires you to initialize the Winsock DLL with the desired
+       // specification version
+       WORD wVersionRequested;
+    wVersionRequested = MAKEWORD(2, 2);
+       WSAStartup(wVersionRequested, &_WSAData);
+#endif
+}
+
 }
index 4ab9379..0643405 100644 (file)
--- a/compat.h
+++ b/compat.h
 #define SWIFT_COMPAT_H
 
 #ifdef _MSC_VER
-#include "compat/stdint.h"
+typedef unsigned char uint8_t;
+typedef signed char int8_t;
+typedef unsigned short uint16_t;
+typedef short int16_t;
+typedef unsigned int uint32_t;
+typedef int int32_t;
+typedef __int64 int64_t;
+typedef unsigned __int64 uint64_t;
 #else
 #include <stdint.h>
 #endif
 
 #ifdef _WIN32
 #define open(a,b,c)    _open(a,b,c)
+#endif
+#ifndef S_IRUSR
 #define S_IRUSR _S_IREAD
-#define S_IWUSR    _S_IWRITE
+#endif
+#ifndef S_IWUSR
+#define S_IWUSR _S_IWRITE
+#endif
+#ifndef S_IRGRP
 #define S_IRGRP _S_IREAD
+#endif
+#ifndef S_IROTH
 #define S_IROTH _S_IREAD
 #endif
 
diff --git a/compat/hirestimeofday.cpp b/compat/hirestimeofday.cpp
deleted file mode 100644 (file)
index 8aa7b1e..0000000
+++ /dev/null
@@ -1,157 +0,0 @@
-/*
- * Inspired by
- * - http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx
- * - Python-2.6.3/Modules/timemodule.c
- */
-
-#include <iostream>
-#include "hirestimeofday.h"
-
-#ifndef _WIN32
-#include <sys/time.h>
-#endif
-
-namespace swift {
-
-HiResTimeOfDay* HiResTimeOfDay::_instance = 0;
-
-HiResTimeOfDay* HiResTimeOfDay::Instance()
-{
-       if (_instance == 0)
-               _instance = new HiResTimeOfDay();
-       return _instance;
-}
-
-
-#ifdef _WIN32
-#include <windows.h>
-#include <sys/timeb.h>
-
-
-HiResTimeOfDay::HiResTimeOfDay(void)
-{
-       frequency = getFrequency();
-       epochstart = getFTime();
-       epochcounter = getCounter();
-}
-
-
-tint HiResTimeOfDay::getTimeUSec(void)
-{
-       LARGE_INTEGER currentcounter;
-       tint      currentstart;
-
-       currentstart = getFTime();
-    currentcounter = getCounter();
-
-    if (currentcounter.QuadPart < epochcounter.QuadPart)
-    {
-
-       // Wrap around detected, reestablish baseline
-       epochstart = currentstart;
-       epochcounter = currentcounter;
-    }
-    return epochstart + (1000000*(currentcounter.QuadPart-epochcounter.QuadPart))/frequency.QuadPart;
-}
-
-
-// Private
-tint HiResTimeOfDay::getFTime()
-{
-       struct timeb t;
-       ftime(&t);
-       tint usec;
-       usec =  t.time * 1000000;
-       usec += t.millitm * 1000;
-       return usec;
-}
-
-
-
-LARGE_INTEGER HiResTimeOfDay::getFrequency(void)
-{
-    LARGE_INTEGER proc_freq;
-
-    if (!::QueryPerformanceFrequency(&proc_freq))
-       std::cerr << "HiResTimeOfDay: QueryPerformanceFrequency() failed";
-
-    return proc_freq;
-}
-
-LARGE_INTEGER HiResTimeOfDay::getCounter()
-{
-    LARGE_INTEGER counter;
-
-    DWORD_PTR oldmask = ::SetThreadAffinityMask(::GetCurrentThread(), 0);
-    if (!::QueryPerformanceCounter(&counter))
-       std::cerr << "HiResTimeOfDay: QueryPerformanceCounter() failed";
-    ::SetThreadAffinityMask(::GetCurrentThread(), oldmask);
-
-    return counter;
-}
-
-#else
-
-HiResTimeOfDay::HiResTimeOfDay(void)
-{
-}
-
-
-tint HiResTimeOfDay::getTimeUSec(void)
-{
-       struct timeval t;
-       gettimeofday(&t,NULL);
-       tint ret;
-       ret = t.tv_sec;
-       ret *= 1000000;
-       ret += t.tv_usec;
-       return ret;
-}
-#endif
-
-  
-  
-  
-// ARNOTODO: move to swift.cpp
-
-#ifdef _WIN32
-static WSADATA _WSAData;
-#endif
-  
-void LibraryInit(void)
-{
-#ifdef _WIN32
-       // win32 requires you to initialize the Winsock DLL with the desired
-       // specification version
-       WORD wVersionRequested;
-    wVersionRequested = MAKEWORD(2, 2);
-       WSAStartup(wVersionRequested, &_WSAData);
-#endif
-}
-  
-  
-} // end of namespace
-
-
-
-
-#ifdef TEST
-#include <iostream>
-
-using namespace swift;
-
-int main()
-{
-       HiResTimeOfDay *t = HiResTimeOfDay::Instance();
-       for (int i=0; i<100; i++)
-       {
-               tint st = t->getTimeUSec();
-               Sleep(1000);
-               tint et = t->getTimeUSec();
-               tint diff = et - st;
-               std::cout << "diffxTime is " << diff << "\n";
-       }
-       return 0;
-}
-#endif
-
diff --git a/compat/hirestimeofday.h b/compat/hirestimeofday.h
deleted file mode 100644 (file)
index bb88e9c..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Written by Arno Bakker
- * see LICENSE.txt for license information
- *
- * Singleton class to retrieve a time-of-day in UTC in usec in a platform-
- * independent manner.
- */
-#ifndef HIRESTIMEOFDAY_H
-#define HIRESTIMEOFDAY_H
-
-#ifdef _MSC_VER
-#include "compat/stdint.h"
-#else
-#include <stdint.h>
-#endif
-#ifdef _WIN32
-#include <windows.h>
-#endif
-
-namespace swift {
-
-typedef int64_t tint;
-#define TINT_SEC ((tint)1000000)
-#define TINT_MSEC ((tint)1000)
-#define TINT_uSEC ((tint)1)
-#define TINT_NEVER ((tint)0x7fffffffffffffffLL)
-
-
-class HiResTimeOfDay
-{
-public:
-    HiResTimeOfDay(void);
-    tint getTimeUSec(void);
-    static HiResTimeOfDay* Instance();
-
-private:
-#ifdef _WIN32
-       tint     epochstart; // in usec
-       LARGE_INTEGER epochcounter;
-    LARGE_INTEGER last;
-    LARGE_INTEGER frequency;
-
-    tint HiResTimeOfDay::getFTime();
-    LARGE_INTEGER getFrequency(void);
-    LARGE_INTEGER getCounter(void);
-#endif
-
-    static HiResTimeOfDay* _instance;
-};
-
-};
-#endif
diff --git a/compat/stdint.h b/compat/stdint.h
deleted file mode 100644 (file)
index 6cb10e6..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Written by Arno Bakker
- * see LICENSE.txt for license information
- */
-
-#ifndef STDINT_H_
-#define STDINT_H_
-
-typedef unsigned char uint8_t;
-typedef signed char int8_t;
-typedef unsigned short uint16_t;
-typedef short int16_t;
-typedef unsigned int uint32_t;
-typedef int int32_t;
-typedef __int64 int64_t;
-typedef unsigned __int64 uint64_t;
-
-#endif /* STDINT_H_ */
diff --git a/compat/unixio.cpp b/compat/unixio.cpp
deleted file mode 100644 (file)
index 614a31b..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Written by Arno Bakker
- * see LICENSE.txt for license information
- */
-#ifdef _WIN32
-
-#include "unixio.h"
-#include <stdio.h>
-#include <io.h>
-#include <winsock2.h>
-
-size_t pread(int fildes, void *buf, size_t nbyte, long offset)
-{
-       _lseek(fildes,offset,SEEK_SET);
-    return read(fildes,buf,nbyte);
-}
-
-size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset)
-{
-    _lseek(fildes,offset,SEEK_SET);
-    return write(fildes,buf,nbyte);
-}
-
-
-int inet_aton(const char *cp, struct in_addr *inp)
-{
-       inp->S_un.S_addr = inet_addr(cp);
-       return 1;
-}
-
-
-
-#endif
diff --git a/compat/unixio.h b/compat/unixio.h
deleted file mode 100644 (file)
index f19757c..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Written by Arno Bakker
- * see LICENSE.txt for license information
- *
- * Defines UNIX like I/O calls and parameters for Win32
- */
-#ifdef _WIN32
-
-#ifndef UNIXIO_H_
-#define UNIXIO_H_
-
-#define open(a,b,c)    _open(a,b,c)
-#define S_IRUSR _S_IREAD
-#define S_IWUSR        _S_IWRITE
-#define S_IRGRP _S_IREAD
-#define S_IROTH _S_IREAD
-#define ftruncate(a, b) _chsize(a,b)
-
-size_t pread(int fildes, void *buf, size_t nbyte, long offset);
-/** UNIX pread approximation. Does change file pointer. Is not thread-safe */
-size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset);
-/** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */
-
-int inet_aton(const char *cp, struct in_addr *inp);
-
-
-#endif /* UNIXIO_H_ */
-
-#endif // WIN32
\ No newline at end of file
diff --git a/compat/util.cpp b/compat/util.cpp
deleted file mode 100644 (file)
index 7469c4a..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Written by Arno Bakker
- * see LICENSE.txt for license information
- */
-
-#include "util.h"
-#include <vector>
-
-#ifdef _WIN32
-#include <windows.h>
-#include <Tchar.h>
-#endif
-
-namespace swift
-{
-
-std::string gettmpdir(void)
-{
-#ifdef _WIN32
-  DWORD result = ::GetTempPath(0, _T(""));
-  if (result == 0)
-       throw std::runtime_error("Could not get system temp path");
-
-  std::vector<TCHAR> tempPath(result + 1);
-  result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);
-  if((result == 0) || (result >= tempPath.size()))
-       throw std::runtime_error("Could not get system temp path");
-
-  return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));
-#else
-         return std::string("/tmp/");
-#endif
-}
-
-
-    
-    
-}; // namespace
-
diff --git a/compat/util.h b/compat/util.h
deleted file mode 100644 (file)
index 42f7de4..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * util.h
- *
- *  Created on: 20-Oct-2009
- *      Author: arno
- */
-
-#ifndef UTIL_H_
-#define UTIL_H_
-
-#include <string>
-
-namespace swift
-{
-       /**
-        * Return path of temporary directory.
-        *
-        * From http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx
-        *
-        * TODO: Unicode... (gets hairy with open() call on Linux. Win32 has _wopen)
-        */
-       std::string gettmpdir(void);
-
-    void print_error(const char* msg);
-    
-};
-
-#endif /* UTIL_H_ */
index a7a162e..a8e1be4 100644 (file)
@@ -9,11 +9,12 @@
 #ifndef DATAGRAM_H
 #define DATAGRAM_H
 
-#ifdef _MSC_VER
-    #include "compat/stdint.h"
-#else
-    #include <stdint.h>
-#endif
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <stdio.h>
+#include <string>
 #ifdef _WIN32
     #include <winsock2.h>
     #include "compat.h"
     #include <netinet/in.h>
     #include <unistd.h>
 #endif
-#include <stdlib.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <string.h>
-#include <stdio.h>
-#include <string>
 #include "hashtree.h"
-#include "compat/util.h"
 #include "compat.h"
 
 
index 2317e61..d432e6c 100644 (file)
@@ -7,8 +7,6 @@
  *
  */
 #include "swift.h"
-#include "compat/util.h"
-
 
 using namespace swift;
 using namespace std;
index f916e29..883fd99 100644 (file)
--- a/swift.cpp
+++ b/swift.cpp
@@ -95,17 +95,14 @@ int main (int argc, char** argv) {
     
     LibraryInit();
     
-    if (root_hash==Sha1Hash() && bindaddr==Address())
-        exit(0);
-
     if (bindaddr!=Address()) { // seeding
         if (Listen(bindaddr)<=0)
             quit("cant listen to %s\n",bindaddr.str())
         if (wait_time==0)
             wait_time=TINT_NEVER;
-    } else {
+    } else if (tracker!=Address()) { // leeching
         int base = rand()%10000, i;
-        for (i=0; i<100 && Listen(Address(INADDR_ANY,i*7+base))<=0; i++);
+        for (i=0; i<100 && Listen(Address((uint32_t)INADDR_ANY,i*7+base))<=0; i++);
         if (i==100)
             quit("cant listen to a port\n");
     }
@@ -117,6 +114,9 @@ int main (int argc, char** argv) {
     // FIXME open err 
     printf("Root hash: %s\n", RootMerkleHash(file).hex().c_str());
 
+       if (root_hash==Sha1Hash() && bindaddr==Address() && tracker==Address())
+        exit(0);
+
     tint start_time = NOW;
     tint end_time = TINT_NEVER;
     
diff --git a/swift.h b/swift.h
index d833c3d..5b61f46 100644 (file)
--- a/swift.h
+++ b/swift.h
@@ -49,11 +49,6 @@ Messages
 #ifndef SWIFT_H
 #define SWIFT_H
 
-#ifdef _MSC_VER
-#include "compat/stdint.h"
-#else
-#include <stdint.h>
-#endif
 #include <deque>
 #include <vector>
 #include <algorithm>
@@ -311,7 +306,9 @@ namespace swift {
         HashTree&   file () { return transfer_->file(); }
         const Address& peer() const { return peer_; }
         tint ack_timeout () {
-            return std::min(30*TINT_SEC,rtt_avg_ + std::max(dev_avg_,MIN_DEV)*4);
+                       tint dev = dev_avg_ < MIN_DEV ? MIN_DEV : dev_avg_;
+                       tint tmo = rtt_avg_ + dev * 4;
+                       return tmo < 30*TINT_SEC ? tmo : 30*TINT_SEC; 
         }
         uint32_t    id () const { return id_; }
         
index e769bec..c8953bb 100644 (file)
@@ -6,16 +6,10 @@
  *  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 "swift.h"
-#include "compat/util.h"
 
 #include "ext/seq_picker.cpp" // FIXME FIXME FIXME FIXME