diff --git a/source/dnetd/dchannel.d b/source/dnetd/dchannel.d index 1316f1d..91b93cc 100644 --- a/source/dnetd/dchannel.d +++ b/source/dnetd/dchannel.d @@ -102,6 +102,30 @@ public class DChannel return memberCount; } + public bool isMember(DConnection client) + { + /* Whether or not you are a member */ + bool isMem; + + /* Lock the members list */ + memberLock.lock(); + + /* CHeck if you are in this channel */ + foreach(DConnection member; members) + { + if(member is client) + { + isMem = true; + break; + } + } + + /* Unlock the members list */ + memberLock.unlock(); + + return isMem; + } + /** * Removes the given client from this channel */ @@ -125,6 +149,8 @@ public class DChannel /* Set it as the new list */ members = newMembers; + /* TODO: Send left message here */ + /* Unlock the members list */ memberLock.unlock(); } @@ -132,9 +158,12 @@ public class DChannel public bool sendMessage(DConnection sender, string message) { bool status; - /* TODO: Generate message */ - /* TODO: Spec out in protocol */ - /* TODO: Reserved tag 0 for notifications */ + + /* The protocol data to send */ + byte[] msg; + + /* Set the notificaiton type */ + msg ~= [0]; /** * Format @@ -143,7 +172,7 @@ public class DChannel * byte length of name of channel/person (dm case) * message-bytes */ - byte[] msg = [cast(byte)1,(cast(byte)sender.getUsername().length)]~cast(byte[])sender.getUsername()~cast(byte[])message; + msg ~= [cast(byte)1,(cast(byte)sender.getUsername().length)]~cast(byte[])sender.getUsername()~cast(byte[])message; /* Send the message to everyone else in the channel */ foreach(DConnection member; members) @@ -152,9 +181,9 @@ public class DChannel if(!(member is sender)) { /* Send the message */ - writeln("Delivering message for channel '"~name~"' to user '"~member.getUsername()~"'..."); + writeln("Delivering message '"~message~"' for channel '"~name~"' to user '"~member.getUsername()~"'..."); status = member.writeSocket(0, msg); - writeln("Delivered message for channel '"~name~"' to user '"~member.getUsername()~"'!"); + writeln("Delivered message '"~message~"' for channel '"~name~"' to user '"~member.getUsername()~"'!"); /* TODO: Errors from status */ } diff --git a/source/dnetd/dconnection.d b/source/dnetd/dconnection.d index 27df4f9..3b2f84d 100644 --- a/source/dnetd/dconnection.d +++ b/source/dnetd/dconnection.d @@ -125,8 +125,36 @@ public class DConnection : Thread else { /* TODO: Error handling */ + writeln("Error with receive: "~to!(string)(this)); + break; } } + + /* Clean up */ + cleanUp(); + } + + private void cleanUp() + { + writeln(to!(string)(this)~" Cleaning up connection..."); + + /* Remove this user from all channels he is in */ + DChannel[] channels = server.getChannels(); + + /* Loop through each channel */ + foreach(DChannel currentChannel; channels) + { + /* Check if you are a member of it */ + if(currentChannel.isMember(this)) + { + /* Leave the channel */ + currentChannel.leave(this); + writeln(to!(string)(this)~" Leaving '"~currentChannel.getName()~"'..."); + } + } + + /* Remove this user from the connection queue */ + /* TODO: Implement me */ } /* TODO: add mutex for writing with message and funciton for doing so */ @@ -482,7 +510,12 @@ public class DConnection : Thread public override string toString() { - string toStr = "["~to!(string)(connType)~"]: "; + string toStr = "["~to!(string)(connType)~" ("; + toStr ~= socket.remoteAddress.toString(); + + + toStr ~= ")]: "; + if(connType == ConnectionType.CLIENT) { diff --git a/source/dnetd/dserver.d b/source/dnetd/dserver.d index c95277b..784a526 100644 --- a/source/dnetd/dserver.d +++ b/source/dnetd/dserver.d @@ -17,6 +17,7 @@ import dnetd.dchannel; import std.string : cmp; import core.sync.mutex : Mutex; import std.stdio; +import std.conv : to; public class DServer : Thread { @@ -121,6 +122,7 @@ public class DServer : Thread /* Add to the connection queue */ connectionQueue ~= connection; + writeln("Added new connection to queue "~to!(string)(connection)); /* Unlock the connections list */ connectionLock.unlock();