Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr Storage Node9// File : MacLustrStorageNode/Models/Models.swift10// Description : Value types shared by the engine and the interface:11// pairing, work items, settings, capacity, activity, node12// snapshot.13// Module : MacLustrStorageNode14// Repository : https://git.spboucher.ai/maclustr-storage-ios15// Project Website : https://storage.spboucher.ai16// Created : 2026-12-0117// Last Modified : 2026-12-0118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: admin@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : None specified (no license file in this repository)84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation9293/// What the coordinator returned at pairing. Stored in the Keychain; holds no secret (the private key never leaves the device).94struct Pairing: Codable, Equatable {95 var deviceID: String96 var host: String97 var pairedAt: Date98 var lanPort: Int?99 var lanPin: String?100}101102/// One unit of work leased by the coordinator. The only four operations a coordinator can ask of this device.103struct WorkItem: Codable, Hashable, Identifiable {104 enum Operation: String, Codable { case store, retrieve, verify, delete }105 let task: String106 let op: Operation107 let fragment: String108 let size: Int64109 let ticket: String?110 let nonce: String?111 let reason: String?112 var id: String { task }113}114115struct NodeSettings: Codable, Equatable {116 var quotaBytes: Int64 = 0117 /// nil = automatic reserve: the larger of 20 GB and 10 % of the device capacity.118 var reserveBytes: Int64? = nil119 var wifiOnly = true120 var allowCellular = false121 var chargingOnly = false122 /// 0 = unlimited. Applied by the coordinator on both directions for this device.123 var rateLimitKbps = 0124 var stationMode = false125 var keepAwakeInStationMode = true126 var useLocalNetwork = true127 var suspended = false128 var nodeName = ""129130 static let key = "node.settings.v1"131 static func load() -> NodeSettings {132 guard let d = UserDefaults.standard.data(forKey: key), let s = try? JSONDecoder().decode(NodeSettings.self, from: d) else { return NodeSettings() }133 return s134 }135 func save() { if let d = try? JSONEncoder().encode(self) { UserDefaults.standard.set(d, forKey: Self.key) } }136}137138struct Capacity: Equatable {139 var total: Int64 = 0 // capacity of the data volume140 var free: Int64 = 0 // space iOS reports as available for important usage141 var reserve: Int64 = 0 // space never given to the cluster142 var quota: Int64 = 0 // maximum chosen by the user143 var used: Int64 = 0 // bytes of fragments held144 var fragments: Int = 0145 /// What can still be written now: bounded by the quota and by the free space above the reserve.146 var writable: Int64 { max(0, min(quota - used, free - reserve)) }147 /// Largest quota that could be honoured today.148 var maxAllocatable: Int64 { max(0, free - reserve + used) }149 var quotaRemaining: Int64 { max(0, quota - used) }150 var personalAndSystem: Int64 { max(0, total - free - used) }151 var unallocatedFree: Int64 { max(0, free - quotaRemaining) }152}153154enum ConnectionState: String { case unpaired, connecting, online, offline, suspended, revoked }155enum LinkPath: String { case lan, gateway, none }156157struct ActiveTransfer: Identifiable, Equatable {158 let id: String // task id159 let fragment: String160 let op: WorkItem.Operation161 let size: Int64162 var done: Int64163 var path: LinkPath164}165166struct ActivityEntry: Identifiable, Equatable {167 enum Kind: String { case stored, returned, verified, deleted, error, integrity, system }168 let id: Int64169 let date: Date170 let kind: Kind171 let fragment: String172 let bytes: Int64173 let millis: Int174 let ok: Bool175 let detail: String176}177178struct DrainProgress: Equatable { var remainingFragments: Int; var remainingBytes: Int64; var awaitingStableCopy: Int }179180/// Everything the interface shows. Produced by the engine from real state only.181struct NodeSnapshot: Equatable {182 var connection: ConnectionState = .unpaired183 var serverStatus = "" // active | suspended | draining | drained (coordinator's view)184 var path: LinkPath = .none185 var capacity = Capacity()186 var accepting = false187 var pauseReason: String? = nil188 var rxBps: Double = 0189 var txBps: Double = 0190 var lastSync: Date? = nil191 var lastError: String? = nil192 var thermal = "nominal"193 var battery: Double? = nil194 var charging = false195 var lowPower = false196 var network = "none"197 var active: [ActiveTransfer] = []198 var pendingOnServer = 0199 var totalReceived: Int64 = 0200 var totalSent: Int64 = 0201 var verifyOK = 0202 var verifyFailed = 0203 var errors = 0204 var drain: DrainProgress? = nil205 var availability24h: Double? = nil // share of the last 24 h this app could reach the cluster (measured on the device)206 var lastScrub: Date? = nil207 var lanAvailable = false208}209210enum Format {211 static func bytes(_ n: Int64) -> String {212 let f = ByteCountFormatter(); f.countStyle = .file; f.allowedUnits = [.useKB, .useMB, .useGB, .useTB]; f.zeroPadsFractionDigits = false213 return n <= 0 ? "0 MB" : f.string(fromByteCount: n)214 }215 static func rate(_ bps: Double) -> String { bps < 1 ? "—" : bytes(Int64(bps)) + "/s" }216 static func percent(_ x: Double?) -> String { x.map { String(format: "%.0f %%", $0 * 100) } ?? "—" }217 static func relative(_ d: Date?) -> String {218 guard let d else { return "never" }219 let s = Int(Date().timeIntervalSince(d))220 if s < 5 { return "just now" }221 if s < 60 { return "\(s) s ago" }222 if s < 3600 { return "\(s / 60) min ago" }223 if s < 86400 { return "\(s / 3600) h ago" }224 return "\(s / 86400) d ago"225 }226 static let gb: Int64 = 1_000_000_000227}228