summaryrefslogtreecommitdiff
path: root/opendc-common/src/main/kotlin
diff options
context:
space:
mode:
authormjkwiatkowski <mati.rewa@gmail.com>2026-02-15 18:26:59 +0100
committermjkwiatkowski <mati.rewa@gmail.com>2026-02-15 18:26:59 +0100
commit86d35fcec83057e346e4982b5a6908f25342a392 (patch)
tree2a9cbd42582fc87b81e7414a78b43ead51c623aa /opendc-common/src/main/kotlin
parent213130fdca719d9dc7f29fe7c11acde40e4f964e (diff)
feat: opendc -> postgresql connection worksHEADmaster
Diffstat (limited to 'opendc-common/src/main/kotlin')
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt72
-rw-r--r--opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt56
2 files changed, 128 insertions, 0 deletions
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<Config>(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