RxBonjour


Source link: https://github.com/mannodermaus/RxBonjour

RxBonjour

A reactive wrapper around network service discovery functionalities for Kotlin and Java.

Download

RxBonjour 2 is available on jcenter() and consists of three distinct components, all of which are detailed below.

// Always include this implementation "de.mannodermaus.rxjava2:rxbonjour:2.0.0-RC1"  // Example: Usage on Android with JmDNS implementation "de.mannodermaus.rxjava2:rxbonjour-platform-android:2.0.0-RC1" implementation "de.mannodermaus.rxjava2:rxbonjour-driver-jmdns:2.0.0-RC1"

For the (less flexible & Android-only) RxJava 1 version, have a look at the 1.x branch.

Components

RxBonjour 2 is composed of a core library, a Platform to run on, and a Driver to access the NSD stack.

Core Library (rxbonjour)

The main entry point to the API, RxBonjour is contained in this library. All other libraries depend on this common core.

Platform Library (rxbonjour-platform-xxx)

Provides access to the host device's IP and Network controls. During the creation of your RxBonjour instance, you attach exactly 1 implementation of the Platform interface.

Below is a list of available Platform libraries supported by RxBonjour 2:

Group Artifact Description
de.mannodermaus.rxjava2 rxbonjour-platform-android Android-aware Platform, utilizing WifiManager APIs
de.mannodermaus.rxjava2 rxbonjour-platform-desktop Default JVM Platform

About the AndroidPlatform

When running on Android, the rxbonjour-platform-android has to be applied to the module. Doing so will add the following permissions to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

Driver Library (rxbonjour-driver-xxx)

Provides the connection to a Network Service Discovery stack. During the creation of your RxBonjour instance, you attach exactly 1 implementation of the Driver interface.

Below is a list of available Driver libraries supported by RxBonjour 2:

Group Artifact Description
de.mannodermaus.rxjava2 rxbonjour-driver-jmdns Service Discovery with JmDNS
de.mannodermaus.rxjava2 rxbonjour-driver-nsdmanager Service Discovery with Android's NsdManager APIs

Usage

Creation

Configure a RxBonjour service object using its Builder, attaching your desired Platform and Driver implementations. If you forget to provide either dependency, an Exception will be thrown:

val rxBonjour = RxBonjour.Builder()
  .platform(AndroidPlatform.create(this))
  .driver(JmDNSDriver.create())
  .create()

Your RxBonjour is ready for use now!

Discovery

Create a network service discovery request using RxBonjour#newDiscovery(String):

val disposable = rxBonjour.newDiscovery("_http._tcp")
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(

{
 event ->

 when(event) {

  is BonjourEvent.Added -> println("Resolved Service: ${
event.service
}
")

  is BonjourEvent.Removed -> println("Lost Service: ${
event.service
}
")

 
}

}
,

{
 error -> println("Error during Discovery: ${
error.message
}
") 
}

  )

Make sure to off-load this work onto a background thread, since the library won't enforce any threading. In this example, RxAndroid is utilized to return the events back to Android's main thread.

Registration

Configure your advertised service & start the broadcast using RxBonjour#newBroadcast(BonjourBroadcastConfig). The only required property to set on a BonjourBroadcastConfig is its Bonjour type, the remaining parameters are filled with defaults as stated in the comments below:

val broadcastConfig = BonjourBroadcastConfig(

type = "_http._tcp",

name = "My Bonjour Service",

  // default: "RxBonjour Service"

address = null,

// default: Fallback to WiFi address provided by Platform

port = 13337,

  // default: 80

txtRecords = mapOf(

  // default: Empty Map

  "my.record" to "my value",

  "other.record" to "0815"))

val disposable = rxBonjour.newBroadcast(broadcastConfig)

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe()

The broadcast is valid until the returned Completable is unsubscribed from. Again, make sure to off-load this work onto a background thread like above, since the library won't do it for you.

License

Copyright 2017 Marcel Schnelle  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

IntelliJ plugin for adding Fragment newInstance Inspection.

IntelliJ plugin for adding Android createIntent Inspection.

This plugin shows a confirmation dialog before executing gradle tasks.

JJWT aims to be the easiest to use and understand library for creating and verifying JSON Web Tokens (JWTs).

This project provide Coloring Loading View for Android.

A dialog fragment with date pickers which can be used to select a time range.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes