Added debug prints for now solved bug (tristanable concurrency fix, early request), also simplified channel joins to be mutex atomic in that all stuff must first complete

This commit is contained in:
Tristan B. Kildaire 2020-09-28 19:23:04 +02:00
parent c0ea99e797
commit e30987546b
3 changed files with 64 additions and 16 deletions

View File

@ -46,10 +46,12 @@ public class DChannel
public bool join(DConnection client)
{
/* Send a message stating the user has joined (TODO: This should be done later, possibly, how defensive should we program) */
broadcastJoin(client);
//broadcastJoin(client);
/* Lock the members list */
writeln("join: mutex lock (about to call)");
memberLock.lock();
writeln("join: mutex lock (completed)");
/**
* Don't allow the user to join a channel he
@ -78,9 +80,10 @@ public class DChannel
members ~= client;
}
/* Unlock the members list */
writeln("join: mutex unlock (about to call)");
memberLock.unlock();
writeln("join: mutex unlock (completed)");
return isPresent;
}
@ -156,7 +159,7 @@ public class DChannel
memberLock.unlock();
/* Send broadcast leave message */
broadcastLeave(client);
//broadcastLeave(client);
}
/**

View File

@ -109,6 +109,7 @@ public class DConnection : Thread
*
* (Does decoding for bformat too)
*/
writeln("waiting");
bool status = receiveMessage(socket, receivedBytes);
/* TODO: Check status */
@ -180,15 +181,19 @@ public class DConnection : Thread
/* Create the tagged message */
DataMessage message = new DataMessage(tag, data);
writeln("writeSocket: mutex lock");
/* Lock the write mutex */
writeLock.lock();
/* Send the message */
writeln("writeSocket: Data: "~to!(string)(data)~" Tag: "~to!(string)(tag));
status = sendMessage(socket, message.encode());
/* Unlock the write mutex */
writeLock.unlock();
writeln("writeSocket: mutex unlock");
return status;
}
@ -246,6 +251,7 @@ public class DConnection : Thread
* between commands and async notifications
*/
long tag = message.tag;
writeln("tag:", tag);
/* The reply */
byte[] reply;
@ -302,6 +308,8 @@ public class DConnection : Thread
string channelList = cast(string)message.data[1..message.data.length];
string[] channels = split(channelList, ",");
writeln("channels, ", channels);
/**
* Loop through each channel, check if it
* exists, if so join it, else create it
@ -310,17 +318,19 @@ public class DConnection : Thread
bool isPresentInfo = false;
foreach(string channelName; channels)
{
/* Attempt to find the channel */
DChannel channel = server.getChannelByName(channelName);
// /* Attempt to find the channel */
// DChannel channel = server.getChannelByName(channelName);
//
// /* Create the channel if it doesn't exist */
// if(channel is null)
// {
// /* TODO: Thread safety for name choice */
// channel = new DChannel(channelName);
//
// server.addChannel(this, channel);
// }
DChannel channel = server.getChannel(this, channelName);
/* Create the channel if it doesn't exist */
if(channel is null)
{
/* TODO: Thread safety for name choice */
channel = new DChannel(channelName);
server.addChannel(this, channel);
}
/* Join the channel */
isPresentInfo = channel.join(this);

View File

@ -125,14 +125,14 @@ public class DServer : Thread
public void addChannel(DConnection causer, DChannel channel)
{
/* Lock the channels list */
channelLock.lock();
// channelLock.lock();
channels ~= channel;
/* TODO: Use causer */
/* Unlock the channels list */
channelLock.unlock();
// channelLock.unlock();
}
public void addConnection(DConnection connection)
@ -174,6 +174,41 @@ public class DServer : Thread
connectionLock.unlock();
}
/* TODO: neew method */
public DChannel getChannel(DConnection causer, string channelName)
{
DChannel channel = null;
channelLock.lock();
foreach(DChannel currentChannel; channels)
{
if(cmp(currentChannel.getName(), channelName) == 0)
{
channel = currentChannel;
break;
}
}
if(channel)
{
}
else
{
channel = new DChannel(channelName);
this.addChannel(causer, channel);
}
channelLock.unlock();
return channel;
}
public DChannel getChannelByName(string channelName)
{
/* The channel */