private RoomInfo Enter(string userId, IRoomObserver observer) { if (_removed) { throw new ResultException(ResultCodeType.RoomRemoved); } if (_userMap.ContainsKey(userId)) { throw new ResultException(ResultCodeType.NeedToBeOutOfRoom); } NotifyToAllObservers(o => o.Enter(userId)); _userMap[userId] = new UserData { EnterTime = DateTime.UtcNow, Observer = (RoomObserver)observer }; return(new RoomInfo { Name = _name, Users = _userMap.Keys.ToList(), History = _history }); }
public static SurrogateForIRoomObserver Convert(IRoomObserver value) { if (value == null) { return(null); } var o = (RoomObserver)value; return(new SurrogateForIRoomObserver { Channel = o.Channel, ObserverId = o.ObserverId }); }
async Task <Tuple <IOccupant, RoomInfo> > IUser.EnterRoom(string name, IRoomObserver observer) { if (_enteredRoomMap.ContainsKey(name)) { throw new ResultException(ResultCodeType.NeedToBeOutOfRoom); } // Try to get room ref IActorRef roomRef; try { var reply = await _clusterContext.RoomTable.GetOrCreate(name, null); roomRef = reply.Actor; } catch (Exception e) { _logger.Warn($"Failed in querying room from RoomTable. (Name={name})", e); throw new ResultException(ResultCodeType.InternalError); } if (roomRef == null) { throw new ResultException(ResultCodeType.RoomRemoved); } var room = roomRef.Cast <RoomRef>().WithRequestWaiter(this); // Let's enter the room ! var info = await room.Enter(_id, observer); // bound actor to this channel or new channel on user gateway IRequestTarget boundTarget = null; try { var gatewayName = _id.StartsWith("bot") ? null : "RoomGateway"; boundTarget = await _channel.BindActorOrOpenChannel( room.CastToIActorRef(), new[] { new TaggedType(typeof(IOccupant), _id) }, ActorBindingFlags.OpenThenNotification | ActorBindingFlags.CloseThenNotification, gatewayName, null); } catch (Exception e) { _logger.Error($"BindActorOrOpenChannel error (Room={name})", e); } if (boundTarget == null) { await room.Exit(_id); _logger.Error($"Failed in binding Occupant"); throw new ResultException(ResultCodeType.InternalError); } _enteredRoomMap[name] = room; return(Tuple.Create((IOccupant)boundTarget.Cast <OccupantRef>(), info)); }
public Task Unsubscribe(IRoomObserver observer) { observers.Unsubscribe(observer); return(TaskDone.Done); }