Ejemplo n.º 1
0
        // LiYoubing 20110712 加入策略调用异常处理
        /// <summary>
        /// 指示是否已经发生过策略调用异常true是false否
        /// 20110820废弃 策略代码中存在条件性除零/数组越界等异常时 不必要直接结束掉策略运行
        /// </summary>
        //bool _strategyExceptionFlag = false;

        //20101229
        /// <summary>
        /// 向Server服务实例发送决策计算结果
        /// 决策算法的调用过程在此
        /// </summary>
        /// <param name="announce">包含仿真使命Mission对象的消息</param>
        /// <returns></returns>
        IEnumerator <ITask> AnnounceDecisionToServer(server.MissionPara announce)
        {
            if (_clientControlBoard.StragegyInterface == null /*|| _strategyExceptionFlag == true*/)
            {// 加上null检查确保程序不会异常 LiYoubing 20110511
                yield break;
            }
            // ClientAnnounceDecision是Server服务主端口接受的一种消息类型
            client.ClientAnnounceDecision announcedec = new client.ClientAnnounceDecision();
            Decision[] decisions = null;

            try
            {
                // 交换半场后交换队伍id处理 LiYoubing 20110712
                // 交换半场后TeamsRef[0]/[1]代表交换前右/左半场的队伍因此teamId应该分别变为1/0
                //int strategyId = announce.Body.CurMission.CommonPara.IsExchangedHalfCourt ? (_teamId + 1) % 2 : _teamId;
                int strategyId = _teamId;
                if (announce.Body.CurMission.CommonPara.TeamCount == 2)
                {// 跟踪有且仅有两支队伍参与的仿真使命的半场状态 LiYoubing 20120520
                    strategyId = announce.Body.CurMission.CommonPara.IsExchangedHalfCourt ? (_teamId + 1) % 2 : _teamId;
                    bool rdoLeftChecked = (announce.Body.CurMission.TeamsRef[strategyId].Para.MyHalfCourt
                                           == HalfCourt.LEFT) ? true : false;
                    WinFormsServicePort.FormInvoke(delegate()
                    {
                        _clientControlBoard.SetRadioTeamState(rdoLeftChecked, !rdoLeftChecked, announce.Body.CurMission.TeamsRef[strategyId].Fishes[0].ColorFish);
                    });
                }
                decisions = _clientControlBoard.StragegyInterface.GetDecision(announce.Body.CurMission, strategyId);
                //decisions = _clientControlBoard.StragegyInterface.GetDecision(announce.Body.CurMission, _teamId);
            }
            catch
            {
                //_strategyExceptionFlag = true;
                MessageBox.Show("Remoting object timeout.\nThe instance of class Strategy has been released.\n"
                                + "Your simulated robofish will not be controlled.",
                                "Confirming", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (decisions == null)
            {
                yield break;
            }
            announcedec.Body = new client.ClientAnnounceDecisionRequest(decisions, _teamId);

            // 使用Uri建立到Server服务实例附加端口的Forwarder引用
            client.ClientBaseOperations alternateServerPort = ServiceForwarder <client.ClientBaseOperations>(_alternateServerPortUri);
            if (alternateServerPort != null)
            {
                alternateServerPort.Post(announcedec);

                DefaultUpdateResponseType announceRsp = null;
                yield return(Arbiter.Choice(announce.ResponsePort,
                                            delegate(DefaultUpdateResponseType rsp)
                {
                    announceRsp = rsp;
                },
                                            delegate(Fault f)
                {
                    WinFormsServicePort.FormInvoke(delegate()
                    {
                        _clientControlBoard.DisplayCompetitionState(string.Format("Connection failed"));
                    });
                }));
            }

            yield break;
        }
Ejemplo n.º 2
0
        //void MissionParaNotificationHandler(server.MissionPara announce)
        //{
        //    // ClientAnnounceDecision是Server服务主端口接受的一种消息类型
        //    client.ClientAnnounceDecision announcedec = new client.ClientAnnounceDecision();
        //    Decision[] decisions = _clientControlBoard.StragegyInterface.GetDecision(announce.Body.CurMission, _teamId);
        //    announcedec.Body = new client.ClientAnnounceDecisionRequest(decisions, _teamId);

        //    // 使用Uri建立到Server服务实例主端口的Forwarder引用
        //    client.ClientBaseOperations alternateServerPort = ServiceForwarder<client.ClientBaseOperations>(_alternateServerPortUri);
        //    if (alternateServerPort != null)
        //    {
        //        alternateServerPort.Post(announcedec);
        //    }
        //}

        /// <summary>
        /// Client服务实例接收到Server服务实例Notify的Mission对象后
        /// 调用策略中的GetDecision方法计算决策结果
        /// 然后将决策结果数组Announce给Server服务实例
        /// </summary>
        /// <param name="announce">Server服务实例Notify的包含Mission对象的消息</param>
        void MissionParaNotificationHandler(server.MissionPara announce)
        {
            //20101229
            SpawnIterator <server.MissionPara>(announce, AnnounceDecisionToServer);
        }