Skip to content

Commit 9c10d12

Browse files
committed
Handle process
1 parent 705f4cc commit 9c10d12

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

Diff for: fmu-wrapper/src/main/kotlin/no/ntnu/ihb/fmuproxy/FmuWrapper.kt

+16-12
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ import no.ntnu.ihb.fmuproxy.thrift.internal.InternalFmuService
99
import org.apache.thrift.protocol.TBinaryProtocol
1010
import org.apache.thrift.transport.TFramedTransport
1111
import org.apache.thrift.transport.TSocket
12-
import java.io.Closeable
13-
import java.io.File
14-
import java.io.FileInputStream
15-
import java.io.StringReader
12+
import java.io.*
13+
import java.util.concurrent.TimeUnit
1614
import javax.xml.bind.JAXB
1715

1816

1917
class FmuWrapper(
20-
args: Map<String, Any>
18+
args: Map<String, Any>
2119
) : Fmi2Slave(args) {
2220

2321
private val fmu: File
2422
private val client: ThriftFmuClient?
23+
private var localProcess: Process? = null
2524

2625
override val automaticallyAssignStartValues = false
2726

@@ -34,7 +33,8 @@ class FmuWrapper(
3433
val remote = settings.remote
3534
if (remote == null) {
3635
val server = getFmuResource("fmu-proxy-server.jar")
37-
val port = startLocalProxy(server, fmu)
36+
val (process, port) = startLocalProxy(server, fmu)
37+
this.localProcess = process
3838
Thread.sleep(1000)
3939
this.client = ThriftFmuClient("localhost", port)
4040
} else {
@@ -45,6 +45,10 @@ class FmuWrapper(
4545
}
4646
}
4747

48+
override fun setupExperiment(startTime: Double, stopTime: Double, tolerance: Double) {
49+
this.client?.setupExperiment(startTime, stopTime, tolerance)
50+
}
51+
4852
override fun enterInitialisationMode() {
4953
this.client?.enterInitializationMode()
5054
}
@@ -53,10 +57,6 @@ class FmuWrapper(
5357
this.client?.exitInitializationMode()
5458
}
5559

56-
override fun setupExperiment(startTime: Double, stopTime: Double, tolerance: Double) {
57-
this.client?.setupExperiment(startTime, stopTime, tolerance)
58-
}
59-
6060
override fun doStep(currentTime: Double, dt: Double) {
6161
this.client?.doStep(dt)
6262
}
@@ -67,6 +67,10 @@ class FmuWrapper(
6767

6868
override fun close() {
6969
client?.close()
70+
71+
localProcess?.apply {
72+
waitFor(2000, TimeUnit.MILLISECONDS)
73+
}
7074
}
7175

7276
override fun getInteger(vr: LongArray): IntArray {
@@ -228,8 +232,8 @@ class FmuWrapper(
228232
}
229233

230234
private class ThriftFmuClient(
231-
host: String,
232-
port: Int
235+
host: String,
236+
port: Int
233237
) : Closeable {
234238

235239
private val transport = TFramedTransport.Factory()

Diff for: fmu-wrapper/src/main/kotlin/no/ntnu/ihb/fmuproxy/misc/helpers.kt

+19-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import org.apache.thrift.protocol.TBinaryProtocol
1212
import org.apache.thrift.transport.TFramedTransport
1313
import org.apache.thrift.transport.TSocket
1414
import java.io.File
15+
import java.io.FileInputStream
1516
import java.io.InputStream
1617
import java.io.StringReader
1718
import java.net.ServerSocket
1819
import java.nio.ByteBuffer
1920
import java.util.zip.ZipEntry
2021
import java.util.zip.ZipInputStream
2122
import javax.xml.bind.JAXB
23+
import kotlin.concurrent.thread
2224

2325
internal fun String.isLoopback(): Boolean {
2426
return this == "localhost" || this == "127.0.0.1"
@@ -33,22 +35,24 @@ private fun getAvailablePort(): Int {
3335
internal fun startRemoteProxy(fmuFile: File, host: String, port: Int): Int {
3436
//with 150 MB max message size
3537
val transport = TFramedTransport.Factory(150000000)
36-
.getTransport(TSocket(host, port))
38+
.getTransport(TSocket(host, port))
3739
val protocol = TBinaryProtocol(transport)
3840
val client = FmuService.Client(protocol)
3941
transport.open()
4042

4143
return if (host.isLoopback()) {
4244
client.loadFromLocalFile(fmuFile.absolutePath)
4345
} else {
44-
val data = fmuFile.readBytes().let {
45-
ByteBuffer.wrap(it)
46+
val data = FileInputStream(fmuFile).buffered().use {
47+
ByteBuffer.wrap(it.readBytes())
4648
}
4749
client.loadFromRemoteFile(fmuFile.name, data)
50+
}.also {
51+
transport.close()
4852
}
4953
}
5054

51-
internal fun startLocalProxy(proxyFile: File, fmuFile: File): Int {
55+
internal fun startLocalProxy(proxyFile: File, fmuFile: File): Pair<Process, Int> {
5256

5357
require(fmuFile.exists()) { "No such file: $fmuFile" }
5458
require(proxyFile.exists()) { "No such file: $proxyFile" }
@@ -60,12 +64,20 @@ internal fun startLocalProxy(proxyFile: File, fmuFile: File): Int {
6064
"$port", fmuFile.absolutePath
6165
)
6266

63-
ProcessBuilder().apply {
67+
val pb = ProcessBuilder().apply {
6468
command(*cmd)
65-
start()
6669
}
6770

68-
return port
71+
val process = pb.start()
72+
thread(true) {
73+
val br = process.inputStream.bufferedReader()
74+
while (true) {
75+
val read = br.readLine()
76+
if (read == null) break else println(read)
77+
}
78+
}
79+
80+
return process to port
6981
}
7082

7183
internal fun extractModelDescriptionXml(stream: InputStream): String {

0 commit comments

Comments
 (0)