/// <summary>
        /// Redirects the HTTP request from an alternate contract to a partner
        /// </summary>
        /// <param name="contract">The contract.</param>
        /// <param name="partner">The partner.</param>
        /// <param name="context">The context.</param>
        /// <param name="responsePort">The response port.</param>
        private void RedirectHttpRequest(
            string contract,
            IPort partner,
            HttpListenerContext context,
            PortSet <HttpResponseType, Fault> responsePort)
        {
            var alternate     = AlternateContractServiceInfo.Find(s => s.Contract == contract) ?? ServiceInfo;
            var basePath      = alternate.HttpServiceAlias.AbsolutePath;
            var requestedPath = context.Request.Url.PathAndQuery;
            var pathSuffix    = requestedPath.Substring(basePath.Length);

            var lookup = new DsspDefaultLookup();

            partner.PostUnknownType(lookup);
            this.Activate(
                lookup.ResponsePort.Choice(
                    svcinfo =>
            {
                var redirectPath = svcinfo.HttpServiceAlias.AbsolutePath + pathSuffix;
                context.Response.Redirect(redirectPath);
                context.Response.Close();
                responsePort.Post(new HttpResponseType());
            },
                    responsePort.Post));
        }
示例#2
0
        /// <summary>
        /// Post DsspDefaultLookup and return the response port.
        /// </summary>
        /// <returns></returns>
        public virtual PortSet <LookupResponse, Fault> DsspDefaultLookup()
        {
            DsspDefaultLookup op = new DsspDefaultLookup();

            op.Body = new LookupRequestType();
            this.Post(op);
            return(op.ResponsePort);
        }
        /// <summary>
        /// Post Dssp Default Lookup and return the response port.
        /// </summary>
        public virtual PortSet <LookupResponse, Fault> DsspDefaultLookup()
        {
            LookupRequestType body = new LookupRequestType();
            DsspDefaultLookup op   = new DsspDefaultLookup(body);

            this.PostUnknownType(op);
            return(op.ResponsePort);
        }
示例#4
0
文件: NxtDirect.cs 项目: yingted/Myro
        private void InitializeState()
        {
            _state.Status = "Not connected";

            DsspDefaultLookup lu = new DsspDefaultLookup();

            _mainPort.Post(lu);
            Activate(Arbiter.Choice(lu.ResponsePort,
                                    delegate(LookupResponse response)
            {
                _state.Status       = "Connected";
                _state.BrickService = response.HttpUri().AbsoluteUri;
            },
                                    delegate(Fault fault) { _state.Status = "Failed to connect"; }));
        }
        /// <summary>
        /// Forwards a subscription to a partner
        /// </summary>
        /// <param name="partner">The partner.</param>
        /// <param name="request">The request.</param>
        /// <param name="responsePort">The response port.</param>
        private void ForwardSubscription(
            IPort partner,
            SubscribeRequestType request,
            PortSet <SubscribeResponseType, Fault> responsePort)
        {
            var lookup = new DsspDefaultLookup();

            partner.PostUnknownType(lookup);
            this.Activate(
                lookup.ResponsePort.Choice(
                    svcinfo =>
            {
                var submgrInfo = svcinfo.PartnerList.Find(p => p.Contract == submgr.Contract.Identifier);
                var subMgrOps  = ServiceForwarder <submgr.SubscriptionManagerPort>(submgrInfo.Service);
                var insert     = new submgr.InsertSubscription(request)
                {
                    ResponsePort = responsePort
                };
                subMgrOps.Post(insert);
            },
                    responsePort.Post));
        }
示例#6
0
 /// <summary>
 /// Post(DsspDefaultLookup)
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public void Post(DsspDefaultLookup item)
 {
     base.PostUnknownType(item);
 }
        /// <summary>
        /// Process Brick State
        /// </summary>
        /// <param name="brickState"></param>
        private IEnumerator <ITask> ProcessBrickState(brick.NxtBrickState brickState)
        {
            foreach (string key in brickState.Runtime.Devices.Keys)
            {
                brick.AttachRequest device = brickState.Runtime.Devices[key];
                if (device.Registration.DeviceType != LegoDeviceType.AnalogSensor &&
                    device.Registration.DeviceType != LegoDeviceType.DigitalSensor)
                {
                    continue;
                }

                PortSet <DsspDefaultLookup, DsspDefaultGet> lookupPort = ServiceForwarder <PortSet <DsspDefaultLookup, DsspDefaultGet> >(device.Registration.ServiceUri);
                DsspDefaultLookup lu = new DsspDefaultLookup();
                lookupPort.Post(lu);
                yield return(Arbiter.Choice(lu.ResponsePort,
                                            delegate(LookupResponse luResponse)
                {
                    foreach (PartnerType pt in luResponse.PartnerList)
                    {
                        // See if this service supports the analog sensor contract
                        if (pt.Contract == analog.Contract.Identifier)
                        {
                            // Check if we have already processed this one.
                            if (_sensorList.ContainsKey(pt.Service))
                            {
                                break;
                            }

                            string name = device.Registration.Name;
                            string model = device.Registration.DeviceModel;
                            int hardwareIdentifier = NxtCommon.HardwareIdentifier(device.Registration.Connection.Port);

                            LogVerbose(LogGroups.Console, string.Format("Configuring {0}:{1} on {2} with analog service at {3}", model, name, hardwareIdentifier, pt.Service));
                            analog.AnalogSensorOperations sensorPort = ServiceForwarder <analog.AnalogSensorOperations>(pt.Service);
                            Activate(Arbiter.Choice(sensorPort.Subscribe(_analogSensorNotificationsPort),
                                                    delegate(SubscribeResponseType response)
                            {
                                // Keep track of the subscription manager response
                                // so that we can unsubscribe later.
                                _sensorList.Add(pt.Service, response);
                            },
                                                    delegate(Fault fault)
                            {
                                LogError(LogGroups.Console, string.Format("Failure subscribing to {0} on port {1}.", model, hardwareIdentifier));
                            }));

                            foreach (SensorConfiguration cfg in _state.SensorConfiguration)
                            {
                                if (cfg.DeviceModel != model)
                                {
                                    continue;
                                }

                                SensorRange range = new SensorRange(hardwareIdentifier, model, name, cfg.RangeName);
                                PortConfiguration portConfig = new PortConfiguration(hardwareIdentifier, range.ContactSensorName, cfg.SuccessRangeMin, cfg.SuccessRangeMax);
                                portConfig.AnalogSensorServiceUri = pt.Service;

                                if (portConfig != null)
                                {
                                    _state.RuntimeConfiguration.Add(range, portConfig);
                                }
                            }
                            break;
                        }
                    }
                },
                                            delegate(Fault f) { }));
            }
        }
        /// <summary>
        /// Process Brick State
        /// </summary>
        /// <param name="brickState"></param>
        private IEnumerator<ITask> ProcessBrickState(brick.NxtBrickState brickState)
        {
            foreach (string key in brickState.Runtime.Devices.Keys)
            {
                brick.AttachRequest device = brickState.Runtime.Devices[key];
                if (device.Registration.DeviceType != LegoDeviceType.AnalogSensor
                    && device.Registration.DeviceType != LegoDeviceType.DigitalSensor)
                {
                    continue;
                }

                PortSet<DsspDefaultLookup, DsspDefaultGet> lookupPort = ServiceForwarder<PortSet<DsspDefaultLookup, DsspDefaultGet>>(device.Registration.ServiceUri);
                DsspDefaultLookup lu = new DsspDefaultLookup();
                lookupPort.Post(lu);
                yield return Arbiter.Choice(lu.ResponsePort,
                    delegate(LookupResponse luResponse)
                    {
                        foreach(PartnerType pt in luResponse.PartnerList)
                        {
                            // See if this service supports the analog sensor contract
                            if (pt.Contract == analog.Contract.Identifier)
                            {
                                // Check if we have already processed this one.
                                if (_sensorList.ContainsKey(pt.Service))
                                    break;

                                string name = device.Registration.Name;
                                string model = device.Registration.DeviceModel;
                                int hardwareIdentifier = NxtCommon.HardwareIdentifier(device.Registration.Connection.Port);

                                LogVerbose(LogGroups.Console, string.Format("Configuring {0}:{1} on {2} with analog service at {3}", model, name, hardwareIdentifier, pt.Service));
                                analog.AnalogSensorOperations sensorPort = ServiceForwarder<analog.AnalogSensorOperations>(pt.Service);
                                Activate(Arbiter.Choice(sensorPort.Subscribe(_analogSensorNotificationsPort),
                                    delegate(SubscribeResponseType response)
                                    {
                                        // Keep track of the subscription manager response
                                        // so that we can unsubscribe later.
                                        _sensorList.Add(pt.Service, response);
                                    },
                                    delegate(Fault fault)
                                    {
                                        LogError(LogGroups.Console, string.Format("Failure subscribing to {0} on port {1}.", model, hardwareIdentifier));
                                    }));

                                foreach (SensorConfiguration cfg in _state.SensorConfiguration)
                                {
                                    if (cfg.DeviceModel != model)
                                        continue;

                                    SensorRange range = new SensorRange(hardwareIdentifier, model, name, cfg.RangeName);
                                    PortConfiguration portConfig = new PortConfiguration(hardwareIdentifier, range.ContactSensorName, cfg.SuccessRangeMin, cfg.SuccessRangeMax);
                                    portConfig.AnalogSensorServiceUri = pt.Service;

                                    if (portConfig != null)
                                        _state.RuntimeConfiguration.Add(range, portConfig);
                                }
                                break;
                            }
                        }
                    },
                    delegate(Fault f) { });

            }
        }