/// <summary>
            /// Internal function to handle command packages and send them to the simulation manager.
            /// </summary>
            /// <param name="commandPackage">The package that contains the commands.</param>
            /// <param name="senderID">The ID of the sender peer.</param>
            /// <returns>True in case of success, false otherwise.</returns>
            private bool CommandPackageArrived(SCPackage commandPackage, int senderID)
            {
                if (commandPackage == null)
                {
                    throw new ArgumentNullException("commandPackage");
                }
                if (!commandPackage.IsCommitted ||
                    DssRoot.DSS_COMMAND != commandPackage.PackageFormat.ID)
                {
                    throw new ArgumentException("commandPackage");
                }
                if (senderID < 0 || senderID >= this.root.OpCount)
                {
                    throw new ArgumentException("senderID");
                }
                if (senderID == this.root.IdOfThisPeer)
                {
                    throw new ArgumentException("senderID");
                }

                int roundIdx = commandPackage.ReadInt(0);
                int frameIdx = commandPackage.ReadInt(1);

                byte[] cmdBuffer = commandPackage.ReadByteArray(2);
                if (cmdBuffer.Length == 0)
                {
                    return(false);
                }

                List <SCPackage> cmds = new List <SCPackage>();

                int offset = 0;

                while (offset < cmdBuffer.Length)
                {
                    /// Parse and check the next command package.
                    int       parsedBytes = 0;
                    SCPackage currPackage = SCPackage.Parse(cmdBuffer, offset, cmdBuffer.Length, out parsedBytes);
                    if (currPackage != null && currPackage.IsCommitted)
                    {
                        cmds.Add(currPackage);
                        offset += parsedBytes;
                    }
                    else
                    {
                        /// Uncommitted command package.
                        return(false);
                    }
                }

                return(this.root.SimulationMgr.RegisterCommands(cmds.ToArray(), roundIdx, frameIdx, senderID));
            }
Example #2
0
            /// <see cref="DssEventHandler.ControlPackageFromClientHdl"/>
            /// public override bool PackageArrivedHdl(int timestamp, SCPackage package, int senderID) { return false; }
            /// ***************************************** NOT IMPLEMENTED *****************************************

            /// <see cref="DssEventHandler.ControlPackageFromClientHdl"/>
            public override bool ControlPackageFromClientHdl(int timestamp, SCPackage package, int senderID)
            {
                if (IsSimulationRunning_i())
                {
                    /// TODO: implement this function
                    return(true);
                }
                else
                {
                    /// If we are not in the simulation stage, then the event will be handled by another DssEventHandler.
                    return(false);
                }
            }
 /// <see cref="DssEventHandler.PackageArrivedHdl"/>
 public override bool PackageArrivedHdl(int timestamp, SCPackage package, int senderID)
 {
     /// This event is handled only when the simulation is not running (in setup stage).
     if (this.session.CurrentState != this.session.Simulating)
     {
         /// TODO: Incoming (non-control) package from another guest during setup stage will be ignored.
         return(true);
     }
     else
     {
         /// If the session is in Simulating state, then the event will be handled by another DssEventHandler.
         return(false);
     }
 }
 /// <see cref="DssEventHandler.ControlPackageFromServerHdl"/>
 public override bool ControlPackageFromServerHdl(int timestamp, SCPackage package)
 {
     /// This event is handled only when the simulation is not running (in setup stage).
     if (this.session.CurrentState != this.session.Simulating)
     {
         /// Will be handled by the session.
         this.session.ControlPackage(timestamp, package);
         return(true);
     }
     else
     {
         /// If the session is in Simulating state, then the event will be handled by another DssEventHandler.
         return(false);
     }
 }
 /// <see cref="DssEventHandler.PackageArrivedHdl"/>
 public override bool PackageArrivedHdl(int timestamp, SCPackage package, int senderID)
 {
     if (IsSimulationRunning_i())
     {
         if (package.PackageFormat.ID == DssRoot.DSS_COMMAND)
         {
             if (!CommandPackageArrived(package, senderID))
             {
                 this.root.SimulationMgr.SimulationStageError();
             }
         }
         else if (package.PackageFormat.ID == DssRoot.DSS_COMMIT)
         {
             if (!this.root.SimulationMgr.RegisterCommit(package, senderID))
             {
                 this.root.SimulationMgr.SimulationStageError();
             }
         }
         else if (package.PackageFormat.ID == DssRoot.DSS_CTRL_COMMIT_ANSWER)
         {
             if (!this.root.SimulationMgr.RegisterCommitAnswer(package, senderID))
             {
                 this.root.SimulationMgr.SimulationStageError();
             }
         }
         else if (package.PackageFormat.ID == DssRoot.DSS_LEAVE)
         {
             if (this.root.SimulationMgr.RegisterLeaveMessage(package, senderID))
             {
                 /// Indicate the leave of an operator to the root (for closing the corresponding channel at host side).
                 this.root.OperatorLeftDuringSim(senderID);
             }
             else
             {
                 this.root.SimulationMgr.SimulationStageError();
             }
         }
         return(true);
     }
     else
     {
         /// If we are not in the simulation stage, then the event will be handled by another DssEventHandler.
         return(false);
     }
 }
            /// <see cref="DssEventHandler.ControlPackageFromServerHdl"/>
            /// public override bool ControlPackageFromServerHdl(int timestamp, SCPackage package) { return false; }
            /// ***************************************** NOT IMPLEMENTED *****************************************

            /// <see cref="DssEventHandler.ControlPackageFromClientHdl"/>
            public override bool ControlPackageFromClientHdl(int timestamp, SCPackage package, int senderID)
            {
                /// This event is handled only when the simulation is not running (in setup stage).
                if (this.manager.CurrentState != this.manager.RunningSimulation)
                {
                    if (this.manager.CurrentState == this.manager.Waiting || this.manager.CurrentState == this.manager.Uninitialized)
                    {
                        /// Will be handled by the manager.
                        this.manager.ControlPackage(timestamp, package, senderID - 1);
                    }
                    else
                    {
                        /// Control packages are only allowed in Waiting or Uninitialized state during setup stage.
                        this.manager.GuestError(senderID - 1); /// line n is represented by channel n-1.
                    }
                    return(true);
                }
                else
                {
                    /// If the DSS-manager is in RunningSimulation state, then the event will be handled by another DssEventHandler.
                    return(false);
                }
            }
 /// <see cref="DssEventHandler.PackageArrivedHdl"/>
 public override bool PackageArrivedHdl(int timestamp, SCPackage package, int senderID)
 {
     /// This event is handled only when the simulation is not running (in setup stage).
     if (this.manager.CurrentState != this.manager.RunningSimulation)
     {
         if (this.manager.CurrentState == this.manager.Waiting)
         {
             /// TODO: Incoming (non-control) package from a guest is only allowed between a stop_sim and the first
             ///       setup_step_answer packages. Those incoming (non-control) packages will be ignored even if
             ///       the DSS-manager is in Waiting state.
         }
         else
         {
             this.manager.GuestError(senderID - 1); /// line n is represented by channel n-1.
         }
         return(true);
     }
     else
     {
         /// If the DSS-manager is in RunningSimulation state, then the event will be handled by another DssEventHandler.
         return(false);
     }
 }