Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

oload_preloader: split src in *_t and *_exe + moved to /lib + basic m…

…an page

	deleted:    contrib/webpeer_preload/webpeer_preload.js
	new file:   lib/oload_preloader_exe.js
	new file:   lib/oload_preloader_t.js
  • Loading branch information...
commit 4a09472ca027fa4efcad82d7b84f29df5354b972 1 parent 76c64eb
@jeromeetienne authored
View
87 contrib/webpeer_preload/webpeer_preload.js
@@ -1,87 +0,0 @@
-/**
- * tools to preload static files in webpeer
- *
- * * TODO change the naming
- * ** class naming + file naming
- * * do i make this a real tool or just a demo
- * ** man page + inline help
- * ** source cleanup
- * * TODO make this more tunable...
- * ** currently hardcoded for http static file
- * ** what if i want to preload a .torrent ?
-*/
-
-// include system dependancies
-var http = require('http');
-// include local dependancies
-var url_builder_oload_t = require('../../lib/url_builder_oload_t');
-var app_detect = require('../../lib/neoip_app_detect');
-
-
-/**
- * Preload static http file in oload
- * * use a trick on neoip-oload
- * ** httpo_maxrate=0k thus neoip-oload will never deliver data on the client http cnx
- * ** read_ahead=2g thus neoip-oload will read up to 2gbyte on this url
-*/
-var oload_content_preload = function(content_url, failure_cb){
- // set default value if needed
- if( !failure_cb ) failure_cb = function(error){}
- // build the webpeer_url
- var webpeer_url = url_builder_oload_t.create(content_url)
- .set('outter_var/httpo_maxrate' , '0k')
- .set('outter_var/read_ahead' , '2g') // FIXME this hardcoded length is lame
- .set('outter_uri', app_detect.cache_get('oload').root_url)
- .to_string();
-
- // create http client
- var parsed_url = require('url').parse(webpeer_url);
- var client = http.createClient((parsed_url.port||80), parsed_url.hostname)
- // bind error cases at the socket level
- client.on("error" , function(e){ failure_cb("error due to "+e.message) });
- client.on("timeout" , function(e){ failure_cb("timeout") });
- // create the request
- var request = client.request('GET', parsed_url.pathname, {
- 'host' : parsed_url.hostname
- });
- // init the reponse
- request.on('response', function(response) {
- // handle error at http level
- if( response.statusCode != 200 ){
- failure_cb("http error ("+response.statusCode+")")
- return;
- }
- });
- request.end();
-
- /** Stop this preload
- */
- var close = function(){
- client_req.connection.destroy();
- }
-
- // return the public API
- return {
- close : close
- }
-}
-
-
-// get content_urls from the cmdline
-var content_urls = process.argv.slice(2);
-
-// discover neoip-oload and act depending on its presence or not
-app_detect.discover_app({
- app_suffix : "oload",
- hostname : "127.0.0.1",
- success_cb : function(root_url, version){
- // once neoip-oload found, preload all content_urls
- for(var i = 0; i < content_urls.length; i++){
- var content_url = content_urls[i];
- oload_content_preload(content_url);
- }
- },
- failure_cb : function(error){
- console.log("Webpeer has not been found (due to "+error+")");
- }
-})
View
77 lib/oload_preloader_exe.js
@@ -0,0 +1,77 @@
+/**
+ * tools to preload static files in webpeer
+ *
+ * * TODO change the naming
+ * ** class naming + file naming
+ * ** _t and _exe
+ * ** put that in lib/oload_preloader_*
+ * * do i make this a real tool or just a demo
+ * ** man page + inline help
+ * ** source cleanup
+ * ** YES for real tool. preload is close to caching
+ * * TODO make this more tunable...
+ * ** currently hardcoded for http static file
+ * ** what if i want to preload a .torrent ?
+ * ** get a url_builder_oload_t as input instead of a put content_url
+*/
+
+// include local dependancies
+var oload_preloader_t = require('./oload_preloader_t');
+var app_detect = require('./neoip_app_detect');
+var url_builder_oload_t = require('./url_builder_oload_t');
+
+// get content_urls from the cmdline
+var content_urls = process.argv.slice(2);
+
+// discover neoip-oload and act depending on its presence or not
+app_detect.discover_app({
+ app_suffix : "oload",
+ hostname : "127.0.0.1",
+ success_cb : function(root_url, version){
+ // once neoip-oload found, preload all content_urls
+ for(var i = 0; i < content_urls.length; i++){
+ var content_url = content_urls[i];
+ var url_builder_oload = url_builder_oload_t.create(content_url);
+ url_builder_oload.set('outter_uri', app_detect.cache_get('oload').root_url);
+ oload_preloader_t.create({
+ url_builder_oload : url_builder_oload
+ });
+ }
+ },
+ failure_cb : function(error){
+ console.log("Webpeer has not been found (due to "+error+")");
+ }
+})
+
+//////////////////////////////////////////////////////////////////////////////////
+// Man Page //
+//////////////////////////////////////////////////////////////////////////////////
+
+/* Man page in perlpod format. to view with "pod2man thisfile | man -l -"
+=pod
+
+=head1 NAME
+
+webpeer-preloader - Preload content in webpeer
+
+=head1 SYNOPSIS
+
+B<webpeer_preloader> url [url...]
+
+=head1 DESCRIPTION
+
+It will preload the content of each URL in webpeer. It aims speed up delivery.
+
+=head1 EXAMPLES
+
+=over
+
+=item B<basic usage>
+
+$ webpeer-preloader http://example.com/movie.mp4 http://example.com/song.mp3
+
+It will discover local webpeer and preload both urls
+
+=cut
+
+*/
View
91 lib/oload_preloader_t.js
@@ -0,0 +1,91 @@
+/**
+ * tools to preload static files in webpeer
+*/
+
+// include system dependancies
+var http = require('http');
+// include local dependancies
+var url_builder_oload_t = require('./url_builder_oload_t');
+
+
+/**
+ * Preload static http file in oload
+ * * use a trick on neoip-oload
+ * ** httpo_maxrate=0k thus neoip-oload will never deliver data on the client http cnx
+ * ** read_ahead=2g thus neoip-oload will read up to 2gbyte on this url
+*/
+var oload_preloader_t = function(ctor_opts){
+ //////////////////////////////////////////////////////////////////////////
+ // class variables //
+ //////////////////////////////////////////////////////////////////////////
+ // copy ctor_opts + set default values if needed
+ var url_builder_oload = ctor_opts.url_builder_oload || console.assert(false);
+ var failure_cb = ctor_opts.failure_cb || function(error){};
+
+ //////////////////////////////////////////////////////////////////////////
+ // ctor/dtor //
+ //////////////////////////////////////////////////////////////////////////
+ var ctor = function(){
+ client_start();
+ }
+ var dtor = function(){
+ idle_timer_stop();
+ client_stop();
+ }
+ //////////////////////////////////////////////////////////////////////////
+ // http client //
+ //////////////////////////////////////////////////////////////////////////
+ var client_req = null;
+ var client_start = function(){
+ // build the webpeer_url
+ var webpeer_url = url_builder_oload
+ .set('outter_var/httpo_maxrate' , '0k')
+ .set('outter_var/read_ahead' , '2g') // FIXME this hardcoded length is lame
+ .to_string();
+ // create http client
+ var parsed_url = require('url').parse(webpeer_url);
+ var client = http.createClient((parsed_url.port||80), parsed_url.hostname)
+ // bind error cases at the socket level
+ client.on("error" , function(e){ failure_cb("error due to "+e.message) });
+ client.on("timeout" , function(e){ failure_cb("timeout") });
+ // create the request
+ var client_req = client.request('GET', parsed_url.pathname, {
+ 'host' : parsed_url.hostname
+ });
+ // init the reponse
+ client_req.on('response', function(response) {
+ // handle error at http level
+ if( response.statusCode != 200 ){
+ failure_cb("http error ("+response.statusCode+")")
+ return;
+ }
+ });
+ client_req.end();
+ }
+ var client_stop = function(){
+ client_req.connection.destroy();
+ }
+
+
+ //////////////////////////////////////////////////////////////////////////
+ // run initialisation //
+ //////////////////////////////////////////////////////////////////////////
+ // call the contructor
+ ctor();
+ // return the public properties
+ return {
+ destroy : dtor
+ }
+}
+
+/**
+ * Class method to create an object
+ * - thus avoid new operator
+*/
+oload_preloader_t.create = function(ctor_opts){
+ return new oload_preloader_t(ctor_opts);
+}
+
+// export it via commonjs
+exports.create = oload_preloader_t.create;
+

0 comments on commit 4a09472

Please sign in to comment.
Something went wrong with that request. Please try again.