123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Reflection;
- using OpenSim.Framework;
- using OpenSim.Services.Connectors.Friends;
- using OpenSim.Services.Connectors.Hypergrid;
- using OpenSim.Services.Interfaces;
- using OpenSim.Services.Connectors.InstantMessage;
- using GridRegion = OpenSim.Services.Interfaces.GridRegion;
- using OpenSim.Server.Base;
- using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
- using OpenMetaverse;
- using log4net;
- using Nini.Config;
- namespace OpenSim.Services.HypergridService
- {
-
-
-
- public class HGInstantMessageService : IInstantMessage
- {
- private static readonly ILog m_log =
- LogManager.GetLogger(
- MethodBase.GetCurrentMethod().DeclaringType);
- private const double CACHE_EXPIRATION_SECONDS = 120000.0;
- static bool m_Initialized = false;
- protected static IGridService m_GridService;
- protected static IPresenceService m_PresenceService;
- protected static IUserAgentService m_UserAgentService;
- protected static IInstantMessageSimConnector m_IMSimConnector;
- protected static Dictionary<UUID, object> m_UserLocationMap = new Dictionary<UUID, object>();
- private static ExpiringCache<UUID, GridRegion> m_RegionCache;
- private static string m_RestURL;
- private static bool m_ForwardOfflineGroupMessages;
- private static bool m_InGatekeeper;
- public HGInstantMessageService(IConfigSource config)
- : this(config, null)
- {
- }
- public HGInstantMessageService(IConfigSource config, IInstantMessageSimConnector imConnector)
- {
- if (imConnector != null)
- m_IMSimConnector = imConnector;
- if (!m_Initialized)
- {
- m_Initialized = true;
- IConfig serverConfig = config.Configs["HGInstantMessageService"];
- if (serverConfig == null)
- throw new Exception(String.Format("No section HGInstantMessageService in config file"));
- string gridService = serverConfig.GetString("GridService", String.Empty);
- string presenceService = serverConfig.GetString("PresenceService", String.Empty);
- string userAgentService = serverConfig.GetString("UserAgentService", String.Empty);
- m_InGatekeeper = serverConfig.GetBoolean("InGatekeeper", false);
- m_log.DebugFormat("[HG IM SERVICE]: Starting... InRobust? {0}", m_InGatekeeper);
- if (gridService == string.Empty || presenceService == string.Empty)
- throw new Exception(String.Format("Incomplete specifications, InstantMessage Service cannot function."));
- Object[] args = new Object[] { config };
- m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
- m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args);
- m_UserAgentService = ServerUtils.LoadPlugin<IUserAgentService>(userAgentService, args);
- m_RegionCache = new ExpiringCache<UUID, GridRegion>();
- IConfig cnf = config.Configs["Messaging"];
- if (cnf == null)
- {
- return;
- }
- m_RestURL = cnf.GetString("OfflineMessageURL", string.Empty);
- m_ForwardOfflineGroupMessages = cnf.GetBoolean("ForwardOfflineGroupMessages", false);
- }
- }
- public bool IncomingInstantMessage(GridInstantMessage im)
- {
- bool success = false;
- if (m_IMSimConnector != null)
- {
-
- success = m_IMSimConnector.SendInstantMessage(im);
- }
- else
- {
- success = TrySendInstantMessage(im, "", true, false);
- }
- if (!success && m_InGatekeeper)
- UndeliveredMessage(im);
- return success;
- }
- public bool OutgoingInstantMessage(GridInstantMessage im, string url, bool foreigner)
- {
- if (url != string.Empty)
- return TrySendInstantMessage(im, url, true, foreigner);
- else
- {
- PresenceInfo upd = new PresenceInfo();
- upd.RegionID = UUID.Zero;
- return TrySendInstantMessage(im, upd, true, foreigner);
- }
- }
- protected bool TrySendInstantMessage(GridInstantMessage im, object previousLocation, bool firstTime, bool foreigner)
- {
- UUID toAgentID = new UUID(im.toAgentID);
- PresenceInfo upd = null;
- string url = string.Empty;
- bool lookupAgent = false;
- lock (m_UserLocationMap)
- {
- if (m_UserLocationMap.ContainsKey(toAgentID))
- {
- object o = m_UserLocationMap[toAgentID];
- if (o is PresenceInfo)
- upd = (PresenceInfo)o;
- else if (o is string)
- url = (string)o;
-
-
- if (!firstTime)
- {
- lookupAgent = true;
- upd = null;
- }
- }
- else
- {
- lookupAgent = true;
- }
- }
-
-
- if (lookupAgent)
- {
-
- PresenceInfo[] presences = m_PresenceService.GetAgents(new string[] { toAgentID.ToString() });
- if (presences != null && presences.Length > 0)
- {
- foreach (PresenceInfo p in presences)
- {
- if (p.RegionID != UUID.Zero)
- {
-
- upd = p;
- break;
- }
- }
- }
- if (upd == null && !foreigner)
- {
-
- m_log.DebugFormat("[HG IM SERVICE]: User is not present. Checking location with User Agent service");
- url = m_UserAgentService.LocateUser(toAgentID);
- }
-
-
-
- if (!firstTime && ((previousLocation is PresenceInfo && upd != null && upd.RegionID == ((PresenceInfo)previousLocation).RegionID) ||
- (previousLocation is string && upd == null && previousLocation.Equals(url))))
- {
-
- m_log.DebugFormat("[HG IM SERVICE]: Fail 2 {0} {1}", previousLocation, url);
- return false;
- }
- }
- if (upd != null)
- {
-
-
-
- return SendIMToRegion(upd, im, toAgentID, foreigner);
- }
- else if (url != string.Empty)
- {
-
-
-
- return ForwardIMToGrid(url, im, toAgentID, foreigner);
- }
- else if (firstTime && previousLocation is string && (string)previousLocation != string.Empty)
- {
- return ForwardIMToGrid((string)previousLocation, im, toAgentID, foreigner);
- }
- else
- m_log.DebugFormat("[HG IM SERVICE]: Unable to locate user {0}", toAgentID);
- return false;
- }
- bool SendIMToRegion(PresenceInfo upd, GridInstantMessage im, UUID toAgentID, bool foreigner)
- {
- bool imresult = false;
- GridRegion reginfo = null;
- if (!m_RegionCache.TryGetValue(upd.RegionID, out reginfo))
- {
- reginfo = m_GridService.GetRegionByUUID(UUID.Zero , upd.RegionID);
- if (reginfo != null)
- m_RegionCache.AddOrUpdate(upd.RegionID, reginfo, CACHE_EXPIRATION_SECONDS);
- }
- if (reginfo != null)
- {
- imresult = InstantMessageServiceConnector.SendInstantMessage(reginfo.ServerURI, im);
- }
- else
- {
- m_log.DebugFormat("[HG IM SERVICE]: Failed to deliver message to {0}", reginfo.ServerURI);
- return false;
- }
- if (imresult)
- {
-
- lock (m_UserLocationMap)
- {
- if (m_UserLocationMap.ContainsKey(toAgentID))
- {
- m_UserLocationMap[toAgentID] = upd;
- }
- else
- {
- m_UserLocationMap.Add(toAgentID, upd);
- }
- }
- return true;
- }
- else
- {
-
-
-
-
-
-
- return TrySendInstantMessage(im, upd, false, foreigner);
- }
- }
- bool ForwardIMToGrid(string url, GridInstantMessage im, UUID toAgentID, bool foreigner)
- {
- if (InstantMessageServiceConnector.SendInstantMessage(url, im))
- {
-
- lock (m_UserLocationMap)
- {
- if (m_UserLocationMap.ContainsKey(toAgentID))
- {
- m_UserLocationMap[toAgentID] = url;
- }
- else
- {
- m_UserLocationMap.Add(toAgentID, url);
- }
- }
- return true;
- }
- else
- {
-
-
- return TrySendInstantMessage(im, url, false, foreigner);
- }
- }
- private bool UndeliveredMessage(GridInstantMessage im)
- {
- if (m_RestURL != string.Empty && (im.offline != 0)
- && (!im.fromGroup || (im.fromGroup && m_ForwardOfflineGroupMessages)))
- {
- return SynchronousRestObjectRequester.MakeRequest<GridInstantMessage, bool>(
- "POST", m_RestURL + "/SaveMessage/", im);
- }
- else
- {
- return false;
- }
- }
- }
- }
|