16#include <zypp-proto/media/provider.pb.h>
20 const std::string &ProviderConfiguration::staticTypeName()
27 return staticTypeName();
33 zypp::proto::Configuration implVar;
34 if ( !implVar.ParseFromString( data ) )
37 insert( implVar.values ().begin (), implVar.values ().end() );
43 zypp::proto::Configuration implVar;
44 implVar.mutable_values()->insert( begin(), end() );
45 implVar.SerializeToString( &
str );
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();
153 const auto validCode = ( c >= ProvideMessage::Code::FirstInformalCode && c <= ProvideMessage::Code::LastInformalCode )
154 || ( c >= ProvideMessage::Code::FirstSuccessCode && c <= ProvideMessage::Code::LastSuccessCode )
155 || ( c >= ProvideMessage::Code::FirstRedirCode && c <= ProvideMessage::Code::LastRedirCode )
156 || ( c >= ProvideMessage::Code::FirstClientErrCode && c <= ProvideMessage::Code::LastClientErrCode )
157 || ( c >= ProvideMessage::Code::FirstSrvErrCode && c <= ProvideMessage::Code::LastSrvErrCode )
158 || ( c >= ProvideMessage::Code::FirstControllerCode && c <= ProvideMessage::Code::LastControllerCode)
159 || ( c >= ProvideMessage::Code::FirstWorkerCode && c <= ProvideMessage::Code::LastWorkerCode );
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;
210 case ProvideMessage::Code::ProvideStarted: {
211 std::exception_ptr error;
223 case ProvideMessage::Code::ProvideFinished: {
224 std::exception_ptr error;
237 case ProvideMessage::Code::AttachFinished: {
238 std::exception_ptr error;
246 case ProvideMessage::Code::DetachFinished: {
250 case ProvideMessage::Code::AuthInfo: {
251 std::exception_ptr error;
268 case ProvideMessage::Code::MediaChanged:
271 case ProvideMessage::Code::Redirect: {
272 std::exception_ptr error;
282 case ProvideMessage::Code::Metalink: {
283 std::exception_ptr error;
293 case ProvideMessage::Code::BadRequest:
294 case ProvideMessage::Code::Unauthorized:
295 case ProvideMessage::Code::Forbidden:
296 case ProvideMessage::Code::PeerCertificateInvalid:
297 case ProvideMessage::Code::NotFound:
298 case ProvideMessage::Code::ExpectedSizeExceeded:
299 case ProvideMessage::Code::ConnectionFailed:
300 case ProvideMessage::Code::Timeout:
301 case ProvideMessage::Code::Cancelled:
302 case ProvideMessage::Code::InvalidChecksum:
303 case ProvideMessage::Code::MountFailed:
304 case ProvideMessage::Code::Jammed:
305 case ProvideMessage::Code::NoAuthData:
306 case ProvideMessage::Code::MediaChangeAbort:
307 case ProvideMessage::Code::MediaChangeSkip:
308 case ProvideMessage::Code::InternalError: {
309 const auto &e = validateErrorMsg(msg);
314 case ProvideMessage::Code::Prov: {
315 std::exception_ptr error;
330 case ProvideMessage::Code::Cancel:
334 case ProvideMessage::Code::Attach: {
335 std::exception_ptr error;
359 if ( ! ( ( has_verify_data == has_verify_type ) && ( has_verify_type == has_media_nr ) ) )
364 case ProvideMessage::Code::Detach: {
365 std::exception_ptr error;
375 case ProvideMessage::Code::AuthDataRequest: {
376 std::exception_ptr error;
389 case ProvideMessage::Code::MediaChangeRequest: {
390 std::exception_ptr error;
409 if ( c >= ProvideMessage::Code::FirstClientErrCode && c <= ProvideMessage::Code::LastSrvErrCode ) {
410 const auto &e = validateErrorMsg(msg);
433 ERR <<
"Invalid message for ID: " << msg.
_impl->request_id() << std::endl;;
439 ERR <<
"Failed to parse message" << std::endl;;
446 *msg.
_impl = std::move(message);
449 ERR <<
"Invalid message for ID: " << msg.
_impl->request_id() << std::endl;;
459 msg.
setCode ( ProvideMessage::Code::ProvideStarted );
464 if ( stagingFilename )
473 msg.
setCode ( ProvideMessage::Code::ProvideFinished );
484 msg.
setCode ( ProvideMessage::Code::AttachFinished );
487 if ( localMountPoint )
496 msg.
setCode ( ProvideMessage::Code::DetachFinished );
505 msg.
setCode ( ProvideMessage::Code::AuthInfo );
510 for (
auto i : extraValues ) {
519 msg.
setCode ( ProvideMessage::Code::MediaChanged );
528 msg.
setCode ( ProvideMessage::Code::Redirect );
538 msg.
setCode ( ProvideMessage::Code::Metalink );
540 for(
const auto &val : newUrls )
550 ZYPP_THROW(std::out_of_range(
"code must be between 400 and 599"));
561 msg.
setCode ( ProvideMessage::Code::Prov );
579 msg.
setCode ( ProvideMessage::Code::Cancel );
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 )
588 msg.
setCode ( ProvideMessage::Code::Attach );
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;
609 msg.
setCode ( ProvideMessage::Code::Detach );
619 msg.
setCode ( ProvideMessage::Code::AuthDataRequest );
622 if ( lastTriedUser.size() )
624 if ( lastAuthTimestamp )
633 msg.
setCode ( ProvideMessage::Code::MediaChangeRequest );
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 ) {
750 {
return new proto::Configuration( *rhs ); }
754 {
return new proto::Capabilities( *rhs ); }
758 {
return new zypp::proto::ProvideMessage(*rhs); }
Container of Capability (currently read only).
std::string asCompleteString() const
Returns a complete string representation of the Url object.
static ProvideMessage createProvideStarted(const uint32_t reqId, const zypp::Url &url, const std::optional< std::string > &localFilename={}, const std::optional< std::string > &stagingFilename={})
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={})
void setCode(const uint32_t newCode)
zypp::RWCOW_pointer< zypp::proto::ProvideMessage > _impl
static ProvideMessage createRedirect(const uint32_t reqId, const zypp::Url &newUrl)
static ProvideMessage createMetalinkRedir(const uint32_t reqId, const std::vector< zypp::Url > &newUrls)
static ProvideMessage createCancel(const uint32_t reqId)
void forEachVal(const std::function< bool(const std::string &name, const FieldVal &val)> &cb) const
HeaderValueMap headers() const
static ProvideMessage createMediaChanged(const uint32_t reqId)
static ProvideMessage createProvideFinished(const uint32_t reqId, const std::string &localFilename, bool cacheHit)
std::vector< FieldVal > values(const std::string_view &str) const
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)
FieldVal value(const std::string_view &str, const FieldVal &defaultVal=FieldVal()) const
void setValue(const std::string &name, const FieldVal &value)
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={})
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)
void setRequestId(const uint id)
static ProvideMessage createDetachFinished(const uint32_t reqId)
void addValue(const std::string &name, const FieldVal &value)
static expected< ProvideMessage > create(const zyppng::RpcMessage &message)
static ProvideMessage createDetach(const uint32_t reqId, const zypp::Url &attachUrl)
static ProvideMessage createAttachFinished(const uint32_t reqId, const std::optional< std::string > &localMountPoint={})
static ProvideMessage createErrorResponse(const uint32_t reqId, const uint code, const std::string &reason, bool transient=false)
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={})
virtual std::string serialize() const
virtual bool deserialize(const std::string &data)=0
virtual const std::string & typeName() const =0
virtual void serializeInto(std::string &str) const =0
static expected< void > parseMessageInto(const RpcMessage &m, T &target)
zypp::RWCOW_pointer< zypp::proto::Capabilities > _data
const std::string & worker_name() const
void set_worker_type(WorkerType t)
void set_protocol_version(uint32_t v)
WorkerType worker_type() const
uint32_t protocol_version() const
void set_worker_name(std::string name)
void set_cfg_flags(Flags f)
static expected success(ConsParams &&...params)
#define ZYPP_IMPL_RPCBASE(Class, ImplClass, implVar)
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
proto::Configuration * rwcowClone< proto::Configuration >(const proto::Configuration *rhs)
zypp::proto::ProvideMessage * rwcowClone< zypp::proto::ProvideMessage >(const zypp::proto::ProvideMessage *rhs)
proto::Capabilities * rwcowClone< proto::Capabilities >(const proto::Capabilities *rhs)
constexpr std::string_view LocalMountPoint("local_mountpoint")
constexpr std::string_view AttachId("attach_id")
constexpr std::string_view VerifyData("verify_data")
constexpr std::string_view VerifyType("verify_type")
constexpr std::string_view Label("label")
constexpr std::string_view MediaNr("media_nr")
constexpr std::string_view Url("url")
constexpr std::string_view LastUser("username")
constexpr std::string_view EffectiveUrl("effective_url")
constexpr std::string_view LastAuthTimestamp("last_auth_timestamp")
constexpr std::string_view Password("password")
constexpr std::string_view Username("username")
constexpr std::string_view AuthTimestamp("auth_timestamp")
constexpr std::string_view Url("url")
constexpr std::string_view Reason("reason")
constexpr std::string_view Transient("transient")
constexpr std::string_view LocalFilename("local_filename")
constexpr std::string_view CacheHit("cacheHit")
constexpr std::string_view Url("url")
constexpr std::string_view ExpectedFilesize("expected_filesize")
constexpr std::string_view DeltaFile("delta_file")
constexpr std::string_view CheckExistOnly("check_existance_only")
constexpr std::string_view Filename("filename")
constexpr std::string_view StagingFilename("staging_filename")
constexpr std::string_view Url("url")
constexpr std::string_view LocalFilename("local_filename")
constexpr std::string_view NewUrl("new_url")
const std::string & messageTypeName()
static void fieldValToProto(const ProvideMessage::FieldVal &val, zypp::proto::DataField &field)
static expected< void > validateMessage(const ProvideMessage &msg)
static ProvideMessage::FieldVal fieldValFromProto(const zypp::proto::DataField &field)
#define FAIL_IF_NOT_SEEN_REQ_FIELD(msgtype, fname)
#define DEF_REQ_FIELD(fname)
#define OR_OPT_FIELD_CHECK(msgtype, fname, ftype)
#define OPT_FIELD_CHECK(msgtype, fname, ftype)
#define REQ_FIELD_CHECK(msgtype, fname, ftype)
#define OR_REQ_FIELD_CHECK(msgtype, fname, ftype)
#define ZYPP_EXCPT_PTR(EXCPT)
Drops a logline and returns Exception as a std::exception_ptr.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.