Exemple #1
0
        /// <summary>
        /// called by (driver) modules to send notifications to modules that have subscribed to operationName on fromPort
        /// </summary>
        /// <param name="fromPort"></param>
        /// <param name="role"></param>
        /// <param name="opName"></param>
        /// <param name="values"></param>
        protected void Notify(Port fromPort, Role role, string opName, params ParamType[] values)
        {
            var operation = role.GetOperation(opName);

            if (operation == null)
            {
                throw new InvalidOperationException(opName + " is not a valid operation for Role " + role.Name());
            }

            if (!operation.Subscribeable())
            {
                throw new InvalidOperationException(opName + " is not a subscribable operation for Role " + role.Name());
            }

            //check the number of elements
            if (values.Length != operation.ReturnValues().Count)
            {
                throw new ArgumentException("Incorrect number of return values for operation " + opName);
            }

            //check the types of elements in return values
            for (int index = 0; index < values.Length; index++)
            {
                if ((values[index].Maintype() != operation.ReturnValues()[index].Maintype()))
                {
                    throw new ArgumentException(String.Format("Return value {0} is of invalid type. Expected {1}. Got {2}.", index, (ParamType.SimpleType)operation.ReturnValues()[index].Maintype(), (ParamType.SimpleType)values[index].Maintype()));
                }
            }

            fromPort.Notify(role.Name(), opName, values);
        }
Exemple #2
0
        /// <summary>
        /// called by (app) modules to subscribe to another port's role and operation pair
        /// </summary>
        /// <param name="port"></param>
        /// <param name="role"></param>
        /// <param name="opName"></param>
        /// <returns></returns>
        protected bool Subscribe(VPort port, Role role, string opName)
        {
            //first check the store if we have the capability
            VCapability capability = GetCapabilityFromStore(port);

            //if not, then try to get it from the platform
            if (capability == null)
            {
                capability = GetCapabilityFromPlatform(port);
            }

            //if capability is still null, throw exception
            if (capability == null)
            {
                throw new AccessViolationException("This module does not have access to this port");
            }

            var operation = role.GetOperation(opName);

            if (operation == null)
            {
                throw new InvalidOperationException(opName + " is not a valid operation for Role " + role.Name());
            }

            if (!operation.Subscribeable())
            {
                throw new InvalidOperationException(opName + " is not a subscribable operation for Role " + role.Name());
            }


            return(port.Subscribe(role.Name(), opName, ControlPort, capability, ControlPortCapability));
        }
Exemple #3
0
        /// <summary>
        /// Called by (app) modules to invoke operations
        /// </summary>
        /// <param name="toPort"></param>
        /// <param name="role"></param>
        /// <param name="opName"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public IList <VParamType> Invoke(VPort toPort, Role role, string opName, params ParamType[] values)
        {
            //first check the store if we have the capability
            VCapability capability = GetCapabilityFromStore(toPort);

            //if not, then try to get it from the platform
            if (capability == null)
            {
                capability = GetCapabilityFromPlatform(toPort);
            }

            //if capability is still null, throw exception
            if (capability == null)
            {
                throw new AccessViolationException("This module does not have access to this port");
            }


            var operation = role.GetOperation(opName);

            if (operation == null)
            {
                throw new InvalidOperationException(opName + " is not a valid operation for Role " + role.Name());
            }

            //check the number of parameters
            if (values.Length != operation.Parameters().Count)
            {
                throw new ArgumentException("Incorrect number of arguments for operation " + opName);
            }

            //check the types of arguments
            for (int index = 0; index < values.Length; index++)
            {
                if ((values[index].Maintype() != operation.Parameters()[index].Maintype()))
                {
                    throw new ArgumentException(String.Format("Argument {0} is of invalid type. Expected {1}. Got {2}.", index, (ParamType.SimpleType)operation.Parameters()[index].Maintype(), (ParamType.SimpleType)values[index].Maintype()));
                }
            }

            //now invoke the operation
            IList <VParamType> retVals = toPort.Invoke(role.Name(), opName, values, ControlPort, capability, ControlPortCapability);

            //if we got an error, pass it along
            if (retVals.Count >= 1 && retVals[0].Maintype() == (int)ParamType.SimpleType.error)
            {
                return(retVals);
            }

            //if we didn't get an error, sanity check the return values

            //check the number of elements
            if (retVals.Count != operation.ReturnValues().Count)
            {
                throw new ArgumentException("Incorrect number of return values for operation " + opName);
            }

            //check the types of elements in return values
            for (int index = 0; index < retVals.Count; index++)
            {
                if ((retVals[index].Maintype() != operation.ReturnValues()[index].Maintype()))
                {
                    throw new ArgumentException(String.Format("Return value {0} is of invalid type. Expected {1}. Got {2}.", index, (ParamType.SimpleType)operation.ReturnValues()[index].Maintype(), (ParamType.SimpleType)retVals[index].Maintype()));
                }
            }

            //otherwise, lets return these values
            return(retVals);
        }