Пример #1
0
        public async Task <IHttpActionResult> Create(CreateChannelBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ChannelDataRepository repository = new ChannelDataRepository();

            if (await repository.FindAsync(model.Name) == null)
            {
                ChannelDataModel channelDataModel = new ChannelDataModel();

                channelDataModel.Id   = model.Id;
                channelDataModel.Name = model.Name;

                repository.Add(channelDataModel);

                using (UserDataRepository db = new UserDataRepository())
                {
                    db.UpdateVisitedChannels(model.Owner, model.Name);
                }

                return(Ok());
            }

            return(GetErrorResult(new IdentityResult("Choose another ID")));
        }
Пример #2
0
        public async Task <IHttpActionResult> Join(JoinChannelBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ChannelDataRepository repository = new ChannelDataRepository();

            var channelModel = await repository.FindAsync(model.ChannelName);

            if (channelModel == null)
            {
                return(GetErrorResult(new IdentityResult("Channel not exists")));
            }

            var channel = ChannelsInfo.GetChannelInfo(channelModel.Name);

            if (channel != null)
            {
                channel.Members.Add(model.User);
                return(Ok(Convert.ToInt32(channel.Port)));
            }

            int         initPort       = 5000;
            VoiceServer newVoiceServer = new VoiceServer();

            while (initPort < 65000)
            {
                try
                {
                    newVoiceServer.Start(initPort);

                    ChannelsInfo.ListOfSockets.Add(initPort.ToString(), newVoiceServer);
                    ChannelsInfo.AddChannel(channelModel, initPort.ToString());
                    ChannelsInfo.AddMember(channelModel.Name, model.User);
                    break;
                }
                catch (Exception e)
                {
                    ChannelsInfo.ListOfSockets.Remove(initPort.ToString());
                    initPort++;
                }
            }

            using (UserDataRepository db = new UserDataRepository())
            {
                var user = db.FindByName(model.User);
                if (!user.RecenltyVisitedRooms.Split(',').Contains(model.ChannelName))
                {
                    db.UpdateVisitedChannels(model.User, model.ChannelName);
                }
            }

            return(Ok(initPort));
        }