Added function to get list of connections, added sendUserMessage function (WIP) to send a message to a given user

This commit is contained in:
Tristan B. Kildaire 2020-09-27 18:31:34 +02:00
parent 16b4ad8b40
commit d4384c439e
2 changed files with 92 additions and 2 deletions

View File

@ -186,6 +186,10 @@ public class DConnection : Thread
{
command = Command.PART;
}
else if(commandByte == cast(ulong)5)
{
command = Command.MSG;
}
else if(commandByte == cast(ulong)6)
{
command = Command.LIST;
@ -372,7 +376,9 @@ public class DConnection : Thread
/* If we are sending to a user */
if(messageType == cast(byte)0)
{
/* TODO Implemet me */
/* Send the message to the user */
bool sendStatus = sendUserMessage(destination, msg);
reply = [sendStatus];
}
/* If we are sending to a channel */
else if(messageType == cast(ubyte)1)
@ -438,11 +444,43 @@ public class DConnection : Thread
return true;
}
/**
* Send user a message
*
* Sends the provided user the specified message
*/
private bool sendUserMessage(string username, string message)
{
/* Find the user to send to */
DConnection user = server.findUser(username);
/* If the user was found */
if(user)
{
/* Send the messge */
/* TODO: Implement me */
/* TODO: Return value should be based off message send success */
return true;
}
/* If the user was not found */
else
{
return false;
}
}
public string getUsername()
{
return username;
}
public ConnectionType getConnectionType()
{
return connType;
}
public override string toString()
{
string toStr = "["~to!(string)(connType)~"]: ";

View File

@ -31,6 +31,7 @@ public class DServer : Thread
* Connection queue
*/
private DConnection[] connectionQueue;
private Mutex connectionLock;
/**
* Channels
@ -89,7 +90,10 @@ public class DServer : Thread
private void initLocks()
{
/* Initialioze the channel lock */
/* Initialize the connection lock */
connectionLock = new Mutex();
/* Initialize the channel lock */
channelLock = new Mutex();
}
@ -112,8 +116,14 @@ public class DServer : Thread
/* Spawn a connection handler */
DConnection connection = new DConnection(this, socket);
/* Lock the connections list */
connectionLock.lock();
/* Add to the connection queue */
connectionQueue ~= connection;
/* Unlock the connections list */
connectionLock.unlock();
}
}
@ -153,6 +163,48 @@ public class DServer : Thread
return channel;
}
/**
* Returns the DConnection with the matching
* username, null if not found
*/
public DConnection findUser(string username)
{
/* Get all the current connections */
DConnection[] connections = getConnections();
/* Find the user with the matching user name */
foreach(DConnection connection; connections)
{
/* The connection must be a user (not unspec or server) */
if(connection.getConnectionType() == DConnection.ConnectionType.CLIENT)
{
/* Match the username */
if(cmp(connection.getUsername(), username) == 0)
{
return connection;
}
}
}
return null;
}
public DConnection[] getConnections()
{
/* The current connections list */
DConnection[] currentConnections;
/* Lock the connections list */
connectionLock.lock();
currentConnections = connectionQueue;
/* Unlock the connections list */
connectionLock.unlock();
return currentConnections;
}
public DChannel[] getChannels()
{
/* The current channels list */