From f5f75dda7bc2047b53b46f5a35b69ec1ea4cc86e Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sat, 11 Jan 2020 12:47:11 +0100 Subject: [PATCH] Info storage: WIP. --- src/server/context.cr | 29 +++++++++++++++++++++++++++++ src/server/handlers.cr | 26 ++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/server/context.cr b/src/server/context.cr index 50c6b28..a855023 100644 --- a/src/server/context.cr +++ b/src/server/context.cr @@ -1,4 +1,6 @@ +require "json" + # keep track of connected users and their requests # TODO: requests should be handled concurrently class User @@ -14,9 +16,36 @@ class User end end +class TransferInfo + include JSON::Serializable + + property owner : Int32 + property file_info : FileInfo + property chunks : Hash(Int32, Bool) + + def initialize(@owner, @file_info) + @chunks = Hash(Int32, Bool).new + @file_info.nb_chunks.times do |n| + @chunks[n] = false + end + end +end + class Context class_property service_name = "filestorage" class_property storage_directory = "./storage" + class_property file_info_directory = "./file-infos" + + class_property db : DODB::DataBase(TransferInfo) = self.init_db + + def init_db + @@db = DODB::DataBase(TransferInfo).new @@file_info_directory + + # init index, partitions and tags + Context.db.new_index "filedigest", &.file_info.digest + Context.db.new_partition "owner", &.owner + Context.db.new_tags "tags", &.tags + end # list of connected users (fd => uid) class_property connected_users = Hash(Int32, Int32).new diff --git a/src/server/handlers.cr b/src/server/handlers.cr index d707431..eeb6a06 100644 --- a/src/server/handlers.cr +++ b/src/server/handlers.cr @@ -13,11 +13,27 @@ def hdl_transfer(message : FileStorage::Transfer, # pp! message - file_info = user.uploads.select do |v| - v.file.digest == message.filedigest - end.first.file + file_info = nil + begin + file_info = user.uploads.select do |v| + v.file.digest == message.filedigest + end.first.file - pp! file_info + pp! file_info + rescue e : IndexError + puts "No recorded upload request for file #{message.filedigest}" + + rescue e + puts "Unexpected error: #{e}" + end + + # Get the transfer info from the db + # is the file info recorded? + by_digest = Context.db.get_index "filedigest" + transfer_info = by_digest.get? message.filedigest + + by_owner = Context.db.get_partition "owner" + pp! by_owner # TODO: verify the digest @@ -41,6 +57,8 @@ def hdl_transfer(message : FileStorage::Transfer, end # TODO: register the file with dodb, with its tags + + Context.db << FileStorage::Response.new mid, "Ok"