Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
xjbeta committed Mar 20, 2020
2 parents 73ff608 + dbd8f88 commit e61c94d
Show file tree
Hide file tree
Showing 47 changed files with 868 additions and 612 deletions.
124 changes: 91 additions & 33 deletions Aria2D.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

90 changes: 51 additions & 39 deletions Aria2D/Aria2/Aria2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ class Aria2: NSObject {
var result: [[[Aria2Object]]]
}

let params = [[Aria2WebsocketParams(method: Aria2Method.tellActive,
params: nil ).object,
Aria2WebsocketParams(method: Aria2Method.tellWaiting,
params: [0, 1000]).object,
Aria2WebsocketParams(method: Aria2Method.tellStopped,
params: [0, 1000]).object]]
let params = [[
Aria2WebsocketParams(
method: Aria2Method.tellActive,
params: nil ).object,
Aria2WebsocketParams(
method: Aria2Method.tellWaiting,
params: [0, 1000]).object,
Aria2WebsocketParams(
method: Aria2Method.tellStopped,
params: [0, 1000]).object]]

send(method: Aria2Method.multicall,
params: params)
Expand All @@ -51,13 +55,17 @@ class Aria2: NSObject {
}

private func sortAllData() {
send(method: Aria2Method.multicall,
params: [[Aria2WebsocketParams(method: Aria2Method.tellActive,
params: [["gid", "status"]]).object,
Aria2WebsocketParams(method: Aria2Method.tellWaiting,
params: [0, 1000, ["gid", "status"]]).object,
Aria2WebsocketParams(method: Aria2Method.tellStopped,
params: [0, 1000, ["gid", "status"]]).object]])
send(method: Aria2Method.multicall,
params: [[
Aria2WebsocketParams(
method: Aria2Method.tellActive,
params: [["gid", "status"]]).object,
Aria2WebsocketParams(
method: Aria2Method.tellWaiting,
params: [0, 1000, ["gid", "status"]]).object,
Aria2WebsocketParams(
method: Aria2Method.tellStopped,
params: [0, 1000, ["gid", "status"]]).object]])
.done { data in
struct GIDList: Decodable {
var result: [[[[String: String]]]]
Expand Down Expand Up @@ -87,19 +95,22 @@ class Aria2: NSObject {

func updateActiveTasks() {
send(method: Aria2Method.multicall,
params: [[Aria2WebsocketParams(method: Aria2Method.tellActive,
params: [["gid",
"status",
"completedLength",
"totalLength",
"downloadSpeed",
"uploadLength",
"uploadSpeed",
"connections",
"bittorrent",
"dir"]]).object,
Aria2WebsocketParams(method: Aria2Method.getGlobalStat,
params: []).object]])
params: [[
Aria2WebsocketParams(
method: Aria2Method.tellActive,
params: [["gid",
"status",
"completedLength",
"totalLength",
"downloadSpeed",
"uploadLength",
"uploadSpeed",
"connections",
"bittorrent",
"dir"]]).object,
Aria2WebsocketParams(
method: Aria2Method.getGlobalStat,
params: []).object]])
.done { data in
struct Result: Decodable {
let result: [ResultObj]
Expand All @@ -123,25 +134,26 @@ class Aria2: NSObject {
try DataManager.shared.updateStatus($0.status)
}
}
}.catch {
Log("\(#function) error \($0)")
}.catch {
Log("\(#function) error \($0)")
}
}

func updateStatus(_ gids: [String], block: (([Aria2Status]) -> Void)? = nil) {
guard effectiveGIDs(gids).count > 0 else { return }
let params = gids.map {
Aria2WebsocketParams(method: Aria2Method.tellStatus,
params: [$0, ["gid",
"status",
"completedLength",
"totalLength",
"downloadSpeed",
"uploadLength",
"uploadSpeed",
"connections",
"bittorrent",
"dir"]]).object
Aria2WebsocketParams(
method: Aria2Method.tellStatus,
params: [$0, ["gid",
"status",
"completedLength",
"totalLength",
"downloadSpeed",
"uploadLength",
"uploadSpeed",
"connections",
"bittorrent",
"dir"]]).object
}

send(method: Aria2Method.multicall,
Expand Down
71 changes: 50 additions & 21 deletions Aria2D/Aria2/Aria2Websocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Cocoa
import SocketRocket
import Starscream
import PromiseKit

struct ConnectedServerInfo {
Expand All @@ -22,14 +22,9 @@ class Aria2Websocket: NSObject {
private override init() {
}

var socket: SRWebSocket? = nil
var socket: WebSocket? = nil

var isConnected: Bool {
get {
return socket?.readyState == .OPEN
}
}

var isConnected = false
var connectedServerInfo = ConnectedServerInfo()

var aria2GlobalOption = [Aria2Option: String]() {
Expand All @@ -44,14 +39,14 @@ class Aria2Websocket: NSObject {
self.timer = nil
}

socket?.close()
socket?.disconnect()

if let url = Preferences.shared.aria2Servers.serverURL() {
guard url.host != nil else { return }

socket = SRWebSocket(url: url)
socket = WebSocket(request: .init(url: url))
socket?.delegate = self
socket?.open()
socket?.connect()
startTimer()
}

Expand All @@ -77,11 +72,11 @@ class Aria2Websocket: NSObject {
timer.schedule(deadline: .now(), repeating: .seconds(1))
timer.setEventHandler {
if !self.isConnected {
self.socket?.close()
let url = self.socket?.url
self.socket = SRWebSocket(url: url!)
self.socket?.disconnect()
guard let url = self.socket?.request.url else { return }
self.socket = WebSocket.init(request: .init(url: url))
self.socket?.delegate = self
self.socket?.open()
self.socket?.connect()
} else {
DispatchQueue.main.async {
guard let count = try? DataManager.shared.activeCount(),
Expand Down Expand Up @@ -203,7 +198,7 @@ class Aria2Websocket: NSObject {
}
}
do {
try socket?.send(data: try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted))
socket?.write(data: try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted), completion: nil)
} catch let er {
resolver.reject(webSocketResult.receiveError(message: "\(er)"))
return
Expand All @@ -222,8 +217,42 @@ enum webSocketResult: Error {
case somethingError
}

extension Aria2Websocket: SRWebSocketDelegate {
func webSocketDidOpen(_ webSocket: SRWebSocket) {
extension Aria2Websocket: WebSocketDelegate {

func didReceive(event: WebSocketEvent, client: WebSocket) {
switch event {
case .connected(let headers):
isConnected = true
Log("websocket is connected: \(headers)")
webSocketDidOpen()
case .disconnected(let reason, let code):
isConnected = false
Log("websocket is disconnected: \(reason) with code: \(code)")
webSocket(didCloseWithCode: Int(code), reason: reason)
case .text(let string):
webSocket(didReceiveMessageWith: string)
case .binary(let data):
Log("Received data: \(data.count)")
case .ping(_):
Log("websocket ping")
case .pong(_):
Log("websocket pong")
case .viablityChanged(_):
Log("websocket viablityChanged")
case .reconnectSuggested(_):
Log("websocket reconnectSuggested")
case .cancelled:
isConnected = false
Log("websocket cancelled")
case .error(let error):
isConnected = false
Log("websocket error \(String(describing: error))")
}
}



func webSocketDidOpen() {
Aria2.shared.initData.run()
connectedServerInfo.name = Preferences.shared.aria2Servers.getSelectedName()
Aria2.shared.getVersion {
Expand All @@ -237,16 +266,16 @@ extension Aria2Websocket: SRWebSocketDelegate {
NotificationCenter.default.post(name: .updateGlobalStat, object: nil, userInfo: ["updateServer": true])
}

func webSocket(_ webSocket: SRWebSocket, didCloseWithCode code: Int, reason: String?, wasClean: Bool) {
connectedServerInfo.version = reason ?? ""
func webSocket(didCloseWithCode code: Int, reason: String) {
connectedServerInfo.version = reason
connectedServerInfo.enabledFeatures = ""
aria2GlobalOption = [:]
DataManager.shared.deleteAllAria2Objects()
NotificationCenter.default.post(name: .updateConnectStatus, object: nil)
NotificationCenter.default.post(name: .updateGlobalStat, object: nil, userInfo: ["updateServer": true])
}

func webSocket(_ webSocket: SRWebSocket, didReceiveMessageWith string: String) {
func webSocket(didReceiveMessageWith string: String) {
if let data = string.data(using: .utf8) {
if let json = try? JSONDecoder().decode(JSONRPC.self, from: data),
json.id.count == 36 {
Expand Down
6 changes: 5 additions & 1 deletion Aria2D/Source/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} catch {
// Customize this code block to include application-specific recovery steps.
let nserror = error as NSError
NSApplication.shared.presentError(nserror)
let re = NSApp.presentError(nserror)
// click ok button
if !re {
NSApp.terminate(self)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Aria2D/Source/Utils/EnumList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum PreferenceKeys: String {
case completeNotice = "app_completeNotice"
case showAria2Features = "app_showAria2Features"
case showGlobalSpeed = "app_showGlobalSpeed"
case showDockIconSpeed = "app_showDockIconSpeed"
case openMagnetLink = "app_openMagnetLink"

case autoStartAria2c = "aria2c_autoStartAria2c"
Expand Down
10 changes: 10 additions & 0 deletions Aria2D/Source/Utils/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ class Preferences: NSObject {
}
}

@objc var showDockIconSpeed: Bool {
get {
return defaults(.showDockIconSpeed) as? Bool ?? true
}
set {
defaultsSet(newValue, forKey: .showDockIconSpeed)
NotificationCenter.default.post(name: .updateGlobalStat, object: nil)
}
}

@objc var openMagnetLink: Bool {
get {
return defaults(.openMagnetLink) as? Bool ?? true
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit e61c94d

Please sign in to comment.