예제 #1
0
 public void ReceiveCommand(DevCommandData data, EntityId sender = default(EntityId))
 {
     if (!IsConnected)
     {
         // whoops we have no network/internet (simulated)
         return;
     }
     data.CoreInstance = core;
     // only execute if there is no onmessage or onmessage allows it
     if (OnMessage == null || OnMessage.Invoke(data))
     {
         core.ReceiveCommand(data, sender);
         AfterMessage?.Invoke(data);
     }
 }
예제 #2
0
파일: DevCore.cs 프로젝트: Coflnet/cloud
        /// <summary>
        /// Will execute commands on the simulated cores
        /// </summary>
        /// <param name="data">Data to send</param>
        /// <param name="serverId">optional serverId, ignored in this implementation</param>
        public override void SendCommand(CommandData data, long serverId = 0)
        {
            // record it
            pastMessages.Add(data);

            if (data.SenderId == data.Recipient)
            {
                // resource is trying to send to itself
                serverId = data.Recipient.ServerId;
            }

            if (executionCount > 100)
            {
                throw new Exception($"to many commands, probalby a loop {data}");
            }
            executionCount++;

            // guess sender if there is none
            if (data.Recipient == userId)
            {
                data.SenderId = new EntityId(data.Recipient.ServerId, 0);
            }

            var devData = new DevCommandData(data);

            devData.Connection = new DevConnection();
            data = devData;

            /*
             * if(data.type == "registerUser" || data.type == "loginUser" || data.type == "response"){
             *
             *      devData.sender = lastAddedClient;
             * }
             *
             * if (data.type == "registeredUser" || data.type == "loginUserResponse" || data.type =="response") {
             *      data.rId = ConfigController.ActiveUserId;
             *
             *      //
             * }*/

            //			// search for the serverId first
            if (serverId != 0 && simulationInstances.ContainsKey(new EntityId(serverId, 0)))
            {
                simulationInstances[new EntityId(serverId, 0)].ReceiveCommand(devData);
            }
            else if (simulationInstances.ContainsKey(data.Recipient))
            {
                // the receiver is known, send it to him
                simulationInstances[data.Recipient].ReceiveCommand(devData);
            }
            else if (simulationInstances.ContainsKey(EntityId.Default) ||
                     simulationInstances.Where(i => i.Value.core.Id == data.Recipient).Any())              // && simulationInstances[SourceReference.Default].core.Id == data.rId)
            {
                // the receiver is unknown but is asigned the last added client since it hasn't got an ID yet
                SimulationInstance value;

                if (!simulationInstances.TryGetValue(default(EntityId), out value))
                {
                    value = simulationInstances.Where(i => i.Value.core.Id == data.Recipient).First().Value;
                }

                simulationInstances[data.Recipient] = value;
                simulationInstances[data.Recipient].ReceiveCommand(devData);

                simulationInstances.Remove(EntityId.Default);
            }
            else if (simulationInstances.ContainsKey(data.Recipient.FullServerId))
            {
                // the receiver itself doesn't exist, but the server for it does
                simulationInstances[data.Recipient.FullServerId].ReceiveCommand(devData);
            }
            else if (data is DevCommandData && (data as DevCommandData).sender != null)
            {
                // no idea what id this is supposed to go but the container has a sender
                (data as DevCommandData).sender.core.EntityManager.ExecuteForReference(data);
            }
            else
            {
                throw new Exception($"the target {data.Recipient} is not registered in the development enviroment {data.Type}");
            }

            /*
             *                                              if (data.rId == ConfigController.ActiveUserId || data.rId == ConfigController.ApplicationSettings.id)
             *                              ReferenceManager.Instance.ExecuteForReference (data);
             *                      else {
             *                              throw new Exception ($"the target {data.rId} is not registered in the development enviroment {data.t}");
             *                      }*/
        }