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
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
request = FileStorage::Request::Login.new auth_token
send @server_fd.not_nil!, request
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
request = FileStorage::Request::Login.new @auth_token
send_now @server_fd.not_nil!, request
parse_message [ FileStorage::Response::Login ], read
end
# Helper function.
def get_file_info(file_path : String)
file_info : FileStorage::FileInfo
file = File.open(file_path)
@ -51,16 +56,13 @@ class FileStorage::Client < IPC::Client
counter,
buffer[0 ... size]
send @server_fd.not_nil!, transfer_message
send_now @server_fd.not_nil!, transfer_message
counter += 1
buffer = Bytes.new buffer_size
# Check for the response
response = parse_message [
FileStorage::Response::PutChunk,
FileStorage::Errors::GenericError
], read
response = parse_message [ FileStorage::Response::PutChunk ], read
if response.responds_to? :mid
if response.mid != transfer_message.mid
@ -77,14 +79,8 @@ class FileStorage::Client < IPC::Client
def download(filedigest = nil, name = nil, tags = nil)
request = FileStorage::Request::Download.new filedigest, name, tags
send @server_fd.not_nil!, request
response = parse_message [
FileStorage::Response::Download,
FileStorage::Errors::GenericError
], read
response
send_now @server_fd.not_nil!, request
parse_message [ FileStorage::Response::Download ], read
end
def upload(file : String)
@ -92,14 +88,9 @@ class FileStorage::Client < IPC::Client
File.open(file) do |f|
file_info = FileStorage::FileInfo.new f
request = FileStorage::Request::Upload.new file_info
send @server_fd.not_nil!, request
send_now @server_fd.not_nil!, request
end
response = parse_message [
FileStorage::Response::Upload,
FileStorage::Errors::GenericError
], read
response
parse_message [ FileStorage::Response::Upload ], read
end
end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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