Esempio n. 1
0
        /// <summary>
        /// Encapsulate the XmlRpc call to standardize security and error handling.
        /// </summary>
        private Hashtable XmlRpcCall(UUID requestingAgentID, string function, Hashtable param)
        {
            XmlRpcResponse resp     = null;
            string         CacheKey = null;

            // Only bother with the cache if it isn't disabled.
            if (m_cacheTimeout > 0)
            {
                if (!function.StartsWith("groups.get"))
                {
                    // Any and all updates cause the cache to clear
                    m_memoryCache.Clear();
                }
                else
                {
                    StringBuilder sb = new StringBuilder(requestingAgentID + function);
                    foreach (object key in param.Keys)
                    {
                        if (param[key] != null)
                        {
                            sb.AppendFormat(",{0}:{1}", key.ToString(), param[key].ToString());
                        }
                    }

                    CacheKey = sb.ToString();
                    m_memoryCache.TryGetValue(CacheKey, out resp);
                }
            }

            if (resp == null)
            {
                if (m_debugEnabled)
                {
                    m_log.DebugFormat("[XMLRPC-GROUPS-CONNECTOR]: Cache miss for key {0}", CacheKey);
                }

                string UserService;
                UUID   SessionID;
                GetClientGroupRequestID(requestingAgentID, out UserService, out SessionID);

                param.Add("RequestingAgentID", requestingAgentID.ToString());
                param.Add("RequestingAgentUserService", UserService);
                param.Add("RequestingSessionID", SessionID.ToString());
                param.Add("ReadKey", m_groupReadKey);
                param.Add("WriteKey", m_groupWriteKey);

                IList parameters = new ArrayList();
                parameters.Add(param);

                ConfigurableKeepAliveXmlRpcRequest req;
                req = new ConfigurableKeepAliveXmlRpcRequest(function, parameters, m_disableKeepAlive);

                try
                {
                    resp = req.Send(m_groupsServerURI, 10000);

                    if ((m_cacheTimeout > 0) && (CacheKey != null))
                    {
                        m_memoryCache.AddOrUpdate(CacheKey, resp, TimeSpan.FromSeconds(m_cacheTimeout));
                    }
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat(
                        "[XMLRPC-GROUPS-CONNECTOR]: An error has occured while attempting to access the XmlRpcGroups server method {0} at {1}",
                        function, m_groupsServerURI);

                    m_log.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: {0}{1}", e.Message, e.StackTrace);

                    foreach (string ResponseLine in req.RequestResponse.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
                    {
                        m_log.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: {0} ", ResponseLine);
                    }

                    foreach (string key in param.Keys)
                    {
                        m_log.WarnFormat("[XMLRPC-GROUPS-CONNECTOR]: {0} :: {1}", key, param[key].ToString());
                    }

                    Hashtable respData = new Hashtable();
                    respData.Add("error", e.ToString());
                    return(respData);
                }
            }

            if (resp.Value is Hashtable)
            {
                Hashtable respData = (Hashtable)resp.Value;
                if (respData.Contains("error") && !respData.Contains("succeed"))
                {
                    LogRespDataToConsoleError(respData);
                }

                return(respData);
            }

            m_log.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: The XmlRpc server returned a {1} instead of a hashtable for {0}", function, resp.Value.GetType().ToString());

            if (resp.Value is ArrayList)
            {
                ArrayList al = (ArrayList)resp.Value;
                m_log.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: Contains {0} elements", al.Count);

                foreach (object o in al)
                {
                    m_log.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: {0} :: {1}", o.GetType().ToString(), o.ToString());
                }
            }
            else
            {
                m_log.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: Function returned: {0}", resp.Value.ToString());
            }

            Hashtable error = new Hashtable();

            error.Add("error", "invalid return value");
            return(error);
        }
        /// <summary>
        ///   Encapsulate the XmlRpc call to standardize security and error handling.
        /// </summary>
        private Hashtable XmlRpcCall(UUID requestingAgentID, string function, Hashtable param)
        {
            XmlRpcResponse resp = null;
            string CacheKey = null;

            // Only bother with the cache if it isn't disabled.
            if (m_cacheTimeout > 0)
            {
                if (!function.StartsWith("groups.get"))
                {
                    // Any and all updates cause the cache to clear
                    m_memoryCache.Clear();
                }
                else
                {
                    StringBuilder sb = new StringBuilder(requestingAgentID + function);
#if (!ISWIN)
                    foreach (object key in param.Keys)
                    {
                        if (param[key] != null)
                        {
                            sb.AppendFormat(",{0}:{1}", key, param[key]);
                        }
                    }
#else
                    foreach (object key in param.Keys.Cast<object>().Where(key => param[key] != null))
                    {
                        sb.AppendFormat(",{0}:{1}", key, param[key]);
                    }
#endif

                    CacheKey = sb.ToString();
                    m_memoryCache.TryGetValue(CacheKey, out resp);
                }
            }

            if (resp == null)
            {
                string UserService;
                UUID SessionID;
                GetClientGroupRequestID(requestingAgentID, out UserService, out SessionID);

                param.Add("RequestingAgentID", requestingAgentID.ToString());
                param.Add("RequestingAgentUserService", UserService);
                param.Add("RequestingSessionID", SessionID.ToString());
                param.Add("ReadKey", m_groupReadKey);
                param.Add("WriteKey", m_groupWriteKey);

                IList parameters = new ArrayList();
                parameters.Add(param);

                ConfigurableKeepAliveXmlRpcRequest req;
                req = new ConfigurableKeepAliveXmlRpcRequest(function, parameters, m_disableKeepAlive);

                try
                {
                    resp = req.Send(m_groupsServerURI, 10000);

                    if ((m_cacheTimeout > 0) && (CacheKey != null))
                    {
                        m_memoryCache.AddOrUpdate(CacheKey, resp, TimeSpan.FromSeconds(m_cacheTimeout));
                    }
                }
                catch (Exception e)
                {
                    MainConsole.Instance.ErrorFormat(
                        "[XMLRPC-GROUPS-CONNECTOR]: An error has occured while attempting to access the XmlRpcGroups server method {0} at {1}",
                        function, m_groupsServerURI);

                    MainConsole.Instance.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: {0}{1}", e.Message, e.StackTrace);

                    foreach (
                        string ResponseLine in
                            req.RequestResponse.Split(new[] {Environment.NewLine}, StringSplitOptions.None))
                    {
                        MainConsole.Instance.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: {0} ", ResponseLine);
                    }

                    foreach (string key in param.Keys)
                    {
                        MainConsole.Instance.WarnFormat("[XMLRPC-GROUPS-CONNECTOR]: {0} :: {1}", key, param[key]);
                    }

                    Hashtable respData = new Hashtable {{"error", e.ToString()}};
                    return respData;
                }
            }

            if (resp.Value is Hashtable)
            {
                Hashtable respData = (Hashtable) resp.Value;
                if (respData.Contains("error") && !respData.Contains("succeed"))
                {
                    LogRespDataToConsoleError(respData);
                }

                return respData;
            }

            MainConsole.Instance.ErrorFormat(
                "[XMLRPC-GROUPS-CONNECTOR]: The XmlRpc server returned a {1} instead of a hashtable for {0}", function,
                resp.Value.GetType());

            if (resp.Value is ArrayList)
            {
                ArrayList al = (ArrayList) resp.Value;
                MainConsole.Instance.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: Contains {0} elements", al.Count);

                foreach (object o in al)
                {
                    MainConsole.Instance.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: {0} :: {1}", o.GetType(), o);
                }
            }
            else
            {
                MainConsole.Instance.ErrorFormat("[XMLRPC-GROUPS-CONNECTOR]: Function returned: {0}", resp.Value);
            }

            Hashtable error = new Hashtable {{"error", "invalid return value"}};
            return error;
        }