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. IT’S ZEROCONF
    Bonjour Android
    Roberto Orgiu
    Giuseppe Mariniello
    Android Developer
    Backend Developer

    View full-size slide

  2. WHAT IS ZEROCONF?

    View full-size slide

  3. “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

    View full-size slide

  4. WHAT IS ZEROCONF
    “IT DOES NOT REQUIRE MANUAL
    OPERATOR INTERVENTION OR SPECIAL
    CONFIGURATION SERVERS.“

    View full-size slide

  5. WHERE CAN WE FIND
    ZEROCONF?

    View full-size slide

  6. ALMOST EVERYWHERE

    View full-size slide

  7. 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

    View full-size slide

  8. APPLE OFTEN REFERS TO ZEROCONF AS
    BONJOUR, GIVING THEIR OWN
    DEFINITION OF THE CONCEPTS
    BONJOUR ANDROID, IT’S ZEROCONF
    FUN FACT

    View full-size slide

  9. Wut? Why do you even…?

    View full-size slide

  10. 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!

    View full-size slide

  11. 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?

    View full-size slide

  12. BONJOUR ANDROID, IT’S ZEROCONF
    ▸ Configure netmask
    ▸ Allocate unique IP address
    IP INTERFACE CONFIGURATION

    View full-size slide

  13. 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

    View full-size slide

  14. 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

    View full-size slide

  15. 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

    View full-size slide

  16. 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

    View full-size slide

  17. BONJOUR ANDROID, IT’S ZEROCONF
    WHAT ABOUT ANDROID?
    ▸ API FROM 4.1
    ▸ JMDNS FOR JAVA
    ▸ APPLE NATIVE (C++) IMPLEMENTATION

    View full-size slide

  18. 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

    View full-size slide

  19. BONJOUR ANDROID, IT’S ZEROCONF
    WHAT ABOUT ANDROID?
    ▸ API FROM 4.1
    ▸ JMDNS FOR JAVA
    ▸ APPLE NATIVE (C++) IMPLEMENTATION

    View full-size slide

  20. 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

    View full-size slide

  21. BONJOUR ANDROID, IT’S ZEROCONF
    WHAT ABOUT ANDROID?
    ▸ API FROM 4.1
    ▸ JMDNS FOR JAVA
    ▸ APPLE NATIVE (C++) IMPLEMENTATION

    View full-size slide

  22. 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

    View full-size slide

  23. 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!

    View full-size slide

  24. 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();

    View full-size slide

  25. 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();

    View full-size slide

  26. 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);
    }};

    View full-size slide

  27. 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);
    }};

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  30. ARE THERE BETTER
    WAYS OF DOING IT?

    View full-size slide

  31. YES, THERE ARE!
    ARE THERE BETTER WAYS OF DOING IT

    View full-size slide

  32. 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

    View full-size slide

  33. Q & A
    CHECK THE REPO @ GITHUB.COM/ENNOVA-IT/DROIDCON-DEMO

    View full-size slide

  34. BONJOUR ANDROID, IT’S ZEROCONF
    THANKS FOR WATCHING!

    View full-size slide