Skip to content

Commit

Permalink
Add new exception type for mdns (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Nov 14, 2023
1 parent 60782fa commit 2e941d7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ Make sure your project meets the minimum requirements:

`flutter pub add viam_sdk`

### Update Info.plist

If you are building for Apple platforms, you may have to update your app's `Info.plist`. `NSLocalNetworkUsageDescription` is needed to establish stable WebRTC connections, and `NSBonjourServices` is required to connect to local devices via mDNS.

```plist
<key>NSLocalNetworkUsageDescription</key>
<string>Viam requires access to your devices local network to connect to your devices.</string>
<key>NSBonjourServices</key>
<array>
<string>_rpc._tcp</string>
</array>
```

## Usage

You can use the Viam SDK to connect to an existing robot (to create a robot, view the [documentation](https://docs.viam.com/) or [try Viam](https://docs.viam.com/try-viam/)).
Expand Down
9 changes: 0 additions & 9 deletions lib/src/errors.dart

This file was deleted.

19 changes: 19 additions & 0 deletions lib/src/exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// Error thrown with connection is lost
class ConnectionLostException implements Exception {
final String? message;

const ConnectionLostException([this.message]);

@override
String toString() => message ?? 'ConnectionLostError';
}

/// Error thrown when mDNS fails to find a match on the local network
class NotLocalAddressException implements Exception {
final String address;

const NotLocalAddressException(this.address);

@override
String toString() => 'Address not on local network';
}
6 changes: 4 additions & 2 deletions lib/src/rpc/dial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:grpc/grpc_connection_interface.dart';
import 'package:grpc/grpc_or_grpcweb.dart';
import 'package:logger/logger.dart';

import '../exceptions.dart';
import '../gen/proto/rpc/v1/auth.pb.dart' as pb;
import '../gen/proto/rpc/v1/auth.pbgrpc.dart';
import '../gen/proto/rpc/webrtc/v1/signaling.pbgrpc.dart';
Expand Down Expand Up @@ -123,6 +124,8 @@ Future<ClientChannelBase> dial(String address, DialOptions? options, String Func
.._usingMdns = true
..authEntity = address;
return _dialWebRtc(mdnsUri, dialOptsCopy, sessionCallback);
} on NotLocalAddressException catch (e) {
_logger.d(e.toString());
} catch (e) {
_logger.d('Error dialing with mDNS; falling back to other methods', error: e);
}
Expand Down Expand Up @@ -158,8 +161,7 @@ Future<String> searchMdns(String address) async {
}

await discovery.stop();

throw Exception('Address not on local network');
throw NotLocalAddressException(address);
}

Future<ClientChannelBase> _dialDirectGrpc(String address, DialOptions options, String Function() sessionCallback) async {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/rpc/web_rtc/web_rtc_transport_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:grpc/grpc.dart';
import 'package:grpc/grpc_connection_interface.dart';

import '../../errors.dart';
import '../../exceptions.dart';
import '../../gen/proto/rpc/webrtc/v1/grpc.pb.dart' as grpc;
import 'web_rtc_client.dart';

Expand Down Expand Up @@ -63,7 +63,7 @@ class WebRtcTransportStream extends GrpcTransportStream {
if (connectionState == RTCPeerConnectionState.RTCPeerConnectionStateFailed ||
connectionState == RTCPeerConnectionState.RTCPeerConnectionStateDisconnected) {
onRequestFailure(
const ConnectionLostError('RTCPeerConnection lost'),
const ConnectionLostException('RTCPeerConnection lost'),
StackTrace.current,
);
return;
Expand Down

0 comments on commit 2e941d7

Please sign in to comment.