39 lines
1.2 KiB
Kotlin
39 lines
1.2 KiB
Kotlin
package com.shr4pnel.ferretirc
|
|
|
|
import com.shr4pnel.ferretirc.net.ClientMessage
|
|
import com.shr4pnel.ferretirc.net.Connection
|
|
import com.shr4pnel.ferretirc.net.MessageReader
|
|
import com.shr4pnel.ferretirc.net.MessageWriter
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.SupervisorJob
|
|
import kotlinx.coroutines.cancel
|
|
import kotlinx.coroutines.channels.Channel
|
|
import kotlinx.coroutines.launch
|
|
import java.net.URL
|
|
|
|
class IrcClient(val nick: String, val sock: URL) {
|
|
val port: Int = sock.port
|
|
val hostname: String = sock.host
|
|
val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
|
private val connection = Connection(hostname, port, scope)
|
|
lateinit var messages: Channel<String>
|
|
|
|
init {
|
|
require(port != -1, fun() = "A port must be specified in your argument to IRCClient")
|
|
require(hostname.isNotEmpty(), fun() = "A port must be specified in your argument to IRCClient")
|
|
}
|
|
|
|
suspend fun connect() {
|
|
connection.connect()
|
|
messages = connection.reader.incomingMessages
|
|
}
|
|
|
|
fun queueMessage(message: ClientMessage) = scope.launch {
|
|
connection.writer.outgoingMessages.send(message)
|
|
}
|
|
|
|
fun close() {
|
|
scope.cancel()
|
|
}
|
|
} |