Skip to content

Commit

Permalink
Better construction and disposement logic for XmppClient (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jul 20, 2019
1 parent 6702faa commit fa71018
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Emulsion/MessageSystem.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type MessageSystemBase(ctx: RestartContext, cancellationToken: CancellationToken

/// Starts the IM connection, manages reconnects. On cancellation could either throw OperationCanceledException or
/// return a unit.
///
/// This method will never be called multiple times in parallel on a single instance.
abstract member RunUntilError : IncomingMessageReceiver -> Async<unit>

/// Sends a message through the message system. Free-threaded. Could throw exceptions; if throws an exception, then
Expand Down
18 changes: 14 additions & 4 deletions Emulsion/Xmpp/Client.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ open Emulsion.Settings

type Client(ctx: RestartContext, cancellationToken: CancellationToken, settings: XmppSettings) =
inherit MessageSystemBase(ctx, cancellationToken)
let client = XmppClient.create settings

override __.RunUntilError receiver =
XmppClient.run settings client receiver
let client = ref None

override __.RunUntilError receiver = async {
use newClient = XmppClient.create settings receiver
try
Volatile.Write(client, Some newClient)
do! XmppClient.run newClient
finally
Volatile.Write(client, None)
}

override __.Send (OutgoingMessage message) = async {
return XmppClient.send settings client message
match Volatile.Read(client) with
| None -> failwith "Client is offline"
| Some client ->
return XmppClient.send settings client message
}
16 changes: 7 additions & 9 deletions Emulsion/Xmpp/XmppClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,39 @@ let private elementHandler = XmppConnection.ElementHandler(fun s e ->
let private presenceHandler = XmppConnection.PresenceHandler(fun s e ->
printfn "[P]: %A" e)

let create (settings: XmppSettings): XmppClient =
let client = XmppClient(JID(settings.login), settings.password)
let create (settings: XmppSettings) (onMessage: IncomingMessage -> unit): XmppClient =
let client = new XmppClient(JID(settings.login), settings.password)
client.add_ConnectionFailed(connectionFailedHandler)
client.add_SignedIn(signedInHandler settings client)
client.add_Element(elementHandler)
client.add_Presence(presenceHandler)
client.add_Message(messageHandler settings onMessage)
client

exception ConnectionFailedError of string
with
override this.ToString() =
sprintf "%A" this

let run (settings: XmppSettings) (client: XmppClient) (onMessage: IncomingMessage -> unit): Async<unit> =
let run (client: XmppClient): Async<unit> =
printfn "Bot name: %s" client.Jid.FullJid
let handler = messageHandler settings onMessage
let tcs = TaskCompletionSource()
let connectionFinished = TaskCompletionSource()
let connectionFailedHandler =
XmppConnection.ConnectionFailedHandler(
fun _ error -> tcs.SetException(ConnectionFailedError error.Message)
fun _ error -> connectionFinished.SetException(ConnectionFailedError error.Message)
)

async {
try
let! token = Async.CancellationToken
use _ = token.Register(fun () -> client.Close())

client.add_Message handler
client.add_ConnectionFailed connectionFailedHandler
do! Async.AwaitTask(client.ConnectAsync token)

do! Async.AwaitTask tcs.Task
do! Async.AwaitTask connectionFinished.Task
finally
client.remove_ConnectionFailed connectionFailedHandler
client.remove_Message handler
}

let send (settings: XmppSettings) (client: XmppClient) (message: Message): unit =
Expand Down

0 comments on commit fa71018

Please sign in to comment.