Skip to content

Commit

Permalink
Implement Protobuf Message Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
alfianlosari committed Jul 14, 2023
1 parent 1e01a8d commit b2474aa
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 2 deletions.
24 changes: 24 additions & 0 deletions courier_dart_sdk_demo/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import 'package:courier_dart_sdk_demo/courier_response_mapper.dart';
import 'package:courier_dart_sdk_demo/local_auth_provider.dart';
import 'package:courier_dart_sdk_demo/test_data_type.dart';
import 'package:courier_dart_sdk_demo/test_person_data.dart';
import 'package:courier_dart_sdk_demo/test_pet.pb.dart';
import 'package:courier_protobuf/protobuf_message_adapter.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -85,6 +87,7 @@ class MyHomePage extends StatelessWidget {
disconnectDelaySeconds: 10,
enableMQTTChuck: true),
messageAdapters: const <MessageAdapter>[
ProtobufMessageAdapter(),
JSONMessageAdapter(),
BytesMessageAdapter(),
StringMessageAdapter()
Expand Down Expand Up @@ -125,6 +128,17 @@ class MyHomePage extends StatelessWidget {
.listen((person) {
print("Message received person: ${person.name}");
});

courierClient.subscribe(
"pet/6b57d4e5-0fce-4917-b343-c8a1c77405e5/update", QoS.one);

courierClient
.courierMessageStream<Pet>(
"pet/6b57d4e5-0fce-4917-b343-c8a1c77405e5/update",
decoder: Pet.fromBuffer)
.listen((pet) {
print("Message received Pet: ${pet.name}");
});
}

void _onUnsubscribe() {
Expand All @@ -133,6 +147,9 @@ class MyHomePage extends StatelessWidget {

courierClient
.unsubscribe("person/6b57d4e5-0fce-4917-b343-c8a1c77405e5/update");

courierClient
.unsubscribe("pet/6b57d4e5-0fce-4917-b343-c8a1c77405e5/update");
}

void _onSend() {
Expand All @@ -149,6 +166,13 @@ class MyHomePage extends StatelessWidget {
payload: Person(name: textMessage),
topic: "person/6b57d4e5-0fce-4917-b343-c8a1c77405e5/update",
qos: QoS.one));

final pet = Pet();
pet.name = "Hello Pet";
courierClient.publishCourierMessage(CourierMessage(
payload: pet,
topic: "pet/6b57d4e5-0fce-4917-b343-c8a1c77405e5/update",
qos: QoS.one));
}

@override
Expand Down
60 changes: 60 additions & 0 deletions courier_dart_sdk_demo/lib/test_pet.pb.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Generated code. Do not modify.
// source: test_pet.proto
//
// @dart = 2.12

// ignore_for_file: annotate_overrides, camel_case_types
// ignore_for_file: constant_identifier_names, library_prefixes
// ignore_for_file: non_constant_identifier_names, prefer_final_fields
// ignore_for_file: unnecessary_import, unnecessary_this, unused_import

import 'dart:core' as $core;

import 'package:protobuf/protobuf.dart' as $pb;

class Pet extends $pb.GeneratedMessage {
factory Pet() => create();
Pet._() : super();
factory Pet.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
factory Pet.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);

static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'Pet', package: const $pb.PackageName(_omitMessageNames ? '' : 'com.pet'), createEmptyInstance: create)
..aOS(1, _omitFieldNames ? '' : 'name')
..hasRequiredFields = false
;

@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.deepCopy] instead. '
'Will be removed in next major version')
Pet clone() => Pet()..mergeFromMessage(this);
@$core.Deprecated(
'Using this can add significant overhead to your binary. '
'Use [GeneratedMessageGenericExtensions.rebuild] instead. '
'Will be removed in next major version')
Pet copyWith(void Function(Pet) updates) => super.copyWith((message) => updates(message as Pet)) as Pet;

$pb.BuilderInfo get info_ => _i;

@$core.pragma('dart2js:noInline')
static Pet create() => Pet._();
Pet createEmptyInstance() => create();
static $pb.PbList<Pet> createRepeated() => $pb.PbList<Pet>();
@$core.pragma('dart2js:noInline')
static Pet getDefault() => _defaultInstance ??= $pb.GeneratedMessage.$_defaultFor<Pet>(create);
static Pet? _defaultInstance;

@$pb.TagNumber(1)
$core.String get name => $_getSZ(0);
@$pb.TagNumber(1)
set name($core.String v) { $_setString(0, v); }
@$pb.TagNumber(1)
$core.bool hasName() => $_has(0);
@$pb.TagNumber(1)
void clearName() => clearField(1);
}


const _omitFieldNames = $core.bool.fromEnvironment('protobuf.omit_field_names');
const _omitMessageNames = $core.bool.fromEnvironment('protobuf.omit_message_names');
27 changes: 27 additions & 0 deletions courier_dart_sdk_demo/lib/test_pet.pbjson.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Generated code. Do not modify.
// source: test_pet.proto
//
// @dart = 2.12

// ignore_for_file: annotate_overrides, camel_case_types
// ignore_for_file: constant_identifier_names, library_prefixes
// ignore_for_file: non_constant_identifier_names, prefer_final_fields
// ignore_for_file: unnecessary_import, unnecessary_this, unused_import

import 'dart:convert' as $convert;
import 'dart:core' as $core;
import 'dart:typed_data' as $typed_data;

@$core.Deprecated('Use petDescriptor instead')
const Pet$json = {
'1': 'Pet',
'2': [
{'1': 'name', '3': 1, '4': 1, '5': 9, '10': 'name'},
],
};

/// Descriptor for `Pet`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List petDescriptor = $convert.base64Decode(
'CgNQZXQSEgoEbmFtZRgBIAEoCVIEbmFtZQ==');

25 changes: 24 additions & 1 deletion courier_dart_sdk_demo/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ packages:
path: "../courier_dart_sdk"
relative: true
source: path
version: "0.0.7"
version: "0.0.9"
courier_protobuf:
dependency: "direct main"
description:
path: "../courier_protobuf"
relative: true
source: path
version: "0.0.1"
crypto:
dependency: transitive
description:
Expand Down Expand Up @@ -88,6 +95,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
fixnum:
dependency: transitive
description:
name: fixnum
sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -210,6 +225,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
protobuf:
dependency: transitive
description:
name: protobuf
sha256: "01dd9bd0fa02548bf2ceee13545d4a0ec6046459d847b6b061d8a27237108a08"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down
3 changes: 2 additions & 1 deletion courier_dart_sdk_demo/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ dependencies:

courier_dart_sdk:
path: ../courier_dart_sdk

courier_protobuf:
path: ../courier_protobuf

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down
7 changes: 7 additions & 0 deletions courier_dart_sdk_demo/test_pet.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package com.pet;

message Pet {
string name = 1;
}
30 changes: 30 additions & 0 deletions courier_protobuf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
10 changes: 10 additions & 0 deletions courier_protobuf/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: f468f3366c26a5092eb964a230ce7892fda8f2f8
channel: stable

project_type: package
3 changes: 3 additions & 0 deletions courier_protobuf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* Initial Release of ProtobufMessageAdapter
1 change: 1 addition & 0 deletions courier_protobuf/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
39 changes: 39 additions & 0 deletions courier_protobuf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
4 changes: 4 additions & 0 deletions courier_protobuf/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
23 changes: 23 additions & 0 deletions courier_protobuf/lib/protobuf_message_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'dart:typed_data';

import 'package:courier_dart_sdk/message_adapter/message_adapter.dart';
import 'package:protobuf/protobuf.dart';

class ProtobufMessageAdapter extends MessageAdapter {
const ProtobufMessageAdapter();

@override
String contentType() => "application/x-protobuf";

@override
T decode<T>(Uint8List bytes, dynamic decoder) => decoder(bytes);

@override
Uint8List encode(Object object, String topic, dynamic encoder) {
if (object is GeneratedMessage) {
return object.writeToBuffer();
}
throw Exception(
'${object.runtimeType} is not of type protobuf generated message');
}
}
21 changes: 21 additions & 0 deletions courier_protobuf/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: courier_protobuf
description: A new Flutter package project.
version: 0.0.1
homepage:

environment:
sdk: ">=2.12.0 <4.0.0"
flutter: ">=1.17.0"

dependencies:
flutter:
sdk: flutter
courier_dart_sdk: 0.0.9
protobuf: ^2.0.1

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0

flutter:

0 comments on commit b2474aa

Please sign in to comment.