From 2b29d0db808784e31a4f2216adf5261ab68eaebf Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 9 Feb 2023 23:28:10 +0100 Subject: [PATCH] Filestoraged can now be compiled. --- shard.yml | 23 ++++++------- src/client/lib.cr | 17 +++++----- src/requests/auth.cr | 2 +- src/requests/download.cr | 2 +- src/requests/login.cr | 2 +- src/requests/transfer.cr | 4 +-- src/requests/upload.cr | 2 +- src/server/main.cr | 58 +++++++++++++++++++-------------- src/server/network.cr | 18 ++++++---- src/server/storage/file_info.cr | 10 ++---- 10 files changed, 72 insertions(+), 66 deletions(-) diff --git a/shard.yml b/shard.yml index f1b7993..1af2e0c 100644 --- a/shard.yml +++ b/shard.yml @@ -1,4 +1,4 @@ -name: filestorage +name: filestoraged version: 0.1.0 authors: @@ -14,20 +14,17 @@ dependencies: git: https://git.baguette.netlib.re/Baguette/dodb.cr authd: git: https://git.baguette.netlib.re/Baguette/authd.git - branch: master baguette-crystal-base: git: https://git.baguette.netlib.re/Baguette/baguette-crystal-base - branch: master - targets: - filestoraged: - main: src/server/main.cr - filestoragec: - main: src/client/main.cr - common-tests: - main: src/tests/common-tests.cr - json-tests: - main: src/tests/json_tests.cr + filestoraged: + main: src/server/main.cr + filestoragec: + main: src/client/main.cr + common-tests: + main: src/tests/common-tests.cr + json-tests: + main: src/tests/json_tests.cr -license: MIT +license: ISC diff --git a/src/client/lib.cr b/src/client/lib.cr index 797fb33..8e58d9a 100644 --- a/src/client/lib.cr +++ b/src/client/lib.cr @@ -1,10 +1,11 @@ require "ipc" -class FileStorage::Client < IPC::Client +class FileStorage::Client < IPC property auth_token : String def initialize(@auth_token, service_name = "filestorage") - super service_name + super + @server_fd = self.connect service_name end # TODO: parse_message should raise exception if response not anticipated @@ -25,10 +26,10 @@ class FileStorage::Client < IPC::Client end end -class FileStorage::Client < IPC::Client +class FileStorage::Client < IPC def login request = FileStorage::Request::Login.new @auth_token - send_now @server_fd.not_nil!, request + write @server_fd.not_nil!, request parse_message [ FileStorage::Response::Login ], read end @@ -57,7 +58,7 @@ class FileStorage::Client < IPC::Client counter, buffer[0 ... size] - send_now @server_fd.not_nil!, transfer_message + write @server_fd.not_nil!, transfer_message counter += 1 buffer = Bytes.new buffer_size @@ -80,7 +81,7 @@ class FileStorage::Client < IPC::Client def download(filedigest = nil, name = nil, tags = nil) request = FileStorage::Request::Download.new filedigest, name, tags - send_now @server_fd.not_nil!, request + write @server_fd.not_nil!, request parse_message [ FileStorage::Response::Download ], read end @@ -96,7 +97,7 @@ class FileStorage::Client < IPC::Client while counter < dl_response.file_info.nb_chunks Baguette::Log.debug "getting #{file_path}: chunk #{counter+1}/#{dl_response.file_info.nb_chunks}" get_chunk_message = FileStorage::Request::GetChunk.new digest, counter - send_now @server_fd.not_nil!, get_chunk_message + write @server_fd.not_nil!, get_chunk_message response = parse_message [ FileStorage::Response::GetChunk ], read case response @@ -134,7 +135,7 @@ class FileStorage::Client < IPC::Client File.open(file) do |f| file_info = FileStorage::FileInfo.new f request = FileStorage::Request::Upload.new file_info - send_now @server_fd.not_nil!, request + write @server_fd.not_nil!, request end parse_message [ FileStorage::Response::Upload ], read diff --git a/src/requests/auth.cr b/src/requests/auth.cr index 9e9248f..4ef64e1 100644 --- a/src/requests/auth.cr +++ b/src/requests/auth.cr @@ -24,7 +24,7 @@ # # If already in users_status: # # check if the requests are the same # # if not: add them to the user structure in users_status -# def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) +# def handle(filestoraged : FileStorage::Service, event : IPC::Event) # # token = filestoraged.auth.decode_token @token # userid = token.uid diff --git a/src/requests/download.cr b/src/requests/download.cr index 572314b..6a46b86 100644 --- a/src/requests/download.cr +++ b/src/requests/download.cr @@ -9,7 +9,7 @@ class FileStorage::Request @mid = UUID.random.to_s end - def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) + def handle(filestoraged : FileStorage::Service, event : IPC::Event) user = filestoraged.get_logged_user event raise NotLoggedException.new if user.nil? diff --git a/src/requests/login.cr b/src/requests/login.cr index 424a7b4..105f718 100644 --- a/src/requests/login.cr +++ b/src/requests/login.cr @@ -13,7 +13,7 @@ class FileStorage::Request @mid = UUID.random.to_s end - def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) + def handle(filestoraged : FileStorage::Service, event : IPC::Event) logged_users = filestoraged.logged_users user, _ = filestoraged.decode_token token diff --git a/src/requests/transfer.cr b/src/requests/transfer.cr index f647dd0..7ab3e56 100644 --- a/src/requests/transfer.cr +++ b/src/requests/transfer.cr @@ -17,7 +17,7 @@ class FileStorage::Request @mid = UUID.random.to_s end - def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) + def handle(filestoraged : FileStorage::Service, event : IPC::Event) user = filestoraged.get_logged_user event raise NotLoggedException.new if user.nil? @@ -51,7 +51,7 @@ class FileStorage::Request @mid = UUID.random.to_s end - def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) + def handle(filestoraged : FileStorage::Service, event : IPC::Event) user = filestoraged.get_logged_user event raise NotLoggedException.new if user.nil? diff --git a/src/requests/upload.cr b/src/requests/upload.cr index 2fd97e3..b981b01 100644 --- a/src/requests/upload.cr +++ b/src/requests/upload.cr @@ -7,7 +7,7 @@ class FileStorage::Request @mid = UUID.random.to_s end - def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) + def handle(filestoraged : FileStorage::Service, event : IPC::Event) user = filestoraged.get_logged_user event raise NotLoggedException.new if user.nil? diff --git a/src/server/main.cr b/src/server/main.cr index 385636d..87779dc 100644 --- a/src/server/main.cr +++ b/src/server/main.cr @@ -1,4 +1,5 @@ require "option_parser" +require "ipc" require "ipc/json" require "authd" @@ -30,7 +31,7 @@ require "./network.cr" require "dodb" class IPC::JSON - def handle(filestoraged : FileStorage::Service, event : IPC::Event::Events) + def handle(filestoraged : FileStorage::Service, event : IPC::Event) raise "unknown request" end end @@ -67,7 +68,7 @@ end -class FileStorage::Service < IPC::Server +class FileStorage::Service < IPC # List of connected users (fd => uid). property connected_users = Hash(Int32, Int32).new @@ -92,6 +93,7 @@ class FileStorage::Service < IPC::Server @auth_key : String def initialize(storage_directory, @auth_key, reindex : Bool) + super() # Data and metadata storage directory. @storage = FileStorage::Storage.new storage_directory, reindex @@ -101,10 +103,10 @@ class FileStorage::Service < IPC::Server @auth = AuthD::Client.new @auth.key = @auth_key - super "filestorage" + self.service_init "filestorage" end - def get_logged_user(event : IPC::Event::Events) + def get_logged_user(event : IPC::Event) fd = event.fd @logged_users[fd]? @@ -129,9 +131,13 @@ class FileStorage::Service < IPC::Server @storage.user_data_per_user.update_or_create user_data.uid.to_s, user_data end - def handle_request(event : IPC::Event::MessageReceived) + def handle_request(event : IPC::Event) + + array = event.message.not_nil! + slice = Slice.new array.to_unsafe, array.size + message = IPCMessage::TypedMessage.deserialize slice + request = FileStorage.requests.parse_ipc_json message.not_nil! - request = FileStorage.requests.parse_ipc_json event.message if request.nil? raise "unknown request type" end @@ -166,7 +172,7 @@ class FileStorage::Service < IPC::Server # in the responses. Allows identifying responses easily. response.id = request.id - send event.fd, response + schedule event.fd, response response_name = response.class.name.sub /^FileStorage::(Response|Errors)::/, "" if response.responds_to?(:reason) @@ -183,15 +189,15 @@ class FileStorage::Service < IPC::Server begin case event - when IPC::Event::Timer - Baguette::Log.debug "IPC::Event::Timer" if @print_timer + when LibIPC::EventType::Timer + Baguette::Log.debug "LibIPC::EventType::Timer" if @print_timer - when IPC::Event::Connection - Baguette::Log.debug "IPC::Event::Connection: #{event.fd}" + when LibIPC::EventType::Connection + Baguette::Log.debug "LibIPC::EventType::Connection: #{event.fd}" @all_connections << event.fd - when IPC::Event::Disconnection - Baguette::Log.debug "IPC::Event::Disconnection: #{event.fd}" + when LibIPC::EventType::Disconnection + Baguette::Log.debug "LibIPC::EventType::Disconnection: #{event.fd}" fd = event.fd @logged_users.delete fd @@ -201,26 +207,29 @@ class FileStorage::Service < IPC::Server fd != event.fd end - when IPC::Event::ExtraSocket - Baguette::Log.warning "IPC::Event::ExtraSocket: should not happen in this service" + when LibIPC::EventType::External + Baguette::Log.warning "LibIPC::EventType::ExtraSocket: should not happen in this service" - when IPC::Event::Switch - Baguette::Log.warning "IPC::Event::Switch: should not happen in this service" + when LibIPC::EventType::SwitchTx + Baguette::Log.warning "LibIPC::EventType::SwitchTx: should not happen in this service" - # IPC::Event::Message has to be the last entry + when LibIPC::EventType::SwitchRx + Baguette::Log.warning "LibIPC::EventType::SwitchRx: should not happen in this service" + + # LibIPC::EventType::Message has to be the last entry # because ExtraSocket and Switch inherit from Message class - when IPC::Event::MessageReceived - Baguette::Log.debug "IPC::Event::Message: #{event.fd}" + when LibIPC::EventType::MessageRx + Baguette::Log.debug "LibIPC::EventType::MessageRx: #{event.fd}" request_start = Time.utc handle_request event duration = Time.utc - request_start Baguette::Log.debug "request took: #{duration}" - when IPC::Event::MessageSent - Baguette::Log.debug "IPC::Event::MessageSent: #{event.fd}" + when LibIPC::EventType::MessageTx + Baguette::Log.debug "LibIPC::EventType::MessageTx: #{event.fd}" else - Baguette::Log.warning "unhandled IPC event: #{event.class}" + Baguette::Log.warning "unhandled LibIPC event: #{event.class}" end rescue exception @@ -315,8 +324,7 @@ class FileStorage::Service < IPC::Server authd_configuration.shared_key, configuration.db_reindex).tap do |service| - service.base_timer = configuration.ipc_timer - service.timer = configuration.ipc_timer + service.timer configuration.ipc_timer service.max_file_size = configuration.max_file_size service.print_timer = configuration.print_ipc_timer service.faulty = configuration.faulty diff --git a/src/server/network.cr b/src/server/network.cr index 195fbee..1b024b4 100644 --- a/src/server/network.cr +++ b/src/server/network.cr @@ -2,9 +2,10 @@ require "ipc" require "ipc/json" require "json" -class IPC::Context - def send(fd : Int32, request : IPC::JSON) - send fd, request.type.to_u8, request.to_json +class IPC + def schedule(fd : Int32, request : IPC::JSON) + m = IPCMessage::TypedMessage.new request.type.to_u8, request.to_json + schedule fd, m end end @@ -14,9 +15,14 @@ module FileStorage class_getter errors = [] of IPC::JSON.class end -class FileStorage::Client < IPC::Client +class FileStorage::Client < IPC def initialize - initialize "filestorage" + super + fd = self.connect "filestorage" + if fd.nil? + raise "couldn't connect to 'auth' IPC service" + end + @server_fd = fd end end @@ -25,5 +31,3 @@ require "../requests/transfer.cr" require "../requests/upload.cr" require "../requests/errors.cr" require "../requests/download.cr" - -# require "../requests/*" diff --git a/src/server/storage/file_info.cr b/src/server/storage/file_info.cr index bddec32..508ce93 100644 --- a/src/server/storage/file_info.cr +++ b/src/server/storage/file_info.cr @@ -24,13 +24,9 @@ module FileStorage # private function def self.data_digest(data : Bytes) - iodata = IO::Memory.new data, false - buffer = Bytes.new FileStorage.file_reading_buffer_size - - io = OpenSSL::DigestIO.new(iodata, "SHA256") - while io.read(buffer) > 0; end - - io.digest.hexstring + digest = OpenSSL::Digest.new "sha256" + digest.update data + digest.hexfinal end # private function