summaryrefslogtreecommitdiff
path: root/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt
blob: e6f18da52e5d689cb0c932edbb1739feb070e583 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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

/**
 * @author Mateusz
 * @property name
 * @property backlog the amount of connections to accept
 * @property address IPv4 address
 * @property port
 * @property postgresql Postgresql port
 * @property username Postgresql user
 * @property password Postgresql password
 * @property database Postgresql database
 * @property topic Kafka topic
 * @property kafka Kafka port
 */
@Serializable
public data class Config(
    val name: String = "",
    var backlog: Int = 0,
    val address: String = "",
    val port: Int = 0,
    val postgresql: Int = 0,
    val username : String = "",
    val password : String = "",
    val database: String = "",
    val topic : String = "",
    val kafka: Int = 0,
){

    public companion object{
        public var input: InputStream? = null
        public var output: OutputStream? = null
        public var connection : Connection? = null
        public var kafka : Kafka? = null
        public var database : PostgresqlDB? = 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
        }

        public fun setKafkaInstance(kafka : Kafka) {
            this.kafka = kafka
        }

        public fun getKafkaInstance() : Kafka? {
            return this.kafka
        }

        public fun setDB(db : PostgresqlDB){
            this.database = db
        }

        public fun getDB() : PostgresqlDB?{
            return this.database
        }
        }
}
/**
 * @author Mateusz
 * 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)
    }
}