-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
143 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package bridge | ||
|
||
interface Bridge: Reader, Writer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.runBlocking | ||
import bridge.Bridge | ||
import bridge.Reader | ||
import technology.idlab.logging.Log | ||
|
||
class MemoryBridge : Bridge { | ||
private var channel = Channel<ByteArray>(10) | ||
|
||
override fun pushSync(value: ByteArray) { | ||
Log.shared.debug("Pushing ${value.size} bytes") | ||
runBlocking { channel.send(value) } | ||
Log.shared.debug("Done") | ||
} | ||
|
||
override fun readSync(): Reader.Result { | ||
Log.shared.debug("Reading bytes") | ||
val result = runBlocking { channel.receiveCatching() } | ||
|
||
// Check if the channel got closed. | ||
if (result.isClosed) { | ||
return Reader.Result.closed() | ||
} | ||
|
||
// If an error occurred, the runner must handle it itself. | ||
if (result.isFailure) { | ||
Log.shared.fatal("Failed to read bytes") | ||
} | ||
|
||
val bytes = result.getOrThrow() | ||
return Reader.Result.success(bytes) | ||
} | ||
|
||
override fun isClosed(): Boolean { | ||
return channel.isClosedForSend | ||
} | ||
|
||
override fun close() { | ||
channel.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package bridge | ||
|
||
import technology.idlab.logging.Log | ||
|
||
|
||
interface Reader { | ||
enum class ResultType { | ||
SUCCESS, CLOSED; | ||
} | ||
|
||
class Result(private val type: ResultType, value: ByteArray) { | ||
val value: ByteArray | ||
get() { | ||
if (type == ResultType.SUCCESS) { | ||
return field | ||
} else { | ||
Log.shared.fatal("Cannot get value from invalid read.") | ||
} | ||
} | ||
|
||
init { | ||
this.value = value | ||
} | ||
|
||
fun isClosed(): Boolean { | ||
return type == ResultType.CLOSED | ||
} | ||
|
||
companion object { | ||
fun success(value: ByteArray): Result { | ||
return Result(ResultType.SUCCESS, value) | ||
} | ||
|
||
fun closed(): Result { | ||
return Result(ResultType.CLOSED, ByteArray(0)) | ||
} | ||
} | ||
} | ||
|
||
fun readSync(): Result | ||
fun isClosed(): Boolean | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package bridge | ||
|
||
interface Writer { | ||
fun pushSync(value: ByteArray) | ||
fun close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
import io.reactivex.rxjava3.core.Observable; | ||
import io.reactivex.rxjava3.disposables.Disposable; | ||
import bridge.Reader; | ||
import java.util.Map; | ||
import runner.Processor; | ||
|
||
public class Reporter extends Processor { | ||
private final Observable<String> incoming; | ||
private final Reader reader; | ||
|
||
public Reporter(Map<String, Object> args) { | ||
// Call super constructor. | ||
super(args); | ||
|
||
// Parameters | ||
this.incoming = this.getArgument("incoming"); | ||
this.reader = this.getArgument("reader"); | ||
} | ||
|
||
public void setup() { | ||
// Local variables | ||
Disposable disposable = incoming.subscribe( | ||
item -> log.info("Received item: " + item), | ||
error -> log.severe("Error: " + error), | ||
() -> log.info("Channel closed.") | ||
); | ||
public void exec() { | ||
while (!reader.isClosed()) { | ||
Reader.Result result = reader.readSync(); | ||
|
||
if (result.isClosed()) { | ||
break; | ||
} | ||
|
||
log.info("Received item: " + new String(result.getValue())); | ||
} | ||
} | ||
} |