Skip to content

Commit

Permalink
- don't use logger from context outside of context
Browse files Browse the repository at this point in the history
- context wrapper objects that are created for each event can not be used to save state so I commented out those non working statements
- try to add real shutdown for event handler and closing the db

Does not crash as often anymore, but:
- [ ] delay after waking up / unsuspeding, link2xt said probably because of missing cache
- [ ] events are not received by the UI anymore, althrough I sucessfully restarted the handler that the events are printed to the console.
- [ ] there is a performance warning because I wait in the main thread for the event thread to finish: "Thread Performance Checker: Thread running at QOS_CLASS_USER_INTERACTIVE waiting on a lower QoS thread running at QOS_CLASS_BACKGROUND. Investigate ways to avoid priority inversions"
- [ ] TODO: eventHandlerActive boolean should proabably be an atomic?
  • Loading branch information
Simon-Laux committed Nov 16, 2023
1 parent 42c87cb commit 237bfed
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 69 deletions.
6 changes: 5 additions & 1 deletion DcCore/DcCore/DC/Wrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class DcAccounts {
dc_accounts_unref(accountsPointer)
accountsPointer = nil
}

public func isOpen() -> Bool {
return accountsPointer != nil
}

public func migrate(dbLocation: String) -> Int {
return Int(dc_accounts_migrate_account(accountsPointer, dbLocation))
Expand Down Expand Up @@ -110,7 +114,7 @@ public class DcAccounts {

public class DcContext {

public var logger: Logger?
private var logger: Logger?
var contextPointer: OpaquePointer?
public var lastWarningString: String = "" // temporary thing to get a grip on some weird errors
public var maxConfigureProgress: Int = 0 // temporary thing to get a grip on some weird errors
Expand Down
59 changes: 30 additions & 29 deletions DcCore/DcCore/DC/events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,39 @@ public let dcNotificationWebxdcUpdate = Notification.Name(rawValue: "dcNotificat

public class DcEventHandler {
let dcAccounts: DcAccounts
let logger: Logger

public init(dcAccounts: DcAccounts) {
self.dcAccounts = dcAccounts
self.logger = self.dcAccounts.logger!
}

public func handleEvent(event: DcEvent) {
let id = event.id
let data1 = event.data1Int
let data2 = event.data2Int
let dcContext = dcAccounts.get(id: event.accountId)
// let dcContext = dcAccounts.get(id: event.accountId) // gets every time new context, so saving data on it makes no sense

if id >= DC_EVENT_ERROR && id <= 499 {
let s = event.data2String
dcContext.logger?.error("event: \(s)")
self.logger.error("event: \(s)")
return
}

switch id {

case DC_EVENT_INFO:
let s = event.data2String
dcContext.logger?.info("event: \(s)")
self.logger.info("event: \(s)")

case DC_EVENT_WARNING:
let s = event.data2String
dcContext.lastWarningString = s
dcContext.logger?.warning("event: \(s)")
// dcContext.lastWarningString = s
self.logger.warning("event: \(s)")

case DC_EVENT_CONFIGURE_PROGRESS:
dcContext.maxConfigureProgress = max(dcContext.maxConfigureProgress, Int(data1))
dcContext.logger?.info("configure progress: \(Int(data1)) \(Int(data2))")
// dcContext.maxConfigureProgress = max(dcContext.maxConfigureProgress, Int(data1))
self.logger.info("configure progress: \(Int(data1)) \(Int(data2))")
let nc = NotificationCenter.default
DispatchQueue.main.async {
let done = Int(data1) == 1000
Expand Down Expand Up @@ -78,19 +80,19 @@ public class DcEventHandler {
"progress": Int(data1),
"error": Int(data1) == 0,
"done": Int(data1) == 1000,
"errorMessage": dcContext.lastErrorString as Any,
"errorMessage": "not" // dcContext.lastErrorString as Any,
]
)
}

case DC_EVENT_IMAP_CONNECTED, DC_EVENT_SMTP_CONNECTED:
dcContext.logger?.warning("network: \(event.data2String)")
self.logger.warning("network: \(event.data2String)")

case DC_EVENT_MSGS_CHANGED, DC_EVENT_MSG_READ, DC_EVENT_MSG_DELIVERED, DC_EVENT_MSG_FAILED:
if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}
dcContext.logger?.info("change: \(id)")
self.logger.info("change: \(id)")

let nc = NotificationCenter.default

Expand All @@ -107,7 +109,7 @@ public class DcEventHandler {
}

case DC_EVENT_MSGS_NOTICED:
if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}
let nc = NotificationCenter.default
Expand All @@ -122,10 +124,10 @@ public class DcEventHandler {
}

case DC_EVENT_CHAT_MODIFIED:
if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}
dcContext.logger?.info("chat modified: \(id)")
self.logger.info("chat modified: \(id)")
let nc = NotificationCenter.default
DispatchQueue.main.async {
nc.post(
Expand All @@ -137,10 +139,10 @@ public class DcEventHandler {
)
}
case DC_EVENT_CHAT_EPHEMERAL_TIMER_MODIFIED:
if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}
dcContext.logger?.info("chat ephemeral timer modified: \(id)")
self.logger.info("chat ephemeral timer modified: \(id)")
let nc = NotificationCenter.default
DispatchQueue.main.async {
nc.post(
Expand All @@ -159,8 +161,7 @@ public class DcEventHandler {
object: nil,
userInfo: nil)
}

if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}

Expand All @@ -169,21 +170,21 @@ public class DcEventHandler {
"chat_id": Int(data1),
]

dcContext.logger?.info("incoming message \(userInfo)")
self.logger.info("incoming message \(userInfo)")
DispatchQueue.main.async {
nc.post(name: dcNotificationIncoming,
object: nil,
userInfo: userInfo)
}

case DC_EVENT_SMTP_MESSAGE_SENT:
dcContext.logger?.info("network: \(event.data2String)")
self.logger.info("network: \(event.data2String)")

case DC_EVENT_SECUREJOIN_INVITER_PROGRESS:
if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}
dcContext.logger?.info("securejoin inviter progress \(data1)")
self.logger.info("securejoin inviter progress \(data1)")

let nc = NotificationCenter.default
DispatchQueue.main.async {
Expand All @@ -199,10 +200,10 @@ public class DcEventHandler {
}

case DC_EVENT_CONTACTS_CHANGED:
if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}
dcContext.logger?.info("contact changed: \(data1)")
self.logger.info("contact changed: \(data1)")
let nc = NotificationCenter.default
DispatchQueue.main.async {
nc.post(
Expand All @@ -218,10 +219,10 @@ public class DcEventHandler {
if let sem = dcAccounts.fetchSemaphore, dcAccounts.isAllWorkDone() {
sem.signal()
}
if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}
dcContext.logger?.info("network: DC_EVENT_CONNECTIVITY_CHANGED: \(dcContext.getConnectivity())")
self.logger.info("network: DC_EVENT_CONNECTIVITY_CHANGED: \(dcAccounts.get(id: event.accountId).getConnectivity())")
DispatchQueue.main.async {
let nc = NotificationCenter.default
nc.post(
Expand All @@ -232,10 +233,10 @@ public class DcEventHandler {
}

case DC_EVENT_WEBXDC_STATUS_UPDATE:
if dcContext.id != dcAccounts.getSelected().id {
if event.accountId != dcAccounts.getSelected().id {
return
}
dcContext.logger?.info("webxdc: update!")
self.logger.info("webxdc: update!")
DispatchQueue.main.async {
let nc = NotificationCenter.default
nc.post(
Expand All @@ -248,7 +249,7 @@ public class DcEventHandler {
}

default:
dcContext.logger?.warning("unknown event: \(id)")
self.logger.warning("unknown event: \(id)")
}
}

Expand Down
2 changes: 1 addition & 1 deletion DcCore/DcCore/Helper/DcUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct DcUtils {
interaction.groupIdentifier = "\(context.id)"
interaction.donate(completion: { error in
if error != nil {
context.logger?.error(error.debugDescription)
print(DateUtils.getTimestamp(), "❤️", "donateSendMessageIntent failed:" + error.debugDescription)
}
})
}
Expand Down
23 changes: 12 additions & 11 deletions DcShare/Helper/ShareAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protocol ShareAttachmentDelegate: class {
}

class ShareAttachment {
let logger = SimpleLogger()

weak var delegate: ShareAttachmentDelegate?
let dcContext: DcContext
Expand Down Expand Up @@ -80,7 +81,7 @@ class ShareAttachment {
case let url as URL:
result = SDAnimatedImage(contentsOfFile: url.path)
default:
self.dcContext.logger?.debug("Unexpected data: \(type(of: data))")
self.logger.debug("Unexpected data: \(type(of: data))")
}
if let result = result {
let path = ImageFormat.saveImage(image: result, directory: .cachesDirectory)
Expand All @@ -93,7 +94,7 @@ class ShareAttachment {
self.delegate?.onThumbnailChanged()
}
if let error = error {
self.dcContext.logger?.error("Could not load share item as image: \(error.localizedDescription)")
self.logger.error("Could not load share item as image: \(error.localizedDescription)")
}
}
}
Expand All @@ -110,7 +111,7 @@ class ShareAttachment {
case let url as URL:
result = ImageFormat.loadImageFrom(url: url)
default:
self.dcContext.logger?.debug("Unexpected data: \(type(of: data))")
self.logger.debug("Unexpected data: \(type(of: data))")
result = nil
}
if let result = result,
Expand All @@ -125,7 +126,7 @@ class ShareAttachment {
}
}
if let error = error {
self.dcContext.logger?.error("Could not load share item as image: \(error.localizedDescription)")
self.logger.error("Could not load share item as image: \(error.localizedDescription)")
}
}
}
Expand All @@ -146,10 +147,10 @@ class ShareAttachment {

}
default:
self.dcContext.logger?.debug("Unexpected data: \(type(of: data))")
self.logger.debug("Unexpected data: \(type(of: data))")
}
if let error = error {
self.dcContext.logger?.error("Could not load share item as video: \(error.localizedDescription)")
self.logger.error("Could not load share item as video: \(error.localizedDescription)")
}
}
}
Expand Down Expand Up @@ -182,10 +183,10 @@ class ShareAttachment {
self.generateThumbnailRepresentations(url: url)
}
default:
self.dcContext.logger?.debug("Unexpected data: \(type(of: data))")
self.logger.debug("Unexpected data: \(type(of: data))")
}
if let error = error {
self.dcContext.logger?.error("Could not load share item: \(error.localizedDescription)")
self.logger.error("Could not load share item: \(error.localizedDescription)")
}
}
}
Expand All @@ -210,7 +211,7 @@ class ShareAttachment {
generator.generateRepresentations(for: request) { (thumbnail, _, error) in
DispatchQueue.main.async {
if thumbnail == nil || error != nil {
self.dcContext.logger?.warning(error?.localizedDescription ?? "Could not create thumbnail.")
self.logger.warning(error?.localizedDescription ?? "Could not create thumbnail.")
} else {
self.attachmentThumbnail = thumbnail?.uiImage
self.delegate?.onThumbnailChanged()
Expand All @@ -231,10 +232,10 @@ class ShareAttachment {
case let url as URL:
delegate.onUrlShared(url: url)
default:
self.dcContext.logger?.debug("Unexpected data: \(type(of: data))")
self.logger.debug("Unexpected data: \(type(of: data))")
}
if let error = error {
self.dcContext.logger?.error("Could not share URL: \(error.localizedDescription)")
self.logger.error("Could not share URL: \(error.localizedDescription)")
}
}
}
Expand Down
Loading

0 comments on commit 237bfed

Please sign in to comment.