예제 #1
0
        public static void Run()
        {
//			ThreadPool.SetMaxThreads(8, 8);
//			var configuration = SimulatorConfiguration.Build2P(1920, 1080);
            var configuration = SimulatorConfiguration.Build(1, 1920, 1080);
            var agents        = ConstructAgents(configuration);

            new SimulatorGame(configuration, agents).Run();
        }
예제 #2
0
        public SimulatorGame(SimulatorConfiguration configuration, DeviceAgent[] agents)
        {
            this.configuration = configuration;
            this.agents        = agents;

            graphicsDeviceManager = new GraphicsDeviceManager(this)
            {
                PreferredBackBufferWidth  = configuration.DisplayWidth,
                PreferredBackBufferHeight = configuration.DisplayHeight,
                PreferMultiSampling       = true
            };
        }
예제 #3
0
        private static DeviceAgent[] ConstructAgents(SimulatorConfiguration configuration)
        {
            var random = new Random(2);

            var agents = new DeviceAgent[configuration.AgentCount];

            for (int i = 0; i < agents.Length; i++)
            {
                agents[i] = new DeviceAgent {
                    BluetoothAdapterId = Guid.NewGuid(),
                    Position           = new Vector2(
                        random.Next(configuration.AgentRadius, configuration.FieldWidth - configuration.AgentRadius),
                        random.Next(configuration.AgentRadius, configuration.FieldHeight - configuration.AgentRadius)
                        ),
                    Velocity = Vector2.Transform(new Vector2(10, 0), Matrix.CreateRotationZ((float)(random.NextDouble() * Math.PI * 2)))
                };
            }

            agents[0].Position = new Vector2(300, 300);
            agents[1].Position = new Vector2(310, 300);

            var agentsPerRow = (4 * (int)Math.Sqrt(agents.Length)) / 3;
            var hSPacing     = 60;
            var vSPacing     = 60;
            var gw           = agentsPerRow * hSPacing;
            var gh           = (agents.Length / agentsPerRow) * vSPacing;
            var ox           = (configuration.FieldWidth - gw) / 2;
            var oy           = (configuration.FieldHeight - gh) / 2;

            for (var i = 0; i < agents.Length; i++)
            {
                agents[i].Position = new Vector2((i % agentsPerRow) * hSPacing + ox, (i / agentsPerRow) * vSPacing + oy);
            }

            for (var i = 0; i < agents.Length; i++)
            {
                agents[i].Position += agents[i].Velocity * 10;
                agents[i].Velocity  = Vector2.Zero;
            }

            var agentIndexToNeighborsByAdapterId = Enumerable.Range(0, agents.Length).ToDictionary(
                i => i,
                i => new Dictionary <Guid, SimulationBluetoothAdapter.SimulationBluetoothNeighbor>());

            for (int i = 0; i < agents.Length; i++)
            {
                var agent            = agents[i];
                var bluetoothAdapter = agent.BluetoothAdapter = new SimulationBluetoothAdapter(agent, agentIndexToNeighborsByAdapterId[i]);
                agent.BluetoothAdapter.Permit(SimulationBluetoothAdapter.MAX_RATE_LIMIT_TOKENS * (float)random.NextDouble());

                var broadcastMessageSerializer = new BroadcastMessageSerializer();
                var merkleTreeFactory          = new ClientMerkleTreeFactory(broadcastMessageSerializer, new InMemoryCampfireNetObjectStore());
                var identity = agent.CampfireNetIdentity = (Identity) new Identity(new IdentityManager(), $"Agent_{i}");
                if (i == 0 || i == 1)
                {
                    agent.CampfireNetIdentity.GenerateRootChain();
                }
                else
                {
                    var rootAgent = agents[i % 2];
                    agent.CampfireNetIdentity.AddTrustChain(rootAgent.CampfireNetIdentity.GenerateNewChain(identity.PublicIdentity, Permission.All, Permission.None, identity.Name));
                }

                var client = agent.Client = new CampfireNetClient(identity, bluetoothAdapter, broadcastMessageSerializer, merkleTreeFactory);
                client.MessageReceived += e => {
                    var epoch = BitConverter.ToInt32(e.Message.DecryptedPayload, 0);
//               Console.WriteLine($"{client.AdapterId:n} recv {epoch}");
                    agent.Value = Math.Max(agent.Value, epoch);
                };
                client.RunAsync().ContinueWith(task => {
                    if (task.IsFaulted)
                    {
                        Console.WriteLine(task.Exception);
                    }
                });
            }

            return(agents);
        }