Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Improve presentation of connection status #6

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions lib/core/ros.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:web_socket_channel/io.dart';
import 'request.dart';

/// Status enums.
enum Status { NONE, CONNECTING, CONNECTED, CLOSED, ERRORED }
enum Status { NONE, CONNECTING, CONNECTED, CLOSING, CLOSED, ERRORED }
enum TopicStatus {
SUBSCRIBED,
UNSUBSCRIBED,
Expand All @@ -21,13 +21,16 @@ enum TopicStatus {
class Ros {
/// Initializes the [_statusController] as a broadcast.
/// The [url] of the ROS node can be optionally specified at this point.
Ros({this.url}) {
Ros({this.url, this.timeout}) {
_statusController = StreamController<Status>.broadcast();
}

/// The url of ROS node running the rosbridge server.
dynamic url;

/// The timeout of the connection
Duration timeout;

/// Total subscribers to ever connect.
int subscribers = 0;

Expand Down Expand Up @@ -62,17 +65,25 @@ class Ros {
Status status = Status.NONE;

/// Connect to the ROS node, the [url] can override what was provided in the constructor.
void connect({dynamic url}) {
void connect({dynamic url, Duration timeout}) {
this.url = url ?? this.url;
url ??= this.url;
timeout ??= this.timeout;
// Initialize the connection to the ROS node with a Websocket channel.
try {
_channel = IOWebSocketChannel.connect(url);
_channel = IOWebSocketChannel.connect(url, timeout: timeout);
stream =
_channel.stream.asBroadcastStream().map((raw) => json.decode(raw));
// Update the connection status.
status = Status.CONNECTED;
status = Status.CONNECTING;
_statusController.add(status);

// Listen to the ready status (connection to the websocket established)
_channel.ready.then((data) {
status = Status.CONNECTED;
_statusController.add(status);
});

// Listen for messages on the connection to update the status.
_channelListener = stream.listen((data) {
print('INCOMING: $data');
Expand All @@ -87,7 +98,7 @@ class Ros {
status = Status.CLOSED;
_statusController.add(status);
});
} on WebSocketException catch (e) {
} on WebSocketException catch (_) {
status = Status.ERRORED;
_statusController.add(status);
}
Expand All @@ -96,13 +107,22 @@ class Ros {
/// Close the connection to the ROS node, an exit [code] and [reason] can
/// be optionally specified.
Future<void> close([int code, String reason]) async {
status = Status.CLOSING;
_statusController.add(status);

/// Close listener and websocket.
await _channelListener?.cancel();
await _channel?.sink?.close(code, reason);

List<Future> futures = [];
if (_channel != null && _channel.sink != null)
futures.add(_channel?.sink?.close(code, reason));
if (timeout != null) futures.add(Future.delayed(timeout));

await Future.any(futures);

/// Update the connection status.
_statusController.add(Status.CLOSED);
status = Status.CLOSED;
_statusController.add(status);
}

/// Send a [message] to the ROS node
Expand Down
10 changes: 6 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ packages:
web_socket_channel:
dependency: "direct main"
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.12"
path: "."
ref: "feature/ready"
resolved-ref: "5584f5e5cc031aa2a08f32b0df50aade0d551fb9"
url: "git://github.com/artrmz/web_socket_channel.git"
source: git
version: "1.0.13"
sdks:
dart: ">=2.2.2 <3.0.0"
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ environment:
dependencies:
flutter:
sdk: flutter
web_socket_channel: ^1.0.12
web_socket_channel:
git:
url: git://github.com/artrmz/web_socket_channel.git
ref: feature/ready

dev_dependencies:
flutter_test:
Expand Down
4 changes: 1 addition & 3 deletions test/roslib_errors_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright (c) 2019 Conrad Heidebrecht.

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:roslib/roslib.dart';

void main() {

Ros ros;

setUp(() {
Expand All @@ -21,5 +19,5 @@ void main() {
expect(ros.statusStream, isNotNull);
expect(ros.status, Status.ERRORED);
});

}