deserialize method
Deserialize a given message
Implementation
@override
AbstractMessage? deserialize(Uint8List? message) {
if (message is List) {
final decodedMessage = cbor.decode(message!.toList());
if (decodedMessage is CborList) {
final cborMessageId = decodedMessage[0];
if (cborMessageId is CborInt) {
final messageId = cborMessageId.toInt();
if (messageId == MessageTypes.codeAbort &&
decodedMessage.length == 3) {
return Abort((decodedMessage[2] as CborString).toString(),
message: decodedMessage[1] is CborMap &&
(decodedMessage[1] as CborMap)[CborString('message')] !=
null
? ((decodedMessage[1] as CborMap)[CborString('message')]
as CborString)
.toString()
: null);
}
if (messageId == MessageTypes.codeChallenge &&
decodedMessage.length == 3) {
return Challenge(
(decodedMessage[1] as CborString).toString(),
Extra(
challenge: (decodedMessage[2] as CborMap)[CborString('challenge')] == null
? null
: ((decodedMessage[2] as CborMap)[CborString('challenge')] as CborString)
.toString(),
salt: (decodedMessage[2] as CborMap)[CborString('salt')] == null
? null
: ((decodedMessage[2] as CborMap)[CborString('salt')] as CborString)
.toString(),
keyLen: (decodedMessage[2] as CborMap)[CborString('keylen')] == null
? null
: ((decodedMessage[2] as CborMap)[CborString('keylen')] as CborInt)
.toInt(),
iterations: (decodedMessage[2] as CborMap)[CborString('iterations')] == null
? null
: ((decodedMessage[2] as CborMap)[CborString('iterations')] as CborInt)
.toInt(),
memory: (decodedMessage[2] as CborMap)[CborString('memory')] == null
? null
: ((decodedMessage[2] as CborMap)[CborString('memory')] as CborInt)
.toInt(),
kdf: (decodedMessage[2] as CborMap)[CborString('kdf')] == null
? null
: ((decodedMessage[2] as CborMap)[CborString('kdf')] as CborString).toString(),
nonce: (decodedMessage[2] as CborMap)[CborString('nonce')] == null ? null : ((decodedMessage[2] as CborMap)[CborString('nonce')] as CborString).toString()));
}
if (messageId == MessageTypes.codeWelcome &&
decodedMessage.length == 3) {
final details = Details();
details.realm =
(((decodedMessage[2] as CborMap)[CborString('realm')] ??
CborString('')) as CborString)
.toString();
details.authid =
(((decodedMessage[2] as CborMap)[CborString('authid')] ??
CborString('')) as CborString)
.toString();
details.authprovider =
(((decodedMessage[2] as CborMap)[CborString('authprovider')] ??
CborString('')) as CborString)
.toString();
details.authmethod =
(((decodedMessage[2] as CborMap)[CborString('authmethod')] ??
CborString('')) as CborString)
.toString();
details.authrole =
(((decodedMessage[2] as CborMap)[CborString('authrole')] ??
CborString('')) as CborString)
.toString();
if ((decodedMessage[2] as CborMap)[CborString('authextra')] !=
null) {
((decodedMessage[2] as CborMap)[CborString('authextra')]
as CborMap)
.forEach((key, value) {
details.authextra ??= <String, dynamic>{};
if (value is CborString) {
details.authextra![(key as CborString).toString()] =
value.toString();
}
if (value is CborInt) {
details.authextra![(key as CborString).toString()] =
value.toInt();
}
if (value is CborFloat) {
details.authextra![(key as CborString).toString()] =
value.value;
}
if (value is CborBool) {
details.authextra![(key as CborString).toString()] =
value.value;
}
if (value is CborBase64) {
details.authextra![(key as CborString).toString()] =
value.toString();
}
});
}
if ((decodedMessage[2] as CborMap)[CborString('roles')] != null) {
details.roles = Roles();
if (((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')] !=
null) {
details.roles!.dealer = Dealer();
if ((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')] !=
null) {
details.roles!.dealer!.features = DealerFeatures();
details.roles!.dealer!.features!.callerIdentification =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('caller_identification')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!.callTrustLevels =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[CborString('call_trustlevels')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!.patternBasedRegistration =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('pattern_based_registration')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!.registrationMetaApi =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('registration_meta_api')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!
.sharedRegistration = ((((((decodedMessage[2]
as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[CborString('shared_registration')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!.sessionMetaApi =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[CborString('session_meta_api')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!.callTimeout =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[CborString('call_timeout')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!.callCanceling =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[CborString('call_canceling')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!.progressiveCallResults =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('progressive_call_results')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.dealer!.features!.payloadPassThruMode =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('dealer')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('payload_passthru_mode')] ??
CborBool(false)) as CborBool)
.value;
}
}
if (((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')] !=
null) {
details.roles!.broker = Broker();
if ((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')] !=
null) {
details.roles!.broker!.features = BrokerFeatures();
details.roles!.broker!.features!.publisherIdentification =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('publisher_identification')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.broker!.features!.publicationTrustLevels =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('publication_trustlevels')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.broker!.features!.patternBasedSubscription =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('pattern_based_subscription')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.broker!.features!.subscriptionMetaApi =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('subscription_meta_api')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.broker!.features!
.subscriberBlackWhiteListing = ((((((decodedMessage[2]
as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('subscriber_blackwhite_listing')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.broker!.features!.sessionMetaApi =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[CborString('session_meta_api')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.broker!.features!
.publisherExclusion = ((((((decodedMessage[2]
as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[CborString('publisher_exclusion')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.broker!.features!.eventHistory =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[CborString('event_history')] ??
CborBool(false)) as CborBool)
.value;
details.roles!.broker!.features!.payloadPassThruMode =
((((((decodedMessage[2] as CborMap)[CborString('roles')]
as CborMap)[CborString('broker')]
as CborMap)[CborString('features')]
as CborMap)[
CborString('payload_passthru_mode')] ??
CborBool(false)) as CborBool)
.value;
}
}
}
return Welcome((decodedMessage[1] as CborInt).toInt(), details);
}
if (messageId == MessageTypes.codeRegistered &&
decodedMessage.length == 3) {
return Registered((decodedMessage[1] as CborInt).toInt(),
(decodedMessage[2] as CborInt).toInt());
}
if (messageId == MessageTypes.codeUnregistered &&
decodedMessage.length == 2) {
return Unregistered((decodedMessage[1] as CborInt).toInt());
}
if (messageId == MessageTypes.codeInvocation &&
decodedMessage.length > 3) {
return _addPayload(
Invocation(
(decodedMessage[1] as CborInt).toInt(),
(decodedMessage[2] as CborInt).toInt(),
InvocationDetails(
(decodedMessage[3] as CborMap)[CborString('caller')] ==
null
? null
: ((decodedMessage[3]
as CborMap)[CborString('caller')] as CborInt)
.toInt(),
(decodedMessage[3] as CborMap)[CborString('procedure')] ==
null
? null
: ((decodedMessage[3]
as CborMap)[CborString('procedure')]
as CborString)
.toString(),
(decodedMessage[3]
as CborMap)[CborString('receive_progress')] ==
null
? null
: ((decodedMessage[3] as CborMap)[
CborString('receive_progress')] as CborBool)
.value,
)),
decodedMessage,
4);
}
if (messageId == MessageTypes.codeResult &&
decodedMessage.length > 2) {
return _addPayload(
Result(
(decodedMessage[1] as CborInt).toInt(),
ResultDetails(
progress: (decodedMessage[2]
as CborMap)[CborString('progress')] ==
null
? null
: ((decodedMessage[2]
as CborMap)[CborString('progress')]
as CborBool)
.value,
pptScheme: (decodedMessage[2]
as CborMap)[CborString('ppt_scheme')] ==
null
? null
: (decodedMessage[2]
as CborMap)[CborString('ppt_scheme')] as String,
pptSerializer: (decodedMessage[2]
as CborMap)[CborString('ppt_serializer')] ==
null
? null
: (decodedMessage[2]
as CborMap)[CborString('ppt_serializer')]
as String,
pptCipher: (decodedMessage[2]
as CborMap)[CborString('ppt_cipher')] ==
null
? null
: (decodedMessage[2]
as CborMap)[CborString('ppt_cipher')] as String,
pptKeyId: (decodedMessage[2]
as CborMap)[CborString('ppt_keyid')] ==
null
? null
: (decodedMessage[2]
as CborMap)[CborString('ppt_keyid')] as String,
)),
decodedMessage,
3);
}
if (messageId == MessageTypes.codePublished &&
decodedMessage.length == 3) {
return Published((decodedMessage[1] as CborInt).toInt(),
(decodedMessage[2] as CborInt).toInt());
}
if (messageId == MessageTypes.codeSubscribed &&
decodedMessage.length == 3) {
return Subscribed((decodedMessage[1] as CborInt).toInt(),
(decodedMessage[2] as CborInt).toInt());
}
if (messageId == MessageTypes.codeUnsubscribed &&
decodedMessage.length > 1) {
return Unsubscribed(
(decodedMessage[1] as CborInt).toInt(),
decodedMessage.length == 2
? null
: UnsubscribedDetails(
(decodedMessage[2]
as CborMap)[CborString('subscription')] ==
null
? null
: ((decodedMessage[2]
as CborMap)[CborString('subscription')]
as CborInt)
.toInt(),
(decodedMessage[2] as CborMap)[CborString('reason')] ==
null
? null
: ((decodedMessage[2]
as CborMap)[CborString('reason')]
as CborString)
.toString()));
}
if (messageId == MessageTypes.codeEvent &&
decodedMessage.length > 3) {
return _addPayload(
Event(
(decodedMessage[1] as CborInt).toInt(),
(decodedMessage[2] as CborInt).toInt(),
EventDetails(
publisher:
(decodedMessage[3] as CborMap)[CborString('publisher')] == null
? null
: ((decodedMessage[3]
as CborMap)[CborString('publisher')]
as CborInt)
.toInt(),
trustlevel: (decodedMessage[3]
as CborMap)[CborString('trustlevel')] ==
null
? null
: ((decodedMessage[3]
as CborMap)[CborString('trustlevel')]
as CborInt)
.toInt(),
topic: (decodedMessage[3] as CborMap)[CborString('topic')] == null
? null
: ((decodedMessage[3] as CborMap)[CborString('topic')]
as CborString)
.toString())),
decodedMessage,
4);
}
if (messageId == MessageTypes.codeError &&
decodedMessage.length > 4) {
return _addPayload(
Error(
(decodedMessage[1] as CborInt).toInt(),
(decodedMessage[2] as CborInt).toInt(),
Map<String, Object>.from(
(decodedMessage[3] as CborMap).toObject() as Map),
(decodedMessage[4] as CborString).toString()),
decodedMessage,
5);
}
if (messageId == MessageTypes.codeGoodbye) {
return Goodbye(
decodedMessage.length == 1
? null
: GoodbyeMessage((decodedMessage[1]
as CborMap)[CborString('message')] ==
null
? null
: ((decodedMessage[1] as CborMap)[CborString('message')]
as CborString)
.toString()),
(decodedMessage[2] as CborString).toString());
}
}
}
return null;
}
_logger.shout('Could not deserialize the message: $message');
// TODO respond with an error
return null;
}