1
0
mirror of https://github.com/deavminet/dnetd synced 2024-09-21 17:53:39 +02:00
dnetd_old/source/dnetd/dchannel.d

96 lines
1.5 KiB
D
Raw Normal View History

2020-09-23 18:52:11 +02:00
/**
* DChannel
*
* Represents a channel and its
* associated information such
* as its name, topic, members
*/
module dnetd.dchannel;
import dnetd.dconnection : DConnection;
import core.sync.mutex : Mutex;
2020-09-23 19:39:16 +02:00
import std.conv : to;
2020-09-23 18:52:11 +02:00
public class DChannel
{
/**
* Channel information
*/
private string name;
//private string topic;
/**
* Users in this channel
*/
private DConnection[] members;
private Mutex memberLock;
2020-09-23 18:52:11 +02:00
this(string name)
{
/* Initialize the lock */
memberLock = new Mutex();
2020-09-23 18:52:11 +02:00
}
public string getName()
{
return name;
}
public void join(DConnection client)
{
/* Lock the members list */
memberLock.lock();
/**
* TODO: Error handling if the calling DConnection fails midway
* and doesn't unlock it
*/
/* Add the client */
members ~= client;
/* Unlock the members list */
memberLock.unlock();
}
public void leave(DConnection client)
2020-09-23 18:52:11 +02:00
{
/* Lock the members list */
memberLock.lock();
/* TODO: Get a better implementation */
/* Create a new list without the `client` */
DConnection[] newMembers;
foreach(DConnection currentMember; members)
{
if(!(currentMember is client))
{
newMembers ~= currentMember;
}
}
/* Set it as the new list */
members = newMembers;
/* Unlock the members list */
memberLock.unlock();
2020-09-23 18:52:11 +02:00
}
public override string toString()
{
2020-09-23 21:40:22 +02:00
string toStr;
/* Lock the members list */
memberLock.lock();
toStr = "DChannel [Name: "~name~", Members: "~to!(string)(members)~"]";
/* Unlock the members list */
memberLock.unlock();
return toStr;
}
2020-09-23 18:52:11 +02:00
}