ruby-fcp 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ruby-fcp/communicator.rb +7 -3
- data/lib/ruby-fcp/fcp_client.rb +28 -48
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1155d73ac1fae96727ec974cd20478c07b37fe3
|
4
|
+
data.tar.gz: 6473e4a1b4fb1e1b2bdc16d05294cd1c8ba8869b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9283b3058d5ff46d336ba3ab00084a6e548328fe702b4c58defc0101c8b324097c3c9f85171632b457b3e306c45a70d3d727b2e0cc6421435ba1b9dbce079254
|
7
|
+
data.tar.gz: dc8cff53b6c29c12eedb8fd3edbed5bb188b2a92f8b9fb6403025fe96c69a2ae2d14619fbd865f154f879d9fe0aad147d94c6b0a93cf2fc69984bca5e1ffec9e
|
@@ -4,10 +4,12 @@ require 'ruby-fcp/utils'
|
|
4
4
|
|
5
5
|
class Communicator
|
6
6
|
attr_reader :ConnectionIdentifier
|
7
|
-
attr_accessor :responses, :heartbeat
|
7
|
+
attr_accessor :responses, :heartbeat, :utils
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
# clients name must be unique
|
10
|
+
# This performs NodeHello operations upon initialization.
|
11
|
+
# Communicator handles packet sending and recieving and sorting
|
12
|
+
def initialize(client, host='127.0.0.1', port=9481, version = 2.0)
|
11
13
|
@version = version
|
12
14
|
@ConnectionIdentifier = ""
|
13
15
|
@host = host
|
@@ -18,6 +20,7 @@ class Communicator
|
|
18
20
|
@state = false
|
19
21
|
@queue = Queue.new
|
20
22
|
@heartbeat = 300
|
23
|
+
@utils = Utils.new
|
21
24
|
connect
|
22
25
|
end
|
23
26
|
|
@@ -125,6 +128,7 @@ class Communicator
|
|
125
128
|
end
|
126
129
|
end
|
127
130
|
|
131
|
+
# Send disconnect message and close the socket
|
128
132
|
def close
|
129
133
|
@tex.synchronize{@sock.write "Disconnect EndMessage\n"}
|
130
134
|
@sock.close
|
data/lib/ruby-fcp/fcp_client.rb
CHANGED
@@ -5,31 +5,16 @@ require 'digest'
|
|
5
5
|
require 'base64'
|
6
6
|
require 'ruby-fcp/communicator'
|
7
7
|
|
8
|
-
class FCPClient
|
9
|
-
|
10
|
-
attr_accessor :utils, :com
|
11
|
-
|
12
|
-
# clients name must be unique
|
13
|
-
# This performs NodeHello operations upon initialization.
|
14
|
-
# Communicator handles packet sending and recieving and sorting
|
15
|
-
def initialize(client, host = "127.0.0.1", port = 9481)
|
16
|
-
@utils = Utils.new
|
17
|
-
@com = Communicator.new(client,host,port)
|
18
|
-
end
|
8
|
+
class FCPClient < Communicator
|
19
9
|
|
20
10
|
# Simple attribute reader for you ConnectionIdentifier
|
21
11
|
def identifier
|
22
|
-
@
|
23
|
-
end
|
24
|
-
|
25
|
-
# Simple attribute reader to display all responses recieved
|
26
|
-
def responses
|
27
|
-
@com.responses
|
12
|
+
@ConnectionIdentifier
|
28
13
|
end
|
29
14
|
|
30
15
|
# return last response from request defined by Identifier
|
31
16
|
def last_response(id)
|
32
|
-
@
|
17
|
+
@responses[id].last
|
33
18
|
end
|
34
19
|
|
35
20
|
# Simple interface to put a single file onto freenet from your disk uses ClientPut message
|
@@ -41,7 +26,7 @@ class FCPClient
|
|
41
26
|
id = @utils.id_generate
|
42
27
|
options = { "URI" => uri, "Identifier" => id, "UploadFrom" => 'disk', "Filename" => filename, "FileHash" => @utils.filehash_maker(id, filename,identifier), "Verbosity" => "111111111" }.merge(opts)
|
43
28
|
options["TargetFilename"] = filename.split(File::SEPARATOR)[-1] if uri =~ /CHK@/
|
44
|
-
|
29
|
+
send_packet @utils.packet_mangler(options,"ClientPut")
|
45
30
|
#@com.fcpackets.client_put uri, id, options
|
46
31
|
if wait
|
47
32
|
wait_for id, /PutFailed|PutSuccessful/
|
@@ -59,7 +44,7 @@ class FCPClient
|
|
59
44
|
def direct_put(uri,data, wait = true, opts = {})
|
60
45
|
id = @utils.id_generate
|
61
46
|
options = {"Identifier" => id, "URI" => uri ,"UploadFrom" => "direct", "DataLength" => data.bytesize }.merge(opts)
|
62
|
-
|
47
|
+
send_packet @utils.packet_mangler(options,"ClientPut").sub! "EndMessage\n", "Data\n#{data}"
|
63
48
|
if wait
|
64
49
|
wait_for id,/PutFailed|PutSuccessful/
|
65
50
|
else
|
@@ -77,7 +62,7 @@ class FCPClient
|
|
77
62
|
id = @utils.id_generate
|
78
63
|
ddarun(dir,true,false)
|
79
64
|
options = {"Identifier" => id,"URI" => uri,"Filename" => dir, "Global" => 'true', "AllowUnreadableFiles" => 'true', "IncludeHiddenValues" => 'false'}.merge(opts)
|
80
|
-
|
65
|
+
send_packet @utils.packet_mangler(options,"ClientPutDiskDir")
|
81
66
|
if wait
|
82
67
|
wait_for(id,/PutFailed|PutSuccessful/)
|
83
68
|
else
|
@@ -115,7 +100,7 @@ class FCPClient
|
|
115
100
|
message = @utils.packet_mangler(options,"ClientPutComplexDir")
|
116
101
|
files.each { |f| message << f[:data] if f.has_key? :data}
|
117
102
|
puts message
|
118
|
-
|
103
|
+
send_packet message
|
119
104
|
if wait
|
120
105
|
wait_for(id,/PutFailed|PutSuccessful/)
|
121
106
|
else
|
@@ -126,7 +111,7 @@ class FCPClient
|
|
126
111
|
# performs TestDDARequest and TestDDAResponse automagically
|
127
112
|
# read and write are true or false values
|
128
113
|
def ddarun(directory,read, write)
|
129
|
-
|
114
|
+
send_packet @utils.packet_mangler({"Directory" => directory,"WantReadDirectory" => read, "WantWriteDirectory" => write} ,"TestDDARequest")
|
130
115
|
res = wait_for(:dda, /TestDDAReply/).pop
|
131
116
|
content = nil
|
132
117
|
if write
|
@@ -136,7 +121,7 @@ class FCPClient
|
|
136
121
|
elsif read
|
137
122
|
content = File.open(res["ReadFilename"],'r').read
|
138
123
|
end
|
139
|
-
|
124
|
+
send_packet @utils.packet_mangler({"Directory" => directory,"ReadContent" => content}, "TestDDAResponse")
|
140
125
|
response = wait_for(:dda ,/TestDDAComplete/).pop
|
141
126
|
File.delete(res["WriteFilename"]) if write
|
142
127
|
response
|
@@ -149,7 +134,7 @@ class FCPClient
|
|
149
134
|
saveloc = File.join directory, uri.split('/')[-1]
|
150
135
|
ddarun(directory,false, true)
|
151
136
|
options = {"URI" => uri, "Identifier" => id, "ReturnType" => 'disk', "Filename" => saveloc, "TempFilename" => saveloc+".tmp" , "Persistence" => 'forever', "Global" => false, "Verbosity" => 1111111}.merge(opts)
|
152
|
-
|
137
|
+
send_packet @utils.packet_mangler(options,"ClientGet")
|
153
138
|
if wait
|
154
139
|
wait_for(id,/GetFailed|DataFound/)
|
155
140
|
else
|
@@ -160,7 +145,7 @@ class FCPClient
|
|
160
145
|
def direct_get(uri ,wait = true, opts={})
|
161
146
|
id = @utils.id_generate
|
162
147
|
options = {"URI" => uri, "Identifier" => id, "ReturnType" => 'direct', "Global" => false}.merge(opts)
|
163
|
-
|
148
|
+
send_packet @utils.packet_mangler(options,"ClientGet")
|
164
149
|
if wait
|
165
150
|
wait_for(id,/AllData|GetFailed/)
|
166
151
|
else
|
@@ -171,45 +156,45 @@ class FCPClient
|
|
171
156
|
# Implements GetPluginInfo
|
172
157
|
def get_plugin_info(pluginname, detailed = false)
|
173
158
|
id = @utils.id_generate
|
174
|
-
|
159
|
+
send_packet @utils.packet_mangler({"PluginName" => pluginname, "Identifier" => id, "Detailed" => detailed },"GetPluginInfo")
|
175
160
|
wait_for id, /PluginInfo/
|
176
161
|
end
|
177
162
|
|
178
163
|
# Straigt forward, ListPeers, sometimes it may choke up and give you end list peers before your peers, in that case check the id
|
179
164
|
def listpeers
|
180
165
|
id = @utils.id_generate
|
181
|
-
|
166
|
+
send_packet @utils.packet_mangler({"Identifier" => id, "WithMetaData" => true, "WithVolatile" => false},"ListPeers")
|
182
167
|
wait_for id, /EndListPeers/
|
183
168
|
end
|
184
169
|
|
185
170
|
#Uses GenerateSSK
|
186
171
|
def new_ssk_pair
|
187
172
|
id = @utils.id_generate
|
188
|
-
|
173
|
+
send_packet @utils.packet_mangler({"Identifier" => id}, "GenerateSSK")
|
189
174
|
wait_for id, /SSKKeypair/
|
190
175
|
end
|
191
176
|
|
192
177
|
# returns information on a given peer not peers implements ListPeer
|
193
178
|
def peerinfo(peer)
|
194
|
-
|
179
|
+
send_packet @utils.packet_mangler({"NodeIdentifier" => peer,"WithVolatile" => false,"WithMetadata" => true}, "ListPeer")
|
195
180
|
wait_for :peer, /Peer/
|
196
181
|
end
|
197
182
|
|
198
183
|
# List all persistent request just implements ListPersistentRequest
|
199
184
|
def list_persistent_requests
|
200
|
-
|
185
|
+
send_packet "ListPersistentRequests\nEndMessage\n"
|
201
186
|
wait_for :default ,/EndListPersistentRequests/
|
202
187
|
end
|
203
188
|
|
204
189
|
def modify_persistent_request(id,clienttoken,priorityclass)
|
205
|
-
|
190
|
+
send_packet @utils.packet_mangler({"Identifier" => id,"ClientToken" => clienttoken, "PriorityClass" => priorityclass}, "ModifyPersistentRequest")
|
206
191
|
wait_for id, /PersistentRequestModified/
|
207
192
|
end
|
208
193
|
|
209
194
|
#subscirbe to a usk, have to poll it yourself by using responses[id]
|
210
195
|
def subscribe_usk(uri, wait = false ,opts ={})
|
211
196
|
id = @utils.id_generate
|
212
|
-
|
197
|
+
send_packet @utils.packet_mangler({"URI" => uri, "Identifier" => id} ,"SubscribeUSK")
|
213
198
|
id
|
214
199
|
end
|
215
200
|
|
@@ -221,13 +206,13 @@ class FCPClient
|
|
221
206
|
end
|
222
207
|
|
223
208
|
def unsubscribe_usk(id)
|
224
|
-
|
209
|
+
send_packet "UnsubscribeUSK\nIdentifier=#{id}\nEndMessage\n"
|
225
210
|
id
|
226
211
|
end
|
227
212
|
|
228
213
|
def proberequest(type,hopstolive=25,wait = true)
|
229
214
|
id = @utils.id_generate
|
230
|
-
|
215
|
+
send_packet @utils.packet_mangler({"Identifier" => id,"Type" => type,"HopsToLive" => hopstolive}, "ProbeRequest")
|
231
216
|
if wait
|
232
217
|
wait_for id,/Probe/
|
233
218
|
else
|
@@ -240,8 +225,8 @@ class FCPClient
|
|
240
225
|
response = [ ]
|
241
226
|
loop do
|
242
227
|
begin
|
243
|
-
x = @
|
244
|
-
print @
|
228
|
+
x = @responses[id].pop
|
229
|
+
print @responses[:error].pop
|
245
230
|
rescue
|
246
231
|
sleep(2)
|
247
232
|
end
|
@@ -269,30 +254,25 @@ class FCPClient
|
|
269
254
|
def wait_for_ever(id)
|
270
255
|
loop do
|
271
256
|
begin
|
272
|
-
x = @
|
257
|
+
x = @responses[id].pop
|
273
258
|
rescue
|
274
259
|
print '.'
|
275
260
|
sleep(2)
|
276
|
-
print @
|
277
|
-
print @
|
261
|
+
print @responses[:error]
|
262
|
+
print @responses[:default]
|
278
263
|
end
|
279
264
|
unless x.nil?
|
280
265
|
x.each { |key, value| puts "#{key}=#{value}" }
|
281
266
|
else
|
282
267
|
print '.'
|
283
268
|
sleep(1)
|
284
|
-
puts @
|
285
|
-
print @
|
269
|
+
puts @responses[:error]
|
270
|
+
print @responses[:default]
|
286
271
|
end
|
287
272
|
end
|
288
273
|
end
|
289
274
|
|
290
|
-
# Send disconnect message and close the socket
|
291
|
-
def close
|
292
|
-
@com.close
|
293
|
-
end
|
294
|
-
|
295
275
|
def killswitch
|
296
|
-
|
276
|
+
send_packet "Shutdown\nEndMessage\n"
|
297
277
|
end
|
298
278
|
end
|