public void TryAcquire()
        {
            var fb   = PortLocker.Connect().ConfigureAwait(false).GetAwaiter().GetResult();
            var root = fb.Child("portLocks_new").Child("1000");

            pl.Clear();
            root.PostAsync(new DateTime(1900)).ConfigureAwait(false).GetAwaiter().GetResult();
            pl.TryAcquire(1000).Should().BeTrue();
            pl.TryAcquire(1000).Should().BeFalse();
            pl.Clear();
        }
Example #2
0
        public static bool TryCompeteOnArena(string collectorId, string commitHash = "manualRun")
        {
            var portLocker = new PortLocker();
            var match      = ArenaMatch.EmptyMatch;

            try
            {
                var sw = Stopwatch.StartNew();
                IAi ai;
                IServerInteraction interaction;

                lock (Locker)
                {
                    var isPortOpen = false;
                    do
                    {
                        match = GetNextMatch();

                        if (match == null)
                        {
                            Thread.Sleep(5000);
                            log.Warn($"Collector {collectorId}: No matches found, sleeping 5 seconds...");
                            continue;
                        }

                        isPortOpen = portLocker.TryAcquire(match.Port);
                        if (!isPortOpen)
                        {
                            log.Warn($"Collector {collectorId}: {match.Port} taken");
                        }
                    } while (!isPortOpen);

                    log.Info($"Collector {collectorId}: Take {match.Port}");

                    ai = AiFactoryRegistry.GetNextAi(true);

                    log.Info($"Collector {collectorId}: Match on port {match.Port} for {GetBotName(ai.Name)}");

                    try
                    {
                        interaction = new OnlineInteraction(match.Port, GetBotName(ai.Name));
                    }
                    catch (SocketException e)
                    {
                        log.Error(e);
                        portLocker.Free(match.Port);
                        return(false);
                    }
                    if (!interaction.Start())
                    {
                        portLocker.Free(match.Port);
                        return(false);
                    }
                }

                log.Info($"Collector {collectorId}: Running game on port {match.Port}");
                var metaAndData = interaction.RunGame(ai);

                metaAndData.Item1.CommitHash = commitHash;

                Repo.SaveReplay(metaAndData.Item1, metaAndData.Item2);
                log.Info($"Collector {collectorId}: Saved replay {metaAndData.Item1.Scores.ToDelimitedString(", ")}");
                log.Info($"Collector {collectorId}: Elapsed {sw.ElapsedMilliseconds}");
            }
            catch (Exception e)
            {
                log.Error(e, $"Collector {collectorId} failed: {e}");
            }

            portLocker.Free(match.Port);
            return(true);
        }