Revision: 185 http://rigsofrods.svn.sourceforge.net/rigsofrods/?rev=185&view=rev Author: rorthomas Date: 2009-04-20 01:28:15 +0000 (Mon, 20 Apr 2009)
Log Message: ----------- updated wsync Modified Paths: -------------- trunk/build/CMakeLists.txt trunk/build/wsync/CMakeLists.txt trunk/build/wsync/include/wsync.h trunk/build/wsync/source/main.cpp trunk/build/wsync/source/wsync.cpp Modified: trunk/build/CMakeLists.txt =================================================================== --- trunk/build/CMakeLists.txt 2009-04-19 22:53:13 UTC (rev 184) +++ trunk/build/CMakeLists.txt 2009-04-20 01:28:15 UTC (rev 185) @@ -65,7 +65,7 @@ #option(ROR_BUILD_ANGELSCRIPT "build and use the new scripting engine" FALSE) set(ROR_BUILD_ANGELSCRIPT TRUE) #option(WSYNC_BUILD_WSYNC "builds the wsync tool" FALSE) -set(WSYNC_BUILD_WSYNC FALSE) +set(WSYNC_BUILD_WSYNC TRUE) set(WSYNC_BUILD_INSTALLER FALSE) #option(ROR_BUILD_LUASCRIPT "adds the LUA Scripting engine" TRUE) set(ROR_BUILD_LUASCRIPT TRUE) Modified: trunk/build/wsync/CMakeLists.txt =================================================================== --- trunk/build/wsync/CMakeLists.txt 2009-04-19 22:53:13 UTC (rev 184) +++ trunk/build/wsync/CMakeLists.txt 2009-04-20 01:28:15 UTC (rev 185) @@ -17,8 +17,15 @@ # first compile the lib, then the console tool add_library(libwsync STATIC source/SHA1.cpp source/wsync.cpp include/wsync.h) -add_executable(wsync source/main source/icon.rc) +IF(WIN32) + add_definitions("-D_CRT_SECURE_NO_WARNINGS") + add_executable(wsync source/main source/icon.rc) +ELSEIF(UNIX) + add_executable(wsync source/main) +ELSEIF(APPLE) +ENDIF(WIN32) + target_link_libraries(wsync libwsync) IF(WIN32) Modified: trunk/build/wsync/include/wsync.h =================================================================== --- trunk/build/wsync/include/wsync.h 2009-04-19 22:53:13 UTC (rev 184) +++ trunk/build/wsync/include/wsync.h 2009-04-20 01:28:15 UTC (rev 185) @@ -12,6 +12,8 @@ #define INDEXFILENAME "files.index" +#define API_SERVER "api.rigsofrods.com" +#define API_MIRROR "/getwsyncmirror/" #define WMO_FULL 0x00000001 #define WMO_NODELETE 0x00000010 @@ -48,7 +50,7 @@ WSync(); ~WSync(); // main functions - int sync(boost::filesystem::path localDir, std::string server, std::string remoteDir); + int sync(boost::filesystem::path localDir, std::string server, std::string remoteDir, bool useMirror=true); int createIndex(boost::filesystem::path localDir, int mode); // useful util functions @@ -74,5 +76,7 @@ std::string formatFilesize(boost::uintmax_t size); void progressOutput(float progress, float speed=-1); void progressOutputShort(float progress); + int getMirrorURL(std::string &server, std::string &path, std::string &type); + int cleanURL(std::string &url); }; #endif Modified: trunk/build/wsync/source/main.cpp =================================================================== --- trunk/build/wsync/source/main.cpp 2009-04-19 22:53:13 UTC (rev 184) +++ trunk/build/wsync/source/main.cpp 2009-04-20 01:28:15 UTC (rev 185) @@ -1,5 +1,9 @@ #include "wsync.h" +#ifdef WIN32 +#include <conio.h> // for getch +#endif + using namespace std; using namespace boost::filesystem; @@ -50,6 +54,12 @@ string remote_path = "/"; WSync *w = new WSync(); w->sync(local_path, remote_server, remote_path); + if(argc == 1) + { + // wait for key press + printf("Press any key to continue...\n"); + getch(); + } /* } else if(argc == 2 && !strcmp(argv[1], "test")) { Modified: trunk/build/wsync/source/wsync.cpp =================================================================== --- trunk/build/wsync/source/wsync.cpp 2009-04-19 22:53:13 UTC (rev 184) +++ trunk/build/wsync/source/wsync.cpp 2009-04-20 01:28:15 UTC (rev 185) @@ -23,8 +23,65 @@ { } -int WSync::sync(boost::filesystem::path localDir, string server, string remoteDir) +int WSync::getMirrorURL(string &serverStr, string &remotePath, string &infoStr) { + path filename; + if(getTempFilename(filename)) + printf("error creating tempfile!\n"); + + if(downloadFile(filename.string(), API_SERVER, API_MIRROR)) + { + printf("error communicating with RoR API\n"); + return -1; + } + + FILE *f = fopen(filename.string().c_str(), "r"); + if (!f) + { + printf("error opening file '%s'", filename.string().c_str()); + return -1; + } + while(!feof(f)) + { + char server[2048]=""; + char dir[2048]=""; + char type[256]=""; + boost::uintmax_t filesize = 0; + int res = fscanf(f, "%s %s %s\n", server, dir, type); + if(res < 3) + { + printf("error communicating with RoR API\n"); + return -1; + } else + { + serverStr = std::string(server); + remotePath = std::string(dir); + infoStr = std::string(type); + return 0; + } + } + return 0; +} + +int WSync::cleanURL(string &url) +{ + int position = url.find("//"); + while (position != string::npos) + { + url.replace(position, 2, "/" ); + position = url.find( "//", position + 1 ); + } + position = url.find("//"); + while (position != string::npos) + { + url.replace(position, 2, "/" ); + position = url.find( "//", position + 1 ); + } + return 0; +} + +int WSync::sync(boost::filesystem::path localDir, string server, string remoteDir, bool useMirror) +{ // download remote currrent file first path remoteFileIndex; if(getTempFilename(remoteFileIndex)) @@ -102,17 +159,49 @@ if(filesToDownload) printf("downloading %d files now (%s) ..\n", filesToDownload, formatFilesize(predDownloadSize).c_str()); + // now find a suitable mirror + string mirror_server="", mirror_dir="", mirror_info=""; + bool use_mirror = false; + if(useMirror) + { + printf("searching for suitable mirror...\n"); + if(!getMirrorURL(mirror_server, mirror_dir, mirror_info)) + { + use_mirror=true; + printf("using mirror server: %s, %s\n", mirror_server.c_str(), mirror_info.c_str()); + } + } + + string server_use = server, dir_use = remoteDir; + if(use_mirror) + { + server_use = mirror_server; + dir_use = mirror_dir; + } + int changeCounter = 0, changeMax = changedFiles.size() + newFiles.size() + deletedFiles.size(); // do things now! if(newFiles.size()) { for(itf=newFiles.begin();itf!=newFiles.end();itf++, changeCounter++) { + int retrycount = 0; +retry: progressOutputShort(float(changeCounter)/float(changeMax)); printf(" A %s (%s) ", itf->filename.c_str(), formatFilesize(itf->filesize).c_str()); path localfile = localDir / itf->filename; - string url = "/" + remoteDir + "/" + itf->filename; - if(downloadFile(localfile, server, url, true)) + string url = "/" + dir_use + "/" + itf->filename; + int stat = downloadFile(localfile, server_use, url, true); + if(stat == -404 && retrycount == 0) + { + // fallback to main server! + printf("falling back to main server.\n"); + server_use = server; + dir_use = remoteDir; + retrycount++; + goto retry; + } + if(stat) printf("\nunable to create file: %s\n", itf->filename.c_str()); else printf(" \n"); @@ -123,11 +212,23 @@ { for(itf=changedFiles.begin();itf!=changedFiles.end();itf++, changeCounter++) { + int retrycount = 0; +retry2: progressOutputShort(float(changeCounter)/float(changeMax)); printf(" U %s (%s) ", itf->filename.c_str(), formatFilesize(itf->filesize).c_str()); path localfile = localDir / itf->filename; - string url = "/" + remoteDir + "/" + itf->filename; - if(downloadFile(localfile, server, url, true)) + string url = "/" + dir_use + "/" + itf->filename; + int stat = downloadFile(localfile, server_use, url, true); + if(stat == -404 && retrycount == 0) + { + // fallback to main server! + printf("falling back to main server.\n"); + server_use = server; + dir_use = remoteDir; + retrycount++; + goto retry2; + } + if(stat) printf("\nunable to update file: %s\n", itf->filename.c_str()); else printf(" \n"); @@ -357,7 +458,6 @@ int WSync::loadHashMapFromFile(boost::filesystem::path &filename, std::map<string, Hashentry> &hashMap, int &mode) { - FILE *f = fopen(filename.string().c_str(), "r"); if (!f) { @@ -388,11 +488,13 @@ int WSync::downloadFile(boost::filesystem::path localFile, string server, string path, bool displayProgress) { + // remove '//' and '///' from url + cleanURL(path); + //printf("\n downloading: http://%s%s\n", server.c_str(), path.c_str()); try { std::ofstream myfile; ensurePathExist(localFile); - myfile.open(localFile.string().c_str(), ios::out | ios::binary); boost::asio::io_service io_service; @@ -447,6 +549,7 @@ if (status_code != 200) { std::cout << endl << "Error: Response returned with status code " << status_code << "\n"; + return -status_code; } // Read the response headers, which are terminated by a blank line. @@ -465,6 +568,9 @@ } //printf("filesize: %d bytes\n", reported_filesize); + // open local file late -> prevent creating emoty files + myfile.open(localFile.string().c_str(), ios::out | ios::binary); + // Write whatever content we already have to output. if (response.size() > 0) myfile << &response; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Stay on top of everything new and different, both inside and around Java (TM) technology - register by April 22, and save $200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco. 300 plus technical and hands-on sessions. Register today. Use priority code J9JMT32. http://p.sf.net/sfu/p _______________________________________________ Rigsofrods-devel mailing list Rigsofrods-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rigsofrods-devel