Exemple #1
0
        ///// <summary>
        ///// </summary>
        //private void subscribeAutos()
        //{
        //    // Get the list of partners whose names start with "auto:"
        //    //var request = new partnerList.GetOperation()
        //    //{
        //    //    Body = new GetRequestType(),
        //    //    ResponsePort = new DsspResponsePort<PartnerListType>()
        //    //};
        //    //base.PartnerListManagerPort.Post(request);
        //    //Activate(Arbiter.Choice<PartnerListType, Fault>(
        //    //    request.ResponsePort,
        //    //    delegate(PartnerListType partners)
        //    //    {
        //    //        string autoPrefix = "Auto_";
        //    //        List<PartnerType> autoPartners = new List<PartnerType>(
        //    //            from partner in partners.PartnerList
        //    //            where partner.Name.Name.StartsWith(autoPrefix)
        //    //            select partner);

        //    //        foreach (var p in autoPartners)
        //    //            Console.WriteLine(p.Name);

        //    //        // This method will take care of subscribing and updating state
        //    //        //if (autoPartners.Count > 0)
        //    //        //    subscribeAutos2(autoPartners, autoPrefix.Length);
        //    //    },
        //    //    delegate(Fault failure)
        //    //    {
        //    //        LogError("Fault while getting partner list to subscribe to autos", failure);
        //    //    }));

        //    //List<PartnerType> partnerList = new List<PartnerType>();
        //    //var responsePort = new DsspResponsePort<DefaultUpsertResponseType>();
        //    //Activate(Arbiter.MultipleItemReceive(responsePort, _state.AutoSubsets.Count,
        //    //    delegate(ICollection<DefaultUpsertResponseType> successes, ICollection<Fault> failures)
        //    //    {
        //    //        foreach (Fault failure in failures)
        //    //            LogError("Inserting partner failed", failure);
        //    subscribeAutos2();
        //    //}));
        //}

        /// <summary>
        /// Subscribes to any services specified in the partner list to
        /// automatically update the state.
        /// Once we have a list of auto partners, this method creates AutoDefiniton
        /// objects for each auto partner.  It then waits for each partner to be
        /// found, and calls subscribeAutoSingle to subscribe to each one.
        /// </summary>
        /// <param name="partList"></param>
        /// <param name="removeFromName"></param>
        private void subscribeAutos()
        {
            //foreach (var subset in _state.AutoSubsets)
            //{
            //    PartnerType partner = new PartnerType() { Name = new XmlQualifiedName(subset.PartnerName, Contract.Identifier) };
            //    partnerList.Add(partner);
            //    //base.PartnerListManagerPort.Post(new partnerList.Upsert() { Body = partner, ResponsePort = responsePort });
            //}
            // Create AutoDefinition objects
            autoDefs = new List <AutoDefinition>(_state.AutoSubsets.Count);
            //int lastIndex = 0;
            //List<string> allKeys = new List<string>();
            List <double> allValues = new List <double>();

            foreach (var subset in _state.AutoSubsets)
            {
                // Create AutoDefinition object and add to list
                //string[] keys = part.Name.Name.Substring(removeFromName).Split('_');
                //AutoSubset subset = _state.AutoSubsets.Find((s => part.Name.Name.Equals(s.PartnerName, StringComparison.OrdinalIgnoreCase)));
                //if (subset != default(AutoSubset))
                //{
                //var newKeys = new List<string>();
                //for (int i = 0; i < subset.startIndex; i++)
                //    newKeys.Add("");
                //newKeys.AddRange(subset.keys);
                //}
                AutoDefinition autoDef = new AutoDefinition()
                {
                    partnerName = subset.PartnerName,
                    startIndex  = 0,
                    count       = 0,
                    subsetStart = (subset == default(AutoSubset) ? 0 : subset.StartIndex),
                    subsetCount = (subset == default(AutoSubset) ? 0 : subset.Count),
                    keys        = (subset == default(AutoSubset) ? new List <string>() : subset.Keys)
                };
                autoDefs.Add(autoDef);
            }

            foreach (var autoDef in autoDefs)
            {
                if (autoDef.subsetCount >= 1)
                {
                    // Also build a default vector
                    var values = new List <double>(autoDef.subsetCount);
                    for (int i = 0; i < autoDef.subsetCount; i++)
                    {
                        values.Add(0.0);
                    }
                    modifyStateSubset(autoDef, values);
                }
            }


            // Set values, and take keys provided in subset defs by user
            //_state.SetAll(allValues, DateTime.Now);
            //applyKeysFromSubsets();

            //var upsertRespPort = new DsspResponsePort<DefaultUpsertResponseType>();
            //PartnerListManagerPort.Post(new partnerList.Upsert() { Body = new PartnerType() { Name = new XmlQualifiedName("iRobotGenericDrive", Microsoft.Dss.ServiceModel.Dssp.Contract.Identifier) }, ResponsePort = upsertRespPort });
            //Activate(Arbiter.Choice(upsertRespPort,
            //    delegate(DefaultUpsertResponseType suc)
            //    {
            //        //var queryRespPort = new DsspResponsePort<Microsoft.Dss.Schemas.PartnerListManager.QueryResponseType>();
            //        //PartnerListManagerPort.Post(new partnerList.Query() { Body = new partnerListS.QueryRequestType() { Name = new XmlQualifiedName("iRobotGenericDrive", Contract.Identifier) }, ResponsePort = queryRespPort });
            //        //Activate(Arbiter.Choice(queryRespPort,
            //        //    delegate(partnerListS.QueryResponseType qsuc)
            //        //    {
            //        //        Console.WriteLine(qsuc.Partner.Name + " : " + qsuc.Partner.Contract + " : " + qsuc.Partner.Service);
            //        //    },
            //        //    delegate(Fault failure)
            //        //    {
            //        //        Console.WriteLine("Fault querying");
            //        //    }));
            //        var partsRespPort = base.IssuePartnerSubscribe(new XmlQualifiedName("iRobotGenericDrive", Microsoft.Dss.ServiceModel.Dssp.Contract.Identifier));
            //        Activate(Arbiter.Choice(partsRespPort,
            //            delegate(PartnerListType partListResponse)
            //            {
            //                foreach (var partner in partListResponse.PartnerList)
            //                {
            //                    Console.WriteLine(partner.Name + " : " + partner.Contract + " : " + partner.Service);
            //                }
            //            },
            //            delegate(Fault failure)
            //            {
            //                Console.WriteLine("Fault querying");
            //            }));
            //    },
            //    delegate(Fault fail)
            //    {
            //        Console.WriteLine("Fault upserting");
            //    }));


            // Create the new vector state reflecting the keys
            //List<double> allValues = new List<double>(from k in allKeys select 0.0);
            //VectorState newState = new VectorState(allValues, allKeys, DateTime.Now);
            //var responsePort = new DsspResponsePort<DefaultReplaceResponseType>();
            //OperationsPort.Post(new Replace(newState, responsePort));
            //Activate(Arbiter.Choice(responsePort,
            //    delegate(DefaultReplaceResponseType r) { },
            //    delegate(Fault f) { LogError("Fault while replacing initial vector state", f); }));

            // Try to subscribe to compatible contracts with the AutoDefinition objects
            foreach (var autoDef in autoDefs)
            {
                AutoDefinition myAutoDef = autoDef;
                //Console.WriteLine("Waiting 10 seconds before subscribing to partner list...");
                //Thread.Sleep(10000);
                var partRespPort = base.IssuePartnerSubscribe(new XmlQualifiedName(myAutoDef.partnerName, Contract.Identifier));
                //Console.WriteLine("Looking for " + myAutoDef.infoAsPartner.Name);
                Activate(Arbiter.Choice(partRespPort,
                                        delegate(PartnerListType partListResponse)
                {
                    //Console.WriteLine("Found " + partListResponse.PartnerList.Count + " for " + myAutoDef.infoAsPartner.Name);
                    if (partListResponse.PartnerList.Count > 1)
                    {
                        LogWarning("More than one partner found for " + myAutoDef.partnerName);
                    }
                    else if (partListResponse.PartnerList.Count < 1)
                    {
                        LogWarning("No partners found for " + myAutoDef.partnerName);
                    }
                    foreach (var partner in partListResponse.PartnerList)
                    {
                        if (myAutoDef.infoAsConnected == null)
                        {
                            //Console.WriteLine("Searching for a contract for " + partner.Service);
                            Activate(Arbiter.Choice(RSUtils.FindCompatibleContract(TaskQueue, new Uri(partner.Service), new List <string>()
                            {
                                analog.Contract.Identifier, analogArray.Contract.Identifier, contact.Contract.Identifier
                            }),
                                                    delegate(ServiceInfoType serviceInfoResponse)
                            {
                                try
                                {
                                    subscribeAutoSingle(myAutoDef, serviceInfoResponse);
                                    myAutoDef.infoAsConnected = serviceInfoResponse;
                                    LogInfo("Vector service " + base.ServiceInfo.Service + " subscribed auto update to " + serviceInfoResponse.Service + " with contract " + serviceInfoResponse.Contract);
                                }
                                catch (Exception e)
                                {
                                    LogError("Exception while subscribing to auto partner", e);
                                }
                            },
                                                    delegate(Fault failure)
                            {
                                Exception e = RSUtils.ExceptionOfFault(failure);
                                if (e is NoContractFoundException)
                                {
                                    LogError("Could not subscribe to auto partner " + myAutoDef.partnerName + ".  Could not find a supported contract.");
                                }
                                else if (e is Exception)
                                {
                                    LogError("Fault while searching for compatible contract", e);
                                }
                            }));
                        }
                    }
                },
                                        delegate(Fault failure)
                {
                    Console.WriteLine("Fault for " + myAutoDef.partnerName);
                    LogError("Fault from subscription to partner list service", failure);
                }));
                //Thread.Sleep(2000);
            }
        }