require_relative '../../../../../gen/Design.ORiN3.Common/V1/orin3_common_pb' require_relative '../../../../../gen/Message.ORiN3.Provider/V1/orin3_file_pb' module O3P include Message::ORiN3::Provider::V1::AutoGenerated end module O3 include Design::ORiN3::Common::V1::AutoGenerated include Message::ORiN3::Provider::V1::AutoGenerated::File end module Grpc module Client module ORiN3 module Provider class ORiN3File < ORiN3BaseObject attr_reader :created_datetime include ORiN3ResourceOpener def initialize(channel, internal_id, created_datetime) @created_datetime = created_datetime super(channel, internal_id) end def read(buffer_size = 1048576) begin file = O3::FileService::Stub.new(nil, :this_channel_is_insecure, channel_override: @channel) request = O3::ReadFileRequest.new(common: O3P::CommonRequest.new, id: @internal_id, buffer_size: buffer_size) responses = file.read(request) responses.each do |response| if response.common.result_code != :SUCCEEDED raise MessageClientError.new(response.common.result_code, response.common.detail) end yield response.buffer if block_given? end rescue MessageClientError raise rescue StandardError => e raise MessageClientError.new(e) end end def write(buffer) begin file = O3::FileService::Stub.new(nil, :this_channel_is_insecure, channel_override: @channel) request = O3::WriteFileRequest.new(common: O3P::CommonRequest.new, id: @internal_id, buffer: buffer, total_length: buffer.length) response = file.write( Enumerator.new do |yielder| yielder << request end ) if response.common.result_code != :SUCCEEDED raise MessageClientError.new(response.common.result_code, response.common.detail) end rescue MessageClientError raise rescue StandardError => e raise MessageClientError.new(e) end end def seek(offset, origin = :BEGIN) begin file = O3::FileService::Stub.new(nil, :this_channel_is_insecure, channel_override: @channel) request = O3::SeekFileRequest.new(common: O3P::CommonRequest.new, id: @internal_id, offset: offset, origin: origin) response = file.seek(request) if (response.common.result_code != :SUCCEEDED) raise MessageClientError.new(response.common.result_code, response.common.detail) end return response.position rescue MessageClientError raise rescue StandardError => e raise MessageClientError.new(e) end end def can_read begin file = O3::FileService::Stub.new(nil, :this_channel_is_insecure, channel_override: @channel) request = O3::CanReadFileRequest.new(common: O3P::CommonRequest.new, id: @internal_id) response = file.can_read(request) if (response.common.result_code != :SUCCEEDED) raise MessageClientError.new(response.common.result_code, response.common.detail) end return response.can_read rescue MessageClientError raise rescue StandardError => e raise MessageClientError.new(e) end end def can_write begin file = O3::FileService::Stub.new(nil, :this_channel_is_insecure, channel_override: @channel) request = O3::CanWriteFileRequest.new(common: O3P::CommonRequest.new, id: @internal_id) response = file.can_write(request) if (response.common.result_code != :SUCCEEDED) raise MessageClientError.new(response.common.result_code, response.common.detail) end return response.can_write rescue MessageClientError raise rescue StandardError => e raise MessageClientError.new(e) end end def get_length begin file = O3::FileService::Stub.new(nil, :this_channel_is_insecure, channel_override: @channel) request = O3::GetFileLengthRequest.new(common: O3P::CommonRequest.new, id: @internal_id) response = file.get_length(request) if (response.common.result_code != :SUCCEEDED) raise MessageClientError.new(response.common.result_code, response.common.detail) end return response.length rescue MessageClientError raise rescue StandardError => e raise MessageClientError.new(e) end end def set_length(length) begin file = O3::FileService::Stub.new(nil, :this_channel_is_insecure, channel_override: @channel) request = O3::SetFileLengthRequest.new(common: O3P::CommonRequest.new, id: @internal_id, length: length) response = file.set_length(request) if (response.common.result_code != :SUCCEEDED) raise MessageClientError.new(response.common.result_code, response.common.detail) end rescue MessageClientError raise rescue StandardError => e raise MessageClientError.new(e) end end end end end end end