Exemple #1
0
 private static PolicyHelper GetDefaultPolicy(String url)
 {
     PolicyHelper p = new PolicyHelper();
     p.lastUpdate = DateTime.MinValue;
     p.policy = new TransactionalWebServicePolicy();
     p.policy.AgentsEnabled = true;
     p.policy.BuellerEnabled = true;
     p.policy.PolicyType = policyType.Transactional;
     p.policy.RecordedMessageCap = MAXCAP;
     p.policy.RecordFaultsOnly = true;
     p.policy.RecordHeaders = true;
     p.policy.RecordRequestMessage = false;
     p.policy.RecordResponseMessage = false;
     p.policy.URL = url;
     return p;
 }
Exemple #2
0
        private static PolicyHelper FetchPolicy(string URL)
        {
            MessageProcessor m = MessageProcessor.Instance;
            if (config.OperatingMode == ConfigLoader.OperationMode.OFFLINE)
                return GetDefaultPolicy(URL);
            PolicyHelper myPolicy = null;
            //first lets check our policy cache, if we don't have one cached or it's out of date, update it.
            lock (policyCache)
            {
                myPolicy = (PolicyHelper)policyCache[URL];
                if (myPolicy == null)
                {
                    switch (config.PCSalgo)
                    {
                        case ConfigLoader.Algorithm.FAILOVER:
                            for (int i = 0; i < config.pcsurl.Count; i++)
                            {
                                if (polservice != null)
                                {
                                    try
                                    {
                                        ((IClientChannel)polservice).Close();
                                        ((IClientChannel)polservice).Dispose();
                                    }
                                    catch (Exception ex)
                                    {
                                        //log.TraceEvent(TraceEventType.Warning, 0, "Uh oh" + ex.Message + ex.StackTrace);
                                    }
                                }
                                polservice = config.GetPCSProxy(config.pcsurl[i]);
                                for (int k = 0; k < config.PCSretrycount; k++)
                                {
                                    try
                                    {

                                        ServicePolicyRequestMsg requestr = new ServicePolicyRequestMsg();
                                        //requestr = new ServicePolicyRequestMsg();
                                        requestr.URI = URL;
                                        //remember, .NET/IIS is not case sensitive
                                        // ServicePolicyRequestMsg r = new ServicePolicyRequestMsg();

                                        requestr.classification = currentlevel;
                                        ServicePolicyResponseMsg responsep1 = polservice.GetServicePolicy(requestr);
                                        PolicyHelper temp = new PolicyHelper();
                                        temp.lastUpdate = DateTime.Now;
                                        if (responsep1.policy.GetType() != typeof(TransactionalWebServicePolicy))
                                        {
                                            Logger.info("FGSMS.MessageProcessor" + "Successfully retrieved a policy from the policy configuration service, however it was of an unexpected type " + URL + " type info " + responsep1.policy.GetType().FullName);
                                            throw new ArgumentOutOfRangeException();
                                        }
                                        temp.policy = (TransactionalWebServicePolicy)responsep1.policy;
                                        MAXCAP = temp.policy.RecordedMessageCap;
                                        Enabled = temp.policy.AgentsEnabled;
                                        try
                                        {
                                            policyCache.Add(URL, temp);
                                        }
                                        catch (Exception e)
                                        {
                                            Logger.debug(e, "error caught adding " + URL + " to the policy cache");
                                        }

                                        currentlevel = responsep1.classification;
                                        Logger.info("FGSMS.MessageProcessor" + "Successfully Refreshed policy from the policy configuration service for " + URL);

                                        myPolicy = policyCache[URL] as PolicyHelper;
                                        return myPolicy;
                                    }
                                    //{"There was no endpoint listening at http://localhost:8080/FGSMSServices/PCS that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."}
                                    catch (EndpointNotFoundException ex)
                                    {
                                        _lasterror = ex.GetType().FullName + " " + ex.Message;
                                        Logger.info(ex, "FGSMS.MessageProcessor" + "unable to transmit to " + config.pcsurl[i]);
                                        //this means that the current PCS url is not functioning, try moving to the next one
                                        //polservice = config.GetPCSProxy();
                                    }
                                    catch (Exception ex)
                                    {
                                        Logger.info(ex, "FGSMS.MessageProcessor" + "unable to transmit to " + config.pcsurl[i] + " Policy Updater - failed to policy from the policy configuration service for " + URL + " check that the URL and credentials are correct");
                                    }
                                }

                            }
                            break;
                        case ConfigLoader.Algorithm.ROUNDROBIN:
                            for (int k = 0; k < config.PCSretrycount; k++)
                            {
                                for (int i = 0; i < config.pcsurl.Count; i++)
                                {
                                    try
                                    {
                                        if (polservice != null)
                                        {
                                            try
                                            {
                                                ((IClientChannel)polservice).Close();
                                                ((IClientChannel)polservice).Dispose();
                                            }
                                            catch (Exception ex)
                                            {
                                                //       log.TraceEvent(TraceEventType.Warning, 0, "Uh oh" + ex.Message + ex.StackTrace);
                                            }
                                        }
                                        polservice = config.GetPCSProxy(config.pcsurl[i]);
                                        ServicePolicyRequestMsg requestr = new ServicePolicyRequestMsg();
                                        //requestr = new ServicePolicyRequestMsg();
                                        requestr.URI = URL;
                                        //remember, .NET/IIS is not case sensitive
                                        // GetServicePolicyRequest r = new GetServicePolicyRequest();
                                        // r.request = requestr;
                                        requestr.classification = currentlevel;
                                        ServicePolicyResponseMsg responsep1 = polservice.GetServicePolicy(requestr);
                                        PolicyHelper temp = new PolicyHelper();
                                        temp.lastUpdate = DateTime.Now;
                                        if (responsep1.policy.GetType() != typeof(TransactionalWebServicePolicy))
                                        {
                                            Logger.info("FGSMS.MessageProcessor" + "Successfully retrieved a policy from the policy configuration service, however it was of an unexpected type " + URL + " type info " + responsep1.policy.GetType().FullName);
                                            throw new ArgumentOutOfRangeException();
                                        }
                                        temp.policy = (TransactionalWebServicePolicy)responsep1.policy;
                                        MAXCAP = temp.policy.RecordedMessageCap;
                                        Enabled = temp.policy.AgentsEnabled;
                                        try
                                        {
                                            policyCache.Add(URL, temp);
                                        }
                                        catch (Exception e)
                                        {
                                            Logger.debug(e, "error caught adding " + URL + " to the policy cache");
                                            //its possible that there is another instance of this class running, even through there isn't supposed to be.
                                        }

                                        currentlevel = responsep1.classification;
                                        Logger.info("FGSMS.MessageProcessor" + "Successfully Refreshed policy from the policy configuration service for " + URL);

                                        myPolicy = policyCache[URL] as PolicyHelper;
                                        return myPolicy;
                                    }
                                    //{"There was no endpoint listening at http://localhost:8080/FGSMSServices/PCS that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."}
                                    catch (EndpointNotFoundException ex)
                                    {
                                        _lasterror = ex.GetType().FullName + " " + ex.Message;
                                        //this means that the current PCS url is not function, try moving to the next one
                                        //polservice = config.GetPCSProxy();
                                        Logger.info(ex, "FGSMS.MessageProcessor" + "unable to transmit to " + config.pcsurl[i]);
                                    }
                                    catch (Exception ex)
                                    {
                                        Logger.info(ex, "FGSMS.MessageProcessor" + "unable to transmit to " + config.pcsurl[i] + " Policy Updater - failed to policy from the policy configuration service for " + URL + " check that the URL and credentials are correct");
                                    }
                                }

                            }
                            break;

                    }
                    if (myPolicy == null)
                    {
                        Logger.error("FGSMS.MessageProcessor" + "Cannot contact the policy config service. Reverting to default policy. ");

                        return GetDefaultPolicy(URL);
                    }
                }
            }

            TimeSpan times = XmlConvert.ToTimeSpan(myPolicy.policy.PolicyRefreshRate);
            if ((DateTime.Now.Ticks - myPolicy.lastUpdate.Ticks) > times.Ticks)
            {
                //TODO load balance this
                //if the policy is out of date, update it.
                try
                {
                    //policyConfigurationServiceClient service = new policyConfigurationServiceClient();
                    ServicePolicyRequestMsg requestr = new ServicePolicyRequestMsg();

                    requestr.URI = URL;
                    //ServicePolicyRequest r = new GetServicePolicyRequest();
                    //r.request = requestr;
                    requestr.classification = currentlevel;
                    ServicePolicyResponseMsg responsep = polservice.GetServicePolicy(requestr);
                    policyCache.Remove(URL);
                    PolicyHelper temp = new PolicyHelper();
                    if (responsep.policy.GetType() != typeof(TransactionalWebServicePolicy))
                    {
                        Logger.info("FGSMS.MessageProcessor" + "Successfully retrieved a policy from the policy configuration service, however it was of an unexpected type " + URL + " type info " + responsep.policy.GetType().FullName);
                        throw new ArgumentOutOfRangeException();
                    }
                    temp.policy = (TransactionalWebServicePolicy)responsep.policy;
                    temp.lastUpdate = DateTime.Now;
                    currentlevel = responsep.classification;
                    try
                    {
                        policyCache.Add(URL, temp);
                    }
                    catch (Exception e)
                    {
                        Logger.debug(e, "error caught adding " + URL + " to the policy cache");
                    }
                    Logger.info("Policy Updater- Successfully Refreshed policy from the policy configuration service for " + URL.ToString());
                    return temp;
                }
                catch (Exception ex)
                {
                    Logger.warn(ex, "Policy update failure, unable to update policy for " + URL + ". Using old policy instead. Error:");

                }
            }
            return myPolicy;
        }