Example #1
0
        public string LeaveRoom(Connection left, bool notify)
        {
            if (left == null)
            {
                return("Lefter was not determined");
            }

            List <Connection> beNotified = null;

            lock (_room.ActiveClients)
            {
                if (notify)
                {
                    beNotified = new List <Connection>(_room.ActiveClients);
                }
                _room.ActiveClients.Remove(left);
            }
            if (beNotified != null)
            {
                beNotified.ForEach(a =>
                {
                    try
                    {
                        a.BroadcastChannel <T>().ChatUserLeft(left,
                                                              (ChatRoom)
                                                              ComplexMonitor.CopyFrom(_room));
                    }
                    catch (SystemException)
                    {
                        _mainService.PuntUser(a, "bad connection");
                    }
                });
            }
            return(null);
        }
Example #2
0
        public void CopyFrom_List()
        {
            int?intref = 7;
            var oldl   = new List <int?> {
                1, 2, intref
            };
            var newl = (List <int?>)ComplexMonitor.CopyFrom(oldl);

            Assert.AreEqual(newl.Count, oldl.Count);
            Assert.AreNotSame(newl, oldl);
            Assert.AreEqual(newl[2], intref);
        }
Example #3
0
 public void PropertiesComplexMonitorLock()
 {
     var room = new ChatRoom();
     var m = new ComplexMonitor(room);
     using (m)
     {
         AddActiveUserInParallel(room);
         Thread.Sleep(500);
         room.ActiveClients.Add(
             new Connection {ClientUniqueKey = "2"});
         Assert.AreEqual(1, room.ActiveClients.Count);
     }
     Assert.IsNull(m._LockedObj);
 }
Example #4
0
        public void PropertiesComplexMonitorLock()
        {
            var room = new ChatRoom();
            var m    = new ComplexMonitor(room);

            using (m)
            {
                AddActiveUserInParallel(room);
                Thread.Sleep(500);
                room.ActiveClients.Add(
                    new Connection {
                    ClientUniqueKey = "2"
                });
                Assert.AreEqual(1, room.ActiveClients.Count);
            }
            Assert.IsNull(m._LockedObj);
        }
Example #5
0
        public void CopyFrom_ChatRoomDictionary()
        {
            var r = new ChatRoom
            {
                ActiveClients = new List <Connection>
                {
                    new Connection {
                        ClientUniqueKey = "1"
                    }
                }
            };
            var oldl = new Dictionary <int, ChatRoom> {
                { 0, r }
            };
            var newl = (List <ChatRoom>)ComplexMonitor.CopyFrom(oldl.Values);

            Assert.AreEqual(newl.Count, oldl.Count);
            Assert.AreNotEqual(newl, oldl);
            Assert.AreNotSame(newl[0].ActiveClients, oldl[0].ActiveClients);
            Assert.AreEqual(newl[0].ActiveClients.Count, oldl[0].ActiveClients.Count);
            Assert.AreEqual(newl[0].ActiveClients[0], oldl[0].ActiveClients[0]);
            Assert.AreEqual(newl[0].Name, oldl[0].Name);
        }
Example #6
0
        internal string JoinRoom(Connection initiator, Connection joined, bool withoutAllowanceCheck, bool broadCast)
        {
            if (initiator != null && joined != null)
            {
                bool f;
                lock (_room.AllowedUsers)
                    f = !withoutAllowanceCheck && _room.Moderator != initiator.User &&
                        !_room.AllowedUsers.Contains(joined.User);
                if (f)
                {
                    lock (_room.DeniedUsers)
                    {
                        if (_room.DeniedUsers.Contains(joined.User))
                        {
                            return(Resources.AccessDenied);
                        }
                    }
                    if (!_room.IsPublic)
                    {
                        return(Resources.PrivateRoomAllowedUsersOnly);
                    }

                    if (!_room.IsAutoAccepted)
                    {
                        lock (_userStatuses)
                            if (_userStatuses.ContainsKey(joined.User) &&
                                _userStatuses[joined.User] == ChatUser4RoomStatus.WaitingModeratorApproveToJoin)
                            {
                                return(WcfDomain.Contracts.Chats.Resources.ApprovalNeeded);
                            }

                        if (_room.Moderator == null)
                        {
                            return(Resources.ModeratorNull);
                        }

                        List <Connection> moderatorClients = _mainService.Broadcast(_room.Moderator);
                        bool moderatorNotified             = false;
                        moderatorClients.ForEach(a =>
                        {
                            try
                            {
                                a.BroadcastChannel <T>()
                                .ChatJoinReceived(Initiator.User,
                                                  JoinType.ApproveNeeded,
                                                  _room.Name);
                                moderatorNotified = true;
                            }
                            catch (SystemException)
                            {
                                _mainService.PuntUser(a, "bad connection");
                            }
                        });
                        if (!moderatorNotified)
                        {
                            return(Resources.UserNotConnected);
                        }

                        lock (_userStatuses)
                            _userStatuses[joined.User] = ChatUser4RoomStatus.WaitingModeratorApproveToJoin;
                        return(WcfDomain.Contracts.Chats.Resources.ApprovalNeeded);
                    }
                }

                lock (_room.ActiveClients)
                {
                    if (_room.ActiveClients.Contains(joined))
                    {
                        return(string.Format(Resources.ChatUserAlreadyJoined, joined, _room));
                    }
                    _room.ActiveClients.Add(joined);
                }

                if (broadCast)
                {
                    _mainService.GetClients(_room.ActiveUsersWithModerator, false)
                    .ForEach(a =>
                    {
                        try
                        {
                            a.BroadcastChannel <T>().ChatUserJoined(joined,
                                                                    (ChatRoom)
                                                                    ComplexMonitor.CopyFrom(_room));
                        }
                        catch (SystemException)
                        {
                            _mainService.PuntUser(a, "bad connection");
                        }
                    });
                }
            }
            else
            {
                return(string.Format("Initiator or joiner to room {0} was not determined", _room));
            }

            return(null);
        }
Example #7
0
 public void GetProps()
 {
     Assert.AreEqual(4, ComplexMonitor.GetProps(typeof(ChatRoom)).Count);
 }
Example #8
0
 public List <Connection> RequestUsers()
 {
     return((List <Connection>)ComplexMonitor.CopyFrom(Clients));
 }