@@ -216,55 +216,21 @@ public class MulticastDNSServiceListener implements ServiceListener {
216216
217217 @ Override
218218 public void serviceAdded (ServiceEvent se ) {
219- if (se .getDNS () != null && se .getInfo () != null ) {
220- ServiceInfo info = se .getDNS ().getServiceInfo (CastDevice .SERVICE_TYPE , se .getInfo ().getName ());
221- String id ;
222- if (info == null || Util .isBlank (id = info .getPropertyString ("id" ))) {
223- return ;
224- }
225- InetAddress address ;
226- if (info .getInet4Addresses ().length > 0 ) {
227- address = info .getInet4Addresses ()[0 ];
228- } else if (info .getInet6Addresses ().length > 0 ) {
229- address = info .getInet6Addresses ()[0 ];
230- } else {
231- return ;
232- }
233- CastDevice newDevice = null ;
234- Set <DeviceDiscoveryListener > tmpListeners = null ;
235- synchronized (lock ) {
236- boolean found = false ;
237- for (CastDevice device : castDevices ) {
238- if (
239- id .equals (device .getUniqueId ()) &&
240- Objects .equals (address , device .getAddress ()) &&
241- info .getPort () == device .getPort ()
242- ) {
243- found = true ;
244- break ;
245- }
246- }
247- if (!found ) {
248- newDevice = new CastDevice (info , true );
249- castDevices .add (newDevice );
250- if (!listeners .isEmpty ()) {
251- tmpListeners = new LinkedHashSet <>(listeners );
252- }
253- }
254- }
255- if (newDevice != null && tmpListeners != null ) {
256- for (DeviceDiscoveryListener discoveryListener : tmpListeners ) {
257- discoveryListener .deviceDiscovered (newDevice );
258- }
259- }
260- }
219+ // Wait for it to be resolved
261220 }
262221
263222 @ Override
264223 public void serviceRemoved (ServiceEvent se ) {
265224 ServiceInfo info = se .getInfo ();
225+ LOGGER .trace (Channel .CAST_API_MARKER , "Cast device disappeared: {}" , info );
226+
266227 String id , name ;
267228 if (info == null || Util .isBlank (id = info .getPropertyString ("id" )) || Util .isBlank (name = info .getName ())) {
229+ LOGGER .debug (
230+ Channel .CAST_API_MARKER ,
231+ "Rejecting removal event for cast device {} because to ID was found" ,
232+ info == null ? "null" : info .getNiceTextString ()
233+ );
268234 return ;
269235 }
270236
@@ -292,6 +258,7 @@ public void serviceRemoved(ServiceEvent se) {
292258 }
293259 try {
294260 removed .disconnect ();
261+ LOGGER .debug (Channel .CAST_API_MARKER , "Cast device removed: {}" , removed );
295262 } catch (IOException e ) {
296263 LOGGER .warn (
297264 Channel .CAST_API_MARKER ,
@@ -306,7 +273,59 @@ public void serviceRemoved(ServiceEvent se) {
306273
307274 @ Override
308275 public void serviceResolved (ServiceEvent se ) {
309- // Already handled under "added"
276+ ServiceInfo info = se .getInfo ();
277+ LOGGER .trace (Channel .CAST_API_MARKER , "New cast device discovered: {}" , info );
278+
279+ String id ;
280+ if (info == null || Util .isBlank (id = info .getPropertyString ("id" ))) {
281+ LOGGER .debug (
282+ Channel .CAST_API_MARKER ,
283+ "Rejecting new cast device {} because to ID was found" ,
284+ info == null ? "null" : info .getNiceTextString ()
285+ );
286+ return ;
287+ }
288+ InetAddress address ;
289+ if (info .getInet4Addresses ().length > 0 ) {
290+ address = info .getInet4Addresses ()[0 ];
291+ } else if (info .getInet6Addresses ().length > 0 ) {
292+ address = info .getInet6Addresses ()[0 ];
293+ } else {
294+ LOGGER .debug (
295+ Channel .CAST_API_MARKER ,
296+ "Rejecting new cast device {} because to IP address was found" ,
297+ info .getNiceTextString ()
298+ );
299+ return ;
300+ }
301+ CastDevice newDevice = null ;
302+ Set <DeviceDiscoveryListener > tmpListeners = null ;
303+ synchronized (lock ) {
304+ boolean found = false ;
305+ for (CastDevice device : castDevices ) {
306+ if (
307+ id .equals (device .getUniqueId ()) &&
308+ Objects .equals (address , device .getAddress ()) &&
309+ info .getPort () == device .getPort ()
310+ ) {
311+ found = true ;
312+ break ;
313+ }
314+ }
315+ if (!found ) {
316+ newDevice = new CastDevice (info , true );
317+ castDevices .add (newDevice );
318+ if (!listeners .isEmpty ()) {
319+ tmpListeners = new LinkedHashSet <>(listeners );
320+ }
321+ LOGGER .debug (Channel .CAST_API_MARKER , "New cast device created: {}" , newDevice );
322+ }
323+ }
324+ if (newDevice != null && tmpListeners != null ) {
325+ for (DeviceDiscoveryListener discoveryListener : tmpListeners ) {
326+ discoveryListener .deviceDiscovered (newDevice );
327+ }
328+ }
310329 }
311330 }
312331}
0 commit comments