public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { // Filter out our own broadcasts if (sender != this) { // Forward event Broadcast(e, sender); } }
public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { if (e is NetworkBackgammonGameRoomEvent) { switch (((NetworkBackgammonGameRoomEvent)e).EventType) { case NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerDisconnected: case NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerConnected: { if (InvokeRequired) { // In case the caller has called this routine on a different thread BeginInvoke(new OnNotificationDelegate(UpdateGameRoomList)); } else { UpdateGameRoomList(); } } break; } } //else if ( ((NetworkBackgammonPlayer)sender).PlayerName.CompareTo(NetworkBackgammonClient.Instance.Player.PlayerName) == 0 ) { if (e is NetworkBackgammonChallengeResponseEvent) { NetworkBackgammonChallengeResponseEvent challengeRespEvent = ((NetworkBackgammonChallengeResponseEvent)e); bool challengeAccepted = challengeRespEvent.ChallengeAccepted; if (NetworkBackgammonClient.Instance.Player.PlayerName.CompareTo(challengeRespEvent.ChallengingPlayer) == 0) { lock (gameChallengeMutex) { if (gameChallengeThreadExchangeData != null) { gameChallengeThreadExchangeData.ChallengeResult = challengeAccepted ? GameChallengeDataContainer.ChallengeResultType.CHALLENGE_ACCEPTED : GameChallengeDataContainer.ChallengeResultType.CHALLENGE_REJECTED; } } if (InvokeRequired) { BeginInvoke(new OnChallengeResponseDelegate(OnChallengeResponse)); } else { OnChallengeResponse(); } } } } }
// Broadcast notification to listeners on behalt of a sender public void Broadcast(INetworkBackgammonEvent notificationEvent, INetworkBackgammonNotifier notifier) { lock (padlock) { foreach (INetworkBackgammonListener listner in m_listeners) { listner.OnEventNotification(notifier, notificationEvent); } } }
public bool RemoveNotifier(INetworkBackgammonNotifier notifier) { if (notifier != null && defaultListener != null) { return(defaultListener.RemoveNotifier(notifier)); } else { return(false); } }
// Add a notifier to the list - fails if notifier already is in list bool INetworkBackgammonListener.AddNotifier(INetworkBackgammonNotifier notifier) { bool retval = false; if (!m_attachedNotifiers.Contains(notifier)) { m_attachedNotifiers.Add(notifier); retval = true; } return(retval); }
// Handle incoming notification from a notifier public void OnEventNotification(INetworkBackgammonNotifier notifier, INetworkBackgammonEvent e) { if (e is NetworkBackgammonGameRoomEvent) { NetworkBackgammonGameRoomEvent grEvent = (NetworkBackgammonGameRoomEvent)e; if (grEvent.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerConnected) { m_notifcationRecv = true; } } }
// Remove a notifier from the list - fails if cannot find notifier bool INetworkBackgammonListener.RemoveNotifier(INetworkBackgammonNotifier notifier) { bool retval = false; retval = m_attachedNotifiers.Contains(notifier); if (retval) { m_attachedNotifiers.Remove(notifier); } return(retval); }
public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { if (InvokeRequired) { // In case the caller has called this routine on a different thread BeginInvoke(new OnLogMessageDelegate(Log), sender.ToString() + e.ToString()); BeginInvoke(new OnLogMessageDelegate(Log), ("***** Event ***** ")); BeginInvoke(new OnLogMessageDelegate(Log), "Sender: " + sender.ToString()); BeginInvoke(new OnLogMessageDelegate(Log), "Event: " + e.ToString()); if (e is NetworkBackgammonGameRoomEvent) { NetworkBackgammonGameRoomEvent eGameRoom = (NetworkBackgammonGameRoomEvent)e; if (eGameRoom.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerConnected || eGameRoom.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerDisconnected || eGameRoom.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerPlaying || eGameRoom.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerFinished) { BeginInvoke(new UpdatePlayerListDelegate(UpdateList)); } } } else { Log("***** Event ***** "); Log("Sender: " + sender.ToString()); Log("Event: " + e.ToString()); if (e is NetworkBackgammonGameRoomEvent) { NetworkBackgammonGameRoomEvent eGameRoom = (NetworkBackgammonGameRoomEvent)e; if (eGameRoom.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerConnected || eGameRoom.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerDisconnected || eGameRoom.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerPlaying || eGameRoom.EventType == NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerFinished) { UpdateList(); } } } }
/// <summary> /// Constructor /// </summary> /// <param name="_player1">Network backgammon player 1 to be part of this game session</param> /// <param name="_player2">Network backgammon player 2 to be part of this game session</param> public NetworkBackgammonGameSession(NetworkBackgammonPlayer _player1, NetworkBackgammonPlayer _player2) { defaultNotifier = new NetworkBackgammonNotifier(this); player1 = _player1; player2 = _player2; NetworkBackgammonDice dice1 = new NetworkBackgammonDice(); NetworkBackgammonDice dice2 = new NetworkBackgammonDice(); // Make sure the seeds used for the (pseudo) random generator instances are different // (otherwise, both dice will always roll the same values) while (dice1.Seed == dice2.Seed) { Thread.Sleep(1); dice2 = new NetworkBackgammonDice(); } dice = new NetworkBackgammonDice[] { dice1, dice2 }; }
public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { // Filter out our own broadcasts if (sender != this) { if (e is NetworkBackgammonGameSessionEvent) { eventQueue.Enqueue(new NetworkBackgammonEventQueueElement(e, sender)); try { semStateMachine.Release(); } catch (SemaphoreFullException ex) { // TODO: If this exception occurs calling Release too many times... } } } }
public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { if (e is GameSessionCheckerUpdatedEvent) { GameSessionCheckerUpdatedEvent updateEvent = (GameSessionCheckerUpdatedEvent)e; NetworkBackgammonPlayer curPlayer = updateEvent.GetPlayerByName(Title); if (curPlayer != null) { // Update the pip count if (InvokeRequired) { BeginInvoke(new OnUpdatePipCountDelegate(OnUpdatePipCount), curPlayer.PipCount); } else { OnUpdatePipCount(curPlayer.PipCount); } } } }
public void OnEventNotification(INetworkBackgammonNotifier notifier, INetworkBackgammonEvent e) { if (e is NetworkBackgammonChatEvent) { NetworkBackgammonChatEvent chatEvent = (NetworkBackgammonChatEvent)e; string sender = chatEvent.Sender; string recv = chatEvent.Recipient; string msg = chatEvent.Message; // Check if the message if for this player if (NetworkBackgammonClient.Instance.Player.PlayerName.CompareTo(recv) == 0) { if (InvokeRequired) { BeginInvoke(new OnRecvMessage(RecvMessage), sender, msg); } else { RecvMessage(sender, msg); } } } }
/// <summary> /// Constructor /// </summary> /// <param name="_strPlayerName">Name of this player</param> public NetworkBackgammonPlayer(string _strPlayerName) { defaultNotifier = new NetworkBackgammonNotifier(this); strPlayerName = _strPlayerName; }
public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { // Challegne event /*if (e is NetworkBackgammonChallengeResponseEvent) * { * NetworkBackgammonChallengeResponseEvent challengeRespEvent = ((NetworkBackgammonChallengeResponseEvent)e); * * bool challengeAccepted = challengeRespEvent.ChallengeAccepted; * * if ( (NetworkBackgammonClient.Instance.Player.PlayerName.CompareTo(challengeRespEvent.ChallengingPlayer) == 0) || * (NetworkBackgammonClient.Instance.Player.PlayerName.CompareTo(challengeRespEvent.ChallengedPlayer) == 0) ) * { * if (InvokeRequired) * { * BeginInvoke(new OnChallengeResponseDelegate(ChallengeResponse), challengeAccepted); * } * else * { * ChallengeResponse(challengeAccepted); * } * } * }*/ if (e is NetworkBackgammonGameRoomEvent) { switch (((NetworkBackgammonGameRoomEvent)e).EventType) { case NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerDisconnected: { // TODO: Check if player that disconnected was the opposing player if (InvokeRequired) { BeginInvoke(new OnGameRoomDelegate(ShowGameRoomScreen), true); BeginInvoke(new OnShowBoardDelegate(ShowBoard), false); } else { ShowGameRoomScreen(true); ShowBoard(false); } } break; } } else if (e is NetworkBackgammonChallengeEvent) { string challengingPlayer = ((NetworkBackgammonChallengeEvent)e).ChallengingPlayer; string challengedPlayer = ((NetworkBackgammonChallengeEvent)e).ChallengedPlayer; if (challengingPlayer.CompareTo(NetworkBackgammonClient.Instance.Player.PlayerName) != 0 && challengedPlayer.CompareTo(NetworkBackgammonClient.Instance.Player.PlayerName) == 0) { if (InvokeRequired) { // In case the caller has called this routine on a different thread BeginInvoke(new OnQueryChallengeDelegate(QueryChallenge), challengingPlayer); } else { QueryChallenge(challengingPlayer); } } } else if (e is GameSessionInitialDiceRollEvent) { GameSessionInitialDiceRollEvent initialDiceRollEvent = (GameSessionInitialDiceRollEvent)e; if (InvokeRequired) { BeginInvoke(new OnGameRoomDelegate(ShowGameRoomScreen), false); BeginInvoke(new OnShowBoardWithInitialDiceDelegate(ShowBoard), new object[] { true, initialDiceRollEvent }); } else { ShowGameRoomScreen(false); ShowBoard(true); } } else if (e is GameSessionPlayerResignationEvent) { if (InvokeRequired) { BeginInvoke(new OnGameRoomDelegate(ShowGameRoomScreen), true); // BeginInvoke(new OnShowBoardDelegate(ShowBoard), false); } else { ShowGameRoomScreen(true); // ShowBoard(false); } } else if (e is NetworkBackgammonGameSessionEvent) { NetworkBackgammonGameSessionEvent gameSessionEvt = (NetworkBackgammonGameSessionEvent)e; if (gameSessionEvt.EventType == NetworkBackgammonGameSessionEvent.GameSessionEventType.GameFinished) { if (InvokeRequired) { BeginInvoke(new OnGameRoomDelegate(ShowGameRoomScreen), true); // BeginInvoke(new OnShowBoardDelegate(ShowBoard), false); } else { ShowGameRoomScreen(true); // ShowBoard(false); } } if (gameSessionEvt.EventType == NetworkBackgammonGameSessionEvent.GameSessionEventType.Terminated) { if (InvokeRequired) { BeginInvoke(new OnTerminateDelegate(OnTerminate)); } else { OnTerminate(); } } } }
public bool RemoveNotifier(INetworkBackgammonNotifier notifier) { return(defaultListener.RemoveNotifier(notifier)); }
public void Broadcast(INetworkBackgammonEvent notificationEvent, INetworkBackgammonNotifier notifier) { defaultNotifier.Broadcast(notificationEvent, notifier); }
public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { eventQueue.Enqueue(new KeyValuePair <INetworkBackgammonNotifier, INetworkBackgammonEvent>(sender, e)); }
public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { if (sender is NetworkBackgammonPlayer) { NetworkBackgammonPlayer player = (NetworkBackgammonPlayer)sender; if (e is NetworkBackgammonChallengeResponseEvent) { NetworkBackgammonChallengeResponseEvent challengeResponseEvent = (NetworkBackgammonChallengeResponseEvent)e; if (challengeSyncList.Keys.Contains(player)) { if (challengeSyncList[player] != null) { challengeSyncList[player].ChallengeAccepted = challengeResponseEvent.ChallengeAccepted; try { challengeSyncList[player].ChallengeSemaphore.Release(); } catch (SemaphoreFullException ex) { // TODO: If this exception occurs calling Release too many times... } } } } else if (e is NetworkBackgammonChatEvent) { // Pass the message through to the listeners Broadcast((NetworkBackgammonChatEvent)e); } } else if (sender is NetworkBackgammonGameSession) { NetworkBackgammonGameSession gameSession = (NetworkBackgammonGameSession)sender; if (e is NetworkBackgammonGameSessionEvent) { NetworkBackgammonGameSessionEvent gameSessionEvent = (NetworkBackgammonGameSessionEvent)e; if (gameSessionEvent.EventType == NetworkBackgammonGameSessionEvent.GameSessionEventType.GameFinished) { bool newQueueItemAdd = true; NetworkBackgammonEventQueueElement newQueueItem = new NetworkBackgammonEventQueueElement(e, sender); lock (newQueueItem) { /* * if (gameSessionsEventQueue.Count > 0) * { * NetworkBackgammonEventQueueElement lastQueueItem = gameSessionsEventQueue.Last(); * // Avoid adding the same event (from the same sender) twice * // Reason: Game Room listens to events from all players, i.e. also * // both players that are in one Game Session. Thus, all events broadcasted * // by the Game Session arrive here (at the Game Room) twice * if (gameSessionsEventQueue.Last().Notifier == sender && * gameSessionsEventQueue.Last().Event == e) * { * newQueueItemAdd = false; * } * } */ if (newQueueItemAdd) { gameSessionsEventQueue.Enqueue(new NetworkBackgammonEventQueueElement(e, sender)); gameSessionsSemaphore.Release(); } } } } } }
// Constructor sets the actual notifier public NetworkBackgammonNotifier(INetworkBackgammonNotifier _sender) { sender = _sender; }
// Handle incoming notification from a notifier void INetworkBackgammonListener.OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { }
public void OnEventNotification(INetworkBackgammonNotifier sender, INetworkBackgammonEvent e) { if (InvokeRequired) { Invoke(new NotifyDelegate(OnEventNotification), new object[] { sender, e }); } else { buttonAction.Enabled = false; buttonAction.Text = "[No Action]"; #region Sender: Game Room if (sender is NetworkBackgammonRemoteGameRoom) { try { if (e is NetworkBackgammonGameRoomEvent) { NetworkBackgammonGameRoomEvent gameRoomEvent = (NetworkBackgammonGameRoomEvent)e; switch (gameRoomEvent.EventType) { case NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerConnected: UpdateConnectedPlayersList(); break; case NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerDisconnected: UpdateConnectedPlayersList(); break; } } else if (e is NetworkBackgammonChallengeEvent) { NetworkBackgammonChallengeEvent challengeEvent = (NetworkBackgammonChallengeEvent)e; string challengingPlayer = challengeEvent.ChallengingPlayer; string challengedPlayer = challengeEvent.ChallengedPlayer; if (challengingPlayer.CompareTo(player.PlayerName) != 0 && challengedPlayer.CompareTo(player.PlayerName) == 0) { bool challengeResponse = MessageBox.Show( "Accept game challenge from " + challengeEvent.ChallengingPlayer + "?", "Game Challenge", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes; player.RespondToChallenge(challengeResponse, challengeEvent.ChallengingPlayer); groupBoxGameControls.Enabled = challengeResponse; } } } catch (Exception ex) { listBoxLog.Items.Add(ex.Message); } } #endregion #region Sender: Game Session else if (sender is NetworkBackgammonGameSession) { // Filter out broadcasts from our own player if (sender != player) { try { NetworkBackgammonGameSession gameSession = (NetworkBackgammonGameSession)sender; if (e is NetworkBackgammonGameSessionEvent) { // Generate warning if last event has been overwritten // (Currently every event received is supposed to be responded too by means of an // action (e.g. move, dice roll acknowledge, etc), which resets the last event type to invalid) if (lastGameSessionEventType != null) { listBoxLog.Items.Add("Warning: Overwriting last event (" + lastGameSessionEventType.ToString() + ") with new event " + e.GetType().ToString() + " (i.e. last event hasn't been responded to by means of an action)"); } // Latch the event type lastGameSessionEventType = e.GetType(); } #region Actions for initial dice roll if (e is GameSessionInitialDiceRollEvent) { listBoxCheckers.Items.Clear(); listBoxMoves.Items.Clear(); GameSessionInitialDiceRollEvent gameSessionInitialDiceRollEvent = (GameSessionInitialDiceRollEvent)e; // if (player.InitialDice != null) if (gameSessionInitialDiceRollEvent.GetDiceForPlayer(player.PlayerName) != null) { listBoxLog.Items.Add("Initial dice rolled: " + gameSessionInitialDiceRollEvent.GetDiceForPlayer(player.PlayerName).CurrentValue); buttonAction.Enabled = true; buttonAction.Text = "Confirm initial dice roll"; } else { listBoxLog.Items.Add("Warning: Event received is " + e.GetType().ToString() + " but dice values are missing"); } } #endregion #region Actions for checker update else if (e is GameSessionCheckerUpdatedEvent) { listBoxCheckers.Items.Clear(); listBoxMoves.Items.Clear(); GameSessionCheckerUpdatedEvent gameSessionCheckerUpdateEvent = (GameSessionCheckerUpdatedEvent)e; player = gameSessionCheckerUpdateEvent.GetPlayerByName(player.PlayerName); string strDice = ""; foreach (NetworkBackgammonDice d in gameSessionCheckerUpdateEvent.DiceRolled) { strDice += " " + d.CurrentValue; } listBoxLog.Items.Add("Dice: " + strDice); foreach (NetworkBackgammonChecker checker in player.Checkers) { listBoxCheckers.Items.Add(checker); } } #endregion #region Actions for move expected else if (e is GameSessionMoveExpectedEvent) { GameSessionMoveExpectedEvent gameSessionMoveExpectedEvent = (GameSessionMoveExpectedEvent)e; if (player.PlayerName == gameSessionMoveExpectedEvent.ActivePlayer) { listBoxLog.Items.Add("I'm the active player, expected to make the next move ..."); groupBoxGameControls.BackColor = Color.DarkGreen; buttonAction.Enabled = true; buttonAction.Text = "Make Move"; } else { groupBoxGameControls.BackColor = Color.DarkRed; } } #endregion #region Actions for no (valid) move else if (e is GameSessionNoPossibleMovesEvent) { GameSessionNoPossibleMovesEvent gameSessionNoPossibleMovesEvent = (GameSessionNoPossibleMovesEvent)e; if (player.PlayerName == gameSessionNoPossibleMovesEvent.PlayerName) { listBoxLog.Items.Add("I'm the active player, but have no (valid) moves ..."); groupBoxGameControls.BackColor = Color.DarkGreen; buttonAction.Enabled = true; buttonAction.Text = "Confirm"; } else { groupBoxGameControls.BackColor = Color.DarkRed; } } #endregion #region Actions for player resignation else if (e is GameSessionPlayerResignationEvent) { listBoxCheckers.Items.Clear(); listBoxMoves.Items.Clear(); GameSessionPlayerResignationEvent gameSessionPlayerResignationEvent = (GameSessionPlayerResignationEvent)e; listBoxLog.Items.Add("Player " + gameSessionPlayerResignationEvent.ResigningPlayer + " has resigned from current game"); listBoxCheckers.Items.Clear(); groupBoxGameControls.BackColor = SystemColors.Control; groupBoxGameControls.Enabled = false; } #endregion #region Actions for player won game else if (e is GameSessionPlayerWonEvent) { listBoxCheckers.Items.Clear(); listBoxMoves.Items.Clear(); GameSessionPlayerWonEvent gameSessionPlayerWonEvent = (GameSessionPlayerWonEvent)e; if (gameSessionPlayerWonEvent.WinningPlayer == player.PlayerName) { listBoxLog.Items.Add("Yeah!!! I won the game!!!"); } else { listBoxLog.Items.Add("Player " + gameSessionPlayerWonEvent.WinningPlayer + " won the game"); } listBoxCheckers.Items.Clear(); groupBoxGameControls.BackColor = SystemColors.Control; groupBoxGameControls.Enabled = false; } #endregion else if (e is NetworkBackgammonGameSessionEvent) { listBoxCheckers.Items.Clear(); listBoxMoves.Items.Clear(); NetworkBackgammonGameSessionEvent gameSessionEvent = (NetworkBackgammonGameSessionEvent)e; switch (gameSessionEvent.EventType) { case NetworkBackgammonGameSessionEvent.GameSessionEventType.GameFinished: { listBoxLog.Items.Add("Game finished"); } break; } } } catch (Exception ex) { listBoxLog.Items.Add(ex.Message); } } } #endregion } }
/// <summary> /// Constructor /// </summary> public NetworkBackgammonRemoteGameRoom() { defaultNotifier = new NetworkBackgammonNotifier(this); }
// // You can use the following additional attributes as you write your tests: // // Use ClassInitialize to run code before running the first test in the class // [ClassInitialize()] // public static void MyClassInitialize(TestContext testContext) { } // // Use ClassCleanup to run code after all tests in a class have run // [ClassCleanup()] // public static void MyClassCleanup() { } // // Use TestInitialize to run code before running each test // [TestInitialize()] // public void MyTestInitialize() { } // // Use TestCleanup to run code after each test has run // [TestCleanup()] // public void MyTestCleanup() { } // #endregion #region INetworkBackgammonListener Members public bool AddNotifier(INetworkBackgammonNotifier notifier) { return(m_defaultListener.AddNotifier(notifier)); }
public bool AddNotifier(INetworkBackgammonNotifier notifier) { return(defaultListener != null?defaultListener.AddNotifier(notifier) : false); }
/// <summary> /// Constructor /// </summary> /// <param name="_event">Event that has been posted</param> /// <param name="_notifier">Notifier (sender) who posted the event</param> public NetworkBackgammonEventQueueElement(INetworkBackgammonEvent _event, INetworkBackgammonNotifier _notifier) { gameSessionEvent = _event; gameSessionNotifier = _notifier; }
public bool RemoveNotifier(INetworkBackgammonNotifier notifier) { return(m_localListener.RemoveNotifier(notifier)); }