From 86d35fcec83057e346e4982b5a6908f25342a392 Mon Sep 17 00:00:00 2001 From: mjkwiatkowski Date: Sun, 15 Feb 2026 18:26:59 +0100 Subject: feat: opendc -> postgresql connection works --- .../kotlin/org/opendc/common/utils/ConfigParser.kt | 72 ++++++++++++++++++++++ .../kotlin/org/opendc/common/utils/PostgresqlDB.kt | 56 +++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt create mode 100644 opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt (limited to 'opendc-common/src/main/kotlin/org/opendc/common/utils') diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt new file mode 100644 index 00000000..cb9623bb --- /dev/null +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt @@ -0,0 +1,72 @@ +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) + } +} diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt new file mode 100644 index 00000000..3dc7a0e4 --- /dev/null +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt @@ -0,0 +1,56 @@ +package org.opendc.common.utils + +import java.sql.Connection +import java.sql.DriverManager +import java.sql.SQLException + +public class PostgresqlDB { + public var connection : Connection? = null + public var dbUrl : String = "" + public var user : String = "" + public var password : String = "" + + public fun setupDatabase(address : String, port : Int, dbUser : String, dbPassword : String, db : String){ + dbUrl = "jdbc:postgresql://$address:$port/$db" + user = dbUser + password = dbPassword + println(dbUrl) + try { + connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword) + } catch (e: SQLException) { + print("${e.message}") + } + } + + public fun create(){ + val CREATE_TABLE = """ + CREATE TABLE metrics ( + id SERIAL PRIMARY KEY, + timestamp bigint, + tasksActive integer, + clusterName varchar(10)); + """.trimIndent() + + try { + val conn = DriverManager.getConnection(dbUrl, user, password) + val st = conn.createStatement() + st.executeQuery(CREATE_TABLE) + } catch (e: SQLException){ + println("${e.message}") + } + } + + public fun clear(){ + val DELETE_ALL_TABLES = """ + DROP SCHEMA public CASCADE; + CREATE SCHEMA public; + """.trimIndent() + try { + val conn = DriverManager.getConnection(dbUrl, user, password) + val st = conn.createStatement() + st.executeQuery(DELETE_ALL_TABLES) + } catch (e: SQLException){ + println("${e.message}") + } + } +} \ No newline at end of file -- cgit v1.2.3