Use new server.kt listener methods
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.shr4pnel.ferretirc
|
package com.shr4pnel.ferretirc
|
||||||
|
|
||||||
|
import com.shr4pnel.ferretirc.irc.IRCChannel
|
||||||
import com.shr4pnel.ferretirc.irc.Server
|
import com.shr4pnel.ferretirc.irc.Server
|
||||||
import com.shr4pnel.ferretirc.irc.User
|
import com.shr4pnel.ferretirc.irc.User
|
||||||
import com.shr4pnel.ferretirc.net.Connection
|
import com.shr4pnel.ferretirc.net.Connection
|
||||||
@@ -17,13 +18,13 @@ import org.slf4j.helpers.NOPLogger
|
|||||||
class IrcClient(
|
class IrcClient(
|
||||||
hostname: String,
|
hostname: String,
|
||||||
port: Int,
|
port: Int,
|
||||||
var enableLogging: Boolean = false,
|
val enableLogging: Boolean = false,
|
||||||
) {
|
) {
|
||||||
init {
|
init {
|
||||||
KotlinLoggingConfiguration.logStartupMessage = false
|
KotlinLoggingConfiguration.logStartupMessage = false
|
||||||
}
|
}
|
||||||
|
|
||||||
private val logger = if (enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER)
|
// private val logger = if (enableLogging) KotlinLogging.logger {} else KotlinLogging.logger(NOPLogger.NOP_LOGGER)
|
||||||
val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||||
|
|
||||||
lateinit var server: Server
|
lateinit var server: Server
|
||||||
@@ -43,14 +44,15 @@ class IrcClient(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a message to the IRC server writer
|
* Send a message to the IRC server message writer
|
||||||
* This is asynchronous and ordered
|
|
||||||
* @param message The client message to send to the server
|
* @param message The client message to send to the server
|
||||||
*/
|
*/
|
||||||
suspend fun queueMessage(message: ClientMessage) {
|
suspend fun queueMessage(message: ClientMessage) {
|
||||||
connection.writer.outgoingMessages.send(message)
|
connection.writer.outgoingMessages.send(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun join(channel: IRCChannel) = queueMessage(ClientMessage.Join(channel))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a message to the IRC server writer
|
* Send a message to the IRC server writer
|
||||||
* This is asynchronous and ordered
|
* This is asynchronous and ordered
|
||||||
@@ -85,7 +87,7 @@ class IrcClient(
|
|||||||
* @param predicate A function to filter a message based on its parameters
|
* @param predicate A function to filter a message based on its parameters
|
||||||
* @see com.shr4pnel.ferretirc.IrcClient.waitForNext
|
* @see com.shr4pnel.ferretirc.IrcClient.waitForNext
|
||||||
*/
|
*/
|
||||||
suspend inline fun <reified T : ServerMessage> queueAndWaitForNext(
|
suspend inline fun <reified T : ServerMessage> queueMessageAndWaitForNext(
|
||||||
message: ClientMessage,
|
message: ClientMessage,
|
||||||
noinline predicate: (T) -> Boolean = { true },
|
noinline predicate: (T) -> Boolean = { true },
|
||||||
): T = server.waitForNext(T::class, predicate) { queueMessage(message) }
|
): T = server.waitForNext(T::class, predicate) { queueMessage(message) }
|
||||||
@@ -96,10 +98,10 @@ class IrcClient(
|
|||||||
* @param action A function to execute after message buffer flow collection has begun
|
* @param action A function to execute after message buffer flow collection has begun
|
||||||
* @see com.shr4pnel.ferretirc.IrcClient.waitForNext
|
* @see com.shr4pnel.ferretirc.IrcClient.waitForNext
|
||||||
*/
|
*/
|
||||||
suspend inline fun <reified T : ServerMessage> waitForNextAfterAction(
|
suspend inline fun <reified T : ServerMessage> waitForNextMessageAfterAction(
|
||||||
noinline predicate: (T) -> Boolean = { true },
|
noinline predicate: (T) -> Boolean = { true },
|
||||||
noinline action: suspend () -> Unit,
|
noinline action: suspend () -> Unit,
|
||||||
): T = server.waitForNext(T::class, predicate, action)
|
): T = server.waitForNext(T::class, predicate) { action() }
|
||||||
|
|
||||||
suspend fun register(
|
suspend fun register(
|
||||||
nick: String,
|
nick: String,
|
||||||
@@ -119,6 +121,6 @@ class IrcClient(
|
|||||||
}
|
}
|
||||||
add(ClientMessage.Cap.END())
|
add(ClientMessage.Cap.END())
|
||||||
}.toTypedArray()
|
}.toTypedArray()
|
||||||
queueMessages(*messages)
|
waitForNextMessageAfterAction<ServerMessage.Numeric.RPL_WELCOME> { queueMessages(*messages) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user