Skip to content

A highly modular network service discovery and broadcast library that utilizes Kotlin's coroutines

License

Notifications You must be signed in to change notification settings

mrmitew/coucou_android

Repository files navigation

Coucou (Android)

A highly modular network service discovery and broadcast library that utilizes Kotlin's coroutines. For iOS, go to coucou_ios repo.

Coucou API

suspend fun startDiscovery(type: String, action: suspend (DiscoveryEvent) -> Unit)
suspend fun startBroadcast(config: BroadcastConfig): Disposable?

How to use

Creation

val coucou = Coucou.create {
    platform { AndroidPlatform(applicationContext) }
    driver { NsdManagerDriver(applicationContext) }
    logger { AndroidLogger() }
}

Network Service Discovery

try {
    coucou.startDiscovery(type = "_http._tcp.") { event ->
        when(event) {
            is DiscoveryEvent.Failure -> {
                // TODO: something with event.cause
            }
            is DiscoveryEvent.ServiceResolved -> {
                // TODO: something with event.service
            }
            is DiscoveryEvent.ServiceLost -> {
                // TODO: something with event.service
            }
        }
    }
} catch (e: Exception) {
    // TODO
}

To cancel discovery, just call the cancel() on the coroutine's job. You can obtain it from the coroutine builder or from the coroutine's context.

Network Service Broadcast

val disposable = coucou.startBroadcast(config = BroadcastConfig(type = "_http._tcp.", name = "Coucou", port = 8080)) 
// .... whenever the broadcast isn't needed, dispose
disposable?.dispose()
  • Note: the discovery and broadcast have to be executed in suspending functions or within coroutines. Please see the sample app for a better example.

Installation

Gradle

Modify your root build.gradle to look like so:

allprojects {
    repositories {
        // other repos here
        maven { url 'https://jitpack.io' }
    }
}

Then add the libray dependency to your app's build.gradle

implementation 'com.github.mrmitew:coucou_android:<ENTER_COUCOU_VERSION_HERE>'

Done.

To do

  • Write unit tests
  • Complete sample
  • More documentation
  • Improve the README

Credits

Inspiration for this library and software architecture was taken from RxBonjour by Marcel Schnelle.

About

A highly modular network service discovery and broadcast library that utilizes Kotlin's coroutines

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages