summaryrefslogtreecommitdiff
path: root/src/main/kotlin/Main.kt
blob: 577c3fbccc7d4f390167dd4f5f37c31e73dab344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking
import java.net.ServerSocket

object Main {

    @JvmStatic
    fun main(args: Array<String>) {
        if (args.size != 1) {
            System.err.println("Use ./javamailteste run/install")
        }
        when (args[0]) {
            "run" -> runServer(2500)
        }
    }

    fun runServer(port: Int) = runBlocking(Dispatchers.Default) {
        val ss = ServerSocket(port)
        val jobs = mutableListOf<Job>()
        println("Starting SMTP socket on port $port")
        while (true) {
            val scope = CoroutineScope(Dispatchers.Default)
            val socket = with(Dispatchers.IO) { ss.accept() }
            val prot = SMTPReceiveProtocol("nea89.moe", socket.inetAddress)
            jobs.add(prot.executeAsync(scope, Protocol.IO.FromSocket(socket)))
            println("jobs: $jobs")
        }
    }
}