From 0bc4cf1b5ac15fb89b7bb9bdcf3924791bfc09c0 Mon Sep 17 00:00:00 2001 From: Alan Livio Date: Mon, 8 Mar 2021 17:40:34 -0300 Subject: [PATCH] update dart examples --- discover-client.dart | 26 ++++++++++++------- discover_client_flutter/lib/client.dart | 14 +++++++--- discover_client_flutter/lib/main.dart | 34 ++++++++++++++++--------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/discover-client.dart b/discover-client.dart index 6b53e8f..c122d0d 100644 --- a/discover-client.dart +++ b/discover-client.dart @@ -1,17 +1,25 @@ import './discover_client_flutter/lib/client.dart'; +import 'dart:io'; void main() { Client client = new Client(); var query = "urn:schemas-sbtvd-org:service:GingaCCWebServices:1"; + var found = false; client.search(query, (data) async { - print("-- main(): received headers ... "); - print(data); - RegExp regExp = new RegExp(r"GingaCC-Server-BaseURL: *(.*)\s", caseSensitive: false); - String url; - var match = regExp.firstMatch(data); - if (match != null) { - url = match[1]; - } - print("-- main(): GingaCC-Server-BaseURL =" + url); + if (found) return; + found = true; + print("-- m-search response headers ... " + data); + String locationUrl = new RegExp(r"LOCATION: *(.*)\s", caseSensitive: false) + .firstMatch(data)[1]; + HttpClient client = new HttpClient(); + client.getUrl(Uri.parse(locationUrl)).then((HttpClientRequest request) { + return request.close(); + }).then((HttpClientResponse response) { + print("-- response from " + locationUrl); + print("-- GingaCC-Server-BaseURL = " + + response.headers["GingaCC-Server-BaseURL"][0]); + print("-- GingaCC-Server-SecureBaseURL = " + + response.headers["GingaCC-Server-SecureBaseURL"][0]); + }); }); } diff --git a/discover_client_flutter/lib/client.dart b/discover_client_flutter/lib/client.dart index 7415d61..46f7aaf 100644 --- a/discover_client_flutter/lib/client.dart +++ b/discover_client_flutter/lib/client.dart @@ -3,6 +3,12 @@ import 'dart:async'; import 'dart:convert'; class Client { + bool _log; + + Client([bool enableLog = false]) { + _log = enableLog; + } + final InternetAddress _ipv4Multicast = new InternetAddress("239.255.255.250"); final InternetAddress _ipv6Multicast = new InternetAddress("FF05::C"); List _sockets = []; @@ -18,7 +24,7 @@ class Client { _socket.readEventsEnabled = true; _socket.listen((event) { - print("-- createSocket(): received event " + event.toString()); + if (_log) print("-- createSocket(): received event " + event.toString()); switch (event) { case RawSocketEvent.read: var packet = _socket.receive(); @@ -30,7 +36,7 @@ class Client { } var data = utf8.decode(packet.data); - print("-- createSocket(): data received"); + if (_log) print("-- createSocket(): data received"); fn(data); break; } @@ -73,7 +79,7 @@ class Client { for (var socket in _sockets) { try { var res = socket.send(data, _ipv4Multicast, 1900); - print("-- request(): sended bytes $res"); + if (_log) print("-- request(): sended bytes $res"); } on SocketException {} } } @@ -89,4 +95,4 @@ class Client { request(query); }); } -} \ No newline at end of file +} diff --git a/discover_client_flutter/lib/main.dart b/discover_client_flutter/lib/main.dart index 9c6e2b1..9fd7874 100644 --- a/discover_client_flutter/lib/main.dart +++ b/discover_client_flutter/lib/main.dart @@ -10,6 +10,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart'; import 'client.dart'; +import 'dart:io'; void main() => runApp(MyApp()); @@ -38,7 +39,7 @@ class MyStatefulWidget extends StatefulWidget { class _MyStatefulWidgetState extends State { bool _scanning = false; String url = ""; - Client client = new Client(); + Client client = new Client(true); void _stopScan() { setState(() { @@ -53,16 +54,22 @@ class _MyStatefulWidgetState extends State { }); var query = "urn:schemas-sbtvd-org:service:GingaCCWebServices:1"; client.search(query, (data) async { - print("-- main(): received headers => "); - print(data); - RegExp regExp = - new RegExp(r"GingaCC-Server-BaseURL: *(.*)\s", caseSensitive: false); - var match = regExp.firstMatch(data); - if (match != null) { - url = match[1]; - } - print("-- main(): GingaCC-Server-BaseURL = " + url); - _stopScan(); + print("-- m-search response headers ... " + data); + String locationUrl = + new RegExp(r"LOCATION: *(.*)\s", caseSensitive: false) + .firstMatch(data)[1]; + HttpClient client = new HttpClient(); + client.getUrl(Uri.parse(locationUrl)).then((HttpClientRequest request) { + return request.close(); + }).then((HttpClientResponse response) { + print("-- response from " + locationUrl); + print("-- GingaCC-Server-BaseURL = " + + response.headers["GingaCC-Server-BaseURL"][0]); + print("-- GingaCC-Server-SecureBaseURL = " + + response.headers["GingaCC-Server-SecureBaseURL"][0]); + url = response.headers["GingaCC-Server-BaseURL"][0]; + _stopScan(); + }); }); } @@ -89,7 +96,10 @@ class _MyStatefulWidgetState extends State { title: Text('GingaCC-WS Discover'), ), body: Center( - child: Text((url == "" ? "GingaCC-WS not found": "GingaCC-WS found at " + url), + child: Text( + (url == "" + ? "GingaCC-WS not found" + : "GingaCC-WS found at " + url), style: new TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)), ),