Author: sitharus
Date: 2006-06-09 11:38:29 +0000 (Fri, 09 Jun 2006)
New Revision: 9093

Added:
   trunk/apps/rubyFreenet/rsite/putsite.rb
Modified:
   trunk/apps/rubyFreenet/freenet/fcp/client.rb
   trunk/apps/rubyFreenet/freenet/fcp/message.rb
   trunk/apps/rubyFreenet/freenet/uri.rb
   trunk/apps/rubyFreenet/indexr/indexr.rb
   trunk/apps/rubyFreenet/rsite/rsite.rb
Log:
More work around inserting files
Altered URI to have better behaviour with USKs, but still far from perfect
putsite.rb is a basic site insertion tool, still needs documenting


Modified: trunk/apps/rubyFreenet/freenet/fcp/client.rb
===================================================================
--- trunk/apps/rubyFreenet/freenet/fcp/client.rb        2006-06-09 11:29:13 UTC 
(rev 9092)
+++ trunk/apps/rubyFreenet/freenet/fcp/client.rb        2006-06-09 11:38:29 UTC 
(rev 9093)
@@ -133,7 +133,7 @@
       # Generates a keypair for SSK use. If used synchronously it returns
       # [InsertURI, RequestURI], otherwise extract InsertURI and RequestURI
       # from response.items
-      def generate_keypair(async, &callback)
+      def generate_keypair(async = false, &callback)
         message = Message.new('GenerateSSK', nil, nil, callback)
         send(message, async)
         if async

Modified: trunk/apps/rubyFreenet/freenet/fcp/message.rb
===================================================================
--- trunk/apps/rubyFreenet/freenet/fcp/message.rb       2006-06-09 11:29:13 UTC 
(rev 9092)
+++ trunk/apps/rubyFreenet/freenet/fcp/message.rb       2006-06-09 11:38:29 UTC 
(rev 9093)
@@ -59,7 +59,7 @@
       # called from any thread.
       def wait_for_response
         until @response
-          sleep(0.5)
+          sleep(3)
           lock
           unlock
           Thread.pass

Modified: trunk/apps/rubyFreenet/freenet/uri.rb
===================================================================
--- trunk/apps/rubyFreenet/freenet/uri.rb       2006-06-09 11:29:13 UTC (rev 
9092)
+++ trunk/apps/rubyFreenet/freenet/uri.rb       2006-06-09 11:38:29 UTC (rev 
9093)
@@ -5,7 +5,7 @@
   # This could be completely wrong. Any criticism welcome
   class URI
     include Comparable
-    attr_reader :site, :path, :uri
+    attr_accessor :type, :site, :path, :uri, :version

     # This can take a URI in following formats:
     #  /freenet:SSK at ...
@@ -23,7 +23,8 @@
       raise URIError.new("Invalid Freenet URI: #{uri}") unless uri =~ 
/^(?:[UKS]S|CH)K@/

       @uri = uri
-      @site = @uri.match(/^(?:[UKS]S|CH)K@[^\/]+/)[0]
+      @type = @uri.match(/^(?:[UKS]S|CH)K/)[0]
+      @site = @uri.match(/^(?:[UKS]S|CH)K@([^\/]+)/)[1]
       @path = @uri.match(/(\/[^#\?]+)/)[1] if @uri =~ /\/[^#\?]+/
       @anchor = @uri.match(/#(.+)/)[1] if @uri =~ /#.+/
       @query_string = @uri.match(/\?([^#]+)/)[1] if @uri =~ /\?/
@@ -31,7 +32,7 @@

     # Return a URI that can be fed to Freenet
     def uri
-      "#{@site}#{@path}#{"?#{@query_string}" if @query_string}#{"##{@anchor}" 
if @anchor}"
+      "#{@type}@#{@site}#{@path}#{'/'+ at version.to_s if 
@version}#{"?#{@query_string}" if @query_string}#{"##{@anchor}" if @anchor}"
     end

     # Merge in a URI or a URI fragment and provide the finished URI. Attempts
@@ -45,12 +46,12 @@
       begin
         uri = URI.new(uri) unless uri.respond_to? :uri
         if uri.site == @site
-          return "#{@site}#{merge_paths(@path, uri.path)}"
+          return "#{@type}#{@site}#{merge_paths(@path, uri.path)}"
         else
           return uri.uri
         end
       rescue URIError => e # We have a fragment
-        "#{@site}#{merge_paths(@path, uri)}"
+        "#{@type}#{@site}#{merge_paths(@path, uri)}"
       end
     end


Modified: trunk/apps/rubyFreenet/indexr/indexr.rb
===================================================================
--- trunk/apps/rubyFreenet/indexr/indexr.rb     2006-06-09 11:29:13 UTC (rev 
9092)
+++ trunk/apps/rubyFreenet/indexr/indexr.rb     2006-06-09 11:38:29 UTC (rev 
9093)
@@ -1,5 +1,5 @@
 $:.push('../')
-require 'freenet_client'
+require 'freenet'
 require 'erb'
 require 'rubyful_soup'


Added: trunk/apps/rubyFreenet/rsite/putsite.rb
===================================================================
--- trunk/apps/rubyFreenet/rsite/putsite.rb     2006-06-09 11:29:13 UTC (rev 
9092)
+++ trunk/apps/rubyFreenet/rsite/putsite.rb     2006-06-09 11:38:29 UTC (rev 
9093)
@@ -0,0 +1,63 @@
+#!/usr/bin/ruby
+# == Description
+#
+# Basic wrapper for rsite to put in a new or update an existing freesite from
+# a directory on the same server as the node.
+#
+# == Usage
+#
+# putsite.rb [OPTIONS] ... DIR
+#
+# -h, --help: 
+#     This message
+#
+# -n, --name:
+#     The site name and the file to save the rsite info to. If this is found
+#     in ./ then the details will be loaded from it and the site updated
+#
+# -t, --type:
+#     The type of site, either USK or SSK
+require 'rsite'
+require 'getoptlong'
+require 'rdoc/usage'
+
+opts = GetoptLong.new(
+  ['--help', '-h', GetoptLong::NO_ARGUMENT],
+  ['--name', '-n', GetoptLong::REQUIRED_ARGUMENT],
+  ['--type', '-t', GetoptLong::OPTIONAL_ARGUMENT],
+  ['--keys', '-k', GetoptLong::OPTIONAL_ARGUMENT]
+)
+
+dir = nil
+name = nil
+keys = nil
+type = 'SSK'
+
+opts.each do |opt, arg|
+  case opt
+  when '--help'
+    RDoc::usage
+  when '--name'
+    name = arg.to_s
+  when '--type'
+    type = arg.to_s
+  when '--keys'
+    keys = arg.to_s.split(';')
+  end
+end
+if ARGV.length != 1
+  puts "Missing dir argument (try --help)"
+  exit 0
+end
+
+dir = File.expand_path(ARGV.shift)
+if name and File.exists? name
+  site = Freenet::Site.load(name)
+else
+  site = Freenet::Site.new(type, dir, name)
+end
+
+site.keys = keys if keys
+site.connect
+site.insert_site
+site.save(name)
\ No newline at end of file

Modified: trunk/apps/rubyFreenet/rsite/rsite.rb
===================================================================
--- trunk/apps/rubyFreenet/rsite/rsite.rb       2006-06-09 11:29:13 UTC (rev 
9092)
+++ trunk/apps/rubyFreenet/rsite/rsite.rb       2006-06-09 11:38:29 UTC (rev 
9093)
@@ -1,16 +1,16 @@
-require 'freenet_client'
+$:.push('../')
+require 'freenet'

 module Freenet
   class Site
-    attr_reader :keys
-    attr_accessor :version
+    attr_accessor :version, :client, :keys, :name
     def self.load(file)
       Marshal.load(IO.read(file))
     end

-    def initialize(type, path)
+    def initialize(type, path, name)
       raise SiteError.new('Invalid type') unless 
['USK','SSK','CHK','KSK'].include? type
-      @path, @type = path, type
+      @path, @type, @name = path, type, name
       @version = ''
     end

@@ -22,9 +22,26 @@
       @keys = @client.generate_keypair
     end

-    def insert_site
+    def process_site
+      
     end

+    # Insert a whole site from disk
+    def insert_site(path = nil)
+      path ||= @path
+      case @type
+      when 'CHK', 'KSK' then raise SiteError.new('Invalid key type for site 
insert')
+      end
+      generate_key unless @keys
+      @uri ||= Freenet::URI.new(@keys[0])
+      @uri.type = @type
+      @uri.path = "/#{@name}"
+      @uri.version ||= 0
+      @uri.version += 1
+      puts "Insert key: #{@uri.uri}\nRequest key: #{@uri.uri}"
+      @client.putdir(@uri, path)
+    end
+    
     # Insert a single file. You probably want a CHK for this, use it to insert
     # large files that won't change, eg media files.
     #
@@ -45,17 +62,22 @@
         uri = "#{@type}@#{site}"
       end
       @client.put(uri, nil, true, 'UploadFrom'=>'disk','Filename'=>path) do 
|status, message, response|
-      when :uri_generated
-        puts "URI created: #{response.items['URI']}"
-      when :success
-        puts "File inserted successfully"
-      when :failed
-        puts "File insertion failed!"
+        case status
+        when :uri_generated
+          puts "URI created: #{response.items['URI']}"
+        when :success
+          puts "File inserted successfully"
+        when :failed
+          puts "File insertion failed!"
+        end
       end
     end

     def save(file)
+      client = @client
+      @client = nil
       File.open(file, 'w') {|f| f.write(Marshal.dump(self))}
+      @client = client
     end
   end



Reply via email to