Skip to content
This repository has been archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from Panduza/26-locally-discovered-platform-wo…
Browse files Browse the repository at this point in the history
…nt-connect

get addr and port of the broker and not addr/port of the platform
  • Loading branch information
wardru authored May 21, 2024
2 parents 4ff07d0 + 65294c7 commit 3d1945b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
18 changes: 9 additions & 9 deletions lib/after_setup_pages/discovery_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class DiscoveryPage extends StatefulWidget {

class _DiscoveryPageState extends State<DiscoveryPage> {

List<(InternetAddress, int, String)> platformsIpsPorts = [];
List<(String, int, String)> platformsIpsPorts = [];
bool isLoading = false;

// List of button of local platform detected
Widget localDiscoveryConnections(List<(InternetAddress, int, String)> platformsIpsPorts, bool isLoading) {
Widget localDiscoveryConnections(List<(String, int, String)> platformsIpsPorts, bool isLoading) {

if (isLoading) {
return Center(
Expand Down Expand Up @@ -59,10 +59,10 @@ class _DiscoveryPageState extends State<DiscoveryPage> {
),
),
AutoSizeText(
platformsIpsPorts[index].$1.address
platformsIpsPorts[index].$1
),
const AutoSizeText(
'1883'
AutoSizeText(
platformsIpsPorts[index].$2.toString()
)
],
)
Expand All @@ -74,8 +74,8 @@ class _DiscoveryPageState extends State<DiscoveryPage> {
MaterialPageRoute(
builder: (context) => ManualConnectionPage(
name: platformsIpsPorts[index].$3,
ip: platformsIpsPorts[index].$1.address,
port: "1883"
ip: platformsIpsPorts[index].$1,
port: platformsIpsPorts[index].$2.toString()
),
),
);
Expand All @@ -94,7 +94,7 @@ class _DiscoveryPageState extends State<DiscoveryPage> {
platformDiscovery().then(
(value) {
platformsIpsPorts = value;
platformsIpsPorts.sort((a, b) => a.$1.host.compareTo(b.$1.host));
platformsIpsPorts.sort((a, b) => a.$3.compareTo(b.$3));
setState(() {});
}
);
Expand All @@ -121,7 +121,7 @@ class _DiscoveryPageState extends State<DiscoveryPage> {
platformDiscovery().then(
(value) {
platformsIpsPorts = value;
platformsIpsPorts.sort((a, b) => a.$1.host.compareTo(b.$1.host));
platformsIpsPorts.sort((a, b) => a.$3.compareTo(b.$3));
setState(() {});
}
);
Expand Down
32 changes: 20 additions & 12 deletions lib/data/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,9 @@ Future<bool> checkIfConnectionValid(BuildContext context, String name, String ho
broadcast with UTF-8 format and getting an answer for each platform
*/

Future<List<(InternetAddress, int, String)>> platformDiscovery() async {
Future<List<(String, int, String)>> platformDiscovery() async {

List<(InternetAddress, int, String)> ipPort = [];
String waitedAnswer = '{"name": "panduza_platform","version": 1.0}';
List<(String, int, String)> ipPort = [];

// Could have some problem with some android phone ?
List<NetworkInterface> listInterface = await NetworkInterface.list();
Expand All @@ -303,13 +302,24 @@ Future<List<(InternetAddress, int, String)>> platformDiscovery() async {
String answer = utf8.decode(datagram.data);

Map<String, dynamic> answerMap = jsonDecode(answer);
String? platformName = answerMap["name"];

// if platform name is not given in the answer payload do not add this platform
if (platformName != null) {
// Get addr, port and platform name
if (!ipPort.contains((datagram.address, datagram.port))) ipPort.add((datagram.address, datagram.port, platformName));
}
Map<String, dynamic>? brokerInfo = answerMap["broker"];
Map<String, dynamic>? platformInfo = answerMap["platform"];

if (brokerInfo != null && platformInfo != null) {
String? brokerAddr = brokerInfo["addr"];
int? brokerPort = brokerInfo["port"];
String? platformName = platformInfo["name"];

// if platform name is not given in the answer payload do not add this platform
if (platformName != null && brokerAddr != null && brokerPort != null) {
// Get addr, port and platform name
// if (!ipPort.contains((datagram.address, datagram.port, platformName))) ipPort.add((datagram.address, datagram.port, platformName));
if (!ipPort.contains((datagram.address.address, brokerPort, platformName))) ipPort.add((datagram.address.address, brokerPort, platformName));
}
}


}
});

Expand All @@ -325,6 +335,4 @@ Future<List<(InternetAddress, int, String)>> platformDiscovery() async {
}

return ipPort;
}


}

0 comments on commit 3d1945b

Please sign in to comment.