Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bonjour Android, it's Zeroconf

Bonjour Android, it's Zeroconf

ZeroConf is a protocol that helps us advertising a service or discovering the available services in our networks. This is how Chromecast works, but why don’t we go deeper?

- What is ZeroConf
– Why is it needed
– Discovery VS Advertising
– What are the different implementations that we can find on Android
– And on server side?
– Small demo and code show

Roberto Orgiu

April 08, 2016
Tweet

More Decks by Roberto Orgiu

Other Decks in Technology

Transcript

  1. “ZERO-CONFIGURATION NETWORKING IS A SET OF TECHNOLOGIES THAT AUTOMATICALLY CREATES

    A USABLE COMPUTER NETWORK BASED ON THE INTERNET PROTOCOL SUITE (TCP/IP) WHEN COMPUTERS OR NETWORK PERIPHERALS ARE INTERCONNECTED.“ WHAT IS ZEROCONF
  2. BONJOUR ANDROID, IT’S ZEROCONF THINK “DIFFERENT” ▸ ZeroConf is a

    standard ▸ UPnP/DLNA are somehow similar, but different people and ideas ▸ Apple has its own implementation, called Bonjour (once Rendezvous) ▸ On Android, we have very different solutions for using it
  3. APPLE OFTEN REFERS TO ZEROCONF AS BONJOUR, GIVING THEIR OWN

    DEFINITION OF THE CONCEPTS BONJOUR ANDROID, IT’S ZEROCONF FUN FACT
  4. BONJOUR ANDROID, IT’S ZEROCONF HOW DOES IT WORK? Ehy! I

    am 192.168.1.50 and I have the service DroidConService running on port 56472! Great! Exactly what I was looking for!
  5. BONJOUR ANDROID, IT’S ZEROCONF ▸ No infrastructure needed ▸ Simplicity

    over scalability ▸ 4 areas: IP interface configuration, translation between host name and IP address, IP multicast address allocation, service discovery ▸ Aware of network changes ▸ Still a draft HOW DOES IT WORK?
  6. BONJOUR ANDROID, IT’S ZEROCONF ▸ Allows hostnames to be mapped

    to IP addresses and back ▸ Failure proof with retry mechanism ▸ Conflict detection TRANSLATION BETWEEN HOST NAME AND IP ADDRESS
  7. BONJOUR ANDROID, IT’S ZEROCONF IP MULTICAST ADDRESS ALLOCATION ▸ List

    which of the scopes (local, site-local, link-local) are available ▸ Select a multicast address preventing conflicts
  8. BONJOUR ANDROID, IT’S ZEROCONF SERVICE DISCOVERY ▸ Service must be

    discoverable via identifier and/or type ▸ Discovery without the use of a service-specific protocol and should complete in a timely manner (10s of seconds) ▸ Prompt detection of new services in a timely manner (10s of seconds) D R A F T
  9. BONJOUR ANDROID, IT’S ZEROCONF HOW DOES IT WORK? ▸ Each

    part must start with _ (underscore) ▸ The second part only allows _tcp or _udp _ServiceType._TransportProtocolName. List of common services goo.gl/EXh1g
  10. BONJOUR ANDROID, IT’S ZEROCONF WHAT ABOUT ANDROID? ▸ API FROM

    4.1 ▸ JMDNS FOR JAVA ▸ APPLE NATIVE (C++) IMPLEMENTATION
  11. BONJOUR ANDROID, IT’S ZEROCONF JMDNS FOR JAVA ▸ Easy to

    implement ▸ Runs on the main thread, unless we specify otherwise ▸ Distributed as JAR or via Maven repo ▸ Long start-up time ▸ Compatible with all the Android versions
  12. BONJOUR ANDROID, IT’S ZEROCONF WHAT ABOUT ANDROID? ▸ API FROM

    4.1 ▸ JMDNS FOR JAVA ▸ APPLE NATIVE (C++) IMPLEMENTATION
  13. BONJOUR ANDROID, IT’S ZEROCONF APPLE NATIVE (C++) IMPLEMENTATION ▸ Ported

    to Android by Apple ▸ Open-sourced ▸ Needs Android NDK ▸ Few projects come with it already packed in ▸ Short startup time ▸ More complex logic
  14. BONJOUR ANDROID, IT’S ZEROCONF WHAT ABOUT ANDROID? ▸ API FROM

    4.1 ▸ JMDNS FOR JAVA ▸ APPLE NATIVE (C++) IMPLEMENTATION
  15. BONJOUR ANDROID, IT’S ZEROCONF ANDROID API ▸ From Android 4.1,

    custom attributes added with 5.1 (API 21) ▸ Already asynchronous, with return on the main thread ▸ Verbose, but easy to implement ▸ Based on two steps: discovery and resolution ▸ Only one service can be resolved at a time http://developer.android.com/training/connect-devices-wirelessly/nsd.html
  16. BONJOUR ANDROID, IT’S ZEROCONF ANDROID API - SERVER NsdServiceInfo serviceInfo

    = new NsdServiceInfo(); serviceInfo.setServiceName("DroidConService"); serviceInfo.setServiceType("_http._tcp."); serviceInfo.setPort(randomPort); nsdManager = Context.getSystemService(Context.NSD_SERVICE); nsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener); Remember to unregister the service upon app closing!
  17. BONJOUR ANDROID, IT’S ZEROCONF ANDROID API - CLIENT class Resolver

    extends NsdManager.ResolveListener { @Override public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {} @Override public void onServiceResolved(NsdServiceInfo serviceInfo) { // here we can use our resolved service } }; Resolve resolveListener = new Resolve();
  18. BONJOUR ANDROID, IT’S ZEROCONF ANDROID API - CLIENT class Resolver

    extends NsdManager.ResolveListener { @Override public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {} @Override public void onServiceResolved(NsdServiceInfo serviceInfo) { // here we can use our resolved service } }; Resolve resolveListener = new Resolve();
  19. BONJOUR ANDROID, IT’S ZEROCONF ANDROID API - CLIENT class SimpleDiscoveryListener

    extends NsdManager.DiscoveryListener { @Override public void onDiscoveryStarted(String regType) {} @Override public void onServiceFound(NsdServiceInfo service) { if (service.getServiceName().contains(“DroidConService")){ nsdManager.resolveService(service, resolveListener); } } @Override public void onServiceLost(NsdServiceInfo service) {} @Override public void onDiscoveryStopped(String serviceType) {} @Override public void onStartDiscoveryFailed(String serviceType, int errorCode) { nsdManager.stopServiceDiscovery(this); } @Override public void onStopDiscoveryFailed(String serviceType, int errorCode) { nsdManager.stopServiceDiscovery(this); }};
  20. BONJOUR ANDROID, IT’S ZEROCONF ANDROID API - CLIENT class SimpleDiscoveryListener

    extends NsdManager.DiscoveryListener { @Override public void onDiscoveryStarted(String regType) {} @Override public void onServiceFound(NsdServiceInfo service) { if (service.getServiceName().contains(“DroidConService")){ nsdManager.resolveService(service, resolveListener); } } @Override public void onServiceLost(NsdServiceInfo service) {} @Override public void onDiscoveryStopped(String serviceType) {} @Override public void onStartDiscoveryFailed(String serviceType, int errorCode) { nsdManager.stopServiceDiscovery(this); } @Override public void onStopDiscoveryFailed(String serviceType, int errorCode) { nsdManager.stopServiceDiscovery(this); }};
  21. BONJOUR ANDROID, IT’S ZEROCONF ANDROID API - CLIENT SimpleDiscoveryListener discoveryListener

    = new SimpleDiscoveryListener(); nsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener); nsdManager.stopServiceDiscovery(discoveryListener);
  22. BONJOUR ANDROID, IT’S ZEROCONF ANDROID API - CLIENT SimpleDiscoveryListener discoveryListener

    = new SimpleDiscoveryListener(); nsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener); nsdManager.stopServiceDiscovery(discoveryListener);
  23. BONJOUR ANDROID, IT’S ZEROCONF BETTER WAYS OF DOING IT ▸

    better-zeroconf ▸ RxDNSSD ▸ RxBonjour ▸ ZeRxConf ▸ android-mdns ▸ JmDNS ▸ Apple mDNS ▸ Apple mDNS ▸ JmDNS + Android Native APIs ▸ Apple mDNS