Permalink
Browse files

#46: Checks the user's filesystem quotas accroding to file size and o…

…nly if available uses the filesystem

added apiValidator for filesystem
#44: progress bar for loading the file added
#40: reseed and resume works if previously used filesystem

Changed block size
  • Loading branch information...
1 parent 7c3403e commit 8fbc5a5820577cb424119f9071bc844f3adbbbd1 @shacharz shacharz committed Sep 4, 2013
View
@@ -1,9 +1,9 @@
-![Sharefest](https://raw.github.com/Peer5/ShareFest/master/sharefest/public/img/logo.png)
+![Sharefest](https://raw.github.com/Peer5/ShareFest/master/public/img/logo.png)
One-To-Many sharing application. Serverless.
Eliminates the need to fully upload your file to services such as Dropbox or Google Drive.
-Put your file and start sharing immediately with anyone that enters the page.
-Pure javascript-based. No plugins needed thanks to HTML5 WebRTC Data Channel API - http://webrtc.org
+Put your file and start sharing immidiately with anyone that enters the page.
+Pure javascript-based, no plugins needed, thanks to HTML5 WebRTC Data Channel API - http://webrtc.org
How does it work
================
@@ -16,8 +16,8 @@ Peer5 Coordinator (or any other solution) is used to accomplish this.
Each peer will connect to few other peers in order to maximize the distribution of the file.
Supporting Chrome (>26, now stable) and Firefox (>19)
-First version includes a simple page that one user will drag a file onto to
-share, and other users will enter the first user's url and start downloading the file.
+First version includes a simple page that one user will drag a file to
+share, and a other users will enter the first user's url and start downloading the file.
test it out at: http://sharefest.me
@@ -41,4 +41,4 @@ It was soon open sourced to GitHub and now being developed by Peer5 and a commun
License
==============
-Apache 2.0 - see LICENSE file
+Apache 2.0 - see LICENSE file
@@ -11,7 +11,6 @@
this.peerConnectionImpl = null;
this.prefetchFlag = {};
this.resourceState = {}; //<resourceId,bool> true===everything is normal, false === error/pause/stop
- this.registerEvents();
this.configureBrowserSpecific();
this.availableTimeStamp = 0;
},
@@ -24,6 +23,7 @@
if (forceInit || !this.remoteAvailabilityMaps[swarmId])
this.remoteAvailabilityMaps[swarmId] = {};
this.prefetchFlag[swarmId] = prefetch;
+ this.registerEvents();
},
/** @Public Methods*/
@@ -95,8 +95,11 @@
isAvailable:function(swarmId){
if(!this.resourceState[swarmId]) return false;
var availableTimeStamp = Date.now();
+ var bm = peer5.core.data.BlockCache.get(swarmId);
for (var peerId in this.peerConnections) {
- if (this.remoteAvailabilityMaps[swarmId][peerId] && this.peerConnections[peerId].numOfPendingChunks < 0.9 * this.peerConnections[peerId].maxNumOfPendingChunks) {
+ if (this.remoteAvailabilityMaps[swarmId][peerId]
+ && this.remoteAvailabilityMaps[swarmId][peerId].numOfOnBits > bm.numOfVerifiedBlocks //only consider peers that can actually give me something TODO: change this to diff on availabilitymaps
+ && this.peerConnections[peerId].numOfPendingChunks < 0.9 * this.peerConnections[peerId].maxNumOfPendingChunks) {
// console.log("++++P2P is available++++ " + this.peerConnections[peerId].numOfPendingChunks + " " + this.peerConnections[peerId].maxNumOfPendingChunks + " " + (availableTimeStamp - this.availableTimeStamp));
this.availableTimeStamp = availableTimeStamp;
return true;
@@ -510,7 +513,11 @@
closeConnection:function (peerId) {
peer5.warn("closing connection with " + peerId + " due to high packet loss");
- this.peerConnections[peerId].close();
+ if(this.peerConnections[peerId]){
+ peer5.info(this.peerConnections[peerId]);
+ this.peerConnections[peerId].close();
+ delete this.peerConnections[peerId];
+ }
},
canUpload:function(swarmId){
View
@@ -6,11 +6,11 @@
this.pendingObjectUrlCb = {};
this.finishWriteCbs = [];
// this.removeAll(function(){window.webkitRequestFileSystem(window.TEMPORARY, peer5.config.FS_SIZE, this.onInitFs, this.errorHandler);});
- window.webkitRequestFileSystem(window.TEMPORARY, peer5.config.FS_SIZE, this.onInitFs, this.errorHandler);
+// window.webkitRequestFileSystem(window.TEMPORARY, peer5.config.FS_SIZE, this.onInitFs, this.errorHandler);
},
createResource:function(resourceId,cb,single){ //single:true - single file, :false a directory
- peer5.log("Adding resource " + resourceId + " to the filesystem.");
+ peer5.info("Adding resource " + resourceId + " to the filesystem.");
var thi$ = this;
this.fs.root.getFile(peer5.config.FS_ROOT_DIR + resourceId,{create:true},function(fileEntry){
if(cb) cb(true);
@@ -27,6 +27,19 @@
},function(e){thi$.errorHandler(e);if(cb) cb(false);})
},
+ renameResource:function(oldResourceId,newResourceId,cb){
+ peer5.info("changing resource name from " + oldResourceId + " to " + newResourceId);
+ var thi$ = this;
+ this.fs.root.getDirectory(peer5.config.FS_ROOT_DIR,{create:false},function(dirEntry){
+ dirEntry.getFile(oldResourceId,{create:false},function(fileEntry){
+ fileEntry.moveTo(dirEntry,newResourceId,function(succ){
+ if(succ) peer5.info("succesfully renamed");
+ if(cb) cb(succ);
+ },function(e){thi$.errorHandler(e);if(cb) cb(false);});
+ },function(e){thi$.errorHandler(e);if(cb) cb(false);});
+ },function(e){thi$.errorHandler(e);if(cb) cb(false);});
+ },
+
write:function(resourceId,data,position, cb){ //implementation supports single file only
peer5.debug("Writing to resource " + resourceId);
if(this._writeAvailable()){
@@ -112,20 +125,53 @@
},
removeAll:function(cb){
+ peer5.warn("removing all files in filesystem");
var thi$ = this;
var dirReader = this.fs.root.createReader();
dirReader.readEntries(function(entries) {
for (var i = 0, entry; entry = entries[i]; ++i) {
if (entry.isDirectory) {
- entry.removeRecursively(function() {}, function(e){thi$.errorHandler(e);if(cb) cb(false);});
+ entry.removeRecursively(function() {if(cb) cb(true);}, function(e){thi$.errorHandler(e);if(cb) cb(false);});
} else {
- entry.remove(function() {}, function(e){thi$.errorHandler(e);if(cb) cb(false);});
+ entry.remove(function() {if(cb) cb(true);}, function(e){thi$.errorHandler(e);if(cb) cb(false);});
}
}
peer5.debug('Directory emptied.');
},function(e){thi$.errorHandler(e);if(cb) cb(false);} );
},
+ removeRootDir:function(cb){
+ peer5.warn("removing all files in filesystem under " + peer5.config.FS_ROOT_DIR);
+ var thi$ = this;
+ var dirReader = this.fs.root.createReader();
+ dirReader.readEntries(function(entries) {
+ for (var i = 0, entry; entry = entries[i]; ++i) {
+ if (entry.isDirectory && entry.name + '/' == peer5.config.FS_ROOT_DIR) {
+ entry.removeRecursively(function() {
+ thi$.fs.root.getDirectory(peer5.config.FS_ROOT_DIR,{create:true},function(dirEntry){
+ cb(true);
+ },thi$.errorHandler)
+ }, function(e){thi$.errorHandler(e);if(cb) cb(false);});
+ }
+ }
+ peer5.debug('Directory emptied.');
+ },function(e){thi$.errorHandler(e);if(cb) cb(false);} );
+ },
+
+ //cb(succ,freespace)
+ requestQuota:function(size,cb){
+ peer5.info("requesting quota size = " + size);
+ var thi$ = this;
+ var requestSize = 1.1*size; //taking 10% overhead
+ window.webkitRequestFileSystem(window.TEMPORARY, requestSize, function(fs){
+ thi$.onInitFs(fs);
+ thi$.queryQuota(function(succ,usage,quota){
+ cb(true,quota-usage);
+ })
+ },function(e){thi$.errorHandler(e);if(cb) cb(false);});
+ },
+
+ //cb(succ,usage,quota)
queryQuota:function(cb){
navigator.webkitTemporaryStorage.queryUsageAndQuota(function(usage, quota) {
peer5.info('Using: ' + (usage / quota) * 100 + '% of temporary storage');
@@ -182,7 +228,7 @@
fileWriter.write(data); //assuming data is of type blob
}
},function(e){thi$.errorHandler(e);if(cb) cb(false);});
- });
+ },function(e){thi$.errorHandler(e);if(cb) cb(false);});
},
_addWriteCommand:function(resourceId,data,position,cb){
@@ -195,7 +241,7 @@
this.onInitFs = function(fs){
thi$.fs = fs;
fs.root.getDirectory(peer5.config.FS_ROOT_DIR,{create:true},function(dirEntry){
- peer5.info("initiate filesystem");
+ peer5.info("initiated filesystem");
},thi$.errorHandler)
};
@@ -51,12 +51,24 @@
deserializeAndCopy:function (bitArray) {
this.bitArray = bitArray;
+ var numOfOnBits = 0;
+ for(var i=0; i < bitArray.length; ++i){
+ if(bitArray[i] != 0){
+ //how many on bits in this byte
+ for(var j=0;j<this.bitMask.length;++j){
+ if(this.bitArray[i] & this.bitMask[j])
+ numOfOnBits++;
+ }
+ }
+ }
+ this.numOfOnBits = numOfOnBits;
},
deserializeAndUpdate:function (blockIds) {
for (var i = 0; i < blockIds; ++i) {
this.set(blockIds[i]);
}
+ this.numOfOnBits += blockIds.length;
},
/** @Private Methods*/
Oops, something went wrong.

0 comments on commit 8fbc5a5

Please sign in to comment.