source: launchers/macosx/I2PLauncher/routermgmt/RouterManager.swift @ 7a72049

Last change on this file since 7a72049 was 7a72049, checked in by meeh <meeh@…>, 2 years ago

Mac OS X Launcher:

  • Bugfixes as always
  • Added Sparkle (native updater, https://sparkle-project.org/ )
  • The launcher will now extract and overwrite older versions if found
  • Rewrite of the java extraction part (to enable overwrite)
  • Move more functionality to use EventManager? as it works quite well
  • Added check for updates menu item
  • Property mode set to 100644
File size: 3.1 KB
Line 
1//
2//  RouterManager.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 22/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Foundation
10
11class RouterManager : NSObject {
12 
13  // MARK: - Properties
14 
15  static let packedVersion : String = "0.9.36"
16 
17  let eventManager = EventManager()
18 
19  var logViewStorage: NSTextStorage?
20 
21  private static func handleRouterStart(information:Any?) {
22    NSLog("event! - handle router start")
23    RouterProcessStatus.routerStartedAt = Date()
24    RouterProcessStatus.isRouterChildProcess = true
25    RouterProcessStatus.isRouterRunning = true
26  }
27  private static func handleRouterStop(information:Any?) {
28    NSLog("event! - handle router stop")
29    RouterProcessStatus.routerStartedAt = nil
30    RouterProcessStatus.isRouterChildProcess = false
31    RouterProcessStatus.isRouterRunning = false
32  }
33  private static func handleRouterPid(information:Any?) {
34    Swift.print("event! - handle router pid: %s", information ?? "")
35  }
36  private static func handleRouterVersion(information:Any?) {
37    Swift.print("event! - handle router version: %s", information ?? "")
38    let currentVersion : String = information as! String
39    if (packedVersion.compare(currentVersion, options: .numeric) == .orderedDescending) {
40      Swift.print("event! - router version: Packed version is newer, gonna re-deploy")
41      RouterManager.shared().eventManager.trigger(eventName: "router_must_upgrade", information: "got new version")
42    } else {
43      Swift.print("event! - router version: No update needed")
44      RouterManager.shared().eventManager.trigger(eventName: "router_can_start", information: "all ok")
45    }
46  }
47 
48  private static var sharedRouterManager: RouterManager = {
49    let inst = DetectJava()
50    let routerManager = RouterManager(detectJavaInstance: inst)
51   
52    // Configuration
53    // ...
54    routerManager.updateState()
55   
56    routerManager.eventManager.listenTo(eventName: "router_start", action: handleRouterStart)
57    routerManager.eventManager.listenTo(eventName: "router_stop", action: handleRouterStop)
58    routerManager.eventManager.listenTo(eventName: "router_pid", action: handleRouterPid)
59    routerManager.eventManager.listenTo(eventName: "router_version", action: handleRouterVersion)
60    return routerManager
61  }()
62 
63  // MARK: -
64 
65  let detectJava: DetectJava
66  private var routerInstance: I2PRouterTask?{
67    //Called after the change
68    didSet{
69      print("RouterManager.routerInstance did change to ", self.routerInstance ?? "null")
70      if (self.routerInstance != nil) {
71        RouterProcessStatus.isRouterRunning = (self.routerInstance?.isRouterRunning)!
72      }
73    }
74  };
75 
76  // Initialization
77 
78  private init(detectJavaInstance: DetectJava) {
79    self.detectJava = detectJavaInstance
80  }
81 
82  // MARK: - Accessors
83 
84  class func shared() -> RouterManager {
85    return sharedRouterManager
86  }
87 
88  func setRouterTask(router: I2PRouterTask) {
89    self.routerInstance = router
90  }
91 
92  func getRouterTask() -> I2PRouterTask? {
93    return self.routerInstance
94  }
95 
96  func updateState() {
97    self.routerInstance = SBridge.sharedInstance()?.currentRouterInstance
98  }
99 
100}
Note: See TracBrowser for help on using the repository browser.