From 219b1b821164b2b399e2bbc10e519c930284a23b Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Fri, 4 Dec 2020 18:00:00 +0200 Subject: [PATCH] Upgraded to new libdnet, working on restoring notifications support to skippy --- dub.json | 2 +- dub.selections.json | 2 +- source/app.d | 2 +- source/notifications.d | 159 +++++++++++++++++++++-------------------- 4 files changed, 84 insertions(+), 81 deletions(-) diff --git a/dub.json b/dub.json index e380fb8..af9bbe3 100644 --- a/dub.json +++ b/dub.json @@ -4,7 +4,7 @@ ], "copyright": "Copyright © 2020, Tristan B. Kildaire", "dependencies": { - "libdnet": "~>0.1.13" + "libdnet": "~>0.1.15" }, "description": "dnet client", "license": "GPLv3", diff --git a/dub.selections.json b/dub.selections.json index 1236855..67df67e 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -2,7 +2,7 @@ "fileVersion": 1, "versions": { "bformat": "1.0.8", - "libdnet": "0.1.13", + "libdnet": "0.1.15", "tristanable": "0.1.1" } } diff --git a/source/app.d b/source/app.d index 5d2793b..88fb3cb 100644 --- a/source/app.d +++ b/source/app.d @@ -128,7 +128,7 @@ void commandLine() addr = getAddress(address, to!(ushort)(port))[0]; writeln("Connecting to "~to!(string)(addr)~"..."); dclient = new DClient(addr); - //dnotifications= new NotificationWatcher(dclient.getManager()); + dnotifications= new NotificationWatcher(dclient); writeln("Connected!"); /* Get the message of the day */ diff --git a/source/notifications.d b/source/notifications.d index c0a132c..7aaeb3e 100644 --- a/source/notifications.d +++ b/source/notifications.d @@ -1,91 +1,94 @@ -// import core.thread : Thread; -// import tristanable.manager; -// import tristanable.notifications; -// import std.stdio; -// import core.time : dur; -// import tristanable.encoding; +import core.thread : Thread; +import tristanable.manager; +import tristanable.notifications; +import std.stdio; +import core.time : dur; +import tristanable.encoding; -// public class NotificationWatcher : Thread -// { -// private Manager manager; +public class NotificationWatcher : Thread +{ + private DClient client; -// this(Manager manager) -// { -// super(&worker); + this(DClient manager) + { + super(&worker); -// this.manager = manager; -// manager.reserveTag(0); + this.manager = manager; + manager.reserveTag(0); -// start(); -// } + start(); + } -// private void worker() -// { -// while(true) -// { -// /* Check for notifications every 2 seconds */ -// NotificationReply[] notifications =manager.popNotifications(); + private void worker() + { + while(true) + { + /* Check for notifications every 2 seconds */ + NotificationReply[] notifications =manager.popNotifications(); -// if(notifications.length) -// { -// //writeln(notifications); -// foreach(NotificationReply notificationReply; notifications) -// { -// //writeln(notificationReply.getData()); -// string msg = cast(string)notificationReply.getData(); -// writeln("!> "~msg); -// process(notificationReply.getData()); -// } -// } + if(notifications.length) + { + //writeln(notifications); + foreach(NotificationReply notificationReply; notifications) + { + //writeln(notificationReply.getData()); -// Thread.getThis().sleep(dur!("seconds")(2)); -// } -// } + /* Await a notification */ + manager.awaitNotification(); + string msg = cast(string)notificationReply.getData(); + writeln("!> "~msg); + process(notificationReply.getData()); + } + } -// /** -// * Processes an incoming notification -// * accordingly -// */ -// private void process(byte[] data) -// { -// /* TODO: Implement me */ + Thread.getThis().sleep(dur!("seconds")(2)); + } + } -// /* TODO: Check notification type */ -// byte notificationType = data[0]; + /** + * Processes an incoming notification + * accordingly + */ + private void process(byte[] data) + { + /* TODO: Implement me */ -// /* For normal message (to channel or user) */ -// if(notificationType == cast(byte)0) -// { -// /* TODO: Decode using tristanable */ -// writeln("new message"); -// } -// /* Channel notification (ntype=1) */ -// else if(notificationType == cast(byte)1) -// { -// /* TODO: Decode using tristanable */ -// /* TODO: Get the username of the user that left */ -// //writeln("user left/join message"); + /* TODO: Check notification type */ + byte notificationType = data[0]; -// /* Get the sub-type */ -// byte subType = data[1]; + /* For normal message (to channel or user) */ + if(notificationType == cast(byte)0) + { + /* TODO: Decode using tristanable */ + writeln("new message"); + } + /* Channel notification (ntype=1) */ + else if(notificationType == cast(byte)1) + { + /* TODO: Decode using tristanable */ + /* TODO: Get the username of the user that left */ + //writeln("user left/join message"); -// /* If the notification was leave (stype=0) */ -// if(subType == cast(byte)0) -// { -// string username = cast(string)data[2..data.length]; -// writeln("<-- "~username~" left the channel"); -// } -// /* If the notification was join (stype=1) */ -// else if(subType == cast(byte)1) -// { -// string username = cast(string)data[2..data.length]; -// writeln("--> "~username~" joined the channel"); -// } -// /* TODO: Unknown */ -// else -// { + /* Get the sub-type */ + byte subType = data[1]; + + /* If the notification was leave (stype=0) */ + if(subType == cast(byte)0) + { + string username = cast(string)data[2..data.length]; + writeln("<-- "~username~" left the channel"); + } + /* If the notification was join (stype=1) */ + else if(subType == cast(byte)1) + { + string username = cast(string)data[2..data.length]; + writeln("--> "~username~" joined the channel"); + } + /* TODO: Unknown */ + else + { -// } -// } -// } -// } \ No newline at end of file + } + } + } +} \ No newline at end of file