Compilable client.

This commit is contained in:
Karchnu 2020-10-21 03:27:51 +02:00
parent aec3445771
commit 7bb6bf909a
15 changed files with 42 additions and 52 deletions

View File

@ -7,26 +7,31 @@ class FileStorage::Client < IPC::Client
super service_name super service_name
end end
# TODO: parse_message should raise exception if response not anticipated
def parse_message(expected_messages, message)
em = Array(IPC::JSON.class).new
expected_messages.each do |e|
em << e
end
em << FileStorage::Errors::GenericError
em << FileStorage::Errors::Authorization
em << FileStorage::Errors::ChunkAlreadyUploaded
em << FileStorage::Errors::ChunkUploadDenied
em << FileStorage::Errors::FileExists
em << FileStorage::Errors::FileDoesNotExist
em << FileStorage::Errors::FileFullyUploaded
em.parse_ipc_json message
end
end
class FileStorage::Client < IPC::Client
def login def login
request = FileStorage::Request::Login.new auth_token request = FileStorage::Request::Login.new @auth_token
send @server_fd.not_nil!, request send_now @server_fd.not_nil!, request
parse_message [ FileStorage::Response::Login ], read
response = parse_message [
FileStorage::Response::Login,
FileStorage::Errors::GenericError
], read
if response.responds_to? :mid
if request.mid != response.mid
raise "mid from response != request"
end
else
raise "response doen't even have mid"
end
response
end end
# Helper function.
def get_file_info(file_path : String) def get_file_info(file_path : String)
file_info : FileStorage::FileInfo file_info : FileStorage::FileInfo
file = File.open(file_path) file = File.open(file_path)
@ -51,16 +56,13 @@ class FileStorage::Client < IPC::Client
counter, counter,
buffer[0 ... size] buffer[0 ... size]
send @server_fd.not_nil!, transfer_message send_now @server_fd.not_nil!, transfer_message
counter += 1 counter += 1
buffer = Bytes.new buffer_size buffer = Bytes.new buffer_size
# Check for the response # Check for the response
response = parse_message [ response = parse_message [ FileStorage::Response::PutChunk ], read
FileStorage::Response::PutChunk,
FileStorage::Errors::GenericError
], read
if response.responds_to? :mid if response.responds_to? :mid
if response.mid != transfer_message.mid if response.mid != transfer_message.mid
@ -77,14 +79,8 @@ class FileStorage::Client < IPC::Client
def download(filedigest = nil, name = nil, tags = nil) def download(filedigest = nil, name = nil, tags = nil)
request = FileStorage::Request::Download.new filedigest, name, tags request = FileStorage::Request::Download.new filedigest, name, tags
send @server_fd.not_nil!, request send_now @server_fd.not_nil!, request
parse_message [ FileStorage::Response::Download ], read
response = parse_message [
FileStorage::Response::Download,
FileStorage::Errors::GenericError
], read
response
end end
def upload(file : String) def upload(file : String)
@ -92,14 +88,9 @@ class FileStorage::Client < IPC::Client
File.open(file) do |f| File.open(file) do |f|
file_info = FileStorage::FileInfo.new f file_info = FileStorage::FileInfo.new f
request = FileStorage::Request::Upload.new file_info request = FileStorage::Request::Upload.new file_info
send @server_fd.not_nil!, request send_now @server_fd.not_nil!, request
end end
response = parse_message [ parse_message [ FileStorage::Response::Upload ], read
FileStorage::Response::Upload,
FileStorage::Errors::GenericError
], read
response
end end
end end

View File

@ -6,11 +6,10 @@ require "json"
require "base64" require "base64"
require "./authd_api.cr" require "./authd_api.cr"
require "./lib.cr"
require "../server/network.cr" require "../server/network.cr"
require "../server/storage.cr" require "../server/storage.cr"
require "../server/storage/*" require "../server/storage/*"
require "../common/*"
require "../common/requests/*"
# TODO # TODO
# For now, this example only upload files. # For now, this example only upload files.

View File

@ -6,8 +6,8 @@ require "colorize"
require "baguette-crystal-base" require "baguette-crystal-base"
require "../common/colors" require "../colors"
# require "../common/filestorage.cr" # require "../filestorage.cr"
# TODO: if the user is disconnected, we should ask him if it still want to process # TODO: if the user is disconnected, we should ask him if it still want to process
# for old requests. # for old requests.

View File

@ -1,4 +1,5 @@
require "ipc" require "ipc"
require "ipc/json"
require "json" require "json"
class IPC::Context class IPC::Context
@ -19,11 +20,10 @@ class FileStorage::Client < IPC::Client
end end
end end
require "../common/requests/client.cr" require "../requests/login.cr"
require "../common/requests/login.cr" require "../requests/transfer.cr"
require "../common/requests/transfer.cr" require "../requests/upload.cr"
require "../common/requests/upload.cr" require "../requests/errors.cr"
require "../common/requests/errors.cr" require "../requests/download.cr"
require "../common/requests/download.cr"
# require "../common/requests/*" # require "../requests/*"

View File

@ -1,4 +1,4 @@
require "../common/filestorage.cr" require "../filestorage.cr"
# This file test the following code # This file test the following code
# classes: # classes:

View File

@ -1,8 +1,8 @@
require "../common/filestorage.cr" require "../filestorage.cr"
require "ipc" require "ipc"
require "option_parser" require "option_parser"
require "../common/utils.cr" require "../utils.cr"
require "../server/context.cr" require "../server/context.cr"
filename = "./README.md" filename = "./README.md"

View File

@ -1,6 +1,6 @@
require "dodb" require "dodb"
require "json" require "json"
require "../common/filestorage.cr" require "../filestorage.cr"
# this is a copy of User and TransferInfo classes from src/server/context.cr # this is a copy of User and TransferInfo classes from src/server/context.cr
class User class User

View File

@ -1,6 +1,6 @@
require "json" require "json"
require "../common/filestorage.cr" require "../filestorage.cr"
unless ARGV.size > 0 unless ARGV.size > 0
raise "Usage: json_tests file" raise "Usage: json_tests file"