Class: FCPClient

Inherits:
Communicator show all
Defined in:
lib/ruby-fcp/fcp_client.rb

Instance Attribute Summary

Attributes inherited from Communicator

#ConnectionIdentifier, #heartbeat, #responses, #utils

Instance Method Summary collapse

Methods inherited from Communicator

#close, #connect, #grab_response, #initialize, #keep_alive, #send_packet, #sock_thrd, #sort_out

Constructor Details

This class inherits a constructor from Communicator

Instance Method Details

#ddarun(directory, read, write) ⇒ Object

performs TestDDARequest and TestDDAResponse automagically read and write are true or false values


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ruby-fcp/fcp_client.rb', line 113

def ddarun(directory,read, write)
  send_packet @utils.packet_mangler({"Directory" => directory,"WantReadDirectory" => read, "WantWriteDirectory" => write} ,"TestDDARequest")
  res = wait_for(:dda, /TestDDAReply/).pop
  content = nil
  if write
    f = File.open(res["WriteFilename"],'w+')
    f.write res["ContentToWrite"]
    f.close
  elsif read
    content = File.open(res["ReadFilename"],'r').read
  end
  send_packet @utils.packet_mangler({"Directory" => directory,"ReadContent" => content}, "TestDDAResponse")
  response = wait_for(:dda ,/TestDDAComplete/).pop
  File.delete(res["WriteFilename"]) if write
  response
end

#direct_get(uri, wait = true, opts = {}) ⇒ Object


145
146
147
148
149
150
151
152
153
154
# File 'lib/ruby-fcp/fcp_client.rb', line 145

def direct_get(uri ,wait = true, opts={})
  id = @utils.id_generate
  options = {"URI" => uri, "Identifier" => id, "ReturnType" => 'direct', "Global" => false}.merge(opts)
  send_packet @utils.packet_mangler(options,"ClientGet")
  if wait
   wait_for(id,/AllData|GetFailed/) 
  else
   id
  end
end

#direct_put(uri, data, wait = true, opts = {}) ⇒ Object

Another interface to ClientPut that allows you to directly put data pnto freenet data is your data

Possible values of uri:


44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby-fcp/fcp_client.rb', line 44

def direct_put(uri,data, wait = true, opts = {})
  id = @utils.id_generate
  options = {"Identifier" => id, "URI" => uri ,"UploadFrom" =>  "direct", "DataLength" => data.bytesize }.merge(opts)
  send_packet @utils.packet_mangler(options,"ClientPut").sub! "EndMessage\n", "Data\n#{data}"
  if wait
    wait_for id,/PutFailed|PutSuccessful/
  else
    id
  end
end

#get_plugin_info(pluginname, detailed = false) ⇒ Object

returns information on plugin, must be full class name as listed in freenet interface Implements GetPluginInfo


157
158
159
160
161
# File 'lib/ruby-fcp/fcp_client.rb', line 157

def get_plugin_info(pluginname, detailed = false)
  id = @utils.id_generate
  send_packet @utils.packet_mangler({"PluginName" => pluginname, "Identifier" => id, "Detailed" => detailed },"GetPluginInfo")
  wait_for id, /PluginInfo/
end

#identifierObject

Simple attribute reader for you ConnectionIdentifier


11
12
13
# File 'lib/ruby-fcp/fcp_client.rb', line 11

def identifier
  @ConnectionIdentifier
end

#killswitchObject


275
276
277
# File 'lib/ruby-fcp/fcp_client.rb', line 275

def killswitch
  send_packet "Shutdown\nEndMessage\n"
end

#last_response(id) ⇒ Object

return last response from request defined by Identifier


16
17
18
# File 'lib/ruby-fcp/fcp_client.rb', line 16

def last_response(id)
  @responses[id].last
end

#list_persistent_requestsObject

List all persistent request just implements ListPersistentRequest


184
185
186
187
# File 'lib/ruby-fcp/fcp_client.rb', line 184

def list_persistent_requests
  send_packet "ListPersistentRequests\nEndMessage\n"
  wait_for :default ,/EndListPersistentRequests/
end

#listpeersObject

Straigt forward, ListPeers, sometimes it may choke up and give you end list peers before your peers, in that case check the id


164
165
166
167
168
# File 'lib/ruby-fcp/fcp_client.rb', line 164

def listpeers
  id = @utils.id_generate
  send_packet @utils.packet_mangler({"Identifier" => id, "WithMetaData" => true, "WithVolatile" => false},"ListPeers")
  wait_for id, /EndListPeers/
end

#modify_persistent_request(id, clienttoken, priorityclass) ⇒ Object


189
190
191
192
# File 'lib/ruby-fcp/fcp_client.rb', line 189

def modify_persistent_request(id,clienttoken,priorityclass)
  send_packet @utils.packet_mangler({"Identifier" => id,"ClientToken" => clienttoken, "PriorityClass" => priorityclass}, "ModifyPersistentRequest")
  wait_for id, /PersistentRequestModified/
end

#new_ssk_pairObject

Uses GenerateSSK


171
172
173
174
175
# File 'lib/ruby-fcp/fcp_client.rb', line 171

def new_ssk_pair
  id = @utils.id_generate
  send_packet @utils.packet_mangler({"Identifier" => id}, "GenerateSSK")
  wait_for id, /SSKKeypair/
end

#peerinfo(peer) ⇒ Object

returns information on a given peer not peers implements ListPeer


178
179
180
181
# File 'lib/ruby-fcp/fcp_client.rb', line 178

def peerinfo(peer)
  send_packet @utils.packet_mangler({"NodeIdentifier" => peer,"WithVolatile" => false,"WithMetadata" => true}, "ListPeer")
  wait_for :peer, /Peer/
end

#proberequest(type, hopstolive = 25, wait = true) ⇒ Object


213
214
215
216
217
218
219
220
221
# File 'lib/ruby-fcp/fcp_client.rb', line 213

def proberequest(type,hopstolive=25,wait = true)
  id = @utils.id_generate
  send_packet @utils.packet_mangler({"Identifier" => id,"Type" => type,"HopsToLive" => hopstolive}, "ProbeRequest")
  if wait
    wait_for id,/Probe/
  else
   id
  end
end

#put_complex_dir(uri, files, wait = true, opts = {}) ⇒ Object

simpler staight forward interface to ClientPutComplexDir, you provide with

Possible values of uri:

As well as a list of hashes for files you want to put

File hashlist format:

  • name: lib/hello or index.html, / will interperate as directory nesting

  • filename: in case of disk location of file on disk

  • uploadfrom: 'direct', 'disk' or 'redirect'

  • targeturi: in case of redirect, the location your are redirecting to

  • mimetype: not needed, but can be helpful

  • data: only nessecery in direct mode


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby-fcp/fcp_client.rb', line 86

def put_complex_dir(uri, files, wait = true,opts = {})
  dirs = []
  id = @utils.id_generate
  files.each{ |f| dirs << f[:filename].split(File::SEPARATOR)[0...-1].join(File::SEPARATOR) + File::SEPARATOR if f.has_key? :filename }
  (dirs.uniq).each { |dir| ddarun(dir,true,false) }
  options = {"URI" => uri, "Identifier" => id}.merge(opts)
  files.each_with_index do |file, index|
    options["Files.#{index}.Name"] = file[:name]
    options["Files.#{index}.UploadFrom"] = file[:uploadfrom]
    options["Files.#{index}.DataLength"] = file[:data].bytesize if file.has_key? :data
    options["Files.#{index}.Filename"] = file[:filename] if file[:uploadfrom].include? 'disk'
    options["Files.#{index}.TargetURI"] = file[:targeturi] if file[:uploadfrom].include? 'redirect'
    options["Files.#{index}.Metadata.ContentType"] = file[:mimetype] if file.has_key? :mimetype
  end
  message = @utils.packet_mangler(options,"ClientPutComplexDir")
  files.each { |f| message << f[:data] if f.has_key? :data}
  puts message
  send_packet message
  if wait
   wait_for(id,/PutFailed|PutSuccessful/)  
  else
   id
  end
end

#simple_dir_put(uri, dir, wait = true, opts = {}) ⇒ Object

Simple directory upload, upload one directory all at once automates the TestDDA for you just provide uri and directory Implements ClientPutDiskDir


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby-fcp/fcp_client.rb', line 61

def simple_dir_put(uri, dir, wait = true, opts={})
  id = @utils.id_generate
  ddarun(dir,true,false)
  options = {"Identifier" => id,"URI" => uri,"Filename" => dir, "Global" => 'true', "AllowUnreadableFiles" => 'true', "IncludeHiddenValues" => 'false'}.merge(opts)
  send_packet @utils.packet_mangler(options,"ClientPutDiskDir")
  if wait
    wait_for(id,/PutFailed|PutSuccessful/) 
  else
    id
  end
end

#simple_get(uri, directory, wait = true, opts = {}) ⇒ Object

just provide uri and download path/directory Implements ClientGet


132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ruby-fcp/fcp_client.rb', line 132

def simple_get(uri,directory,wait = true, opts={})
  id = @utils.id_generate
  saveloc = File.join directory, uri.split('/')[-1]
  ddarun(directory,false, true)
  options = {"URI" => uri, "Identifier" => id, "ReturnType" => 'disk', "Filename" => saveloc, "TempFilename" => saveloc+".tmp" , "Persistence" => 'forever', "Global" => false, "Verbosity" => 1111111}.merge(opts)
  send_packet @utils.packet_mangler(options,"ClientGet")
  if wait
   wait_for(id,/GetFailed|DataFound/) 
  else
   id
  end
end

#simple_put(uri, filename, wait = true, opts = {}) ⇒ Object

Simple interface to put a single file onto freenet from your disk uses ClientPut message

Possible values of uri:


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby-fcp/fcp_client.rb', line 25

def simple_put(uri, filename, wait = true, opts = {})
  id = @utils.id_generate
  options = { "URI" => uri, "Identifier" => id, "UploadFrom" => 'disk', "Filename" => filename, "FileHash" => @utils.filehash_maker(id, filename,identifier), "Verbosity" => "111111111" }.merge(opts)
  options["TargetFilename"] = filename.split(File::SEPARATOR)[-1] unless options.has_key? "TargetFilename"
  send_packet @utils.packet_mangler(options,"ClientPut")
  #@com.fcpackets.client_put uri, id, options
  if wait
    wait_for id, /PutFailed|PutSuccessful/
  else
    id
  end
end

#subscribe_usk(uri, wait = false, opts = {}) ⇒ Object

subscirbe to a usk, have to poll it yourself by using responses


195
196
197
198
199
# File 'lib/ruby-fcp/fcp_client.rb', line 195

def subscribe_usk(uri, wait = false ,opts ={})
  id = @utils.id_generate
  send_packet @utils.packet_mangler({"URI" => uri, "Identifier" => id} ,"SubscribeUSK")
  id
end

#unsubscribe_usk(id) ⇒ Object


208
209
210
211
# File 'lib/ruby-fcp/fcp_client.rb', line 208

def unsubscribe_usk(id)
  send_packet "UnsubscribeUSK\nIdentifier=#{id}\nEndMessage\n"
  id
end

#usks_latest(uri) ⇒ Object


201
202
203
204
205
206
# File 'lib/ruby-fcp/fcp_client.rb', line 201

def usks_latest(uri)
  id = subscribe_usk(uri)
  response = (wait_for id, /SubscribedUSKUpdate/).pop
  unsubscribe_usk(id)
  response["Edition"].to_i
end

#wait_for(id, pattern) ⇒ Object

Waits for a specific pattern in a message identified by ID


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/ruby-fcp/fcp_client.rb', line 224

def wait_for(id, pattern)
  response = [ ]
  loop do
    begin
    x = @responses[id].pop
    print @responses[:error].pop
    rescue
    sleep(2)
    end
    unless x.nil?
      if x[:head] =~ pattern
        response << x
        x.each { |key, value| puts "#{key}=#{value}" }
        break
      elsif x[:head] =~ /ProtocolError/
        response << x
        x.each { |key, value| puts "#{key}=#{value}" }
        break
      else
        response << x
        x.each { |key, value| puts "#{key}=#{value}" }
      end
    else
      sleep(1)
    end
  end
  response
end

#wait_for_ever(id) ⇒ Object

Just wait and wait given a id


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/ruby-fcp/fcp_client.rb', line 254

def wait_for_ever(id)
  loop do
    begin
      x = @responses[id].pop
    rescue
      print '.'
      sleep(2)
      print @responses[:error]
      print @responses[:default]
    end
    unless x.nil?
      x.each { |key, value| puts "#{key}=#{value}" }
    else
      print '.'
      sleep(1)
      puts @responses[:error]
      print @responses[:default]
    end
  end
end