From 7bb6bf909a3e63e0844babcd2d0a2c602b585af5 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Wed, 21 Oct 2020 03:27:51 +0200 Subject: [PATCH] Compilable client. --- .../requests/client.cr => client/lib.cr} | 63 ++++++++----------- src/client/main.cr | 3 +- src/{common => }/colors.cr | 0 src/{common => }/requests/auth.cr | 0 src/{common => }/requests/download.cr | 0 src/{common => }/requests/errors.cr | 0 src/{common => }/requests/login.cr | 0 src/{common => }/requests/transfer.cr | 0 src/{common => }/requests/upload.cr | 0 src/server/main.cr | 4 +- src/server/network.cr | 14 ++--- src/tests/common-tests.cr | 2 +- src/tests/context-tests.cr | 4 +- src/tests/dodb-tests.cr | 2 +- src/tests/json_tests.cr | 2 +- 15 files changed, 42 insertions(+), 52 deletions(-) rename src/{common/requests/client.cr => client/lib.cr} (58%) rename src/{common => }/colors.cr (100%) rename src/{common => }/requests/auth.cr (100%) rename src/{common => }/requests/download.cr (100%) rename src/{common => }/requests/errors.cr (100%) rename src/{common => }/requests/login.cr (100%) rename src/{common => }/requests/transfer.cr (100%) rename src/{common => }/requests/upload.cr (100%) diff --git a/src/common/requests/client.cr b/src/client/lib.cr similarity index 58% rename from src/common/requests/client.cr rename to src/client/lib.cr index af3c82d..9c42606 100644 --- a/src/common/requests/client.cr +++ b/src/client/lib.cr @@ -7,26 +7,31 @@ class FileStorage::Client < IPC::Client super service_name end - 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" + # 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 - response +class FileStorage::Client < IPC::Client + def login + 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 diff --git a/src/client/main.cr b/src/client/main.cr index e577bd2..3579923 100644 --- a/src/client/main.cr +++ b/src/client/main.cr @@ -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. diff --git a/src/common/colors.cr b/src/colors.cr similarity index 100% rename from src/common/colors.cr rename to src/colors.cr diff --git a/src/common/requests/auth.cr b/src/requests/auth.cr similarity index 100% rename from src/common/requests/auth.cr rename to src/requests/auth.cr diff --git a/src/common/requests/download.cr b/src/requests/download.cr similarity index 100% rename from src/common/requests/download.cr rename to src/requests/download.cr diff --git a/src/common/requests/errors.cr b/src/requests/errors.cr similarity index 100% rename from src/common/requests/errors.cr rename to src/requests/errors.cr diff --git a/src/common/requests/login.cr b/src/requests/login.cr similarity index 100% rename from src/common/requests/login.cr rename to src/requests/login.cr diff --git a/src/common/requests/transfer.cr b/src/requests/transfer.cr similarity index 100% rename from src/common/requests/transfer.cr rename to src/requests/transfer.cr diff --git a/src/common/requests/upload.cr b/src/requests/upload.cr similarity index 100% rename from src/common/requests/upload.cr rename to src/requests/upload.cr diff --git a/src/server/main.cr b/src/server/main.cr index 8d38a16..705a99b 100644 --- a/src/server/main.cr +++ b/src/server/main.cr @@ -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. diff --git a/src/server/network.cr b/src/server/network.cr index 5c96764..195fbee 100644 --- a/src/server/network.cr +++ b/src/server/network.cr @@ -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/*" diff --git a/src/tests/common-tests.cr b/src/tests/common-tests.cr index 584e906..3c77b56 100644 --- a/src/tests/common-tests.cr +++ b/src/tests/common-tests.cr @@ -1,4 +1,4 @@ -require "../common/filestorage.cr" +require "../filestorage.cr" # This file test the following code # classes: diff --git a/src/tests/context-tests.cr b/src/tests/context-tests.cr index aa01070..66c5d08 100644 --- a/src/tests/context-tests.cr +++ b/src/tests/context-tests.cr @@ -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" diff --git a/src/tests/dodb-tests.cr b/src/tests/dodb-tests.cr index 6786120..7748b53 100644 --- a/src/tests/dodb-tests.cr +++ b/src/tests/dodb-tests.cr @@ -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 diff --git a/src/tests/json_tests.cr b/src/tests/json_tests.cr index e0a586f..57301a6 100644 --- a/src/tests/json_tests.cr +++ b/src/tests/json_tests.cr @@ -1,6 +1,6 @@ require "json" -require "../common/filestorage.cr" +require "../filestorage.cr" unless ARGV.size > 0 raise "Usage: json_tests file"