Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ public class SimulatorCard: CardType {
ALog("Error while deinit: [\(error)]")
}
}

public var description: String {
"SimulatorCard: [\(host):\(port)]"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import CardReaderProviderApi
import Foundation
import GemCommonsKit
import OSLog
import SwiftSocket

protocol TCPClientType: InputStreaming, OutputStreaming {
Expand Down Expand Up @@ -98,7 +99,8 @@ public class SimulatorCardChannel: CardChannelType {
.illegalState
}
let message = try command.bytes.berTlvEncoded()
DLog("SEND: \(message.map { String(format: "%02hhX", $0) }.joined())") // hexString
Logger.cardSimulationCardReaderProvider
.debug("SEND: \(message.map { String(format: "%02hhX", $0) }.joined())")
_ = message.withUnsafeBytes {
outputStream.write($0, maxLength: message.count)
}
Expand All @@ -122,12 +124,16 @@ public class SimulatorCardChannel: CardChannelType {
} while inputStream.hasBytesAvailable || (responseData.isEmpty && Date() < timeoutTime)

guard !responseData.isEmpty else {
ALog("Error when reading the response from the CardSimulator connection" +
" or there were no bytes available to be read.")
Logger.cardSimulationCardReaderProvider
.warning(
// swiftlint:disable:next line_length
"Error when reading the response from the CardSimulator connection or there were no bytes available to be read."
)
throw SimulatorError.noResponse.connectionError
}

DLog("RESPONSE: \(responseData.map { String(format: "%02hhX", $0) }.joined())") // hexString
Logger.cardSimulationCardReaderProvider
.debug("RESPONSE: \(responseData.map { String(format: "%02hhX", $0) }.joined())") // hexString
let extractedResponseData = try responseData.berTlvDecoded()
guard extractedResponseData.count <= maxResponseLength else {
throw SimulatorError.responseSizeTooLarge(maxSize: maxResponseLength, length: responseData.count)
Expand All @@ -150,7 +156,8 @@ public class SimulatorCardChannel: CardChannelType {
.illegalState
}
let message = try command.bytes.berTlvEncoded()
DLog("SEND: \(message.map { String(format: "%02hhX", $0) }.joined())") // hexString
Logger.cardSimulationCardReaderProvider
.debug("SEND: \(message.map { String(format: "%02hhX", $0) }.joined())") // hexString
_ = message.withUnsafeBytes {
outputStream.write($0, maxLength: message.count)
}
Expand All @@ -174,12 +181,16 @@ public class SimulatorCardChannel: CardChannelType {
} while inputStream.hasBytesAvailable || (responseData.isEmpty && Date() < timeoutTime)

guard !responseData.isEmpty else {
ALog("Error when reading the response from the CardSimulator connection" +
" or there were no bytes available to be read.")
Logger.cardSimulationCardReaderProvider
.warning(
// swiftlint:disable:next line_length
"Error when reading the response from the CardSimulator connection or there were no bytes available to be read."
)
throw SimulatorError.noResponse.connectionError
}

DLog("RESPONSE: \(responseData.map { String(format: "%02hhX", $0) }.joined())") // hexString
Logger.cardSimulationCardReaderProvider
.debug("RESPONSE: \(responseData.map { String(format: "%02hhX", $0) }.joined())") // hexString
let extractedResponseData = try responseData.berTlvDecoded()
guard extractedResponseData.count <= maxResponseLength else {
throw SimulatorError.responseSizeTooLarge(maxSize: maxResponseLength, length: responseData.count)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Copyright (c) 2024 gematik GmbH
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import OSLog

extension Logger {
static let cardSimulationCardReaderProvider = Logger(
subsystem: "de.gematik.ti.ohcapp4ios",
category: "cardSimulationCardReaderProvider"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public class SimulatorCardReader: CardReaderType {
}
}
}

public var description: String {
"SimulatorCardReader: \(name)"
}
}

extension SimulationProcessMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import Foundation
import GemCommonsKit
import OSLog

/**
KeepAliveRunLoop keeps a thread running in .default until the thread gets cancelled
Expand Down Expand Up @@ -44,7 +45,7 @@ public class KeepAliveRunLoop: Thread {

/// Do not call directly
override public func main() {
DLog("KeepAliveRunLoop started")
Logger.cardSimulationLoader.debug("KeepAliveRunLoop started")
runloop = RunLoop.current
let port = Port()
RunLoop.current.add(port, forMode: .common)
Expand All @@ -53,7 +54,7 @@ public class KeepAliveRunLoop: Thread {
}
RunLoop.current.remove(port, forMode: .common)
port.invalidate()
DLog("Port invalidated and thread ended")
Logger.cardSimulationLoader.debug("Port invalidated and thread ended")
}

/// Cancel the Thread and RunLoop
Expand Down
24 changes: 24 additions & 0 deletions CardSimulationTestKit/Sources/CardSimulationLoader/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Copyright (c) 2024 gematik GmbH
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import OSLog

extension Logger {
static let cardSimulationLoader = Logger(
subsystem: "de.gematik.ti.ohcapp4ios",
category: "cardSimulationLoader"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import Foundation
import GemCommonsKit
import OSLog

/// Protocol that describes G2-Kartensimulation Configuration files (pre-)processors
public protocol ConfigurationFileProcessor {
Expand Down Expand Up @@ -139,7 +140,7 @@ public class SimulationManager {
public init(tempDir: URL = NSTemporaryDirectory().asURL.appendingPathComponent(
ProcessInfo.processInfo.globallyUniqueString, isDirectory: true
)) {
DLog("Init with tempDir: [\(tempDir)]")
Logger.cardSimulationLoader.debug("Init with tempDir: [\(tempDir)]")
tempDirectory = tempDir
}

Expand Down Expand Up @@ -225,7 +226,7 @@ public class SimulationManager {
- Parameter flag: indicate whether this function should wait until all the processes have been terminated
*/
public func stopAll(waitUntilDone flag: Bool = true) {
DLog("Stop all simulators")
Logger.cardSimulationLoader.debug("Stop all simulators")
runners.forEach { [unowned self] in
self.stop(simulation: $0, waitUntilDone: flag)
}
Expand All @@ -237,7 +238,7 @@ public class SimulationManager {
$0.1 === simulation
}
.forEach {
DLog("Cleaning: \($0.url)")
Logger.cardSimulationLoader.debug("Cleaning: \($0.url)")
do {
try FileManager.default.removeItem(at: $0.url)
} catch {
Expand Down Expand Up @@ -301,7 +302,8 @@ extension SimulationManager: SimulationRunnerDelegate {
Simulation manager's delegate for the runner.
*/
public func simulation(runner: SimulationRunnerType, changed mode: SimulationProcessMode) {
DLog("Simulation: \(runner) -> [\(mode)]")
let runnerDescription = runner.description
Logger.cardSimulationLoader.debug("Simulation: \(runnerDescription) -> [\(mode)]")
if mode.isRunning {
/// Inform delegates
_delegates.array.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

import Foundation
import GemCommonsKit
import OSLog
import StreamReader

/// Protocol that describes `SimulationRunnerType` behaviour.
public protocol SimulationRunnerType: AnyObject {
public protocol SimulationRunnerType: AnyObject, CustomStringConvertible {
/// The mode the runner is currently in
var mode: SimulationProcessMode { get }

Expand Down Expand Up @@ -124,7 +125,7 @@ extension SimulationRunner: SimulationRunnerType {
/// - Parameter flag: whether to pause further execution until the JavaProcess has been (successfully) launched.
public func start(waitUntilLaunched flag: Bool) {
guard mode.isNotRunning else {
DLog("WARN: double start")
Logger.cardSimulationLoader.debug("WARN: double start")
return
}
mode = .initializing
Expand All @@ -140,13 +141,17 @@ extension SimulationRunner: SimulationRunnerType {
}
}

DLog("start END")
Logger.cardSimulationLoader.debug("start END")
}

/// Stop the simulation runner
/// - Parameter flag: whether to pause further execution until the JavaProcess has been terminated.
public func stop(waitUntilTerminated flag: Bool) {
DLog("Simulator runner STOP [wait:\(String(describing: flag))]")
Logger.cardSimulationLoader.debug("Simulator runner STOP [wait:\(String(describing: flag))]")
processLoader?.terminate(waitUntilDone: flag)
}

public var description: String {
"SimulationRunner: \(mode)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import Foundation
import GemCommonsKit
import OSLog

protocol JavaProcessConfig {
#if os(macOS) || os(Linux)
Expand Down Expand Up @@ -77,6 +78,19 @@ class JavaProcess {
}
}

extension JavaProcess.Config: CustomStringConvertible {
var description: String {
"""
Config:
- classPath: \(classPath)
- mainClass: \(mainClass)
- arguments: \(arguments)
- workingDirectory: \(workingDirectory)
- launchPath: \(launchPath)
"""
}
}

protocol JavaProcessUpdateDelegate: AnyObject {
func processDidLaunch(_ process: JavaProcess, pid: Int32)
func processDidTerminate(_ process: JavaProcess, with status: Int32)
Expand Down Expand Up @@ -132,7 +146,7 @@ extension JavaProcess {
}

task.launch()
DLog("JavaProcess launched PID: [\(task.processIdentifier)]")
Logger.cardSimulationLoader.debug("JavaProcess launched PID: [\(task.processIdentifier)]")
process = task
DispatchQueue.global().async {
DispatchQueue.main.sync {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import AEXML
import Foundation
import GemCommonsKit
import OSLog

extension SimulationManager {
struct DependencyInfo {
Expand Down Expand Up @@ -96,8 +97,11 @@ extension SimulationManager {
.flatMap { dependencyInfo in
guard !dependencyInfo.simulatorExists else {
// Skip (consecutive) downloading of same/existing simulator version
DLog("Skip (consecutive) download of same/existing simulator version.\n* classPath: " +
"[\(String(describing: dependencyInfo.simulatorClassPath?.absoluteString))]")
Logger.cardSimulationLoader
.debug(
// swiftlint:disable:next line_length
"Skip (consecutive) download of same/existing simulator version.\n* classPath: [\(String(describing: dependencyInfo.simulatorClassPath?.absoluteString))]"
)
return Result.success(dependencyInfo)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@
import Foundation
import GemCommonsKit
import ObjCCommonsKit
import OSLog

extension SimulationRunner: JavaProcessUpdateDelegate {
func processDidLaunch(_: JavaProcess, pid: Int32) {
DLog("Simulator process launched PID: [\(pid)]")
Logger.cardSimulationLoader.debug("Simulator process launched PID: [\(pid)]")
streamThread = KeepAliveRunLoop()
streamThread.start()
streamThread.runloop.perform(inModes: [.default]) { [unowned self] in
if Thread.current === Thread.main {
preconditionFailure("New thread runs on Main thread")
}

DLog("Card simulator: [STREAM START]")
Logger.cardSimulationLoader.debug("Card simulator: [STREAM START]")

var tcpPort: Int32?
var successfullyLaunched = false
Expand All @@ -37,7 +38,7 @@ extension SimulationRunner: JavaProcessUpdateDelegate {
if let exception = gemTryBlock({
var line = self.stdoutInfo.reader.nextLine()
repeat {
DLog("Card simulator: [\(String(describing: line))]")
Logger.cardSimulationLoader.debug("Card simulator: [\(String(describing: line))]")
if let line = line, !successfullyLaunched {
// Check for TCP TLV interface port
if tcpPort == nil, let port = line.match(pattern: "TCPIP: TLV Interface at Port (\\d*)$",
Expand All @@ -49,7 +50,7 @@ extension SimulationRunner: JavaProcessUpdateDelegate {

// !mode.isRunning means mode.isInitializing = true
if !self.mode.isRunning, successfullyLaunched, let tcpPort = tcpPort {
DLog("Simulator started successfully in port [\(tcpPort)]")
Logger.cardSimulationLoader.debug("Simulator started successfully in port [\(tcpPort)]")
self.mode = .running(onTCPPort: tcpPort)
} else if successfullyLaunched {
tcpPort = 12350 // G2-Kartensimulation does not log default TLV port 12350
Expand All @@ -60,16 +61,19 @@ extension SimulationRunner: JavaProcessUpdateDelegate {
}) {
// Caught NSException
ALog("Raised NSException while reading Process stdout")
DLog("NSException: \(exception)")
Logger.cardSimulationLoader.debug("NSException: \(exception)")
}

DLog("Card simulator: [STREAM END]")
Logger.cardSimulationLoader.debug("Card simulator: [STREAM END]")
}
}

func processDidTerminate(_: JavaProcess, with status: Int32) {
mode = .terminated(terminationStatus: status)
DLog("Simulator process ended with: [\(status)] - [CONFIG: \(config)]")
let statusDescription = status.description
let configDescription = config.description
Logger.cardSimulationLoader
.debug("Simulator process ended with: [\(statusDescription)] - [CONFIG: \(configDescription)]")
processThread.cancel()
streamThread.cancel()
stdoutInfo.reader.close()
Expand Down
24 changes: 24 additions & 0 deletions CardSimulationTestKit/Sources/CardSimulationRunner/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Copyright (c) 2024 gematik GmbH
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import OSLog

extension Logger {
static let cardSimulationRunner = Logger(
subsystem: "de.gematik.ti.ohcapp4ios",
category: "cardSimulationRunner"
)
}
Loading