Upload files to "src/MeloNX/MeloNX/App/Core/JIT/JitStreamerEB"

- Add ability to set the Jitstreamer-EB server IP for self-host purposes
- Defaults to the [fd00::] IP still
This commit is contained in:
uproot9945 2025-02-13 03:03:06 +00:00
parent 1b69c0bdc6
commit 302ddeee85

View File

@ -6,32 +6,75 @@
//
import Foundation
import UIKit
func enableJITEB() {
// MARK: - Server Address Management
/// Key for storing the JITEB server address in UserDefaults
private let JITEB_SERVER_ADDRESS_KEY = "jitServerAddress"
/// Returns the current JITEB server address.
/// Defaults to `[fd00::]` if no address is set.
func getJITServerAddress() -> String {
return UserDefaults.standard.string(forKey: JITEB_SERVER_ADDRESS_KEY) ?? "[fd00::]"
}
/// Updates the JITEB server address in UserDefaults.
func setJITServerAddress(_ address: String) {
UserDefaults.standard.set(address, forKey: JITEB_SERVER_ADDRESS_KEY)
}
// MARK: - JIT Enablement
/// Enables JIT execution by communicating with the JITEB server.
func enableJITEB() {
guard let bundleID = Bundle.main.bundleIdentifier else {
print("Error: Unable to retrieve bundle ID.")
return
}
let address = URL(string: "http://[fd00::]:9172/launch_app/\(bundleID)")!
// Construct the URL using the current server address
let serverAddress = getJITServerAddress()
let urlString = "http://\(serverAddress):9172/launch_app/\(bundleID)"
guard let address = URL(string: urlString) else {
print("Error: Invalid server address.")
return
}
// Send a network request to the JITEB server
let task = URLSession.shared.dataTask(with: address) { data, response, error in
if error != nil {
if let error = error {
print("Network error: \(error.localizedDescription)")
return
}
guard let httpResponse = response as? HTTPURLResponse else {
print("Error: Invalid server response.")
return
}
DispatchQueue.main.async {
showLaunchAppAlert(jsonData: data!, in: UIApplication.shared.windows.last!.rootViewController!)
guard let data = data else {
print("Error: No data received from server.")
return
}
return
// Show the launch status to the user
DispatchQueue.main.async {
if let rootViewController = UIApplication.shared.windows.last?.rootViewController {
showLaunchAppAlert(jsonData: data, in: rootViewController)
} else {
print("Error: Unable to access root view controller.")
}
}
}
task.resume()
}
// MARK: - Launch App Response Handling
/// Struct to decode the JSON response from the JITEB server
struct LaunchApp: Codable {
let ok: Bool
let error: String?
@ -40,6 +83,7 @@ struct LaunchApp: Codable {
let mounting: Bool
}
/// Displays an alert with the app launch status.
func showLaunchAppAlert(jsonData: Data, in viewController: UIViewController) {
do {
let result = try JSONDecoder().decode(LaunchApp.self, from: jsonData)
@ -76,3 +120,26 @@ func showLaunchAppAlert(jsonData: Data, in viewController: UIViewController) {
}
}
}
// MARK: - Server Address Configuration
/// Displays an alert to allow the user to change the JITEB server address.
func configureJITServerAddress(in viewController: UIViewController) {
let alert = UIAlertController(title: "Configure JITEB Server", message: "Enter the server address (e.g., [fd00::] or 192.168.1.100):", preferredStyle: .alert)
alert.addTextField { textField in
textField.placeholder = "Server Address"
textField.text = getJITServerAddress()
}
alert.addAction(UIAlertAction(title: "Save", style: .default) { _ in
if let textField = alert.textFields?.first, let newAddress = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
setJITServerAddress(newAddress)
print("JITEB server address updated to: \(newAddress)")
}
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
viewController.present(alert, animated: true)
}