예제 #1
0
 public static void BroadcastScore(Score score, int roundIndex, int diverIndex, SimhoppMessage.ClientAction action = SimhoppMessage.ClientAction.Ping)
 {
     if (_server == null)
         return;
     _serverThread = new Thread(() => SendScoreToConnectedClients(score, roundIndex, diverIndex, action)) { IsBackground = true };
     _serverThread.Start();
 }
예제 #2
0
        public void CommitScore(int judgeIndex, Score score)
        {
            SimhoppMessage.SimhoppStatus status = new SimhoppMessage.SimhoppStatus(Presenter.CurrentRoundIndex, Presenter.CurrentDiverIndex, null);
            SimhoppMessage msg = new SimhoppMessage(judgeIndex, SimhoppMessage.ClientAction.SubmitScore, "", score.Points, status);

            Messages.Enqueue(msg);
        }
예제 #3
0
        public static void KickJudge(int judgeIndex)
        {
            try
            {
                _judges.Remove(judgeIndex);
                _presenter.LogoutClient(judgeIndex);

                Console.WriteLine("Kick index: " + judgeIndex);
                foreach (IPEndPoint ipep in _judgeClients.Keys)
                {
                    Console.WriteLine("ipep: " + ipep.ToString());
                    Console.WriteLine("Index: " + _judgeClients[ipep]);
                    if (_judgeClients[ipep] == judgeIndex)
                    {
                        //Skapa terminate message...
                        SimhoppMessage msg;
                        msg = new SimhoppMessage(-2, SimhoppMessage.ClientAction.ServerTerminating);
                        var sendData = Encoding.ASCII.GetBytes(msg.Serialize());

                        //...och skicka
                        _server.Send(sendData, sendData.Length, ipep);

                        //Och ta bort den
                        _judgeClients.Remove(ipep);

                        //Sen break
                        break;
                    }
                }
            }
            catch(Exception ex)
            {
                /* Ignore */
            }
        }
예제 #4
0
        /// <summary>
        /// Inloggnings-svar från servern. Loggar in som vald domare
        /// </summary>
        /// <param name="msg"></param>
        public void AssignLogin(SimhoppMessage msg)
        {
            EventPresenter presenter = new EventPresenter(null, msg.Status.Contest);
            presenter.SetMode(EventPresenter.ViewMode.Client, (int)msg.Value, judgeClient, this.Icon);
            Console.WriteLine("Assigning login: " + msg.Serialize());
            judgeClient.Presenter = presenter;

            this.Hide();
            presenter.ShowView();
        }
예제 #5
0
        /// <summary>
        /// Inloggnings-svar från servern. Loggar in som vald domare
        /// </summary>
        /// <param name="msg"></param>
        public void AssignLogin(SimhoppMessage msg)
        {
            EventPresenter presenter = new EventPresenter(null, msg.Status.Contest);

            presenter.SetMode(EventPresenter.ViewMode.Client, (int)msg.Value, judgeClient, this.Icon);
            Console.WriteLine("Assigning login: " + msg.Serialize());
            judgeClient.Presenter = presenter;

            this.Hide();
            presenter.ShowView();
        }
예제 #6
0
        /// <summary>
        /// Skricka inloggningsförfrågan till servern
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            listBoxJudges.Visible = false;
            imgLoading.Visible    = true;
            labelJudgeList.Text   = "Loggar in...";
            btnLogin.Enabled      = false;

            SimhoppMessage msg = new SimhoppMessage(-1, SimhoppMessage.ClientAction.Login, listBoxJudges.SelectedItem.ToString());

            judgeClient.Messages.Enqueue(msg);
        }
예제 #7
0
        /// <summary>
        /// Skriver ut domare som går att ansluta som
        /// </summary>
        /// <param name="msg"></param>
        public void PopulateJudgeList(SimhoppMessage msg)
        {
            listBoxJudges.Visible = true;
            imgLoading.Visible    = false;
            labelTitle.Text       = msg.Status.Contest.Name;
            labelJudgeList.Text   = "Logga in som domare:";

            foreach (Judge judge in msg.Status.Contest.Judges)
            {
                listBoxJudges.Items.Add(judge);
            }
        }
예제 #8
0
 private void PopulateJudgeList(SimhoppMessage msg)
 {
     try
     {
         ProcessMessage d = new ProcessMessage(_view.PopulateJudgeList);
         _view.Invoke(d, msg);
     }
     catch (Exception ex)
     {
         ExceptionHandler.Handle(ex);
     }
 }
예제 #9
0
 private void AssignLogin(SimhoppMessage msg)
 {
     try
     {
         ProcessMessage d = new ProcessMessage(_view.AssignLogin);
         _view.Invoke(d, msg);
     }
     catch (Exception ex)
     {
         ExceptionHandler.Handle(ex);
     }
 }
예제 #10
0
        /// <summary>
        /// Skriver ut domare som går att ansluta som
        /// </summary>
        /// <param name="msg"></param>
        public void PopulateJudgeList(SimhoppMessage msg)
        {
            listBoxJudges.Visible = true;
            imgLoading.Visible = false;
            labelTitle.Text = msg.Status.Contest.Name;
            labelJudgeList.Text = "Logga in som domare:";

            foreach (Judge judge in msg.Status.Contest.Judges)
            {
                listBoxJudges.Items.Add(judge);
            }
        }
예제 #11
0
 private void NotAccepted(SimhoppMessage msg)
 {
     try
     {
         ProcessMessage d = new ProcessMessage(_view.NotAccepted);
         _view.Invoke(d, msg);
     }
     catch (Exception ex)
     {
         ExceptionHandler.Handle(ex);
     }
 }
예제 #12
0
        private void LogMessage(SimhoppMessage msg)
        {
            return;

            try
            {
                ProcessMessage d = new ProcessMessage(_view.LogMessage);
                _view.Invoke(d, msg);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
            }
        }
예제 #13
0
 public void NotAccepted(SimhoppMessage msg)
 {
     DialogResult res = MessageBox.Show("Inloggning nekad!", "Nekad!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);
     if (res == System.Windows.Forms.DialogResult.Retry)
     {
         btnLogin_Click(null, null);
     }
     else
     {
         listBoxJudges.Visible = true;
         imgLoading.Visible = false;
         labelJudgeList.Text = "Logga in som domare:";
         btnLogin.Enabled = true;
     }
 }
예제 #14
0
        public void NotAccepted(SimhoppMessage msg)
        {
            DialogResult res = MessageBox.Show("Inloggning nekad!", "Nekad!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);

            if (res == System.Windows.Forms.DialogResult.Retry)
            {
                btnLogin_Click(null, null);
            }
            else
            {
                listBoxJudges.Visible = true;
                imgLoading.Visible    = false;
                labelJudgeList.Text   = "Logga in som domare:";
                btnLogin.Enabled      = true;
            }
        }
예제 #15
0
        public void SendLogout(int judgeIndex)
        {
            try
            {
                SimhoppMessage msg = new SimhoppMessage(judgeIndex, SimhoppMessage.ClientAction.Logout, "");

                byte[] data = Encoding.ASCII.GetBytes(msg.Serialize());
                client.Send(data, data.Length, ipep);

                _view.Close();
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
            }
        }
예제 #16
0
        public void SendLogout(int judgeIndex)
        {
            try
            {
                SimhoppMessage msg = new SimhoppMessage(judgeIndex, SimhoppMessage.ClientAction.Logout, "");

                byte[] data = Encoding.ASCII.GetBytes(msg.Serialize());
                client.Send(data, data.Length, ipep);

                _view.Close();
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
            }
        }
예제 #17
0
 private static void LogoutClient(SimhoppMessage msg)
 {
     _judges.Remove(msg.Id);
     _presenter.LogoutClient(msg.Id);
 }
예제 #18
0
        /// <summary>
        /// Skricka inloggningsförfrågan till servern
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            listBoxJudges.Visible = false;
            imgLoading.Visible = true;
            labelJudgeList.Text = "Loggar in...";
            btnLogin.Enabled = false;

            SimhoppMessage msg = new SimhoppMessage(-1, SimhoppMessage.ClientAction.Login, listBoxJudges.SelectedItem.ToString());
            judgeClient.Messages.Enqueue(msg);
        }
예제 #19
0
 private static void SubmitScore(SimhoppMessage msg)
 {
     _presenter.SubmitClientScore(msg.Value, msg.Id, msg.Status.RoundIndex, msg.Status.DiverIndex);
     //_presenter.ScoreDive(msg.Value, msg.Id);
 }
예제 #20
0
        private static void SendScoreToConnectedClients(Score score, int roundIndex, int diverIndex, SimhoppMessage.ClientAction action = SimhoppMessage.ClientAction.Ping)
        {
            if (_judgeClients == null)
                return;
            IPEndPoint toDelete = null;
            bool delete = false;
            //Skicka poäng (eller status / request) till anslutna domarklienter
            foreach (IPEndPoint ipep in _judgeClients.Keys)
            {
                try
                {
                    //Skapa statusmeddelande med runda/hopp
                    SimhoppMessage.SimhoppStatus status = new SimhoppMessage.SimhoppStatus(
                        roundIndex,
                        diverIndex,
                        null);

                    SimhoppMessage msg;
                    if (score == null)
                        msg = new SimhoppMessage(-2, action, "", 0, status);
                    else
                        msg = new SimhoppMessage(score.judge.Index((_presenter.Judges)), SimhoppMessage.ClientAction.SubmitScore, "", score.Points, status);

                    LogToServer("Send: " + msg.Serialize());
                    var sendData = Encoding.ASCII.GetBytes(msg.Serialize());
                    _server.Send(sendData, sendData.Length, ipep);
                }
                catch (Exception ex)
                {
                    delete = true;
                    ExceptionHandler.Handle(ex);
                }
            }
            if (delete && toDelete != null)
            {
                try
                {
                    int judgeIndex = _judgeClients[toDelete];
                    _judgeClients.Remove(toDelete);
                    _judges.Remove(judgeIndex);
                    _presenter.LogoutClient(judgeIndex);
                }
                catch (Exception ex)
                {
                    /* Ignore */
                }

            }
        }
예제 #21
0
        private SimhoppMessage SendReceive(SimhoppMessage msg = null, bool broadcast = false)
        {
            byte[]         data;
            string         responseData;
            SimhoppMessage responseMsg;

            if (msg == null && !Messages.TryDequeue(out msg))
            {
                try
                {
                    client.Client.ReceiveTimeout = 1000;
                    data         = client.Receive(ref ipep);
                    responseData = Encoding.ASCII.GetString(data);

                    responseMsg = SimhoppMessage.Deserialize(responseData);

                    return(responseMsg);
                }
                catch (Exception)
                {
                    return(null);
                }
            }

            if (msg == null)
            {
                return(null);
            }

            data = Encoding.ASCII.GetBytes(msg.Serialize());

            int tries = 0;

            while (true)
            {
                tries++;
                try
                {
                    //Öka timeout för vaje försök
                    client.Client.ReceiveTimeout = 1000 + Math.Min(tries * 100, 10000);

                    if (broadcast)
                    {
                        data = Encoding.ASCII.GetBytes(SimhoppMessage.PingMessage().Serialize());
                        client.Send(data, data.Length, new IPEndPoint(IPAddress.Broadcast, 60069));
                    }
                    else
                    {
                        client.Send(data, data.Length, ipep);
                    }
                    data = client.Receive(ref ipep);
                    break;
                }
                catch (SocketException ex)
                {
                    LogMessage(SimhoppMessage.ErrorMessage(Encoding.ASCII.GetString(data)));
                    ExceptionHandler.Handle(ex);
                }
            }

            responseData = Encoding.ASCII.GetString(data);
            responseMsg  = SimhoppMessage.Deserialize(responseData);

            return(responseMsg);
        }
예제 #22
0
 public void LogMessage(SimhoppMessage msg)
 {
     textBoxLog.Text = msg.Serialize() + "\r\n" + textBoxLog.Text;
 }
예제 #23
0
 private void NotAccepted(SimhoppMessage msg)
 {
     try
     {
         ProcessMessage d = new ProcessMessage(_view.NotAccepted);
         _view.Invoke(d, msg);
     }
     catch (Exception ex)
     {
         ExceptionHandler.Handle(ex);
     }
 }
예제 #24
0
        private SimhoppMessage SendReceive(SimhoppMessage msg = null, bool broadcast = false)
        {
            byte[] data;
            string responseData;
            SimhoppMessage responseMsg;
            if (msg == null && !Messages.TryDequeue(out msg))
            {
                try
                {
                    client.Client.ReceiveTimeout = 1000;
                    data = client.Receive(ref ipep);
                    responseData = Encoding.ASCII.GetString(data);

                    responseMsg = SimhoppMessage.Deserialize(responseData);

                    return responseMsg;
                }
                catch (Exception)
                {
                    return null;
                }
            }

            if (msg == null)
                return null;

            data = Encoding.ASCII.GetBytes(msg.Serialize());

            int tries = 0;
            while (true)
            {
                tries++;
                try
                {
                    //Öka timeout för vaje försök
                    client.Client.ReceiveTimeout = 1000 + Math.Min(tries * 100, 10000);

                    if (broadcast)
                    {
                        data = Encoding.ASCII.GetBytes(SimhoppMessage.PingMessage().Serialize());
                        client.Send(data, data.Length, new IPEndPoint(IPAddress.Broadcast, 60069));
                    }
                    else
                    {
                        client.Send(data, data.Length, ipep);
                    }
                    data = client.Receive(ref ipep);
                    break;
                }
                catch (SocketException ex)
                {
                    LogMessage(SimhoppMessage.ErrorMessage(Encoding.ASCII.GetString(data)));
                    ExceptionHandler.Handle(ex);
                }
            }

            responseData = Encoding.ASCII.GetString(data);
            responseMsg = SimhoppMessage.Deserialize(responseData);

            return responseMsg;
        }
예제 #25
0
 private void LogMessage(SimhoppMessage msg)
 {
     return;
     try
     {
         ProcessMessage d = new ProcessMessage(_view.LogMessage);
         _view.Invoke(d, msg);
     }
     catch (Exception ex)
     {
         ExceptionHandler.Handle(ex);
     }
 }
예제 #26
0
        /// <summary>
        /// Letar efter server och upprättar en anslutning
        /// </summary>
        private void ServerFinder()
        {
            //Broadcasta ping-meddelande
            client = new UdpClient();
            ipep   = new IPEndPoint(IPAddress.Any, 0);
            client.EnableBroadcast = true;

            SimhoppMessage msg = SendReceive(SimhoppMessage.PingMessage(), true);

            switch (msg.Action)
            {
            case SimhoppMessage.ClientAction.List:
                PopulateJudgeList(msg);
                break;
            }

            LogMessage(msg);

            //Klient-tråd
            //Tar emot meddelanden från servern
            while (true)
            {
                try
                {
                    Thread.Sleep(100);

                    msg = SendReceive();

                    if (msg == null)
                    {
                        continue;
                    }

                    switch (msg.Action)
                    {
                    case SimhoppMessage.ClientAction.List:
                        PopulateJudgeList(msg);
                        break;

                    case SimhoppMessage.ClientAction.NotAccepted:
                        NotAccepted(msg);
                        break;

                    case SimhoppMessage.ClientAction.AssignId:
                        AssignLogin(msg);
                        break;

                    case SimhoppMessage.ClientAction.SubmitScore:
                        if (Presenter != null)
                        {
                            Presenter.SubmitClientScore(msg.Value, msg.Id, msg.Status.RoundIndex, msg.Status.DiverIndex);
                        }
                        break;

                    case SimhoppMessage.ClientAction.RequestScore:
                        if (Presenter != null)
                        {
                            Presenter.ScoreRequested(msg);
                        }
                        break;

                    case SimhoppMessage.ClientAction.StatusUpdate:
                        if (Presenter != null)
                        {
                            Presenter.StatusUpdated(msg);
                        }
                        break;

                    case SimhoppMessage.ClientAction.ServerTerminating:
                        if (Presenter != null)
                        {
                            Presenter.Close(true);
                        }
                        break;
                    }

                    LogMessage(msg);
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(ex);
                }
            }
        }
예제 #27
0
 private void PopulateJudgeList(SimhoppMessage msg)
 {
     try
     {
         ProcessMessage d = new ProcessMessage(_view.PopulateJudgeList);
         _view.Invoke(d, msg);
     }
     catch (Exception ex)
     {
         ExceptionHandler.Handle(ex);
     }
 }
예제 #28
0
        private static void UdpListener()
        {
            try
            {
                _server = new UdpClient(60069); //60069
                _server.EnableBroadcast = true;
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(ex);
            }

            while ( true )
            {
                try
                {
                    if (_server == null)
                        return;

                    //Vänta på broadcast från klient
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 60069);

                    //Ta emot msg från klienten
                    byte[] data = _server.Receive(ref ipep);
                    string receiveMessage = Encoding.ASCII.GetString(data, 0, data.Length);

                    SimhoppMessage msg = SimhoppMessage.Deserialize(receiveMessage);
                    SimhoppMessage response;

                    switch (msg.Action)
                    {
                        case SimhoppMessage.ClientAction.Ping:
                            response = SendContestStatus();
                            break;
                        case SimhoppMessage.ClientAction.Login:
                            if (_presenter.NewConnections)
                                response = AssignIdToJudge(msg, ipep);
                            else
                                response = new SimhoppMessage(-2, SimhoppMessage.ClientAction.NotAccepted);
                            break;
                        case SimhoppMessage.ClientAction.SubmitScore:
                            SubmitScore(msg);
                            response = new SimhoppMessage(-2, SimhoppMessage.ClientAction.Confirm);
                            break;
                        case SimhoppMessage.ClientAction.Logout:
                            LogoutClient(msg);
                            response = new SimhoppMessage(-2, SimhoppMessage.ClientAction.Confirm);
                            break;
                        default:
                            response = SimhoppMessage.ErrorMessage("Not implemented");
                            break;
                    }

                    //Svara
                    var responseData = Encoding.ASCII.GetBytes(response.Serialize());
                    _server.Send(responseData, responseData.Length, ipep);

                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(ex);
                }
            }
        }
예제 #29
0
 public void CommitScore(int judgeIndex, Score score)
 {
     SimhoppMessage.SimhoppStatus status = new SimhoppMessage.SimhoppStatus(Presenter.CurrentRoundIndex, Presenter.CurrentDiverIndex, null);
     SimhoppMessage msg = new SimhoppMessage(judgeIndex, SimhoppMessage.ClientAction.SubmitScore, "", score.Points, status);
     Messages.Enqueue(msg);
 }
예제 #30
0
 public void LogMessage(SimhoppMessage msg)
 {
     textBoxLog.Text = msg.Serialize() + "\r\n" + textBoxLog.Text;
 }
예제 #31
0
 private void AssignLogin(SimhoppMessage msg)
 {
     try
     {
         ProcessMessage d = new ProcessMessage(_view.AssignLogin);
         _view.Invoke(d, msg);
     }
     catch (Exception ex)
     {
         ExceptionHandler.Handle(ex);
     }
 }
예제 #32
0
        private static SimhoppMessage AssignIdToJudge(SimhoppMessage msg, IPEndPoint ipep)
        {
            int index = 0;
            foreach (Judge judge in _presenter.Judges)
            {
                if (judge.Name == msg.Data)
                {
                    Guid guid = Guid.NewGuid();
                    _judges[index] = judge;

                    _judgeClients.Add(ipep, index);

                    SimhoppMessage.SimhoppStatus status = new SimhoppMessage.SimhoppStatus(0, 0, _presenter.CurrentEvent);
                    SimhoppMessage response = new SimhoppMessage(-2, SimhoppMessage.ClientAction.AssignId, guid.ToString(), judge.Index(_presenter.Judges), status);
                    _presenter.AssignJudgeAsClient(index);
                    return response;
                }
                index++;
            }
            return SimhoppMessage.ErrorMessage("Judge not found");
        }