コード例 #1
0
        /// <summary>
        /// Register a lap timer
        /// </summary>
        /// <param name="ipAddressString"></param>
        /// <returns>
        /// Either ID if successful or a negative code
        /// Codes:
        /// -1 = Registration closed (not in registration state)
        /// -2 = Max participants reached
        /// </returns>
        public int Register(string ipAddressString)
        {
            int returnIdOrCode = -1;

            if (_raceState == RaceState.Registration)
            {
                try
                {
                    IPAddress ipAddress;
                    ipAddress = IPAddress.Parse(ipAddressString);
                    LapTimer lapTimer = _lapTimerManager.GetLapTimerByIPAddress(ipAddress);

                    if (lapTimer == null) // not registered
                    {
                        if (_lapTimerManager.GetAllLapTimers().Count >= _maxParticipants)
                        {
                            returnIdOrCode = -2;
                        }
                        else
                        {
                            returnIdOrCode = _lapTimerManager.RegisterLapTimer(ipAddress);
                        }
                    }
                    else
                    {
                        returnIdOrCode = lapTimer.GetId();
                    }
                }
                catch (FormatException)
                {
                    throw new FormatException("Could not parse IP address.");
                }
                catch (ArgumentNullException)
                {
                    throw new ArgumentNullException("Must provide device IP address to register.");
                }
            }

            return(returnIdOrCode);
        }
コード例 #2
0
        /// <summary>
        /// Registers a lap timer by IP address or returns id of timer if it is already registered
        /// </summary>
        public int RegisterLapTimer(IPAddress lapTimerIpAddress)
        {
            int      returnId      = -1;
            LapTimer existingTimer = GetLapTimerByIPAddress(lapTimerIpAddress);

            if (existingTimer == null)
            {
                LapTimer lapTimer = new LapTimer(_nextId);
                bool     added    = _lapTimers.TryAdd(lapTimerIpAddress, lapTimer);

                if (added)
                {
                    returnId = _nextId;
                    _nextId++;
                }
            }
            else
            {
                returnId = existingTimer.GetId();
            }

            return(returnId);
        }