forked from gizmomogwai/flutter_mdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdns.dart
147 lines (118 loc) · 3.76 KB
/
mdns.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import 'package:flutter/services.dart';
class ServiceInfo{
String name;
String type;
String host;
int port;
ServiceInfo(this.name, this.type, this.host, this.port);
static ServiceInfo fromMap(Map fromChannel){
String name = "";
String type = "";
String host = "";
int port = 0;
if ( fromChannel.containsKey("name") ) {
name = fromChannel["name"];
}
if (fromChannel.containsKey("type")) {
type = fromChannel["type"];
}
if (fromChannel.containsKey("host")) {
host = fromChannel["host"];
}
if (fromChannel.containsKey("port")) {
port = fromChannel["port"];
}
return new ServiceInfo(name, type, host, port);
}
@override
String toString(){
return "Name: $name, Type: $type, Host: $host, Port: $port";
}
}
typedef void ServiceInfoCallback(ServiceInfo info);
typedef void IntCallback (int data);
typedef void VoidCallback();
class DiscoveryCallbacks{
VoidCallback onDiscoveryStarted;
VoidCallback onDiscoveryStopped;
ServiceInfoCallback onDiscovered;
ServiceInfoCallback onResolved;
ServiceInfoCallback onLost;
DiscoveryCallbacks({
this.onDiscoveryStarted,
this.onDiscoveryStopped,
this.onDiscovered,
this.onResolved,
this.onLost,
});
}
class AdvertiseCallbacks{
VoidCallback onAdvertisingStarted;
VoidCallback onAdvertisingStopped;
AdvertiseCallbacks ({
this.onAdvertisingStarted,
this.onAdvertisingStopped,
});
}
class Mdns {
static const String NAMESPACE = "com.somepanic.mdns";
final MethodChannel _channel =
const MethodChannel('$NAMESPACE/mdns');
final EventChannel _serviceDiscoveredChannel =
const EventChannel("$NAMESPACE/discovered");
final EventChannel _serviceResolvedChannel =
const EventChannel("$NAMESPACE/resolved");
final EventChannel _serviceLostChannel =
const EventChannel("$NAMESPACE/lost");
final EventChannel _discoveryRunningChannel =
const EventChannel("$NAMESPACE/running");
DiscoveryCallbacks discoveryCallbacks;
AdvertiseCallbacks advertiseCallbacks;
Mdns({this.discoveryCallbacks, this.advertiseCallbacks}){
if ( discoveryCallbacks != null ) {
//Configure all the discovery related callbacks and event channels
_serviceDiscoveredChannel.receiveBroadcastStream().listen((data) {
print("Service discovered ${data.toString()}");
if (discoveryCallbacks.onDiscovered != null) {
discoveryCallbacks.onDiscovered(ServiceInfo.fromMap(data));
}
});
_serviceResolvedChannel.receiveBroadcastStream().listen((data) {
print("Service resolved ${data.toString()}");
if (discoveryCallbacks.onResolved != null) {
discoveryCallbacks.onResolved(ServiceInfo.fromMap(data));
}
});
_serviceLostChannel.receiveBroadcastStream().listen((data) {
print("Service lost ${data.toString()}");
if (discoveryCallbacks.onLost != null) {
discoveryCallbacks.onLost(ServiceInfo.fromMap(data));
}
});
_discoveryRunningChannel.receiveBroadcastStream().listen((running) {
print("Discovery Running? $running");
if (running && discoveryCallbacks.onDiscoveryStarted != null) {
discoveryCallbacks.onDiscoveryStarted();
} else if (discoveryCallbacks.onDiscoveryStopped != null) {
discoveryCallbacks.onDiscoveryStopped();
}
});
}
if (advertiseCallbacks != null) {
//TODO advertise stuff
}
}
Mdns startDiscovery(String serviceType) {
Map args = new Map();
args["serviceType"] = serviceType;
_channel.invokeMethod("startDiscovery", args);
return this;
}
Mdns stopDiscovery(){
_channel.invokeMethod("stopDiscovery", new Map());
return this;
}
addService(String serviceName) {
//TODO advertising
}
}