public bool CheckThreatLevel(string SessionID, string function, ThreatLevel defaultThreatLevel)
        {
            if (!m_useRegistrationService)
            {
                return(true);
            }

            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", SessionID);

            if (urls != null)
            {
                //Past time for it to expire
                if (m_useSessionTime && urls.Expiration < DateTime.UtcNow)
                {
                    MainConsole.Instance.Warn("[GridRegService]: URLs expired for " + SessionID);
                    RemoveUrlsForClient(SessionID);
                    return(false);
                }
                //First find the threat level that this setting has to have do be able to run
                ThreatLevel functionThreatLevel = PermissionSet.FindThreatLevelForFunction(function, defaultThreatLevel);
                //Now find the permission for that threat level
                //else, check it against the threat level that the region has
                ThreatLevel regionThreatLevel = FindRegionThreatLevel(SessionID);
                //Return whether the region threat level is higher than the function threat level
                if (!(functionThreatLevel <= regionThreatLevel))
                {
                    MainConsole.Instance.Warn("[GridRegService]: checkThreatLevel (" + function + ") failed for " + SessionID + ", fperm " + functionThreatLevel + ", rperm " + regionThreatLevel + "!");
                }
                return(functionThreatLevel <= regionThreatLevel);
            }
            MainConsole.Instance.Warn("[GridRegService]: Could not find URLs for checkThreatLevel for " + SessionID + "!");
            return(false);
        }
        public bool CheckThreatLevel(string SessionID, ulong RegionHandle, string function, ThreatLevel defaultThreatLevel)
        {
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", RegionHandle.ToString(), new GridRegistrationURLs());

            if (urls != null)
            {
                //Past time for it to expire
                if (urls.Expiration < DateTime.Now)
                {
                    RemoveUrlsForClient(SessionID, RegionHandle);
                    return(false);
                }
                //First find the threat level that this setting has to have do be able to run
                ThreatLevel functionThreatLevel = PermissionSet.FindThreatLevelForFunction(function, defaultThreatLevel);
                //Now find the permission for that threat level
                //else, check it against the threat level that the region has
                GridRegion region = FindRegion(RegionHandle);
                if (region == null)
                {
                    return(false);
                }
                string      rThreat           = region.GenericMap["ThreatLevel"].AsString();
                ThreatLevel regionThreatLevel = m_defaultRegionThreatLevel;
                if (rThreat != "")
                {
                    regionThreatLevel = (ThreatLevel)Enum.Parse(typeof(ThreatLevel), rThreat);
                }
                //Return whether the region threat level is higher than the function threat level
                return(functionThreatLevel <= regionThreatLevel);
            }
            return(false);
        }
        public void RemoveUrlsForClient(string SessionID)
        {
            if (!m_useRegistrationService)
            {
                return;
            }

            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", SessionID);

            if (urls != null)
            {
                MainConsole.Instance.WarnFormat("[GridRegService]: Removing URLs for {0}", SessionID);
                //Remove all the handlers from the HTTP Server
                foreach (IGridRegistrationUrlModule module in m_modules.Values)
                {
                    if (!urls.URLS.ContainsKey(module.UrlName))
                    {
                        continue;
                    }
                    try
                    {
                        module.RemoveUrlForClient(urls.SessionID, urls.URLS[module.UrlName], urls.Ports[module.UrlName]);
                    }
                    catch
                    {
                    }
                }
            }
            //Remove from the database so that they don't pop up later
            m_genericsConnector.RemoveGeneric(UUID.Zero, "GridRegistrationUrls", SessionID);
        }
        private bool CheckModuleNames(GridRegistrationURLs urls)
        {
#if (!ISWIN)
            foreach (string urlName in m_modules.Keys)
            {
#if (!ISWIN)
                bool found = false;
                foreach (string o in urls.URLS.Keys)
                {
                    if (o == urlName)
                    {
                        found = true;
                        break;
                    }
                }
#else
                bool found = urls.URLS.Keys.Any(o => o == urlName);
#endif
                if (!found)
                {
                    return(false);
                }
            }
            return(true);
#else
            return(m_modules.Keys.Select(urlName => urls.URLS.Keys.Any(o => o == urlName)).All(found => found));
#endif
        }
        public OSDMap RegionRemoteHandlerURL(GridRegion regionInfo, UUID sessionID, UUID oldSessionID)
        {
            List <GridRegistrationURLs> urls = m_genericsConnector.GetGenerics <GridRegistrationURLs>(
                regionInfo.RegionID, "RegionRegistrationUrls");

            m_genericsConnector.RemoveGeneric(regionInfo.RegionID, "RegionRegistrationUrls");
            if ((urls == null) || (urls.Count == 0) || (urls[0].Expiration < DateTime.UtcNow))
            {
                urls = new List <GridRegistrationURLs>();
                OSDMap hostnames    = new OSDMap();
                OSDMap databaseSave = new OSDMap
                {
                    {
                        "regionURI",
                        "http://" + regionInfo.ExternalEndPoint.Address + ":" + regionInfo.ExternalEndPoint.Port + "/region" + UUID.Random()
                    }
                };
                OSDMap ports = new OSDMap {
                    { "regionURI", regionInfo.InternalEndPoint.Port }
                };

                GridRegistrationURLs urls2 = new GridRegistrationURLs
                {
                    URLS       = databaseSave,
                    SessionID  = sessionID.ToString(),
                    Ports      = ports,
                    HostNames  = hostnames,
                    Expiration = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout * 60)
                };
                urls.Add(urls2);
            }
            m_genericsConnector.AddGeneric(regionInfo.RegionID, "RegionRegistrationUrls", sessionID.ToString(), urls[0].ToOSD());
            return(urls[0].ToOSD());
        }
Example #6
0
            public override IDataTransferable Duplicate()
            {
                GridRegistrationURLs url = new GridRegistrationURLs();

                url.FromOSD(ToOSD());
                return(url);
            }
        public void UpdateUrlsForClient(string SessionID)
        {
            if (!m_useRegistrationService)
            {
                return;
            }

            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", SessionID);

            InnerUpdateUrlsForClient(urls);
        }
 private void InnerUpdateUrlsForClient(GridRegistrationURLs urls)
 {
     if (urls != null)
     {
         urls.Expiration = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout * 60);
         m_genericsConnector.AddGeneric(UUID.Zero, "GridRegistrationUrls", urls.SessionID, urls.ToOSD());
         MainConsole.Instance.DebugFormat("[GridRegistrationService]: Updated URLs for {0}", urls.SessionID);
     }
     else
     {
         MainConsole.Instance.ErrorFormat("[GridRegistrationService]: Failed to find URLs to update for {0}", "unknown");
     }
 }
Example #9
0
        public void UpdateUrlsForClient(ulong RegionHandle)
        {
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", RegionHandle.ToString(), new GridRegistrationURLs());

            if (urls != null)
            {
                urls.Expiration = DateTime.Now.AddHours(m_timeBeforeTimeout);
                m_genericsConnector.AddGeneric(UUID.Zero, "GridRegistrationUrls", RegionHandle.ToString(), urls.ToOSD());
                m_log.WarnFormat("[GridRegistrationService]: Updated URLs for {0}", RegionHandle);
            }
            else
            {
                m_log.ErrorFormat("[GridRegistrationService]: Failed to find URLs to update for {0}", RegionHandle);
            }
        }
Example #10
0
        public OSDMap GetUrlForRegisteringClient(string SessionID, ulong RegionHandle)
        {
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", RegionHandle.ToString(), new GridRegistrationURLs());
            OSDMap retVal = new OSDMap();

            if (urls != null)
            {
                urls.Expiration   = DateTime.Now.AddHours(m_timeBeforeTimeout);
                urls.SessionID    = SessionID;
                urls.RegionHandle = RegionHandle;
                foreach (KeyValuePair <string, OSD> module in urls.URLS)
                {
                    //Build the URL
                    retVal[module.Key] = m_loadBalancer.GetHost() + ":" + m_modules[module.Key].Port + module.Value.AsString();
                }
                return(retVal);
            }
            OSDMap databaseSave = new OSDMap();

            //Get the URLs from all the modules that have registered with us
            foreach (IGridRegistrationUrlModule module in m_modules.Values)
            {
                //Build the URL
                databaseSave[module.UrlName] = module.GetUrlForRegisteringClient(SessionID, RegionHandle);
            }
            foreach (KeyValuePair <string, OSD> module in databaseSave)
            {
                //Build the URL
                retVal[module.Key] = m_loadBalancer.GetHost() + ":" + m_modules[module.Key].Port + module.Value.AsString();
            }

            //Save into the database so that we can rebuild later if the server goes offline
            urls              = new GridRegistrationURLs();
            urls.URLS         = databaseSave;
            urls.RegionHandle = RegionHandle;
            urls.SessionID    = SessionID;
            urls.Expiration   = DateTime.Now.AddHours(m_timeBeforeTimeout);
            m_genericsConnector.AddGeneric(UUID.Zero, "GridRegistrationUrls", RegionHandle.ToString(), urls.ToOSD());

            return(retVal);
        }
Example #11
0
        public void RemoveUrlsForClient(string SessionID, ulong RegionHandle)
        {
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", RegionHandle.ToString(), new GridRegistrationURLs());

            if (urls != null)
            {
                //Remove all the handlers from the HTTP Server
                foreach (KeyValuePair <string, OSD> kvp in urls.URLS)
                {
                    if (!m_modules.ContainsKey(kvp.Key))
                    {
                        continue;
                    }
                    IGridRegistrationUrlModule module = m_modules[kvp.Key];
                    IHttpServer server = m_simulationBase.GetHttpServer(module.Port);
                    server.RemoveHTTPHandler("POST", kvp.Value);
                }
                //Remove from the database so that they don't pop up later
                m_genericsConnector.RemoveGeneric(UUID.Zero, "GridRegistrationUrls", RegionHandle.ToString());
            }
        }
 public override IDataTransferable Duplicate()
 {
     GridRegistrationURLs url = new GridRegistrationURLs();
     url.FromOSD(ToOSD());
     return url;
 }
        public OSDMap GetUrlForRegisteringClient(string SessionID, ulong RegionHandle)
        {
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric<GridRegistrationURLs>(UUID.Zero,
                "GridRegistrationUrls", RegionHandle.ToString(), new GridRegistrationURLs());
            OSDMap retVal = new OSDMap();
            if (urls != null)
            {
                urls.Expiration = DateTime.Now.AddHours(m_timeBeforeTimeout);
                urls.SessionID = SessionID;
                urls.RegionHandle = RegionHandle;
                foreach (KeyValuePair<string, OSD> module in urls.URLS)
                {
                    //Build the URL
                    retVal[module.Key] = m_loadBalancer.GetHost() + ":" + m_modules[module.Key].Port + module.Value.AsString();
                }
                return retVal;
            }
            OSDMap databaseSave = new OSDMap();
            //Get the URLs from all the modules that have registered with us
            foreach (IGridRegistrationUrlModule module in m_modules.Values)
            {
                //Build the URL
                databaseSave[module.UrlName] = module.GetUrlForRegisteringClient(SessionID, RegionHandle);
            }
            foreach (KeyValuePair<string, OSD> module in databaseSave)
            {
                //Build the URL
                retVal[module.Key] = m_loadBalancer.GetHost() + ":" + m_modules[module.Key].Port + module.Value.AsString();
            }

            //Save into the database so that we can rebuild later if the server goes offline
            urls = new GridRegistrationURLs();
            urls.URLS = databaseSave;
            urls.RegionHandle = RegionHandle;
            urls.SessionID = SessionID;
            urls.Expiration = DateTime.Now.AddHours(m_timeBeforeTimeout);
            m_genericsConnector.AddGeneric(UUID.Zero, "GridRegistrationUrls", RegionHandle.ToString(), urls.ToOSD());

            return retVal;
        }
        public OSDMap GetUrlForRegisteringClient(string SessionID)
        {
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric<GridRegistrationURLs>(UUID.Zero,
                "GridRegistrationUrls", SessionID.ToString (), new GridRegistrationURLs ());
            OSDMap retVal = new OSDMap();
            if (urls != null)
            {
                if(urls.HostNames == null || urls.Ports == null || urls.URLS == null || urls.SessionID != SessionID)
                {
                    RemoveUrlsForClient(SessionID.ToString());
                }
                else
                {
                    urls.Expiration = DateTime.Now.AddHours(m_timeBeforeTimeout);
                    urls.SessionID = SessionID;
                    foreach (KeyValuePair<string, OSD> module in urls.URLS)
                    {
                        //Build the URL
                        retVal[module.Key] = urls.HostNames[module.Key] + ":" + urls.Ports[module.Key] + module.Value.AsString ();
                    }
                    return retVal;
                }
            }
            OSDMap databaseSave = new OSDMap();
            OSDMap ports = new OSDMap();
            OSDMap hostnames = new OSDMap();
            //Get the URLs from all the modules that have registered with us
            foreach (IGridRegistrationUrlModule module in m_modules.Values)
            {
                ports[module.UrlName] = m_loadBalancer.GetPort (module.UrlName);
                hostnames[module.UrlName] = m_loadBalancer.GetHost (module.UrlName);
                //Build the URL
                databaseSave[module.UrlName] = module.GetUrlForRegisteringClient (SessionID, ports[module.UrlName]);
            }
            foreach (KeyValuePair<string, OSD> module in databaseSave)
            {
                //Build the URL
                retVal[module.Key] = hostnames[module.Key] + ":" + ports[module.Key] + module.Value.AsString ();
            }

            //Save into the database so that we can rebuild later if the server goes offline
            urls = new GridRegistrationURLs();
            urls.URLS = databaseSave;
            urls.SessionID = SessionID;
            urls.Ports = ports;
            urls.HostNames = hostnames;
            urls.Expiration = DateTime.Now.AddHours(m_timeBeforeTimeout);
            m_genericsConnector.AddGeneric (UUID.Zero, "GridRegistrationUrls", SessionID.ToString (), urls.ToOSD ());

            return retVal;
        }
        public OSDMap GetUrlForRegisteringClient(string SessionID)
        {
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", SessionID);
            OSDMap retVal = new OSDMap();

            if (urls != null)
            {
                if (urls.HostNames == null || urls.Ports == null ||
                    urls.URLS == null || urls.SessionID != SessionID ||
                    !CheckModuleNames(urls) || urls.VersionNumber < GridRegistrationURLs.CurrentVersionNumber)
                {
                    if (urls.VersionNumber == GridRegistrationURLs.CurrentVersionNumber)
                    {
                        if (CheckModuleNames(urls))
                        {
                            MainConsole.Instance.Warn("[GridRegService]: Null stuff in GetUrls, HostNames " + (urls.HostNames == null) + ", Ports " +
                                                      (urls.Ports == null) + ", URLS " + (urls.URLS == null) + ", SessionID 1 " + SessionID + ", SessionID 2 " + urls.SessionID +
                                                      ", checkModuleNames: " + CheckModuleNames(urls));
                        }
                    }
                    RemoveUrlsForClient(urls.SessionID);
                }
                else
                {
                    urls.Expiration = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout * 60);
                    urls.SessionID  = SessionID;
                    InnerUpdateUrlsForClient(urls);
                    foreach (KeyValuePair <string, OSD> module in urls.URLS)
                    {
                        //Build the URL
                        retVal[module.Key] = urls.HostNames[module.Key] + ":" + urls.Ports[module.Key] + module.Value.AsString();
                    }
                    return(retVal);
                }
            }
            OSDMap databaseSave = new OSDMap();
            OSDMap ports        = new OSDMap();
            OSDMap hostnames    = new OSDMap();

            //Get the URLs from all the modules that have registered with us
            foreach (IGridRegistrationUrlModule module in m_modules.Values)
            {
                uint   port;
                string hostName;
                string innerURL;

                m_loadBalancer.GetHost(module.UrlName, module, SessionID, out port, out hostName, out innerURL);

                ports[module.UrlName]        = port;
                hostnames[module.UrlName]    = hostName;
                databaseSave[module.UrlName] = innerURL;
            }
            foreach (KeyValuePair <string, OSD> module in databaseSave)
            {
                //Build the URL
                if (module.Value.AsString().StartsWith("http://") || module.Value.AsString().StartsWith("https://"))
                {
                    retVal[module.Key] = module.Value.AsString();
                }
                else
                {
                    retVal[module.Key] = hostnames[module.Key] + ":" + ports[module.Key] + module.Value.AsString();
                }
            }

            //Save into the database so that we can rebuild later if the server goes offline
            urls = new GridRegistrationURLs
            {
                URLS       = databaseSave,
                SessionID  = SessionID,
                Ports      = ports,
                HostNames  = hostnames,
                Expiration = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout * 60)
            };
            m_genericsConnector.AddGeneric(UUID.Zero, "GridRegistrationUrls", SessionID, urls.ToOSD());

            return(retVal);
        }
        public OSDMap RegionRemoteHandlerURL(GridRegion regionInfo, UUID sessionID, UUID oldSessionID)
        {
            List<GridRegistrationURLs> urls = m_genericsConnector.GetGenerics<GridRegistrationURLs>(
                regionInfo.RegionID, "RegionRegistrationUrls");
            m_genericsConnector.RemoveGeneric(regionInfo.RegionID, "RegionRegistrationUrls");
            if ((urls == null) || (urls.Count == 0) || (urls[0].Expiration < DateTime.UtcNow))
            {
                urls = new List<GridRegistrationURLs>();
                OSDMap hostnames = new OSDMap();
                OSDMap databaseSave = new OSDMap
                                          {
                                              {
                                                  "regionURI",
                                                  "http://" + regionInfo.ExternalEndPoint.Address + ":" + regionInfo.ExternalEndPoint.Port + "/region" + UUID.Random()
                                              }
                                          };
                OSDMap ports = new OSDMap { { "regionURI", regionInfo.InternalEndPoint.Port } };

                GridRegistrationURLs urls2 = new GridRegistrationURLs
                {
                    URLS = databaseSave,
                    SessionID = sessionID.ToString(),
                    Ports = ports,
                    HostNames = hostnames,
                    Expiration = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout * 60)
                };
                urls.Add(urls2);
            }
            m_genericsConnector.AddGeneric(regionInfo.RegionID, "RegionRegistrationUrls", sessionID.ToString(), urls[0].ToOSD());
            return urls[0].ToOSD();
        }
        public OSDMap GetUrlForRegisteringClient(string SessionID)
        {
            if (!m_useRegistrationService)
            {
                return(null);
            }
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric <GridRegistrationURLs>(UUID.Zero,
                                                                                              "GridRegistrationUrls", SessionID);
            OSDMap retVal = new OSDMap();

            if (urls != null)
            {
                if (urls.HostNames == null || urls.Ports == null ||
                    urls.URLS == null || urls.SessionID != SessionID ||
                    MainServer.Instance.HostName != urls._ParentHostName ||
                    !CheckModuleNames(urls) || urls.VersionNumber < GridRegistrationURLs.CurrentVersionNumber)
                {
                    if (urls.VersionNumber == GridRegistrationURLs.CurrentVersionNumber)
                    {
                        if (CheckModuleNames(urls))
                        {
                            MainConsole.Instance.Warn("[GridRegService]: Null stuff in GetUrls, HostNames " + (urls.HostNames == null) + ", Ports " +
                                                      (urls.Ports == null) + ", URLS " + (urls.URLS == null) + ", SessionID 1 " + SessionID + ", SessionID 2 " + urls.SessionID +
                                                      ", checkModuleNames: " + CheckModuleNames(urls));
                        }
                    }
                    RemoveUrlsForClient(urls.SessionID);
                }
                else
                {
                    urls.Expiration = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout * 60);
                    urls.SessionID  = SessionID;
                    InnerUpdateUrlsForClient(urls);
                    foreach (KeyValuePair <string, OSD> module in urls.URLS)
                    {
                        if (module.Value.Type == OSDType.Array)
                        {
                            OSDArray array = new OSDArray();
                            int      i     = 0;
                            foreach (OSD o in (OSDArray)module.Value)
                            {
                                //Build the URL
                                if (o.AsString().StartsWith("http://") || o.AsString().StartsWith("https://"))
                                {
                                    array.Add(o.AsString());
                                }
                                else
                                {
                                    array.Add(((OSDArray)urls.HostNames[module.Key])[i] + ":" +
                                              (int)((OSDArray)urls.Ports[module.Key])[i].AsUInteger() +
                                              o.AsString());
                                }
                                i++;
                            }
                            retVal[module.Key] = array;
                        }
                        else
                        {
                            //Build the URL
                            if (module.Value.AsString().StartsWith("http://") || module.Value.AsString().StartsWith("https://"))
                            {
                                retVal[module.Key] = module.Value.AsString();
                            }
                            else
                            {
                                retVal[module.Key] = urls.HostNames[module.Key] + ":" + urls.Ports[module.Key] + module.Value.AsString();
                            }
                        }
                    }
                    return(retVal);
                }
            }
            OSDMap databaseSave = new OSDMap();
            OSDMap ports        = new OSDMap();
            OSDMap hostnames    = new OSDMap();

            //Get the URLs from all the modules that have registered with us
            foreach (IGridRegistrationUrlModule module in m_modules.Values)
            {
                List <uint>   port;
                List <string> hostName;
                List <string> innerURL;

                m_loadBalancer.GetHost(module.UrlName, module, SessionID, out port, out hostName, out innerURL);
                ports[module.UrlName]        = port.Count == 1 ? (OSD)port[0] : (OSD)port.ToOSDArray();
                hostnames[module.UrlName]    = hostName.Count == 1 ? (OSD)hostName[0] : (OSD)hostName.ToOSDArray();
                databaseSave[module.UrlName] = innerURL.Count == 1 ? (OSD)innerURL[0] : (OSD)innerURL.ToOSDArray();
            }
            foreach (KeyValuePair <string, OSD> module in databaseSave)
            {
                if (module.Value.Type == OSDType.Array)
                {
                    OSDArray array = new OSDArray();
                    int      i     = 0;
                    foreach (OSD o in (OSDArray)module.Value)
                    {
                        //Build the URL
                        if (o.AsString().StartsWith("http://") || o.AsString().StartsWith("https://"))
                        {
                            array.Add(o.AsString());
                        }
                        else
                        {
                            array.Add(((OSDArray)hostnames[module.Key])[i] + ":" +
                                      (int)((OSDArray)ports[module.Key])[i].AsUInteger() +
                                      o.AsString());
                        }
                        i++;
                    }
                    retVal[module.Key] = array;
                }
                else
                {
                    //Build the URL
                    if (module.Value.AsString().StartsWith("http://") || module.Value.AsString().StartsWith("https://"))
                    {
                        retVal[module.Key] = module.Value.AsString();
                    }
                    else
                    {
                        retVal[module.Key] = hostnames[module.Key] + ":" + ports[module.Key] + module.Value.AsString();
                    }
                }
            }

            //Save into the database so that we can rebuild later if the server goes offline
            urls = new GridRegistrationURLs
            {
                URLS            = databaseSave,
                SessionID       = SessionID,
                Ports           = ports,
                HostNames       = hostnames,
                Expiration      = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout * 60),
                _ParentHostName = MainServer.Instance.HostName
            };
            m_genericsConnector.AddGeneric(UUID.Zero, "GridRegistrationUrls", SessionID, urls.ToOSD());

            return(retVal);
        }
        private bool CheckModuleNames (GridRegistrationURLs urls)
        {
#if (!ISWIN)
            foreach (string urlName in m_modules.Keys)
            {
#if (!ISWIN)
                bool found = false;
                foreach (string o in urls.URLS.Keys)
                {
                    if (o == urlName)
                    {
                        found = true;
                        break;
                    }
                }
#else
                bool found = urls.URLS.Keys.Any(o => o == urlName);
#endif
                if (!found) return false;
            }
            return true;
#else
            return m_modules.Keys.Select(urlName => urls.URLS.Keys.Any(o => o == urlName)).All(found => found);
#endif
        }
        public OSDMap GetUrlForRegisteringClient(string SessionID)
        {
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric<GridRegistrationURLs>(UUID.Zero,
                "GridRegistrationUrls", SessionID);
            OSDMap retVal = new OSDMap();
            if (urls != null)
            {
                if(urls.HostNames == null || urls.Ports == null ||
                    urls.URLS == null || urls.SessionID != SessionID ||
                    !CheckModuleNames(urls) || urls.VersionNumber < GridRegistrationURLs.CurrentVersionNumber)
                {
                    if (urls.VersionNumber == GridRegistrationURLs.CurrentVersionNumber)
                    {
                        if (CheckModuleNames(urls))
                        {
                            MainConsole.Instance.Warn("[GridRegService]: Null stuff in GetUrls, HostNames " + (urls.HostNames == null) + ", Ports " +
                                (urls.Ports == null) + ", URLS " + (urls.URLS == null) + ", SessionID 1 " + SessionID + ", SessionID 2 " + urls.SessionID +
                                ", checkModuleNames: " + CheckModuleNames(urls));
                        }
                    }
                    RemoveUrlsForClient(urls.SessionID);
                }
                else
                {
                    urls.Expiration = DateTime.UtcNow.AddMinutes (m_timeBeforeTimeout * 60);
                    urls.SessionID = SessionID;
                    InnerUpdateUrlsForClient (urls);
                    foreach (KeyValuePair<string, OSD> module in urls.URLS)
                    {
                        //Build the URL
                        retVal[module.Key] = urls.HostNames[module.Key] + ":" + urls.Ports[module.Key] + module.Value.AsString ();
                    }
                    return retVal;
                }
            }
            OSDMap databaseSave = new OSDMap();
            OSDMap ports = new OSDMap();
            OSDMap hostnames = new OSDMap();
            //Get the URLs from all the modules that have registered with us
            foreach (IGridRegistrationUrlModule module in m_modules.Values)
            {
                uint port;
                string hostName;
                string innerURL;

                m_loadBalancer.GetHost (module.UrlName, module, SessionID, out port, out hostName, out innerURL);
                
                ports[module.UrlName] = port;
                hostnames[module.UrlName] = hostName;
                databaseSave[module.UrlName] = innerURL;
            }
            foreach (KeyValuePair<string, OSD> module in databaseSave)
            {
                //Build the URL
                if (module.Value.AsString().StartsWith("http://") || module.Value.AsString().StartsWith("https://"))
                    retVal[module.Key] = module.Value.AsString();
                else
                    retVal[module.Key] = hostnames[module.Key] + ":" + ports[module.Key] + module.Value.AsString ();
            }

            //Save into the database so that we can rebuild later if the server goes offline
            urls = new GridRegistrationURLs
                       {
                           URLS = databaseSave,
                           SessionID = SessionID,
                           Ports = ports,
                           HostNames = hostnames,
                           Expiration = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout*60)
                       };
            m_genericsConnector.AddGeneric (UUID.Zero, "GridRegistrationUrls", SessionID, urls.ToOSD ());

            return retVal;
        }
 private void InnerUpdateUrlsForClient(GridRegistrationURLs urls)
 {
     if (urls != null)
     {
         urls.Expiration = DateTime.Now.AddHours(m_timeBeforeTimeout);
         m_genericsConnector.AddGeneric (UUID.Zero, "GridRegistrationUrls", urls.SessionID.ToString (), urls.ToOSD ());
         m_log.WarnFormat ("[GridRegistrationService]: Updated URLs for {0}", urls.SessionID);
     }
     else
         m_log.ErrorFormat ("[GridRegistrationService]: Failed to find URLs to update for {0}", urls.SessionID);
 }
        public OSDMap GetUrlForRegisteringClient(string SessionID)
        {
            if (!m_useRegistrationService)
                return null;
            GridRegistrationURLs urls = m_genericsConnector.GetGeneric<GridRegistrationURLs>(UUID.Zero,
                "GridRegistrationUrls", SessionID);
            OSDMap retVal = new OSDMap();
            if (urls != null)
            {
                if(urls.HostNames == null || urls.Ports == null ||
                    urls.URLS == null || urls.SessionID != SessionID ||
                    MainServer.Instance.HostName != urls._ParentHostName ||
                    !CheckModuleNames(urls) || urls.VersionNumber < GridRegistrationURLs.CurrentVersionNumber)
                {
                    if (urls.VersionNumber == GridRegistrationURLs.CurrentVersionNumber)
                    {
                        if (CheckModuleNames(urls))
                        {
                            MainConsole.Instance.Warn("[GridRegService]: Null stuff in GetUrls, HostNames " + (urls.HostNames == null) + ", Ports " +
                                (urls.Ports == null) + ", URLS " + (urls.URLS == null) + ", SessionID 1 " + SessionID + ", SessionID 2 " + urls.SessionID +
                                ", checkModuleNames: " + CheckModuleNames(urls));
                        }
                    }
                    RemoveUrlsForClient(urls.SessionID);
                }
                else
                {
                    urls.Expiration = DateTime.UtcNow.AddMinutes (m_timeBeforeTimeout * 60);
                    urls.SessionID = SessionID;
                    InnerUpdateUrlsForClient (urls);
                    foreach (KeyValuePair<string, OSD> module in urls.URLS)
                    {
                        if (module.Value.Type == OSDType.Array)
                        {
                            OSDArray array = new OSDArray();
                            int i = 0;
                            foreach (OSD o in (OSDArray)module.Value)
                            {
                                //Build the URL
                                if (o.AsString().StartsWith("http://") || o.AsString().StartsWith("https://"))
                                    array.Add(o.AsString());
                                else
                                    array.Add(((OSDArray)urls.HostNames[module.Key])[i] + ":" +
                                        (int)((OSDArray)urls.Ports[module.Key])[i].AsUInteger() +
                                        o.AsString());
                                i++;
                            }
                            retVal[module.Key] = array;
                        }
                        else
                        {
                            //Build the URL
                            if (module.Value.AsString().StartsWith("http://") || module.Value.AsString().StartsWith("https://"))
                                retVal[module.Key] = module.Value.AsString();
                            else
                                retVal[module.Key] = urls.HostNames[module.Key] + ":" + urls.Ports[module.Key] + module.Value.AsString();
                        }
                    }
                    return retVal;
                }
            }
            OSDMap databaseSave = new OSDMap();
            OSDMap ports = new OSDMap();
            OSDMap hostnames = new OSDMap();
            //Get the URLs from all the modules that have registered with us
            foreach (IGridRegistrationUrlModule module in m_modules.Values)
            {
                List<uint> port;
                List<string> hostName;
                List<string> innerURL;

                m_loadBalancer.GetHost (module.UrlName, module, SessionID, out port, out hostName, out innerURL);
                ports[module.UrlName] = port.Count == 1 ? (OSD)port[0] : (OSD)port.ToOSDArray();
                hostnames[module.UrlName] = hostName.Count == 1 ? (OSD)hostName[0] : (OSD)hostName.ToOSDArray();
                databaseSave[module.UrlName] = innerURL.Count == 1 ? (OSD)innerURL[0] : (OSD)innerURL.ToOSDArray();
            }
            foreach (KeyValuePair<string, OSD> module in databaseSave)
            {
                if (module.Value.Type == OSDType.Array)
                {
                    OSDArray array = new OSDArray();
                    int i = 0;
                    foreach (OSD o in (OSDArray)module.Value)
                    {
                        //Build the URL
                        if (o.AsString().StartsWith("http://") || o.AsString().StartsWith("https://"))
                            array.Add(o.AsString());
                        else
                            array.Add(((OSDArray)hostnames[module.Key])[i] + ":" +
                                (int)((OSDArray)ports[module.Key])[i].AsUInteger() +
                                o.AsString());
                        i++;
                    }
                    retVal[module.Key] = array;
                }
                else
                {
                    //Build the URL
                    if (module.Value.AsString().StartsWith("http://") || module.Value.AsString().StartsWith("https://"))
                        retVal[module.Key] = module.Value.AsString();
                    else
                        retVal[module.Key] = hostnames[module.Key] + ":" + ports[module.Key] + module.Value.AsString();
                }
            }

            //Save into the database so that we can rebuild later if the server goes offline
            urls = new GridRegistrationURLs
                       {
                           URLS = databaseSave,
                           SessionID = SessionID,
                           Ports = ports,
                           HostNames = hostnames,
                           Expiration = DateTime.UtcNow.AddMinutes(m_timeBeforeTimeout*60),
                           _ParentHostName = MainServer.Instance.HostName
                       };
            m_genericsConnector.AddGeneric (UUID.Zero, "GridRegistrationUrls", SessionID, urls.ToOSD ());

            return retVal;
        }
 private bool CheckModuleNames (GridRegistrationURLs urls)
 {
     foreach (string urlName in m_modules.Keys)
     {
         bool found = false;
         foreach (string o in urls.URLS.Keys)
         {
             if (o == urlName)
             {
                 found = true;
                 break;
             }
         }
         if (!found)
             return false;
     }
     return true;
 }
 private void InnerUpdateUrlsForClient(GridRegistrationURLs urls)
 {
     if (urls != null)
     {
         urls.Expiration = DateTime.UtcNow.AddMinutes (m_timeBeforeTimeout * 60);
         m_genericsConnector.AddGeneric(UUID.Zero, "GridRegistrationUrls", urls.SessionID, urls.ToOSD());
         MainConsole.Instance.DebugFormat ("[GridRegistrationService]: Updated URLs for {0}", urls.SessionID);
     }
     else
         MainConsole.Instance.ErrorFormat ("[GridRegistrationService]: Failed to find URLs to update for {0}", "unknown");
 }
 private void InnerUpdateUrlsForClient(GridRegistrationURLs urls)
 {
     if (urls != null)
     {
         urls.Expiration = DateTime.UtcNow.AddMinutes (m_timeBeforeTimeout * 60);
         //Remove it first just to make sure it is replaced
         m_genericsConnector.RemoveGeneric (UUID.Zero, "GridRegistrationUrls", urls.SessionID.ToString ());
         m_genericsConnector.AddGeneric (UUID.Zero, "GridRegistrationUrls", urls.SessionID.ToString (), urls.ToOSD ());
         m_log.WarnFormat ("[GridRegistrationService]: Updated URLs for {0}", urls.SessionID);
     }
     else
         m_log.ErrorFormat ("[GridRegistrationService]: Failed to find URLs to update for {0}", urls.SessionID);
 }