Skip to content

Commit

Permalink
update dart examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Mar 8, 2021
1 parent b1b633f commit 0bc4cf1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
26 changes: 17 additions & 9 deletions discover-client.dart
Original file line number Diff line number Diff line change
@@ -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]);
});
});
}
14 changes: 10 additions & 4 deletions discover_client_flutter/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<RawDatagramSocket> _sockets = <RawDatagramSocket>[];
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 {}
}
}
Expand All @@ -89,4 +95,4 @@ class Client {
request(query);
});
}
}
}
34 changes: 22 additions & 12 deletions discover_client_flutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -38,7 +39,7 @@ class MyStatefulWidget extends StatefulWidget {
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool _scanning = false;
String url = "";
Client client = new Client();
Client client = new Client(true);

void _stopScan() {
setState(() {
Expand All @@ -53,16 +54,22 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget> {
});
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();
});
});
}

Expand All @@ -89,7 +96,10 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget> {
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)),
),
Expand Down

0 comments on commit 0bc4cf1

Please sign in to comment.