Bonjour for Java

6,032 views
5,344 views

Published on

How to use Bonjour in Java
The slides show you how to create bonjour based services on server site and use them on client site. All examples are completely written in Java.

Published in: Technology, Education

Bonjour for Java

  1. 1. Port Example @hendrikEbbers www.guigarage.com hendrik.ebbers@web.de Bonjour Don‘t be afraid no French today
  2. 2. Content • ZeroConf & Bonjour • AirPlay • Java API • Demos keep your fingers crossed for me
  3. 3. HomeAutomationModule Welcome, please configure your Home Automation Server: IP-Adress Port ok Example Spooky, isn‘t it? ANTIPATTERN!!
  4. 4. HomeAutomationModule Hi, please choose your Home Automation Server: ok HA Server living room (12.0.0.34:9001) My Server pool house (12.0.0.31:9001) This is how it goes Cool, isn‘t it?
  5. 5. ZeroConf • methodology and set of special technologies • automatically create a usable computer network • assign numeric network addresses to devices • distribution and resolution of hostnames • location of network services
  6. 6. Bonjour • Based on ZeroConf • provides a general method to discover services on a local area network • Used in many tools & applications • Open Source postgreSQL, Adobe Creative Suite, Skype ...
  7. 7. Airplay • Default Services based on Bonjour • Stream Music, Movies & Photos from iOS to AppleTV • No configuration needed Find this icon on your iDevice User has time for other stuff
  8. 8. Defining a Service • Each service has a type that is defined by a namespace • „_servicetype._tcp.local.“ • Each service has a name • Each service has a host and a port • Services can have descriptions and other properties
  9. 9. • iOS Devices can stream Photos to AppleTV • Communication is based on Bonjour & REST • Find AirPlay Server with Bonjour • Send pictures with REST call Stream Photos REST call
  10. 10. Stream Music • iOS Devices can stream Music to AppleTV • Same Workflow as with pictures • _airplay._tcp.local. • Communication is completely based on REST default service type for Airplay ... and documented in the web
  11. 11. JmDNS • Java implementation of multi-cast DNS • fully compatible with Apple's Bonjour • provide simple ways for services to register themselves • discover multicast DNS based services http://jmdns.sourceforge.net
  12. 12. Listen to a Service JmDNS jmDNS = JmDNS.create(); jmDNS.addServiceListener("name._tcp.local.", new ServiceListener() { @Override ! ! ! public void serviceAdded(ServiceEvent event) {...} ! ! ! @Override ! ! ! public void serviceResolved(ServiceEvent event) {...} ! ! ! @Override ! ! ! public void serviceRemoved(ServiceEvent event) {...} } ); create Context listener concept lifecylce wraps all infos
  13. 13. Listen to a Service @Override public void serviceResolved(ServiceEvent event) { ! System.out.println("Type:" + event.getType()); ! System.out.println("Name:" + event.getName()); ! ! ! ! ! System.out.println("IP:" + event.getInfo().getHostAddress()); ! System.out.println("Port:" + event.getInfo().getPort()); ! System.out.println("Name:" + event.getInfo().getName()); ! System.out.println("Desc:" + event.getInfo().getNiceTextString()); ! System.out.println("Entry Point:" + event.getInfo().getURL()); } optional properties
  14. 14. Create a Service JmDNS jmDNS = JmDNS.create(); ServiceInfo info = ServiceInfo.create("_demo1._tcp.local.", "Demo 1", 9090, "I‘m a demo"); jmDNS.registerService(info); create Context register on network type, name, port, description
  15. 15. Example //Server RestServer server = new RestServer(); server.startMp3Player(„/play“, 9090); JmDNS jmDNS = JmDNS.create(); ServiceInfo info = ServiceInfo.create("playmp3._tcp.local.", "MP3 Player", 9090, "play a mp3 file"); jmDNS.registerService(info); //Client jmDNS.addServiceListener("playmp3._tcp.local.", new ServiceListener() { @Override ! ! ! public void serviceAdded(ServiceEvent event) { RestClient.play(„tuut.mp3“, event, „/play“); } }); create REST Server create DNS Service REST Call
  16. 16. REST • Implement your services as REST WS • Client only needs to know the URL path • I usedVert.x You can use any REST API Cause it`s cool
  17. 17. Port Example Demo Time
  18. 18. Port Example Thanks for watching @hendrikEbbers www.guigarage.com hendrik.ebbers@web.de Thanks for watching

×