Skip to content

Commit

Permalink
#27: add simple XMPP logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Dec 12, 2016
1 parent 1366c3c commit b8e1c92
Show file tree
Hide file tree
Showing 5 changed files with 1,228 additions and 729 deletions.
6 changes: 0 additions & 6 deletions Ctor.Xmpp/Program.fs

This file was deleted.

41 changes: 41 additions & 0 deletions Ctor.Xmpp/Robot.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Ctor.Xmpp

open System
open System.Threading

open Microsoft.Extensions.Logging
open SharpXMPP
open SharpXMPP.XMPP

type Robot(loggerFactory: ILoggerFactory,
login: string,
password: string,
roomJid: string,
nickname: string) =
let logger = loggerFactory.CreateLogger<Robot>()
let connection = new XmppClient(JID(login), password)

let connectionFailedHandler = XmppConnection.ConnectionFailedHandler(fun s e ->
logger.LogError(e.ToString())
Thread.Sleep(TimeSpan.FromSeconds(30.0)) // TODO[Friedrich]: Configurable timeout.
())

let signedInHandler = XmppConnection.SignedInHandler(fun s e ->
logger.LogInformation("Connecting to " + roomJid)
SharpXmppHelper.joinRoom connection roomJid nickname)

let messageHandler = XmppConnection.MessageHandler(fun s e ->
logger.LogInformation("<-" + e.ToString()))

let elementHandler = XmppConnection.ElementHandler(fun s e ->
let arrow = if e.IsInput then "<-" else "->"
logger.LogInformation(arrow + " " + e.ToString()))

do
connection.add_ConnectionFailed(connectionFailedHandler)
connection.add_SignedIn(signedInHandler)
connection.add_Message(messageHandler)
connection.add_Element(elementHandler)

interface IDisposable with
member __.Dispose() = connection.Close()
17 changes: 17 additions & 0 deletions Ctor.Xmpp/SharpXmppHelper.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Ctor.Xmpp.SharpXmppHelper

open System.Xml.Linq

open SharpXMPP
open SharpXMPP.XMPP.Client.MUC.Bookmarks.Elements

let private bookmark (roomJid: string) (nickname: string): BookmarkedConference =
let room = BookmarkedConference()
room.SetAttributeValue(XName.Get("jid"), roomJid)
let nickElement = XElement(XName.Get("storage:bookmarks"), "nick", Value = nickname)
room.Add(nickElement)
room

let joinRoom (client: XmppClient) (roomJid: string) (nickname: string): unit =
let room = bookmark roomJid nickname
client.BookmarkManager.Join(room)
7 changes: 5 additions & 2 deletions Ctor.Xmpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"compilerName": "fsc",
"compile": {
"includeFiles": [
"Program.fs"
"SharpXmppHelper.fs",
"Robot.fs"
]
},
"preserveCompilationContext": true
Expand All @@ -14,11 +15,13 @@
"netcoreapp1.0": {}
},
"dependencies": {
"Microsoft.Extensions.Logging": "1.1.0",
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
},
"Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160629"
"SharpXMPP.Shared": "0.0.1-pre01"
},
"tools": {
"dotnet-compile-fsc": "1.0.0-preview2-*"
Expand Down
Loading

0 comments on commit b8e1c92

Please sign in to comment.