13 #include <string_view> 16 #include <zypp-proto/media/provider.pb.h> 20 const std::string &ProviderConfiguration::staticTypeName()
22 return rpc::messageTypeName<zypp::proto::Configuration>();
25 const std::string &ProviderConfiguration::typeName()
const 27 return staticTypeName();
34 if ( !implVar.ParseFromString( data ) )
37 insert( implVar.values ().begin (), implVar.values ().end() );
41 void ProviderConfiguration::serializeInto(std::string &
str)
const 44 implVar.mutable_values()->insert( begin(), end() );
45 implVar.SerializeToString( &
str );
48 std::string ProviderConfiguration::serialize( )
const 60 : _data( new
zypp::proto::Capabilities() )
63 ZYPP_IMPL_RPCBASE(
WorkerCaps, zypp::proto::Capabilities, _data)
70 return _data->protocol_version ();
85 return _data->worker_name();
90 _data->set_protocol_version(v);
95 _data->set_worker_type( static_cast<uint32_t>(t) );
100 _data->set_cfg_flags( static_cast<uint32_t>(f) );
105 _data->set_worker_name ( std::move(name) );
111 switch ( field.field_val_case () ) {
112 case zypp::proto::DataField::FieldValCase::kBoolVal:
113 v = field.bool_val();
115 case zypp::proto::DataField::FieldValCase::kDoubleVal:
116 v = field.double_val();
118 case zypp::proto::DataField::FieldValCase::kIntVal:
121 case zypp::proto::DataField::FieldValCase::kLongVal:
122 v = field.long_val();
124 case zypp::proto::DataField::FieldValCase::kStrVal:
127 case zypp::proto::DataField::FieldValCase::FIELD_VAL_NOT_SET:
128 ZYPP_THROW( std::logic_error(
"Unexpected DataField type"));
137 field.set_str_val( val.
asString () );
138 else if ( val.
isInt() )
139 field.set_int_val( val.
asInt() );
141 field.set_long_val( val.
asInt64() );
143 field.set_double_val( val.
asDouble() );
145 field.set_bool_val( val.
asBool() );
147 ZYPP_THROW( std::logic_error(
"Unexpected FieldVal type"));
152 const auto c = msg.
code();
161 return zyppng::expected<void>::error(
ZYPP_EXCPT_PTR ( InvalidMessageReceivedException(
"Invalid code in ProvideMessage")) );
164 #define DEF_REQ_FIELD( fname ) bool has_##fname = false 166 #define REQ_FIELD_CHECK( msgtype, fname, ftype ) \ 167 if ( name == #fname ) { \ 168 if ( !std::holds_alternative<ftype>(val.asVariant()) ) { \ 169 error = ZYPP_EXCPT_PTR( InvalidMessageReceivedException( zypp::str::Str() << "Parse error " << #msgtype << ", Field " << #fname << " has invalid type" ) ); \ 172 has_##fname = true; \ 175 #define OR_REQ_FIELD_CHECK( msgtype, fname, ftype ) else REQ_FIELD_CHECK( msgtype, fname, ftype ) 177 #define OPT_FIELD_CHECK( msgtype, fname, ftype ) \ 178 if ( name == #fname ) { \ 179 if ( !std::holds_alternative<ftype>(val.asVariant() ) ) { \ 180 error = ZYPP_EXCPT_PTR( InvalidMessageReceivedException( zypp::str::Str() << "Parse error " << #msgtype << ", Field " << #fname << " has invalid type" ) ); \ 185 #define OR_OPT_FIELD_CHECK( msgtype, fname, ftype ) else OPT_FIELD_CHECK( msgtype, fname, ftype ) 187 #define FAIL_IF_NOT_SEEN_REQ_FIELD( msgtype, fname ) \ 188 if ( !has_##fname ) \ 189 return expected<void>::error( ZYPP_EXCPT_PTR( InvalidMessageReceivedException( zypp::str::Str() << #msgtype <<" message does not contain required " << #fname << " field" ) ) ) 191 #define FAIL_IF_ERROR( ) \ 192 if ( error ) return expected<void>::error( error ) 194 const auto &validateErrorMsg = [](
const auto &msg ){
195 std::exception_ptr error;
205 return expected<void>::success();
211 std::exception_ptr error;
224 std::exception_ptr error;
238 std::exception_ptr error;
251 std::exception_ptr error;
272 std::exception_ptr error;
283 std::exception_ptr error;
309 const auto &e = validateErrorMsg(msg);
315 std::exception_ptr error;
335 std::exception_ptr error;
359 if ( ! ( ( has_verify_data == has_verify_type ) && ( has_verify_type == has_media_nr ) ) )
360 return expected<void>::error(
ZYPP_EXCPT_PTR ( InvalidMessageReceivedException(
"Error in Attach message, one of the following fields is not set or invalid: ( verify_type, verify_data, media_nr ). Either none or all need to be set. ")) );
365 std::exception_ptr error;
376 std::exception_ptr error;
390 std::exception_ptr error;
410 const auto &e = validateErrorMsg(msg);
417 return expected<void>::success();
424 ZYPP_IMPL_RPCBASE(
ProvideMessage, zypp::proto::ProvideMessage, _impl )
429 const auto &res = RpcMessageStream::parseMessageInto<zypp::proto::ProvideMessage>( message, *msg.
_impl );
433 ERR <<
"Invalid message for ID: " << msg.
_impl->request_id() << std::endl;;
434 return zyppng::expected<zyppng::ProvideMessage>::error( valid.error() );
437 return zyppng::expected<zyppng::ProvideMessage>::success( std::move(msg) );
439 ERR <<
"Failed to parse message" << std::endl;;
440 return zyppng::expected<zyppng::ProvideMessage>::error( res.error() );
446 *msg.
_impl = std::move(message);
449 ERR <<
"Invalid message for ID: " << msg.
_impl->request_id() << std::endl;;
450 return zyppng::expected<zyppng::ProvideMessage>::error( valid.error() );
453 return zyppng::expected<zyppng::ProvideMessage>::success( std::move(msg) );
464 if ( stagingFilename )
487 if ( localMountPoint )
510 for (
auto i : extraValues ) {
540 for(
const auto &val : newUrls )
550 ZYPP_THROW(std::out_of_range(
"code must be between 400 and 599"));
585 ProvideMessage ProvideMessage::createAttach(
const uint32_t reqId,
const zypp::Url &url,
const std::string attachId,
const std::string &label,
const std::optional<std::string> &verifyType,
const std::optional<std::string> &verifyData,
const std::optional<int32_t> &mediaNr )
594 if ( verifyType.has_value() && verifyData.has_value() && mediaNr.has_value() ) {
599 if ( !( ( verifyType.has_value() == verifyData.has_value() ) && ( verifyData.has_value() == mediaNr.has_value() ) ) )
600 WAR <<
"Attach message requires verifyType, verifyData and mediaNr either set together or not set at all." << std::endl;
622 if ( lastTriedUser.size() )
624 if ( lastAuthTimestamp )
637 for (
const auto &
device : devices )
647 return _impl->request_id();
652 _impl->set_request_id(
id );
657 return _impl->message_code();
662 _impl->set_message_code ( newCode );
667 std::vector<ProvideMessage::FieldVal>
values;
668 const auto &fields =
_impl->fields();
669 for (
const auto &field : fields ) {
670 if ( field.key() !=
str )
684 const auto &fields =
_impl->fields();
685 auto i = std::find_if( fields.rbegin(), fields.rend(), [&
str](
const auto &val ){
return val.key() ==
str; } );
686 if ( i == fields.rend() )
695 auto &fields =
_impl->fields();
696 for (
const auto &val : fields ) {
704 return value( std::string_view(
str), defaultVal );
714 auto &fields = *
_impl->mutable_fields();
715 auto i = std::find_if( fields.rbegin(), fields.rend(), [&name](
const auto &val ){
return val.key() == name; } );
716 if ( i == fields.rend() ) {
717 auto &newVal = *
_impl->add_fields();
718 newVal.set_key( name.data() );
731 auto &newVal = *
_impl->add_fields();
732 newVal.set_key( name.data() );
738 auto &fields =
_impl->fields();
739 for (
const auto &val : fields ) {
753 proto::Capabilities *rwcowClone<proto::Capabilities>(
const proto::Capabilities *rhs)
754 {
return new proto::Capabilities( *rhs ); }
757 zypp::proto::ProvideMessage* rwcowClone<zypp::proto::ProvideMessage>(
const zypp::proto::ProvideMessage * rhs )
758 {
return new zypp::proto::ProvideMessage(*rhs); }
static ProvideMessage createErrorResponse(const uint32_t reqId, const uint code, const std::string &reason, bool transient=false)
constexpr std::string_view Url("url")
constexpr std::string_view LocalFilename("local_filename")
constexpr std::string_view AttachId("attach_id")
static void fieldValToProto(const ProvideMessage::FieldVal &val, zypp::proto::DataField &field)
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
zypp::RWCOW_pointer< zypp::proto::Capabilities > _data
zypp::RWCOW_pointer< zypp::proto::ProvideMessage > _impl
void forEachVal(const std::function< bool(const std::string &name, const FieldVal &val)> &cb) const
constexpr std::string_view VerifyData("verify_data")
static ProvideMessage createMetalinkRedir(const uint32_t reqId, const std::vector< zypp::Url > &newUrls)
static ProvideMessage createProvide(const uint32_t reqId, const zypp::Url &url, const std::optional< std::string > &filename={}, const std::optional< std::string > &deltaFile={}, const std::optional< int64_t > &expFilesize={}, bool checkExistOnly=false)
constexpr std::string_view Filename("filename")
static ProvideMessage::FieldVal fieldValFromProto(const zypp::proto::DataField &field)
static ProvideMessage createAuthInfo(const uint32_t reqId, const std::string &user, const std::string &pw, int64_t timestamp, const std::map< std::string, std::string > &extraValues={})
HeaderValueMap headers() const
void set_cfg_flags(Flags f)
void set_protocol_version(uint32_t v)
static ProvideMessage createAttach(const uint32_t reqId, const zypp::Url &url, const std::string attachId, const std::string &label, const std::optional< std::string > &verifyType={}, const std::optional< std::string > &verifyData={}, const std::optional< int32_t > &mediaNr={})
#define DEF_REQ_FIELD(fname)
static ProvideMessage createMediaChanged(const uint32_t reqId)
String related utilities and Regular expression matching.
constexpr std::string_view MediaNr("media_nr")
#define FAIL_IF_NOT_SEEN_REQ_FIELD(msgtype, fname)
constexpr std::string_view VerifyType("verify_type")
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
void addValue(const std::string &name, const FieldVal &value)
static ProvideMessage createProvideStarted(const uint32_t reqId, const zypp::Url &url, const std::optional< std::string > &localFilename={}, const std::optional< std::string > &stagingFilename={})
constexpr std::string_view Password("password")
constexpr std::string_view Username("username")
void set_worker_type(WorkerType t)
constexpr std::string_view CheckExistOnly("check_existance_only")
constexpr std::string_view NewUrl("new_url")
constexpr std::string_view LocalFilename("local_filename")
FieldVal value(const std::string_view &str, const FieldVal &defaultVal=FieldVal()) const
void setCode(const uint32_t newCode)
constexpr std::string_view LastAuthTimestamp("last_auth_timestamp")
std::vector< FieldVal > values(const std::string_view &str) const
constexpr std::string_view Label("label")
std::string asCompleteString() const
Returns a complete string representation of the Url object.
static ProvideMessage createAuthDataRequest(const uint32_t reqId, const zypp::Url &effectiveUrl, const std::string &lastTriedUser="", const std::optional< int64_t > &lastAuthTimestamp={}, const std::map< std::string, std::string > &extraValues={})
void set_worker_name(std::string name)
static ProvideMessage createDetach(const uint32_t reqId, const zypp::Url &attachUrl)
WorkerType worker_type() const
constexpr std::string_view Reason("reason")
static ProvideMessage createRedirect(const uint32_t reqId, const zypp::Url &newUrl)
uint32_t protocol_version() const
void setValue(const std::string &name, const FieldVal &value)
constexpr std::string_view DeltaFile("delta_file")
constexpr std::string_view EffectiveUrl("effective_url")
const std::string & worker_name() const
constexpr std::string_view Transient("transient")
constexpr std::string_view AuthTimestamp("auth_timestamp")
bool deserialize(const std::string &str_r, DownloadMode &result_r)
#define OR_REQ_FIELD_CHECK(msgtype, fname, ftype)
void setRequestId(const uint id)
static ProvideMessage createCancel(const uint32_t reqId)
constexpr std::string_view Url("url")
constexpr std::string_view LocalMountPoint("local_mountpoint")
static ProvideMessage createProvideFinished(const uint32_t reqId, const std::string &localFilename, bool cacheHit)
constexpr std::string_view Url("url")
zyppng::ProviderConfiguration Configuration
static expected< void > validateMessage(const ProvideMessage &msg)
constexpr std::string_view device("device")
constexpr std::string_view StagingFilename("staging_filename")
#define REQ_FIELD_CHECK(msgtype, fname, ftype)
constexpr std::string_view Url("url")
Easy-to use interface to the ZYPP dependency resolver.
static expected< ProvideMessage > create(const zyppng::RpcMessage &message)
static ProvideMessage createDetachFinished(const uint32_t reqId)
#define OR_OPT_FIELD_CHECK(msgtype, fname, ftype)
constexpr std::string_view CacheHit("cacheHit")
constexpr std::string_view LastUser("username")
static ProvideMessage createMediaChangeRequest(const uint32_t reqId, const std::string &label, int32_t mediaNr, const std::vector< std::string > &devices, const std::optional< std::string > &desc)
static ProvideMessage createAttachFinished(const uint32_t reqId, const std::optional< std::string > &localMountPoint={})
constexpr std::string_view ExpectedFilesize("expected_filesize")
#define OPT_FIELD_CHECK(msgtype, fname, ftype)