package org.opendc.common.utils import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromStream import java.io.File import java.io.IOException import java.io.InputStream import java.io.OutputStream import java.net.Socket import java.sql.Connection import java.sql.DriverManager import java.sql.SQLException /** * @property name * @property backlog the amount of connections to accept * @property databasePath * @property address IPv4 address * @property port */ @Serializable public data class Config( val name: String = "", var backlog: Int = 0, val address: String = "", val port: Int = 8080, val postgresql: Int = 5342, val username : String = "", val password : String = "", val database: String = "" ){ public companion object{ public var input: InputStream? = null public var output: OutputStream? = null public var connection : Connection? = null public var socket: Socket? = null public fun setConfigSocket(socket: Socket?){ this.socket = socket try { input = socket?.getInputStream() output = socket?.getOutputStream() } catch (e: IOException){ print("${e.message}") } } public fun getConfigReader() : InputStream? { return input } public fun getConfigWriter() : OutputStream? { return output } } } /** * Reads `config.json` into Config data class. */ public class ConfigReader { private val jsonReader = Json public fun read(file: File): Config = read(file.inputStream()) @OptIn(ExperimentalSerializationApi::class) public fun read(input: InputStream): Config { return jsonReader.decodeFromStream(input) } }