using Assets.Domain.Agents; using Assets.Message.Decoding; using System.Collections.Generic; using System.Linq; using Unity.Collections; using Unity.Jobs; using UnityEngine; namespace Assets.GAMA.net.Message.Jobs { public class GamaMessageDecodingJobHandler : BaseMessageDecodingJobHandler { private const int MaxAgentsPerMessage = 1000; private GamaMessageDecodingJob Job; private JobHandle Handle; private NativeArray _messageBytes; public NativeArray _headings; public NativeArray _speeds; public NativeArray _hasToBeRemoved; public NativeArray _positions; public NativeArray _names; public NativeArray _species; private NativeArray _agentCount; private NativeArray _experimentCycle; private NativeArray _timestampTicks; public override string Topic { get; } public GamaMessageDecodingJobHandler(byte[] message, string topic) { Topic = topic; _messageBytes = new NativeArray(message, Allocator.Persistent); _agentCount = new NativeArray(1, Allocator.Persistent); _experimentCycle = new NativeArray(1, Allocator.Persistent); _timestampTicks = new NativeArray(1, Allocator.Persistent); _headings = new NativeArray(MaxAgentsPerMessage, Allocator.Persistent); _speeds = new NativeArray(MaxAgentsPerMessage, Allocator.Persistent); _positions = new NativeArray(MaxAgentsPerMessage, Allocator.Persistent); _names = new NativeArray(MaxAgentsPerMessage, Allocator.Persistent); _species = new NativeArray(MaxAgentsPerMessage, Allocator.Persistent); _hasToBeRemoved = new NativeArray(MaxAgentsPerMessage, Allocator.Persistent); } public override void Start() { Job = new GamaMessageDecodingJob() { MessageBytes = _messageBytes, AgentCount = _agentCount, ExperimentCycle = _experimentCycle, Names = _names, Headings = _headings, Speeds = _speeds, Positions = _positions, HasToBeRemoved = _hasToBeRemoved, TimestampTicks = _timestampTicks, Species = _species }; Handle = Job.Schedule(); } protected override DecodedPublishedMessage DoHarvest() { Handle.Complete(); List agents = new List(); long experimentCycle = _experimentCycle.First(); long timestampTicks = _timestampTicks.First(); for (int i = 0; i < _agentCount[0]; i++) { agents.Add(new PeopleSimulationAgent() { Position = _positions[i], Species = _species[i].GetString(), Heading = _headings[i], Name = _names[i].GetString(), Speed = _speeds[i], HasToBeRemoved = _hasToBeRemoved[i] }); } _headings.Dispose(); _names.Dispose(); _species.Dispose(); _speeds.Dispose(); _positions.Dispose(); _messageBytes.Dispose(); _agentCount.Dispose(); _experimentCycle.Dispose(); _hasToBeRemoved.Dispose(); _timestampTicks.Dispose(); return new DecodedPublishedMessage(experimentCycle, timestampTicks, agents); } public override bool IsCompleted() { return Handle.IsCompleted; } } }