Promise aware Message sender

As this could be used elsewhere, ideally it'd added to the actual class
definition, but MessageSender (SSK) doesn't use PromiseKit

// FREEBIE
pull/1/head
Michael Kirk 8 years ago committed by Matthew Chen
parent f9b44c8892
commit 6eecef99ba

@ -178,7 +178,7 @@ fileprivate let timeoutSeconds = 60
return self.peerConnectionClient!.setLocalSessionDescription(sessionDescription).then(on: CallService.signalingQueue) { return self.peerConnectionClient!.setLocalSessionDescription(sessionDescription).then(on: CallService.signalingQueue) {
let offerMessage = OWSCallOfferMessage(callId: call.signalingId, sessionDescription: sessionDescription.sdp) let offerMessage = OWSCallOfferMessage(callId: call.signalingId, sessionDescription: sessionDescription.sdp)
let callMessage = OWSOutgoingCallMessage(thread: thread, offerMessage: offerMessage) let callMessage = OWSOutgoingCallMessage(thread: thread, offerMessage: offerMessage)
return self.sendMessage(callMessage) return self.messageSender.sendCallMessage(callMessage)
} }
}.catch(on: CallService.signalingQueue) { error in }.catch(on: CallService.signalingQueue) { error in
Logger.error("\(self.TAG) placing call failed with error: \(error)") Logger.error("\(self.TAG) placing call failed with error: \(error)")
@ -215,7 +215,7 @@ fileprivate let timeoutSeconds = 60
if pendingIceUpdateMessages.count > 0 { if pendingIceUpdateMessages.count > 0 {
let callMessage = OWSOutgoingCallMessage(thread: thread, iceUpdateMessages: pendingIceUpdateMessages) let callMessage = OWSOutgoingCallMessage(thread: thread, iceUpdateMessages: pendingIceUpdateMessages)
_ = sendMessage(callMessage).catch { error in _ = messageSender.sendCallMessage(callMessage).catch { error in
Logger.error("\(self.TAG) failed to send ice updates in \(#function) with error: \(error)") Logger.error("\(self.TAG) failed to send ice updates in \(#function) with error: \(error)")
} }
} }
@ -329,7 +329,7 @@ fileprivate let timeoutSeconds = 60
let answerMessage = OWSCallAnswerMessage(callId: newCall.signalingId, sessionDescription: negotiatedSessionDescription.sdp) let answerMessage = OWSCallAnswerMessage(callId: newCall.signalingId, sessionDescription: negotiatedSessionDescription.sdp)
let callAnswerMessage = OWSOutgoingCallMessage(thread: thread, answerMessage: answerMessage) let callAnswerMessage = OWSOutgoingCallMessage(thread: thread, answerMessage: answerMessage)
return self.sendMessage(callAnswerMessage) return self.messageSender.sendCallMessage(callAnswerMessage)
}.then(on: CallService.signalingQueue) { }.then(on: CallService.signalingQueue) {
Logger.debug("\(self.TAG) successfully sent callAnswerMessage") Logger.debug("\(self.TAG) successfully sent callAnswerMessage")
@ -438,7 +438,7 @@ fileprivate let timeoutSeconds = 60
if self.sendIceUpdatesImmediately { if self.sendIceUpdatesImmediately {
let callMessage = OWSOutgoingCallMessage(thread: thread, iceUpdateMessage: iceUpdateMessage) let callMessage = OWSOutgoingCallMessage(thread: thread, iceUpdateMessage: iceUpdateMessage)
_ = sendMessage(callMessage) _ = self.messageSender.sendCallMessage(callMessage)
} else { } else {
// For outgoing messages, we wait to send ice updates until we're sure client received our call message. // For outgoing messages, we wait to send ice updates until we're sure client received our call message.
// e.g. if the client has blocked our message due to an identity change, we'd otherwise // e.g. if the client has blocked our message due to an identity change, we'd otherwise
@ -702,7 +702,7 @@ fileprivate let timeoutSeconds = 60
// If the call hasn't started yet, we don't have a data channel to communicate the hang up. Use Signal Service Message. // If the call hasn't started yet, we don't have a data channel to communicate the hang up. Use Signal Service Message.
let hangupMessage = OWSCallHangupMessage(callId: call.signalingId) let hangupMessage = OWSCallHangupMessage(callId: call.signalingId)
let callMessage = OWSOutgoingCallMessage(thread: thread, hangupMessage: hangupMessage) let callMessage = OWSOutgoingCallMessage(thread: thread, hangupMessage: hangupMessage)
_ = sendMessage(callMessage).then(on: CallService.signalingQueue) { _ = self.messageSender.sendCallMessage(callMessage).then(on: CallService.signalingQueue) {
Logger.debug("\(self.TAG) successfully sent hangup call message to \(thread)") Logger.debug("\(self.TAG) successfully sent hangup call message to \(thread)")
}.catch(on: CallService.signalingQueue) { error in }.catch(on: CallService.signalingQueue) { error in
Logger.error("\(self.TAG) failed to send hangup call message to \(thread) with error: \(error)") Logger.error("\(self.TAG) failed to send hangup call message to \(thread) with error: \(error)")
@ -820,12 +820,6 @@ fileprivate let timeoutSeconds = 60
} }
} }
private func sendMessage(_ message: OWSOutgoingCallMessage) -> Promise<Void> {
return Promise { fulfill, reject in
self.messageSender.send(message, success: fulfill, failure: reject)
}
}
public func handleFailedCall(error: CallError) { public func handleFailedCall(error: CallError) {
assertOnSignalingQueue() assertOnSignalingQueue()
Logger.error("\(TAG) call failed with error: \(error)") Logger.error("\(TAG) call failed with error: \(error)")
@ -1037,3 +1031,14 @@ fileprivate extension RTCIceConnectionState {
} }
} }
} }
fileprivate extension MessageSender {
/**
* Wrap message sending in a Promise for easier callback chaining.
*/
fileprivate func sendCallMessage(_ message: OWSOutgoingCallMessage) -> Promise<Void> {
return Promise { fulfill, reject in
self.send(message, success: fulfill, failure: reject)
}
}
}

Loading…
Cancel
Save