diff options
Diffstat (limited to 'OpenSim/Services/HypergridService/HGInstantMessageService.cs')
-rw-r--r-- | OpenSim/Services/HypergridService/HGInstantMessageService.cs | 349 |
1 files changed, 349 insertions, 0 deletions
diff --git a/OpenSim/Services/HypergridService/HGInstantMessageService.cs b/OpenSim/Services/HypergridService/HGInstantMessageService.cs new file mode 100644 index 0000000..ded589d --- /dev/null +++ b/OpenSim/Services/HypergridService/HGInstantMessageService.cs | |||
@@ -0,0 +1,349 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Net; | ||
31 | using System.Reflection; | ||
32 | |||
33 | using OpenSim.Framework; | ||
34 | using OpenSim.Services.Connectors.Friends; | ||
35 | using OpenSim.Services.Connectors.Hypergrid; | ||
36 | using OpenSim.Services.Interfaces; | ||
37 | using OpenSim.Services.Connectors.InstantMessage; | ||
38 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
39 | using OpenSim.Server.Base; | ||
40 | using FriendInfo = OpenSim.Services.Interfaces.FriendInfo; | ||
41 | |||
42 | using OpenMetaverse; | ||
43 | using log4net; | ||
44 | using Nini.Config; | ||
45 | |||
46 | namespace OpenSim.Services.HypergridService | ||
47 | { | ||
48 | /// <summary> | ||
49 | /// Inter-grid IM | ||
50 | /// </summary> | ||
51 | public class HGInstantMessageService : IInstantMessage | ||
52 | { | ||
53 | private static readonly ILog m_log = | ||
54 | LogManager.GetLogger( | ||
55 | MethodBase.GetCurrentMethod().DeclaringType); | ||
56 | |||
57 | private const double CACHE_EXPIRATION_SECONDS = 120000.0; // 33 hours | ||
58 | |||
59 | static bool m_Initialized = false; | ||
60 | |||
61 | protected static IGridService m_GridService; | ||
62 | protected static IPresenceService m_PresenceService; | ||
63 | protected static IUserAgentService m_UserAgentService; | ||
64 | |||
65 | protected static IInstantMessageSimConnector m_IMSimConnector; | ||
66 | |||
67 | protected static Dictionary<UUID, object> m_UserLocationMap = new Dictionary<UUID, object>(); | ||
68 | private static ExpiringCache<UUID, GridRegion> m_RegionCache; | ||
69 | |||
70 | private static string m_RestURL; | ||
71 | private static bool m_ForwardOfflineGroupMessages; | ||
72 | private static bool m_InGatekeeper; | ||
73 | |||
74 | public HGInstantMessageService(IConfigSource config) | ||
75 | : this(config, null) | ||
76 | { | ||
77 | } | ||
78 | |||
79 | public HGInstantMessageService(IConfigSource config, IInstantMessageSimConnector imConnector) | ||
80 | { | ||
81 | if (imConnector != null) | ||
82 | m_IMSimConnector = imConnector; | ||
83 | |||
84 | if (!m_Initialized) | ||
85 | { | ||
86 | m_Initialized = true; | ||
87 | |||
88 | IConfig serverConfig = config.Configs["HGInstantMessageService"]; | ||
89 | if (serverConfig == null) | ||
90 | throw new Exception(String.Format("No section HGInstantMessageService in config file")); | ||
91 | |||
92 | string gridService = serverConfig.GetString("GridService", String.Empty); | ||
93 | string presenceService = serverConfig.GetString("PresenceService", String.Empty); | ||
94 | string userAgentService = serverConfig.GetString("UserAgentService", String.Empty); | ||
95 | m_InGatekeeper = serverConfig.GetBoolean("InGatekeeper", false); | ||
96 | m_log.DebugFormat("[HG IM SERVICE]: Starting... InRobust? {0}", m_InGatekeeper); | ||
97 | |||
98 | |||
99 | if (gridService == string.Empty || presenceService == string.Empty) | ||
100 | throw new Exception(String.Format("Incomplete specifications, InstantMessage Service cannot function.")); | ||
101 | |||
102 | Object[] args = new Object[] { config }; | ||
103 | m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args); | ||
104 | m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args); | ||
105 | m_UserAgentService = ServerUtils.LoadPlugin<IUserAgentService>(userAgentService, args); | ||
106 | |||
107 | m_RegionCache = new ExpiringCache<UUID, GridRegion>(); | ||
108 | |||
109 | IConfig cnf = config.Configs["Messaging"]; | ||
110 | if (cnf == null) | ||
111 | { | ||
112 | return; | ||
113 | } | ||
114 | |||
115 | m_RestURL = cnf.GetString("OfflineMessageURL", string.Empty); | ||
116 | m_ForwardOfflineGroupMessages = cnf.GetBoolean("ForwardOfflineGroupMessages", false); | ||
117 | |||
118 | } | ||
119 | } | ||
120 | |||
121 | public bool IncomingInstantMessage(GridInstantMessage im) | ||
122 | { | ||
123 | m_log.DebugFormat("[HG IM SERVICE]: Received message from {0} to {1}", im.fromAgentID, im.toAgentID); | ||
124 | UUID toAgentID = new UUID(im.toAgentID); | ||
125 | |||
126 | bool success = false; | ||
127 | if (m_IMSimConnector != null) | ||
128 | { | ||
129 | //m_log.DebugFormat("[XXX] SendIMToRegion local im connector"); | ||
130 | success = m_IMSimConnector.SendInstantMessage(im); | ||
131 | } | ||
132 | else | ||
133 | { | ||
134 | success = TrySendInstantMessage(im, "", true, false); | ||
135 | } | ||
136 | |||
137 | if (!success && m_InGatekeeper) // we do this only in the Gatekeeper IM service | ||
138 | UndeliveredMessage(im); | ||
139 | |||
140 | return success; | ||
141 | } | ||
142 | |||
143 | public bool OutgoingInstantMessage(GridInstantMessage im, string url, bool foreigner) | ||
144 | { | ||
145 | m_log.DebugFormat("[HG IM SERVICE]: Sending message from {0} to {1}@{2}", im.fromAgentID, im.toAgentID, url); | ||
146 | if (url != string.Empty) | ||
147 | return TrySendInstantMessage(im, url, true, foreigner); | ||
148 | else | ||
149 | { | ||
150 | PresenceInfo upd = new PresenceInfo(); | ||
151 | upd.RegionID = UUID.Zero; | ||
152 | return TrySendInstantMessage(im, upd, true, foreigner); | ||
153 | } | ||
154 | |||
155 | } | ||
156 | |||
157 | protected bool TrySendInstantMessage(GridInstantMessage im, object previousLocation, bool firstTime, bool foreigner) | ||
158 | { | ||
159 | UUID toAgentID = new UUID(im.toAgentID); | ||
160 | |||
161 | PresenceInfo upd = null; | ||
162 | string url = string.Empty; | ||
163 | |||
164 | bool lookupAgent = false; | ||
165 | |||
166 | lock (m_UserLocationMap) | ||
167 | { | ||
168 | if (m_UserLocationMap.ContainsKey(toAgentID)) | ||
169 | { | ||
170 | object o = m_UserLocationMap[toAgentID]; | ||
171 | if (o is PresenceInfo) | ||
172 | upd = (PresenceInfo)o; | ||
173 | else if (o is string) | ||
174 | url = (string)o; | ||
175 | |||
176 | // We need to compare the current location with the previous | ||
177 | // or the recursive loop will never end because it will never try to lookup the agent again | ||
178 | if (!firstTime) | ||
179 | { | ||
180 | lookupAgent = true; | ||
181 | upd = null; | ||
182 | } | ||
183 | } | ||
184 | else | ||
185 | { | ||
186 | lookupAgent = true; | ||
187 | } | ||
188 | } | ||
189 | |||
190 | //m_log.DebugFormat("[XXX] Neeed lookup ? {0}", (lookupAgent ? "yes" : "no")); | ||
191 | |||
192 | // Are we needing to look-up an agent? | ||
193 | if (lookupAgent) | ||
194 | { | ||
195 | // Non-cached user agent lookup. | ||
196 | PresenceInfo[] presences = m_PresenceService.GetAgents(new string[] { toAgentID.ToString() }); | ||
197 | if (presences != null && presences.Length > 0) | ||
198 | { | ||
199 | foreach (PresenceInfo p in presences) | ||
200 | { | ||
201 | if (p.RegionID != UUID.Zero) | ||
202 | { | ||
203 | //m_log.DebugFormat("[XXX]: Found presence in {0}", p.RegionID); | ||
204 | upd = p; | ||
205 | break; | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | |||
210 | if (upd == null && !foreigner) | ||
211 | { | ||
212 | // Let's check with the UAS if the user is elsewhere | ||
213 | m_log.DebugFormat("[HG IM SERVICE]: User is not present. Checking location with User Agent service"); | ||
214 | url = m_UserAgentService.LocateUser(toAgentID); | ||
215 | } | ||
216 | |||
217 | // check if we've tried this before.. | ||
218 | // This is one way to end the recursive loop | ||
219 | // | ||
220 | if (!firstTime && ((previousLocation is PresenceInfo && upd != null && upd.RegionID == ((PresenceInfo)previousLocation).RegionID) || | ||
221 | (previousLocation is string && upd == null && previousLocation.Equals(url)))) | ||
222 | { | ||
223 | // m_log.Error("[GRID INSTANT MESSAGE]: Unable to deliver an instant message"); | ||
224 | m_log.DebugFormat("[HG IM SERVICE]: Fail 2 {0} {1}", previousLocation, url); | ||
225 | |||
226 | return false; | ||
227 | } | ||
228 | } | ||
229 | |||
230 | if (upd != null) | ||
231 | { | ||
232 | // ok, the user is around somewhere. Let's send back the reply with "success" | ||
233 | // even though the IM may still fail. Just don't keep the caller waiting for | ||
234 | // the entire time we're trying to deliver the IM | ||
235 | return SendIMToRegion(upd, im, toAgentID, foreigner); | ||
236 | } | ||
237 | else if (url != string.Empty) | ||
238 | { | ||
239 | // ok, the user is around somewhere. Let's send back the reply with "success" | ||
240 | // even though the IM may still fail. Just don't keep the caller waiting for | ||
241 | // the entire time we're trying to deliver the IM | ||
242 | return ForwardIMToGrid(url, im, toAgentID, foreigner); | ||
243 | } | ||
244 | else if (firstTime && previousLocation is string && (string)previousLocation != string.Empty) | ||
245 | { | ||
246 | return ForwardIMToGrid((string)previousLocation, im, toAgentID, foreigner); | ||
247 | } | ||
248 | else | ||
249 | m_log.DebugFormat("[HG IM SERVICE]: Unable to locate user {0}", toAgentID); | ||
250 | return false; | ||
251 | } | ||
252 | |||
253 | bool SendIMToRegion(PresenceInfo upd, GridInstantMessage im, UUID toAgentID, bool foreigner) | ||
254 | { | ||
255 | bool imresult = false; | ||
256 | GridRegion reginfo = null; | ||
257 | if (!m_RegionCache.TryGetValue(upd.RegionID, out reginfo)) | ||
258 | { | ||
259 | reginfo = m_GridService.GetRegionByUUID(UUID.Zero /*!!!*/, upd.RegionID); | ||
260 | if (reginfo != null) | ||
261 | m_RegionCache.AddOrUpdate(upd.RegionID, reginfo, CACHE_EXPIRATION_SECONDS); | ||
262 | } | ||
263 | |||
264 | if (reginfo != null) | ||
265 | { | ||
266 | imresult = InstantMessageServiceConnector.SendInstantMessage(reginfo.ServerURI, im); | ||
267 | } | ||
268 | else | ||
269 | { | ||
270 | m_log.DebugFormat("[HG IM SERVICE]: Failed to deliver message to {0}", reginfo.ServerURI); | ||
271 | return false; | ||
272 | } | ||
273 | |||
274 | if (imresult) | ||
275 | { | ||
276 | // IM delivery successful, so store the Agent's location in our local cache. | ||
277 | lock (m_UserLocationMap) | ||
278 | { | ||
279 | if (m_UserLocationMap.ContainsKey(toAgentID)) | ||
280 | { | ||
281 | m_UserLocationMap[toAgentID] = upd; | ||
282 | } | ||
283 | else | ||
284 | { | ||
285 | m_UserLocationMap.Add(toAgentID, upd); | ||
286 | } | ||
287 | } | ||
288 | return true; | ||
289 | } | ||
290 | else | ||
291 | { | ||
292 | // try again, but lookup user this time. | ||
293 | // Warning, this must call the Async version | ||
294 | // of this method or we'll be making thousands of threads | ||
295 | // The version within the spawned thread is SendGridInstantMessageViaXMLRPCAsync | ||
296 | // The version that spawns the thread is SendGridInstantMessageViaXMLRPC | ||
297 | |||
298 | // This is recursive!!!!! | ||
299 | return TrySendInstantMessage(im, upd, false, foreigner); | ||
300 | } | ||
301 | } | ||
302 | |||
303 | bool ForwardIMToGrid(string url, GridInstantMessage im, UUID toAgentID, bool foreigner) | ||
304 | { | ||
305 | if (InstantMessageServiceConnector.SendInstantMessage(url, im)) | ||
306 | { | ||
307 | // IM delivery successful, so store the Agent's location in our local cache. | ||
308 | lock (m_UserLocationMap) | ||
309 | { | ||
310 | if (m_UserLocationMap.ContainsKey(toAgentID)) | ||
311 | { | ||
312 | m_UserLocationMap[toAgentID] = url; | ||
313 | } | ||
314 | else | ||
315 | { | ||
316 | m_UserLocationMap.Add(toAgentID, url); | ||
317 | } | ||
318 | } | ||
319 | |||
320 | return true; | ||
321 | } | ||
322 | else | ||
323 | { | ||
324 | // try again, but lookup user this time. | ||
325 | |||
326 | // This is recursive!!!!! | ||
327 | return TrySendInstantMessage(im, url, false, foreigner); | ||
328 | } | ||
329 | |||
330 | } | ||
331 | |||
332 | private bool UndeliveredMessage(GridInstantMessage im) | ||
333 | { | ||
334 | if (m_RestURL != string.Empty && (im.offline != 0) | ||
335 | && (!im.fromGroup || (im.fromGroup && m_ForwardOfflineGroupMessages))) | ||
336 | { | ||
337 | m_log.DebugFormat("[HG IM SERVICE]: Message saved"); | ||
338 | return SynchronousRestObjectPoster.BeginPostObject<GridInstantMessage, bool>( | ||
339 | "POST", m_RestURL + "/SaveMessage/", im); | ||
340 | |||
341 | } | ||
342 | |||
343 | else | ||
344 | { | ||
345 | return false; | ||
346 | } | ||
347 | } | ||
348 | } | ||
349 | } | ||