Filestoraged can now be compiled.

dev
Philippe Pittoli 2023-02-09 23:28:10 +01:00
parent 1c311178e6
commit 2b29d0db80
10 changed files with 72 additions and 66 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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?

View File

@ -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?

View File

@ -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

View File

@ -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/*"

View File

@ -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