2020-12-04 16:00:00 +00:00
|
|
|
import core.thread : Thread;
|
|
|
|
import tristanable.manager;
|
|
|
|
import std.stdio;
|
|
|
|
import core.time : dur;
|
|
|
|
import tristanable.encoding;
|
2020-12-04 16:13:16 +00:00
|
|
|
import libdnet.dclient : DClient;
|
2020-09-25 17:51:24 +00:00
|
|
|
|
2020-12-04 16:00:00 +00:00
|
|
|
public class NotificationWatcher : Thread
|
|
|
|
{
|
|
|
|
private DClient client;
|
2020-09-25 17:51:24 +00:00
|
|
|
|
2020-12-04 16:00:43 +00:00
|
|
|
this(DClient client)
|
2020-12-04 16:00:00 +00:00
|
|
|
{
|
|
|
|
super(&worker);
|
2020-12-04 16:00:43 +00:00
|
|
|
this.client = client;
|
2020-09-25 17:51:24 +00:00
|
|
|
|
2020-12-04 16:00:43 +00:00
|
|
|
/* Start the notification watcher */
|
2020-12-04 16:00:00 +00:00
|
|
|
start();
|
|
|
|
}
|
2020-09-25 17:51:24 +00:00
|
|
|
|
2020-12-04 16:00:00 +00:00
|
|
|
private void worker()
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
2020-12-04 16:02:32 +00:00
|
|
|
/* Await a notification */
|
2020-12-04 16:13:16 +00:00
|
|
|
byte[] notification = client.awaitNotification();
|
2020-12-04 16:02:32 +00:00
|
|
|
process(notification);
|
2020-12-04 16:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 20:16:23 +00:00
|
|
|
|
2020-12-04 16:00:00 +00:00
|
|
|
/**
|
|
|
|
* Processes an incoming notification
|
|
|
|
* accordingly
|
|
|
|
*/
|
|
|
|
private void process(byte[] data)
|
|
|
|
{
|
|
|
|
/* TODO: Implement me */
|
2020-09-28 11:47:46 +00:00
|
|
|
|
2020-12-04 16:00:00 +00:00
|
|
|
/* TODO: Check notification type */
|
|
|
|
byte notificationType = data[0];
|
2020-09-28 12:05:43 +00:00
|
|
|
|
2020-12-04 16:00:00 +00:00
|
|
|
/* 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");
|
2020-09-28 12:05:43 +00:00
|
|
|
|
2020-12-04 16:00:00 +00:00
|
|
|
/* 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
|
|
|
|
{
|
2020-09-28 12:05:43 +00:00
|
|
|
|
2020-12-04 16:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|