transfers ok

dev
Philippe PITTOLI 2020-01-04 10:45:39 +01:00
parent f87fd35a64
commit a2393848af
5 changed files with 41 additions and 56 deletions

View File

@ -20,7 +20,7 @@ targets:
main: src/tests/common-tests.cr
json-tests:
main: src/tests/json_tests.cr
server:
filestoraged:
main: src/server/main.cr
client:
main: src/client/main.cr

View File

@ -17,7 +17,7 @@ service_name = "filestorage"
files_and_directories_to_transfer = Array(String).new
# This is the requests we will send to the server
requests = Array(FM::Request).new
upload_requests = Array(FM::UploadRequest).new
OptionParser.parse do |parser|
@ -68,10 +68,10 @@ files_and_directories_to_transfer.each do |f|
end
files_info.values.each do |file_info|
requests << FM::UploadRequest.new file_info
upload_requests << FM::UploadRequest.new file_info
end
pp! requests
# pp! upload_requests
#
# Connection to the service
@ -84,7 +84,8 @@ client = IPC::Client.new service_name
#
token = FileStorage::Token.new 1002, "karchnu"
authentication_message = FM::Authentication.new token, requests
authentication_message = FM::Authentication.new token, upload_requests
pp! authentication_message
client.send FileStorage::MessageType::Authentication.to_u8, authentication_message.to_json
#
@ -99,6 +100,7 @@ response = FM::Response.from_json(String.new m.payload)
if response.mid == authentication_message.mid
puts "This is a response for the authentication message"
pp! response
else
raise "Message IDs from authentication message and its response differ"
end
@ -122,6 +124,23 @@ def file_transfer(client : IPC::Client, file : File, file_info : FileStorage::Fi
counter += 1
buffer = Bytes.new buffer_size
# Check for the response
m = client.read
mtype = FileStorage::MessageType.new m.type.to_i32
if mtype != FileStorage::MessageType::Response
pp! m
raise "Message received was not expected: #{mtype}"
end
response = FM::Response.from_json(String.new m.payload)
if response.mid != transfer_message.mid
raise "Message received has a wrong mid: #{response.mid} != #{transfer_message.mid}"
else
pp! response
end
end
end

View File

@ -127,10 +127,11 @@ module FileStorage
# autogenerated
mid: String,
token: Token,
requests: Array(Request)
uploads: Array(UploadRequest),
downloads: Array(DownloadRequest)
})
def initialize(@token, @requests = Array(Request).new)
def initialize(@token, @uploads = Array(UploadRequest).new, @downloads = Array(DownloadRequest).new)
@mid = UUID.random.to_s
end
end

View File

@ -14,8 +14,8 @@ def hdl_transfer(message : FileStorage::Message::Transfer,
pp! transfer_message
puts "chunk: #{transfer_message.chunk}"
puts "data: #{Base64.decode transfer_message.data}"
# puts "chunk: #{transfer_message.chunk}"
# puts "data: #{Base64.decode transfer_message.data}"
FileStorage::Message::Response.new message.mid, "Ok"
end
@ -98,7 +98,7 @@ def hdl_authentication(event : IPC::Event::Message)
# AuthenticationMessage includes requests.
new_user =
User.new authentication_message.token,
authentication_message.requests
[ authentication_message.uploads, authentication_message.downloads ].flatten
Context.connected_users[event.connection.fd] = userid
@ -121,7 +121,7 @@ def hdl_authentication(event : IPC::Event::Message)
# TODO: quotas
# Quotas are not defined yet.
responses = hdl_requests authentication_message.requests,
responses = hdl_requests [ authentication_message.uploads, authentication_message.downloads ].flatten,
Context.users_status[userid],
event
@ -129,4 +129,6 @@ def hdl_authentication(event : IPC::Event::Message)
# The response is "Ok" when the message is well received and authorized.
response = FileStorage::Message::Responses.new authentication_message.mid, "Ok", responses
event.connection.send FileStorage::MessageType::Responses.to_u8, response.to_json
pp! FileStorage::MessageType::Responses.to_u8
pp! response
end

View File

@ -37,50 +37,13 @@ Context.service.not_nil!.loop do |event|
# First, the user has to be authenticated unless we are receiving its first message
userid = Context.connected_users[event.connection.fd]?
if ! userid
case mtype
when .authentication?
else
mid = "message id not found"
m = String.new event.message.payload
case mtype
when .authentication?
when .upload_request?
puts "Upload request"
request = FileStorage::Message::UploadRequest.from_json(m)
mid = request.mid
when .download_request?
puts "Download request"
request = FileStorage::Message::DownloadRequest.from_json(m)
mid = request.mid
when .response?
puts "Response message"
request = FileStorage::Message::Response.from_json(m)
mid = request.mid
raise "not implemented yet"
when .responses?
puts "Responses message"
request = FileStorage::Message::Responses.from_json(m)
mid = request.mid
raise "not implemented yet"
when .error?
puts "Error message"
request = FileStorage::Message::Error.from_json(m)
mid = request.mid
raise "not implemented yet"
when .transfer?
request = FileStorage::Message::Transfer.from_json(m)
mid = request.mid
else
raise "Event type not supported, message from a non connected user."
end
response = FileStorage::Message::Response.new mid, "Not OK", "Action on non connected user"
event.connection.send FileStorage::MessageType::Response.to_u8, response.to_json
next
end
# if the user is not yet connected but does not try to perform authentication
if ! userid && mtype != FileStorage::MessageType::Authentication
# TODO: replace this with an Error message?
mid = "no message id"
response = FileStorage::Message::Response.new mid, "Not OK", "Action on non connected user"
event.connection.send FileStorage::MessageType::Response.to_u8, response.to_json
end
case mtype
@ -133,10 +96,10 @@ Context.service.not_nil!.loop do |event|
response = hdl_transfer transfer, Context.users_status[userid], event
event.connection.send FileStorage::MessageType::Response.to_u8, response.to_json
hdl_transfer transfer, user, event
end
else
raise "Event type not supported."
end
rescue e
puts "A problem occured : #{e.message}"
end