Hi Gabriele, and others,

think it's best to send my protocol, though it's not
yet working. Remove the 'halt, to get the error.

If someone knows what to do about it, please let me know.


; --- Script starting below this line  ---
REBOL [
   Title:  "REBOL FreeNet Protocol"
   Date:   2000-02-29
   File:   %freenet.r
   Author: "Ingo Hohmann"
   Email:  [EMAIL PROTECTED]
   Version: 0.0.2
   Protocol-name: "Freenet"
   Protocol-version: "1.0"
   Purpose: {
          Read data from / write data to
                freenet servers

        Implements the Freenet Protocol 1.0
        See http://freenet.sourceforge.net/
    }
   Usage:   {

        read freenet://localhost:10001/My/key
          gets data associated with "My/key"
          from the freenet server running at
          localhost, port 10001

        write freenet://localhost:10001/My_key "MyData"
          Adds "MyData", associated with "My_key"
          to freenet server running at
          localhost, port 10001

        (ipaddresses should be of the form protocol/data, with
         tcp currently the only supported protocol, it would become
         tcp/ip-address:port
         if the tcp/ part is omitted, I assume tcp myself, but originally
         the addresses above should have been written:

         read freenet://tcp/localhost:10001/My/key
         write freenet://tcp/localhost:10001/My_key "MyData"
        )

   }
   History: [
    [ 2000-02-29 "Ingo Hohmann" 0.0.2 
        {Freenet addresses allowed (freenet://tcp/localhost:10004 will connect
         to localhost over tcp)} ]
    [ 2000-02-28 "Ingo Hohmann" 0.0.1 
               {first version} ]
   ]
   KnownBugs: [
      {close port -> I cannot return the data read ... }
   ]
   Category: [web]
]

random/seed now

freenet-protocol: make Root-Protocol [
   Scheme: 'freenet
   protocol: rejoin [system/script/header/protocol-name " " 
system/script/header/protocol-version newline]

   Port-id: 10000
   Port-flags: system/standard/port-flags/direct

   open-check: none
   close-check: none

   make-id: func ["Create Message-ID" /local id i chars len] [
      chars: "0123456789abcdef"
      len: length? chars
      id: copy ""
      for i 1 16 1 [
         append id pick chars random len
      ]
      id
   ]

   find-port: func["Find a free port to connect to" /local port-num p] [
      port-num: + random 9999 10000
      while [error? try [p: system/words/open join tcp://: port-num]] [
         print "while"
         port-num: port-num + 1
      ]
      system/words/close p
      port-num
   ]

   init: func [
      "Parse URL and/or check the port spec object"
      port "Unopened port spec"
      spec {Argument passed to open or make (a URL or port-spec)}
      /local scheme
   ][
      if url? spec [
        if freenet://tcp/ = (copy/part spec 14) [
            spec: rejoin [freenet:// copy skip spec 14]
        ]
        net-utils/url-parser/parse-url port spec
      ]
      scheme: port/scheme port/url: spec
      if none? port/host [
         net-error reform ["No network server for" scheme "is specified"]
      ]
      if none? port/port-id [
         net-error reform ["No port address for" scheme "is specified"]
      ]
   ]

   ; unchanged from root-protocol
   open-proto: func [
      {Open the socket connection and confirm server response.}
      port "Initalized port spec"
      /locals sub-port data in-bypass find-bypass bp
   ][
      net-utils/net-log ["Opening tcp for" port/scheme]
      if not system/options/quiet [print ["connecting to:" port/host]]
      find-bypass: func [host bypass /local x] [
         if found? host [
            foreach item bypass [
               if all [x: find/match/any host item tail? x] [return true]
            ]
         ]
         false
      ]
      in-bypass: func [host bypass /local item x] [
         if any [none? bypass empty? bypass] [return false]
         if not tuple? load host [host: form system/words/read join dns:// host]
         either find-bypass host bypass [
            true
         ] [
            host: system/words/read join dns:// host
            find-bypass host bypass
         ]
      ]
      either all [
         port/proxy/host
         bp: not in-bypass port/host port/proxy/bypass
         find [socks4 socks5 socks] port/proxy/type
      ] [
         port/sub-port: net-utils/connect-proxy port
      ] [
         sub-port: system/words/open/lines [ ; ----------------------- OPEN is here
            scheme: 'tcp
            host: either all [port/proxy/type = 'generic bp] [port/proxy/host] 
[port/proxy/host: none port/host]
            user: port/user
            pass: port/pass
            port-id: either all [port/proxy/type = 'generic bp] [port/proxy/port-id] 
[port/port-id]
         ]
         port/sub-port: sub-port
      ]
      port/sub-port/timeout: port/timeout
      port/sub-port/user: port/user
      port/sub-port/pass: port/pass
      port/sub-port/path: port/path
      port/sub-port/target: port/target
      net-utils/confirm/multiline port/sub-port open-check
      port/state/flags: port/state/flags or port-flags
   ]

   open: func [
      {Open the socket connection and confirm server response.}
      port "Initalized port spec"
      /locals sub-port data in-bypass find-bypass bp buffer-length buffer
   ][
      open-proto port

      port/state/with: "^/"
      port/sub-port/state/with: "^/"

      ; Get hello
      buffer-length: 15
      buffer: make string! buffer-length
      read-io port/sub-port buffer buffer-length

      if not buffer = protocol [
         net-error rejoin ["Wrong Protocol-Version: " buffer]
      ]
      ; probe port
   ] ; open

   close: func [
      {Quit server, confirm and close the socket connection}
      port "An open port spec"
   ][
      if not none? port/sub-port [
         port: port/sub-port
         system/words/close port
      ]
   ]

   read: func [
      port [port!]   "The port object"
      data [string!] "A buffer"
      /local messageid hopstolive depth searchkey
      command local-port-num ap c
   ][
      local-port-num: find-port
      MessageID: make-id
      HopsToLive: 256
      depth: 50
      SearchKey: to-string either none? port/path [ port/target ] [rejoin [port/path 
port/target]]

      buffer-length: 2000
      answer: make string! buffer-length

      command: rejoin [
         "DataRequest" newline
         "Source=tcp/" system/network/host ":" local-port-num newline
         "SearchKey=" SearchKey newline
         "UniqueID=" MessageID newline
         "Depth=" depth newline
         "HopsToLive=" HopsToLive newline
      ]

      ap: system/words/open rejoin [ tcp://: local-port-num ]

      ;insert port/sub-port command
      write-io port/sub-port command length? command

      system/words/close port

      wait ap
      c: first ap
      write-io c protocol length? protocol

      answer: copy c

      close c
      close ap

      either parse answer [ "DataReply" newline to end ] [
         parse answer [ thru "Data^/" copy answer to end ]
      ] [
         answer: none
      ]

      probe answer

      ; FIXME: 
      ; prevents port not open error ...
      halt
   ] ; read


   write: func [
      port [port!]   "The port object"
      data [string!] "A buffer"
      /local messageid hopstolive depth searchkey
      command local-port-num ap c
   ][
      local-port-num: find-port
      MessageID: make-id
      HopsToLive: 256
      depth: 50
      SearchKey: to-string either none? port/path [ port/target ] [rejoin [port/path 
port/target]]

      buffer-length: 100
      answer: make string! buffer-length

      command: rejoin [
         "DataInsert" newline
         "DataSource=tcp/" port/host ":" port/port-id newline
         "Source=tcp/" system/network/host ":" local-port-num newline
         "SearchKey=" SearchKey newline
         "UniqueID=" MessageID newline
         "Depth=" depth newline
         "HopsToLive=" HopsToLive newline
         "Data" newline
         data
      ]

      ap: system/words/open rejoin [ tcp://: local-port-num ]

      write-io port/sub-port command length? command

      close port

      wait ap
      c: first ap
      write-io c protocol length? protocol

      answer: copy c

      close c
      close ap

      ;      probe answer
      halt
   ] ; write


   ; install the protocol
   net-utils/net-install freenet self 10000
]
; --- Script ending above this line  ---
halt


regards,

Ingo

--  _     .                                _
ingo@)|_ /|  _| _  <We ARE all ONE   www._|_o _   _ ._ _  
www./_|_) |o(_|(/_  We ARE all FREE> ingo@| |(_|o(_)| (_| 
http://www.2b1.de/Rebol/                     ._|      ._|

Reply via email to