Skip to content

Commit

Permalink
feat: Now generating constants for platform implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Nov 30, 2023
1 parent 814d84f commit 3b0834d
Show file tree
Hide file tree
Showing 30 changed files with 717 additions and 186 deletions.
Expand Up @@ -20,6 +20,7 @@ import io.flutter.plugin.common.EventChannel
abstract class BonsoirAction(
protected val id: Int,
private val action: String,
protected val logMessages: Map<String, String>,
private val printLogs: Boolean,
private val onDispose: Runnable,
protected val nsdManager: NsdManager,
Expand Down Expand Up @@ -67,12 +68,18 @@ abstract class BonsoirAction(
* Triggered when a success occurs.
*
* @param eventId The event id.
* @param message The message.
* @param service The service involved.
* @param message The message.
* @param parameters The message parameters.
*/
fun onSuccess(eventId: String, message: String? = null, service: BonsoirService? = null) {
if (message != null) {
log(message)
fun onSuccess(eventId: String, service: BonsoirService? = null, message: String? = null, parameters: List<Any> = emptyList()) {
val logMessage = message ?: logMessages[eventId]
val logParameters = ArrayList(parameters)
if (service != null && !parameters.contains(service)) {
logParameters.add(0, service)
}
if (logMessage != null) {
log(logMessage, logParameters)
}
Handler(Looper.getMainLooper()).post {
eventSink?.success(SuccessObject(eventId, service).toJson())
Expand All @@ -83,12 +90,14 @@ abstract class BonsoirAction(
* Triggered when an error occurs.
*
* @param message The message.
* @param parameters The message parameters.
* @param details The error details.
*/
fun onError(message: String, details: Any? = null) {
log(message)
fun onError(message: String? = null, parameters: List<Any>, details: Any? = null) {
val logMessage = format(message ?: logMessages["${action}Error"]!!, parameters)
log(logMessage)
Handler(Looper.getMainLooper()).post {
eventSink?.error("${action}Error", message, details)
eventSink?.error("${action}Error", logMessage, details)
}
}

Expand Down Expand Up @@ -131,9 +140,23 @@ abstract class BonsoirAction(
*
* @param message The message.
*/
fun log(message: String) {
fun log(message: String, parameters: List<Any> = emptyList()) {
if (printLogs) {
Log.d(tag, "[$id] $message")
Log.d(tag, "[$action] [$id] ${format(message, parameters)}")
}
}

/**
* Formats a given message with the given parameters.
*
* @param message The message.
* @param parameters The parameters.
*/
private fun format(message: String, parameters: List<Any>): String {
var result: String = message
for (parameter in parameters) {
result = result.replaceFirst("%s", parameter.toString())
}
return result;
}
}
@@ -0,0 +1,82 @@
package fr.skyost.bonsoir

/*
* Generated file that contains some variables used by the platform channel.
*/
class Generated {
companion object {
/**
* Contains the broadcast messages.
*/
public final var broadcastMessages: Map<String, String> = mapOf("broadcastStarted" to "Bonsoir service broadcast started : %s.", "broadcastNameAlreadyExists" to "Trying to broadcast a service with a name that already exists : %s (old name was %s).", "broadcastStopped" to "Bonsoir service broadcast stopped : %s.", "broadcastInitialized" to "Bonsoir service broadcast initialized : %s.", "broadcastError" to "Bonsoir service failed to broadcast : %s (error : %s).")

/**
* broadcastStarted
*/
public final const val broadcastStarted = "broadcastStarted";
/**
* broadcastNameAlreadyExists
*/
public final const val broadcastNameAlreadyExists = "broadcastNameAlreadyExists";
/**
* broadcastStopped
*/
public final const val broadcastStopped = "broadcastStopped";
/**
* broadcastInitialized
*/
public final const val broadcastInitialized = "broadcastInitialized";
/**
* broadcastError
*/
public final const val broadcastError = "broadcastError";


/**
* Contains the discovery messages.
*/
public final var discoveryMessages: Map<String, String> = mapOf("discoveryStarted" to "Bonsoir discovery started : %s.", "discoveryServiceFound" to "Bonsoir has found a service : %s.", "discoveryServiceResolved" to "Bonsoir has resolved a service : %s.", "discoveryServiceResolveFailed" to "Bonsoir has failed to resolve a service : %s (error : %s).", "discoveryServiceLost" to "A Bonsoir service has been lost : %s.", "discoveryStopped" to "Bonsoir discovery stopped : %s.", "discoveryUndiscoveredServiceResolveFailed" to "Trying to resolve an undiscovered service : %s of type %s.", "discoveryTxtResolved" to "Bonsoir has found the attributes of a service : %s (new attributes are : %s).", "discoveryTxtResolveFailed" to "Bonsoir has failed to get the TXT record of a service : %s (error %s).", "discoveryError" to "Bonsoir has encountered an error during discovery : %s (error %s).")

/**
* discoveryStarted
*/
public final const val discoveryStarted = "discoveryStarted";
/**
* discoveryServiceFound
*/
public final const val discoveryServiceFound = "discoveryServiceFound";
/**
* discoveryServiceResolved
*/
public final const val discoveryServiceResolved = "discoveryServiceResolved";
/**
* discoveryServiceResolveFailed
*/
public final const val discoveryServiceResolveFailed = "discoveryServiceResolveFailed";
/**
* discoveryServiceLost
*/
public final const val discoveryServiceLost = "discoveryServiceLost";
/**
* discoveryStopped
*/
public final const val discoveryStopped = "discoveryStopped";
/**
* discoveryUndiscoveredServiceResolveFailed
*/
public final const val discoveryUndiscoveredServiceResolveFailed = "discoveryUndiscoveredServiceResolveFailed";
/**
* discoveryTxtResolved
*/
public final const val discoveryTxtResolved = "discoveryTxtResolved";
/**
* discoveryTxtResolveFailed
*/
public final const val discoveryTxtResolveFailed = "discoveryTxtResolveFailed";
/**
* discoveryError
*/
public final const val discoveryError = "discoveryError";

}
}
Expand Up @@ -4,6 +4,7 @@ import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import fr.skyost.bonsoir.BonsoirAction
import fr.skyost.bonsoir.BonsoirService
import fr.skyost.bonsoir.Generated
import io.flutter.plugin.common.BinaryMessenger

/**
Expand All @@ -26,6 +27,7 @@ class BonsoirServiceBroadcast(
) : BonsoirAction(
id,
"broadcast",
Generated.broadcastMessages,
printLogs,
onDispose,
nsdManager,
Expand All @@ -46,25 +48,25 @@ class BonsoirServiceBroadcast(
if (this.service.name != service.serviceName) {
val oldName = this.service.name
this.service.name = service.serviceName
onSuccess("broadcastNameAlreadyExists", "Trying to broadcast a service with a name that already exists : ${this.service} (old name was ${oldName})", this.service)
onSuccess(Generated.broadcastNameAlreadyExists, this.service, parameters = listOf(oldName))
}
onSuccess("broadcastStarted", "Bonsoir service registered : ${this.service}", this.service)
onSuccess(Generated.broadcastStarted, this.service)
}

override fun onRegistrationFailed(service: NsdServiceInfo, errorCode: Int) {
onError("Bonsoir service registration failed : ${this.service}, error code : $errorCode", errorCode)
onError(parameters = listOf(this.service, errorCode), details = errorCode)
dispose()
}

override fun onServiceUnregistered(service: NsdServiceInfo) {
val wasActive = isActive
makeUnactive()
onSuccess("broadcastStopped", "Bonsoir service broadcast stopped : ${this.service}", this.service)
onSuccess(Generated.broadcastStopped, this.service)
dispose(wasActive)
}

override fun onUnregistrationFailed(service: NsdServiceInfo, errorCode: Int) {
onError("Bonsoir service unregistration failed : ${this.service}, error code : $errorCode", errorCode)
onError("Bonsoir service unregistration failed : %s (error : %s).", listOf(this.service, errorCode), errorCode)
}

override fun stop() {
Expand Down
Expand Up @@ -4,6 +4,7 @@ import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import fr.skyost.bonsoir.BonsoirAction
import fr.skyost.bonsoir.BonsoirService
import fr.skyost.bonsoir.Generated
import io.flutter.plugin.common.BinaryMessenger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -33,6 +34,7 @@ class BonsoirServiceDiscovery(
) : BonsoirAction(
id,
"discovery",
Generated.discoveryMessages,
printLogs,
onDispose,
nsdManager,
Expand Down Expand Up @@ -95,42 +97,42 @@ class BonsoirServiceDiscovery(

override fun onDiscoveryStarted(regType: String) {
makeActive()
onSuccess("discoveryStarted", "Bonsoir discovery started : $regType")
onSuccess(Generated.discoveryStarted, parameters = listOf(regType))
}

override fun onStartDiscoveryFailed(serviceType: String, errorCode: Int) {
onError("Bonsoir failed to start discovery : $errorCode", errorCode)
onError(parameters = listOf(serviceType, errorCode), details = errorCode)
dispose(true)
}

override fun onServiceFound(service: NsdServiceInfo) {
var bonsoirService = findService(service)
var bonsoirService = findService(service)
if (bonsoirService != null) {
return
}
bonsoirService = BonsoirService(service)
services.add(bonsoirService)
onSuccess("discoveryServiceFound", "Bonsoir has found a service : $bonsoirService", bonsoirService)
onSuccess(Generated.discoveryServiceFound, bonsoirService)
queryTXTRecord(bonsoirService)
}

override fun onServiceLost(service: NsdServiceInfo) {
val bonsoirService = findService(service)
val bonsoirService = findService(service)
if (bonsoirService != null) {
services.remove(bonsoirService)
onSuccess("discoveryServiceLost", "A Bonsoir service has been lost : $bonsoirService", bonsoirService)
onSuccess(Generated.discoveryServiceLost, bonsoirService)
}
}

override fun onDiscoveryStopped(serviceType: String) {
val wasActive = isActive
makeUnactive()
onSuccess("discoveryStopped", "Bonsoir discovery stopped : $serviceType")
onSuccess(Generated.discoveryStopped, parameters = listOf(serviceType))
dispose(wasActive)
}

override fun onStopDiscoveryFailed(serviceType: String, errorCode: Int) {
onError("Bonsoir has encountered an error while stopping the discovery : $errorCode", errorCode)
onError("Bonsoir has encountered an error while stopping the discovery : %s (error : %s).", listOf(type, errorCode), errorCode)
}

/**
Expand All @@ -142,9 +144,9 @@ class BonsoirServiceDiscovery(
launch {
val data = TXTRecord.resolveTXTRecord(service)
if (data == null) {
onServiceTXTRecordNotFound(service)
onServiceTxtRecordNotFound(service)
} else {
onServiceTXTRecordFound(service, data)
onServiceTxtRecordFound(service, data)
}
}
}
Expand All @@ -155,12 +157,12 @@ class BonsoirServiceDiscovery(
* @param service The Bonsoir service instance.
* @param txtRecord The TXT record data instance.
*/
private fun onServiceTXTRecordFound(service: BonsoirService, txtRecord: TXTRecordData) {
private fun onServiceTxtRecordFound(service: BonsoirService, txtRecord: TXTRecordData) {
if (service.attributes != txtRecord.dictionary) {
log("Bonsoir has found the attributes of a service : $service (new attributes are ${txtRecord.dictionary})")
onSuccess(eventId = "discoveryServiceLost", service = service)
log(logMessages[Generated.discoveryTxtResolved]!!, listOf(service, txtRecord.dictionary))
onSuccess(Generated.discoveryServiceLost, service)
service.attributes = txtRecord.dictionary
onSuccess(eventId = "discoveryServiceFound", service = service)
onSuccess(Generated.discoveryServiceFound, service)
}
}

Expand All @@ -169,8 +171,8 @@ class BonsoirServiceDiscovery(
*
* @param service The Bonsoir service instance.
*/
private fun onServiceTXTRecordNotFound(service: BonsoirService) {
log("Bonsoir has failed to get the TXT record of a service : $service")
private fun onServiceTxtRecordNotFound(service: BonsoirService) {
log(logMessages[Generated.discoveryTxtResolveFailed]!!, listOf(service))
}

/**
Expand All @@ -179,21 +181,21 @@ class BonsoirServiceDiscovery(
fun resolveService(name: String, type: String) {
val bonsoirService = findService(name, type)
if (bonsoirService == null) {
onError("Trying to resolve an undiscovered service : $name", name)
onError(logMessages[Generated.discoveryUndiscoveredServiceResolveFailed]!!, listOf(name, type))
return
}
val listener = BonsoirDiscoveryResolveListener(
id,
{ _: NsdServiceInfo, errorCode: Int ->
onSuccess("discoveryServiceResolveFailed", "Bonsoir has failed to resolve a service : $errorCode", bonsoirService)
onSuccess(Generated.discoveryServiceResolveFailed, bonsoirService, parameters = listOf(errorCode))
resolveNextInQueue()
},
{ resolvedService: NsdServiceInfo ->
val resolvedBonsoirService = BonsoirService(resolvedService)
bonsoirService.host = resolvedBonsoirService.host
bonsoirService.port = resolvedBonsoirService.port
bonsoirService.attributes = resolvedBonsoirService.attributes
onSuccess("discoveryServiceResolved", "Bonsoir has resolved a service : $bonsoirService", bonsoirService)
onSuccess(Generated.discoveryServiceResolved, bonsoirService)
resolveNextInQueue()
},
)
Expand Down
2 changes: 1 addition & 1 deletion packages/bonsoir_darwin/darwin/Classes/BonsoirAction.swift
Expand Up @@ -75,7 +75,7 @@ class BonsoirAction: NSObject, FlutterStreamHandler {
#if canImport(os)
os_log("[%d] %s", log: OSLog(subsystem: SwiftBonsoirPlugin.package, category: action), type: OSLogType.info, id, message)
#else
NSLog("\n[\(id)] \(message)")
NSLog("\n[\(action)] [\(id)] \(message)")
#endif
}
}
Expand Down
43 changes: 43 additions & 0 deletions packages/bonsoir_darwin/darwin/Classes/Generated.swift
@@ -0,0 +1,43 @@
/// Generated file that contains some variables used by the platform channel.

class Generated {
/// Contains the broadcast messages.
static let broadcastMessages: [String: String] = ["broadcastStarted": "Bonsoir service broadcast started : %s.", "broadcastNameAlreadyExists": "Trying to broadcast a service with a name that already exists : %s (old name was %s).", "broadcastStopped": "Bonsoir service broadcast stopped : %s.", "broadcastInitialized": "Bonsoir service broadcast initialized : %s.", "broadcastError": "Bonsoir service failed to broadcast : %s (error : %s)."]

/// broadcastStarted
static let broadcastStarted: String = "broadcastStarted"
/// broadcastNameAlreadyExists
static let broadcastNameAlreadyExists: String = "broadcastNameAlreadyExists"
/// broadcastStopped
static let broadcastStopped: String = "broadcastStopped"
/// broadcastInitialized
static let broadcastInitialized: String = "broadcastInitialized"
/// broadcastError
static let broadcastError: String = "broadcastError"


/// Contains the discovery messages.
static let discoveryMessages: [String: String] = ["discoveryStarted": "Bonsoir discovery started : %s.", "discoveryServiceFound": "Bonsoir has found a service : %s.", "discoveryServiceResolved": "Bonsoir has resolved a service : %s.", "discoveryServiceResolveFailed": "Bonsoir has failed to resolve a service : %s (error : %s).", "discoveryServiceLost": "A Bonsoir service has been lost : %s.", "discoveryStopped": "Bonsoir discovery stopped : %s.", "discoveryUndiscoveredServiceResolveFailed": "Trying to resolve an undiscovered service : %s of type %s.", "discoveryTxtResolved": "Bonsoir has found the attributes of a service : %s (new attributes are : %s).", "discoveryTxtResolveFailed": "Bonsoir has failed to get the TXT record of a service : %s (error %s).", "discoveryError": "Bonsoir has encountered an error during discovery : %s (error %s)."]

/// discoveryStarted
static let discoveryStarted: String = "discoveryStarted"
/// discoveryServiceFound
static let discoveryServiceFound: String = "discoveryServiceFound"
/// discoveryServiceResolved
static let discoveryServiceResolved: String = "discoveryServiceResolved"
/// discoveryServiceResolveFailed
static let discoveryServiceResolveFailed: String = "discoveryServiceResolveFailed"
/// discoveryServiceLost
static let discoveryServiceLost: String = "discoveryServiceLost"
/// discoveryStopped
static let discoveryStopped: String = "discoveryStopped"
/// discoveryUndiscoveredServiceResolveFailed
static let discoveryUndiscoveredServiceResolveFailed: String = "discoveryUndiscoveredServiceResolveFailed"
/// discoveryTxtResolved
static let discoveryTxtResolved: String = "discoveryTxtResolved"
/// discoveryTxtResolveFailed
static let discoveryTxtResolveFailed: String = "discoveryTxtResolveFailed"
/// discoveryError
static let discoveryError: String = "discoveryError"

}

0 comments on commit 3b0834d

Please sign in to comment.