8 Commits
0.7.2 ... dev

Author SHA1 Message Date
Christian Risi
d05a2c3767 V0.8.0 Arroyo Toad
Fixed problems related to concurrency and segmentation faults
2024-12-17 15:07:12 +00:00
Christian Risi
2ef8cc868b V0.7.3.c Arroyo Toad
Fixed an import
2024-12-16 11:23:43 +01:00
Christian Risi
811bfcd441 V0.7.3.c Arroyo Toad
Fixed an import
2024-12-16 11:23:31 +01:00
Christian Risi
06a5dd9c4a V0.7.3.b Arroyo toad
Modified success paramter to accept async functions
2024-12-15 22:03:02 +00:00
Christian Risi
e0921f4ca4 V0.7.3.a Arroyo toad
Modified success paramter to accept async functions
2024-12-15 22:01:15 +00:00
Christian Risi
52d5cef335 V 0.7.3 Arroyo Toad
Swept a bit of dust from code to make it more concurrent safe
2024-12-11 15:58:20 +00:00
Christian Risi
9dbde70406 V0.7.2.a Arroyo Toad
Fixed a bug about getting IDs
2024-12-11 12:43:19 +00:00
Christian Risi
8c3b36967f V0.7.2.a Arroyo Toad
Fixed a typo
2024-12-11 13:22:39 +01:00
12 changed files with 338 additions and 122788 deletions

View File

@@ -1,25 +1,24 @@
import Foundation
import Crypto
import DataAcquisition
import Foundation
import MessageUtils
public class EdgeDevice: EdgeDeviceP {
public actor EdgeDevice: @preconcurrency EdgeDeviceP {
public let deviceID: UInt128
public let deviceType: DeviceType
public let dataType: DataType
public var disconnected: Bool
public var compromised: Bool
public var location: Location
public var dutyCicle: UInt
public var sensors: [Int: Sensor]
public var privateKey: P256.Signing.PrivateKey
public private(set) var disconnected: Bool
public private(set) var compromised: Bool
public private(set) var location: Location
public private(set) var dutyCicle: UInt
public private(set) var sensors: [Int: Sensor]
public private(set) var privateKey: P256.Signing.PrivateKey
public var publicKey: P256.Signing.PublicKey {
get{
return self.privateKey.publicKey
}
}
private var numberOfSensors: Int {
return sensors.count
@@ -43,17 +42,54 @@ public class EdgeDevice: EdgeDeviceP {
self.dutyCicle = dutyCicle
self.sensors = sensors
self.privateKey = privateKey
}
public func addSensor(sensor: Sensor) {
public func addSensor(sensor: Sensor) async {
self.sensors[numberOfSensors + 1] = sensor
}
public func removeSensor(id: Int) {
public func removeSensor(id: Int) async {
self.sensors.removeValue(forKey: id)
}
public func work(envrionment: PhysicalEnvironment) throws -> Data {
public func setSensorFaulty(id: Int) async {
if id > self.numberOfSensors {
// UGLY: Do nothing
}
let sensor = self.sensors[id]!
await sensor.toggleFaulty()
}
public func setDisconnected(disconnected: Bool) async {
self.disconnected = disconnected
}
public func setCompromised(compromised: Bool) async {
self.compromised = compromised
}
public func setLocation(location: Location) async {
self.location = location
}
public func dutyCycle(time: UInt) async {
self.dutyCicle = time
}
private func getSensor(id: Int) async -> Sensor {
if id > self.numberOfSensors {
// UGLY: Do nothing
}
return self.sensors[id]!
}
public func work(envrionment: PhysicalEnvironment) async throws -> Data {
// UGLY: Declaring here some variables manually, remove them if you want to offere flexibility
let numberOfSamples: Int = 10
@@ -70,7 +106,7 @@ public class EdgeDevice: EdgeDeviceP {
for rowI in 0..<numberOfSamples {
for colI in 0..<self.sensors.count {
rowPointer[rowI]![colI] = self.sensors[colI]!.read(envrionment).value
rowPointer[rowI]![colI] = await self.getSensor(id: colI).read(envrionment).value
}
}
@@ -95,7 +131,8 @@ public class EdgeDevice: EdgeDeviceP {
)
var msgData = serializeV1(msg: msg)
let signature = try signMessage(msgData: msgData, signType: msg.signType, key: self.privateKey)
let signature = try signMessage(
msgData: msgData, signType: msg.signType, key: self.privateKey)
msgData.append(Data(signature))

View File

@@ -1,10 +1,22 @@
import RandomCpp
public class Sensor {
public protocol Sensor : Sendable {
var sensorID: Int { get }
var sensorType: DataType { get }
var faulty: Bool { get }
func setFaulty(value: Bool) async
func toggleFaulty() async
func read(_ environment: PhysicalEnvironment) async -> PhysicalData
}
public actor IdealSensor: @preconcurrency Sensor {
public let sensorID: Int
public let sensorType: DataType
public var faulty: Bool
public private(set) var faulty: Bool
public init(id: Int, sensorType: DataType, faulty: Bool) {
self.sensorID = id
@@ -18,25 +30,25 @@ public class Sensor {
self.faulty = false
}
public func read(_ environment: PhysicalEnvironment) -> PhysicalData {
public func read(_ environment: PhysicalEnvironment) async -> PhysicalData {
let datum = self._read(environment)
let datum = await self._read(environment)
return !self.faulty ? datum : datum + Float.random(in: 1E2...1E10)
}
internal func _read(_ environment: PhysicalEnvironment) -> PhysicalData{
internal func _read(_ environment: PhysicalEnvironment) async -> PhysicalData {
let datum: PhysicalData
do {
datum = try environment.getPhysicalData(self.sensorType)
datum = try await environment.getPhysicalData(self.sensorType)
} catch CoreError.NoPhysicalDataAvailable {
datum = self._defaultReadValue()
} catch {
print(
"This is really a weird problem, to be investigated, but here we are...\n" +
"nonetheless, the error will be handled silently, sowwy :3"
"This is really a weird problem, to be investigated, but here we are...\n"
+ "nonetheless, the error will be handled silently, sowwy :3"
)
datum = self._defaultReadValue()
}
@@ -48,15 +60,24 @@ public class Sensor {
return PhysicalData(self.sensorType, 0)
}
public func setFaulty(value: Bool) async {
self.faulty = value
}
public func toggleFaulty() async {
self.faulty = !self.faulty
}
}
public class RealSensor: Sensor {
public actor RealSensor: @preconcurrency Sensor {
public let sensorID: Int
public let sensorType: DataType
public private(set) var faulty: Bool
public var meanNoise: Float { get{ return _meanNoise}}
public var stdNoise: Float { get{ return _stdNoise}}
public var quantizationBits: Int { get{ return _quantizationBits}}
public var meanNoise: Float { return _meanNoise }
public var stdNoise: Float { return _stdNoise }
public var quantizationBits: Int { return _quantizationBits }
private var _meanNoise: Float
private var _stdNoise: Float
@@ -72,14 +93,16 @@ public class RealSensor: Sensor {
stdNoise: Float,
quantizationBits: Int
) {
self.sensorID = sensorID
self.sensorType = sensorType
self.faulty = faulty
self._meanNoise = meanNoise
self._stdNoise = stdNoise
self._quantizationBits = quantizationBits
self._gaussianNoise = GaussianRNG(self._meanNoise, self._stdNoise)
super.init(id: sensorID, sensorType: sensorType, faulty: faulty)
}
convenience public init(
public init(
sensorID: Int,
sensorType: DataType,
meanNoise: Float,
@@ -96,9 +119,44 @@ public class RealSensor: Sensor {
)
}
override public func read(_ environment: PhysicalEnvironment) -> PhysicalData {
let value: PhysicalData = super.read(environment)
public func read(_ environment: PhysicalEnvironment) async -> PhysicalData {
let datum = await self._read(environment)
let value = !self.faulty ? datum : datum + Float.random(in: 1E2...1E10)
return value + self._gaussianNoise.generate()
}
internal func _read(_ environment: PhysicalEnvironment) async -> PhysicalData {
let datum: PhysicalData
do {
datum = try await environment.getPhysicalData(self.sensorType)
} catch CoreError.NoPhysicalDataAvailable {
datum = self._defaultReadValue()
} catch {
print(
"This is really a weird problem, to be investigated, but here we are...\n"
+ "nonetheless, the error will be handled silently, sowwy :3"
)
datum = self._defaultReadValue()
}
return datum
}
internal func _defaultReadValue() -> PhysicalData {
return PhysicalData(self.sensorType, 0)
}
public func setFaulty(value: Bool) async {
self.faulty = value
}
public func toggleFaulty() async {
self.faulty = !self.faulty
}
}

View File

@@ -1,11 +1,15 @@
public class PhysicalEnvironment {
import Foundation
public actor PhysicalEnvironment {
private var physicalEnvironment: [DataType: PhysicalData]
public let location: String
private let lock: NSLock
public init(_ location: String) {
self.physicalEnvironment = Dictionary()
self.location = location
self.lock = NSLock()
}
/// Gets speficied datum from environment
@@ -13,19 +17,25 @@ public class PhysicalEnvironment {
/// - Throws: NoPhysicalDataAvailable
/// - Returns: The datum asked for
public func getPhysicalData(_ dataType: DataType) throws -> PhysicalData {
self.lock.lock()
guard let result: PhysicalData = self.physicalEnvironment[dataType] else {
throw CoreError.NoPhysicalDataAvailable(dataType: "\(dataType)")
}
self.lock.unlock()
return result
}
public func setPhysicalData(_ dataType: DataType, _ value: PhysicalData) {
self.lock.lock()
self.physicalEnvironment[dataType] = value
self.lock.unlock()
}
public func removePhysicalData(_ dataType: DataType) -> PhysicalData? {
return self.physicalEnvironment.removeValue(forKey: dataType)
self.lock.lock()
let data = self.physicalEnvironment.removeValue(forKey: dataType)
self.lock.unlock()
return data
}
}

View File

@@ -1,6 +1,6 @@
public class PhysicalData {
public final class PhysicalData: Sendable {
public let type: DataType
public let value: Float

View File

@@ -1,4 +1,4 @@
public enum DataType : String{
public enum DataType : String, Sendable {
case Temperature
case Humidity
case Scan

View File

@@ -7,14 +7,13 @@ public actor DeviceFactory {
// TODO: Make it possible to set this
private static var availableIDs: Set<UInt128> = Set(0..<999)
public static func createEdgeDevice(
deviceID: UInt128,
dataType: DataType,
privateKey: P256.Signing.PrivateKey
deviceID: sending UInt128,
dataType: sending DataType,
privateKey: sending P256.Signing.PrivateKey
) throws -> EdgeDevice {
return try DeviceFactory.createEdgeDevice(
deviceID: deviceID
deviceID: deviceID,
dataType: dataType,
privateKey: privateKey,
realSensor: false
@@ -22,10 +21,10 @@ public actor DeviceFactory {
}
public static func createEdgeDevice(
deviceID: UInt128,
dataType: DataType,
privateKey: P256.Signing.PrivateKey,
realSensor: Bool
deviceID: sending UInt128,
dataType: sending DataType,
privateKey: sending P256.Signing.PrivateKey,
realSensor: sending Bool
) throws -> EdgeDevice {
return try DeviceFactory.createEdgeDevice(
@@ -43,12 +42,12 @@ public actor DeviceFactory {
}
public static func createEdgeDevice(
deviceID: UInt128,
dataType: DataType,
location: Location,
dutyCicle: UInt,
privateKey: P256.Signing.PrivateKey,
realSensor: Bool
deviceID: sending UInt128,
dataType: sending DataType,
location: sending Location,
dutyCicle: sending UInt,
privateKey: sending P256.Signing.PrivateKey,
realSensor: sending Bool
) throws -> EdgeDevice {
if !DeviceFactory.availableIDs.contains(deviceID) {
@@ -62,15 +61,21 @@ public actor DeviceFactory {
if !realSensor {
sensors = [
0: Sensor(id: 0, sensorType: dataType),
1: Sensor(id: 1, sensorType: dataType),
2: Sensor(id: 2, sensorType: dataType)
0: IdealSensor(id: 0, sensorType: dataType),
1: IdealSensor(id: 1, sensorType: dataType),
2: IdealSensor(id: 2, sensorType: dataType),
]
} else {
sensors = [
0: RealSensor(sensorID: 0, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8),
1: RealSensor(sensorID: 1, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8),
2: RealSensor(sensorID: 2, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5, quantizationBits: 8)
0: RealSensor(
sensorID: 0, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5,
quantizationBits: 8),
1: RealSensor(
sensorID: 1, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5,
quantizationBits: 8),
2: RealSensor(
sensorID: 2, sensorType: dataType, faulty: false, meanNoise: 0, stdNoise: 0.5,
quantizationBits: 8),
]
}
@@ -81,7 +86,8 @@ public actor DeviceFactory {
location: location,
dutyCicle: dutyCicle,
sensors: sensors,
privateKey: privateKey)
privateKey: privateKey
)
}
public static func getUnusedID() throws -> UInt128 {
@@ -92,7 +98,7 @@ public actor DeviceFactory {
throw CoreError.FinishedIDs
}
return DeviceFactory.availableIDs.removeFirst()
return DeviceFactory.availableIDs.first!
}
}

View File

@@ -3,12 +3,13 @@
import Foundation
public actor IoTSimulatorCore {
@MainActor
public class IoTSimulatorCore {
internal static var enviroments: [String: PhysicalEnvironment] = Dictionary()
internal static var devices: [String: EdgeDeviceP] = Dictionary()
internal static var env_dev: [String: Set<String>] = Dictionary()
internal static var dev_tasks: [String: Task<(), Never>] = Dictionary()
@MainActor internal static var enviroments: [String: PhysicalEnvironment] = Dictionary()
@MainActor internal static var devices: [String: EdgeDeviceP] = Dictionary()
@MainActor internal static var env_dev: [String: Set<String>] = Dictionary()
@MainActor internal static var dev_tasks: [String: Task<(), Never>] = Dictionary()
public static func addEnv(environment: PhysicalEnvironment) {
IoTSimulatorCore.enviroments[environment.location] = environment
@@ -17,7 +18,7 @@ public actor IoTSimulatorCore {
public static func addDevice(
location: String,
device: EdgeDeviceP,
success: sending @escaping (_ msg: Data) throws -> Void,
success: sending @escaping (_ msg: Data) async throws -> Void,
failure: sending @escaping () -> Void
) throws {
if let environment = IoTSimulatorCore.enviroments[location] {
@@ -31,7 +32,7 @@ public actor IoTSimulatorCore {
// schedule work
let task = IoTSimulatorCore.schedule(
envID: environment.location, deviceID: device.deviceID, success: success,
environment: environment, device: device as! EdgeDevice, success: success,
failure: failure)
IoTSimulatorCore.dev_tasks["\(device.deviceID)"] = task
@@ -45,12 +46,6 @@ public actor IoTSimulatorCore {
return IoTSimulatorCore.enviroments[name]
}
public static func addPhysicalData(environmentName: String, physicalData: PhysicalData) {
if let environment = getEnv(name: environmentName) {
environment.setPhysicalData(physicalData.type, physicalData)
}
}
public static func getDev(devID: UInt128) -> EdgeDeviceP? {
return IoTSimulatorCore.devices["\(devID)"]
}
@@ -73,63 +68,59 @@ public actor IoTSimulatorCore {
return false
}
public static func toggleSensor(devID: UInt128, sensorID: Int) -> Bool {
public static func toggleSensor(devID: UInt, sensorID: Int) async -> Bool {
// UGLY: Should throw
if IoTSimulatorCore.getDev(devID: devID) == nil {
return false
}
let a = IoTSimulatorCore.getDev(devID: UInt128(devID))!
// FIXME: Should it throw?
if IoTSimulatorCore.getDev(devID: devID)!.deviceType != .EDGE_SENSOR {
return false
}
let device = IoTSimulatorCore.getDev(devID: devID)! as! EdgeDevice
let device = IoTSimulatorCore.getDev(devID: UInt128(devID))! as! EdgeDevice
// UGLY: It should throw
if device.sensors.count <= sensorID {
return false
}
device.sensors[sensorID]!.faulty = !device.sensors[sensorID]!.faulty
await device.setSensorFaulty(id: sensorID)
return true
}
public static func addPhysicalData(environmentName: String, physicalData: PhysicalData)
async throws
{
guard let environment = getEnv(name: environmentName) else {
throw CoreError.NoEnvironment
}
await environment.setPhysicalData(physicalData.type, physicalData)
}
private static func schedule(
envID: sending String,
deviceID: UInt128,
success: sending @escaping (_ msg: Data) throws -> Void,
environment: PhysicalEnvironment,
device: EdgeDevice,
success: sending @escaping (_ msg: Data) async throws -> Void,
failure: sending @escaping () -> Void
) -> Task<(), Never> {
let _devID: String = "\(deviceID)"
return Task {
var notCancelled: Bool = true
let dev = IoTSimulatorCore.devices["\(_devID)"]!
let env = IoTSimulatorCore.enviroments[envID]!
while notCancelled {
print("Device: \(dev.deviceID)")
// print("Device: \(dev.deviceID)")
do {
let message = try dev.work(envrionment: env)
let message = try await device.work(envrionment: environment)
try success(message)
try await success(message)
} catch {
failure()
}
do {
try await Task.sleep(nanoseconds: UInt64(dev.dutyCicle) * UInt64(1E6))
try await Task.sleep(nanoseconds: UInt64(device.dutyCicle) * UInt64(1E6))
} catch {
notCancelled = false
}
}
print("Bye, Bye, \(dev.deviceID)\n\n")
print("Bye, Bye, \(device.deviceID)\n\n")
}
}

View File

@@ -2,18 +2,24 @@ import Foundation
import Crypto
import MessageUtils
public protocol EdgeDeviceP {
public protocol EdgeDeviceP : Sendable{
var deviceID : UInt128 {get}
var deviceType : DeviceType {get}
var dataType : DataType {get}
var location: Location{get set}
var disconnected : Bool {get set}
var compromised : Bool {get set}
var dutyCicle : UInt {get set}
var location: Location{get }
var disconnected : Bool {get }
var compromised : Bool {get }
var dutyCicle : UInt {get }
var privateKey: P256.Signing.PrivateKey {get}
var publicKey: P256.Signing.PublicKey {get}
func work(envrionment: PhysicalEnvironment) throws -> Data
func work(envrionment: PhysicalEnvironment) async throws -> Data
func setDisconnected(disconnected: Bool) async
func setCompromised(compromised: Bool) async
func dutyCycle(time: UInt) async
func setLocation(location: Location) async
}

View File

@@ -11,10 +11,10 @@ import MessageUtils
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
let sensor : Sensor = Sensor(id: 1, sensorType: .Temperature)
let data = sensor.read(env)
let sensor : IdealSensor = IdealSensor(id: 1, sensorType: .Temperature)
let data = await sensor.read(env)
#expect(data.value == truth.value, "If values match, we are cool")
@@ -27,10 +27,10 @@ import MessageUtils
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
let sensor : Sensor = Sensor(id: 1, sensorType: .Temperature, faulty: true)
let data = sensor.read(env)
let sensor : IdealSensor = IdealSensor(id: 1, sensorType: .Temperature, faulty: true)
let data = await sensor.read(env)
#expect(data.value != truth.value, "If these match, something is not working")
@@ -43,10 +43,10 @@ import MessageUtils
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
let sensor : Sensor = RealSensor(sensorID: 1, sensorType: .Temperature, faulty: false, meanNoise: 0.5, stdNoise: 0.25, quantizationBits: 3)
let data = sensor.read(env)
let data = await sensor.read(env)
#expect(data.value - truth.value < 10, "If these match, we are cool")
@@ -61,10 +61,10 @@ import MessageUtils
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
let sensor : Sensor = RealSensor(sensorID: 1, sensorType: .Temperature, faulty: true, meanNoise: 0.5, stdNoise: 0.25, quantizationBits: 3)
let data = sensor.read(env)
let data = await sensor.read(env)
#expect(data.value - truth.value > 10, "If these match, something is not working")
@@ -79,7 +79,7 @@ import MessageUtils
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
let signKeyPath = "./Private/privateKey.pem"
@@ -92,14 +92,14 @@ import MessageUtils
location: Location(x: 20, y: 10, z: 0),
dutyCicle: 100098,
sensors: [
0: Sensor(id: 0, sensorType: DataType.Temperature),
1: Sensor(id: 0, sensorType: DataType.Temperature),
2: Sensor(id: 0, sensorType: DataType.Temperature, faulty: true)
0: IdealSensor(id: 0, sensorType: DataType.Temperature),
1: IdealSensor(id: 0, sensorType: DataType.Temperature),
2: IdealSensor(id: 0, sensorType: DataType.Temperature, faulty: true)
],
privateKey: privateKey
privateKey: try pem2_P256key(filePath: signKeyPath)
)
let message = try dev.work(envrionment: env)
let message = try await dev.work(envrionment: env)
let signedMessage = try deserializeV1(serializedData: message)
@@ -121,7 +121,7 @@ import MessageUtils
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
let signKeyPath = "./Private/privateKey.pem"
@@ -138,10 +138,10 @@ import MessageUtils
1: RealSensor(sensorID: 1, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3, quantizationBits: 3),
2: RealSensor(sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3, quantizationBits: 3),
],
privateKey: privateKey
privateKey: try pem2_P256key(filePath: signKeyPath)
)
let message = try dev.work(envrionment: env)
let message = try await dev.work(envrionment: env)
let signedMessage = try deserializeV1(serializedData: message)

View File

@@ -10,14 +10,12 @@ import MessageUtils
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
IoTSimulatorCore.addEnv(environment: env)
await IoTSimulatorCore.addEnv(environment: env)
let signKeyPath = "./Private/privateKey.pem"
let privateKey = try pem2_P256key(filePath: signKeyPath)
let dev: EdgeDevice = EdgeDevice(
deviceID: 1,
dataType: .Temperature,
@@ -35,7 +33,7 @@ import MessageUtils
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
quantizationBits: 3),
],
privateKey: privateKey
privateKey: try pem2_P256key(filePath: signKeyPath)
)
let dev2: EdgeDevice = EdgeDevice(
@@ -55,10 +53,10 @@ import MessageUtils
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
quantizationBits: 3),
],
privateKey: privateKey
privateKey: try pem2_P256key(filePath: signKeyPath)
)
try IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
try await IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
print(msg)
let _signedMessage = try! deserializeV1(serializedData: msg)
print(_signedMessage.toString())
@@ -66,7 +64,7 @@ import MessageUtils
failure: {
print("Something went wrong")
})
try IoTSimulatorCore.addDevice(location: "Delta", device: dev2, success: { msg in
try await IoTSimulatorCore.addDevice(location: "Delta", device: dev2, success: { msg in
print(msg)
let _signedMessage = try! deserializeV1(serializedData: msg)
print(_signedMessage.toString())
@@ -81,14 +79,14 @@ import MessageUtils
@Test func stressLoop1() async throws {
let devices: UInt128 = 5000
let devices: UInt128 = 1000000
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
IoTSimulatorCore.addEnv(environment: env)
await IoTSimulatorCore.addEnv(environment: env)
let signKeyPath = "./Private/privateKey.pem"
@@ -96,6 +94,8 @@ import MessageUtils
for i: UInt128 in 0..<devices {
print("\n\nCreating dev \(i)\n\n")
let dev: EdgeDevice = EdgeDevice(
deviceID: i,
dataType: .Temperature,
@@ -113,10 +113,10 @@ import MessageUtils
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
quantizationBits: 3),
],
privateKey: privateKey
privateKey: try pem2_P256key(filePath: signKeyPath)
)
try IoTSimulatorCore.addDevice(
try await IoTSimulatorCore.addDevice(
location: "Delta",
device: dev,
success: { msg in
@@ -128,30 +128,31 @@ import MessageUtils
print("Something went wrong")
}
)
}
let _sleep = 15
let _sleep = 60
for i in 0..<_sleep {
print("Hi, at \(i)s\n\n")
sleep(1)
}
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
IoTSimulatorCore.nukeAll()
await IoTSimulatorCore.nukeAll()
}
@Test func stressLoop2() async throws {
let devices: UInt128 = 1
let devices: UInt128 = 10
let env = PhysicalEnvironment("Delta")
let truth = PhysicalData(.Temperature, 22)
env.setPhysicalData(DataType.Temperature, truth)
await env.setPhysicalData(DataType.Temperature, truth)
IoTSimulatorCore.addEnv(environment: env)
await IoTSimulatorCore.addEnv(environment: env)
let signKeyPath = "./Private/privateKey.pem"
@@ -159,7 +160,7 @@ import MessageUtils
for i: UInt128 in 0..<devices {
let dev: EdgeDevice = EdgeDevice(
let dev: EdgeDevice = await EdgeDevice(
deviceID: i,
dataType: .Temperature,
disconnected: false,
@@ -176,12 +177,12 @@ import MessageUtils
sensorID: 2, sensorType: .Temperature, faulty: false, meanNoise: 1, stdNoise: 3,
quantizationBits: 3),
],
privateKey: privateKey
privateKey: try pem2_P256key(filePath: signKeyPath)
)
try IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
try await IoTSimulatorCore.addDevice(location: "Delta", device: dev, success: { msg in
print(msg)
let _signedMessage = try! deserializeV1(serializedData: msg)
let _signedMessage = try deserializeV1(serializedData: msg)
print(_signedMessage.toString())
},
failure: {
@@ -194,12 +195,11 @@ import MessageUtils
for i in 0..<_sleep {
print("Hi, at \(i)s\n\n")
sleep(1)
IoTSimulatorCore.toggleSensor(devID: 0, sensorID: 0)
await IoTSimulatorCore.toggleSensor(devID: UInt.random(in: 0..<UInt(devices)), sensorID: 0)
}
print("NUKE EM ALLLLLLLLLL!!!!!\n\n")
IoTSimulatorCore.nukeAll()
await IoTSimulatorCore.nukeAll()
}

122636
test.txt

File diff suppressed because it is too large Load Diff