diff options
author | Diva Canto | 2011-05-25 12:32:21 -0700 |
---|---|---|
committer | Diva Canto | 2011-05-25 12:32:21 -0700 |
commit | 5c2168cae758ae19367f4c2f5a02713e74fc0912 (patch) | |
tree | 7d189c03a177ca716c91cf903cc7fd2e91fed1dc /OpenSim/Services/HypergridService/HGInstantMessageService.cs | |
parent | Added necessary code to drop inventory on hg friends using the profile window... (diff) | |
download | opensim-SC-5c2168cae758ae19367f4c2f5a02713e74fc0912.zip opensim-SC-5c2168cae758ae19367f4c2f5a02713e74fc0912.tar.gz opensim-SC-5c2168cae758ae19367f4c2f5a02713e74fc0912.tar.bz2 opensim-SC-5c2168cae758ae19367f4c2f5a02713e74fc0912.tar.xz |
HG: Instant Message working. Tested on HG standalones only. Needs a lot more testing.
Diffstat (limited to 'OpenSim/Services/HypergridService/HGInstantMessageService.cs')
-rw-r--r-- | OpenSim/Services/HypergridService/HGInstantMessageService.cs | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/OpenSim/Services/HypergridService/HGInstantMessageService.cs b/OpenSim/Services/HypergridService/HGInstantMessageService.cs new file mode 100644 index 0000000..6178ca1 --- /dev/null +++ b/OpenSim/Services/HypergridService/HGInstantMessageService.cs | |||
@@ -0,0 +1,311 @@ | |||
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 Dictionary<UUID, object> m_UserLocationMap = new Dictionary<UUID, object>(); | ||
68 | private ExpiringCache<UUID, GridRegion> m_RegionCache; | ||
69 | |||
70 | public HGInstantMessageService(IConfigSource config) | ||
71 | : this(config, null) | ||
72 | { | ||
73 | } | ||
74 | |||
75 | public HGInstantMessageService(IConfigSource config, IInstantMessageSimConnector imConnector) | ||
76 | { | ||
77 | if (imConnector != null) | ||
78 | m_IMSimConnector = imConnector; | ||
79 | |||
80 | if (!m_Initialized) | ||
81 | { | ||
82 | m_Initialized = true; | ||
83 | |||
84 | m_log.DebugFormat("[HG IM SERVICE]: Starting..."); | ||
85 | |||
86 | IConfig serverConfig = config.Configs["HGInstantMessageService"]; | ||
87 | if (serverConfig == null) | ||
88 | throw new Exception(String.Format("No section HGInstantMessageService in config file")); | ||
89 | |||
90 | string gridService = serverConfig.GetString("GridService", String.Empty); | ||
91 | string presenceService = serverConfig.GetString("PresenceService", String.Empty); | ||
92 | string userAgentService = serverConfig.GetString("UserAgentService", String.Empty); | ||
93 | |||
94 | if (gridService == string.Empty || presenceService == string.Empty) | ||
95 | throw new Exception(String.Format("Incomplete specifications, InstantMessage Service cannot function.")); | ||
96 | |||
97 | Object[] args = new Object[] { config }; | ||
98 | m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args); | ||
99 | m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args); | ||
100 | m_UserAgentService = ServerUtils.LoadPlugin<IUserAgentService>(userAgentService, args); | ||
101 | |||
102 | m_RegionCache = new ExpiringCache<UUID, GridRegion>(); | ||
103 | |||
104 | } | ||
105 | } | ||
106 | |||
107 | public bool IncomingInstantMessage(GridInstantMessage im) | ||
108 | { | ||
109 | m_log.DebugFormat("[HG IM SERVICE]: Received message {0} from {1} to {2}", im.message, im.fromAgentID, im.toAgentID); | ||
110 | UUID toAgentID = new UUID(im.toAgentID); | ||
111 | |||
112 | if (m_IMSimConnector != null) | ||
113 | { | ||
114 | m_log.DebugFormat("[XXX] SendIMToRegion local im connector"); | ||
115 | return m_IMSimConnector.SendInstantMessage(im); | ||
116 | } | ||
117 | else | ||
118 | return TrySendInstantMessage(im, "", true); | ||
119 | } | ||
120 | |||
121 | public bool OutgoingInstantMessage(GridInstantMessage im, string url) | ||
122 | { | ||
123 | m_log.DebugFormat("[HG IM SERVICE]: Sending message {0} from {1} to {2}@{3}", im.message, im.fromAgentID, im.toAgentID, url); | ||
124 | if (url != string.Empty) | ||
125 | return TrySendInstantMessage(im, url, true); | ||
126 | else | ||
127 | { | ||
128 | PresenceInfo upd = new PresenceInfo(); | ||
129 | upd.RegionID = UUID.Zero; | ||
130 | return TrySendInstantMessage(im, upd, true); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | protected bool TrySendInstantMessage(GridInstantMessage im, object previousLocation, bool firstTime) | ||
135 | { | ||
136 | UUID toAgentID = new UUID(im.toAgentID); | ||
137 | |||
138 | PresenceInfo upd = null; | ||
139 | string url = string.Empty; | ||
140 | |||
141 | bool lookupAgent = false; | ||
142 | |||
143 | lock (m_UserLocationMap) | ||
144 | { | ||
145 | if (m_UserLocationMap.ContainsKey(toAgentID)) | ||
146 | { | ||
147 | object o = m_UserLocationMap[toAgentID]; | ||
148 | if (o is PresenceInfo) | ||
149 | upd = (PresenceInfo)o; | ||
150 | else if (o is string) | ||
151 | url = (string)o; | ||
152 | |||
153 | // We need to compare the current location with the previous | ||
154 | // or the recursive loop will never end because it will never try to lookup the agent again | ||
155 | if (!firstTime) | ||
156 | { | ||
157 | lookupAgent = true; | ||
158 | } | ||
159 | } | ||
160 | else | ||
161 | { | ||
162 | lookupAgent = true; | ||
163 | } | ||
164 | } | ||
165 | |||
166 | m_log.DebugFormat("[XXX] Neeed lookup ? {0}", (lookupAgent ? "yes" : "no")); | ||
167 | |||
168 | // Are we needing to look-up an agent? | ||
169 | if (lookupAgent) | ||
170 | { | ||
171 | bool isPresent = false; | ||
172 | // Non-cached user agent lookup. | ||
173 | PresenceInfo[] presences = m_PresenceService.GetAgents(new string[] { toAgentID.ToString() }); | ||
174 | if (presences != null && presences.Length > 0) | ||
175 | { | ||
176 | foreach (PresenceInfo p in presences) | ||
177 | { | ||
178 | if (p.RegionID != UUID.Zero) | ||
179 | { | ||
180 | upd = p; | ||
181 | break; | ||
182 | } | ||
183 | else | ||
184 | isPresent = true; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | if (upd == null && isPresent) | ||
189 | { | ||
190 | // Let's check with the UAS if the user is elsewhere | ||
191 | url = m_UserAgentService.LocateUser(toAgentID); | ||
192 | } | ||
193 | |||
194 | if (upd != null || url != string.Empty) | ||
195 | { | ||
196 | // check if we've tried this before.. | ||
197 | // This is one way to end the recursive loop | ||
198 | // | ||
199 | if (!firstTime && ((previousLocation is PresenceInfo && upd != null && upd.RegionID == ((PresenceInfo)previousLocation).RegionID) || | ||
200 | (previousLocation is string && previousLocation.Equals(url)))) | ||
201 | { | ||
202 | // m_log.Error("[GRID INSTANT MESSAGE]: Unable to deliver an instant message"); | ||
203 | m_log.DebugFormat("[XXX] Fail 1 {0} {1}", previousLocation, url); | ||
204 | |||
205 | return false; | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | |||
210 | if (upd != null) | ||
211 | { | ||
212 | // ok, the user is around somewhere. Let's send back the reply with "success" | ||
213 | // even though the IM may still fail. Just don't keep the caller waiting for | ||
214 | // the entire time we're trying to deliver the IM | ||
215 | return SendIMToRegion(upd, im, toAgentID); | ||
216 | } | ||
217 | else if (url != string.Empty) | ||
218 | { | ||
219 | // ok, the user is around somewhere. Let's send back the reply with "success" | ||
220 | // even though the IM may still fail. Just don't keep the caller waiting for | ||
221 | // the entire time we're trying to deliver the IM | ||
222 | return ForwardIMToGrid(url, im, toAgentID); | ||
223 | } | ||
224 | else if (firstTime && previousLocation is string && (string)previousLocation != string.Empty) | ||
225 | { | ||
226 | return ForwardIMToGrid((string)previousLocation, im, toAgentID); | ||
227 | } | ||
228 | else | ||
229 | m_log.DebugFormat("[HG IM SERVICE]: Unable to locate user {0}", toAgentID); | ||
230 | return false; | ||
231 | } | ||
232 | |||
233 | bool SendIMToRegion(PresenceInfo upd, GridInstantMessage im, UUID toAgentID) | ||
234 | { | ||
235 | bool imresult = false; | ||
236 | GridRegion reginfo = null; | ||
237 | if (!m_RegionCache.TryGetValue(upd.RegionID, out reginfo)) | ||
238 | { | ||
239 | reginfo = m_GridService.GetRegionByUUID(UUID.Zero /*!!!*/, upd.RegionID); | ||
240 | if (reginfo != null) | ||
241 | m_RegionCache.AddOrUpdate(upd.RegionID, reginfo, CACHE_EXPIRATION_SECONDS); | ||
242 | } | ||
243 | |||
244 | if (reginfo != null) | ||
245 | imresult = InstantMessageServiceConnector.SendInstantMessage(reginfo.ServerURI, im); | ||
246 | else | ||
247 | return false; | ||
248 | |||
249 | if (imresult) | ||
250 | { | ||
251 | // IM delivery successful, so store the Agent's location in our local cache. | ||
252 | lock (m_UserLocationMap) | ||
253 | { | ||
254 | if (m_UserLocationMap.ContainsKey(toAgentID)) | ||
255 | { | ||
256 | m_UserLocationMap[toAgentID] = upd; | ||
257 | } | ||
258 | else | ||
259 | { | ||
260 | m_UserLocationMap.Add(toAgentID, upd); | ||
261 | } | ||
262 | } | ||
263 | return true; | ||
264 | } | ||
265 | else | ||
266 | { | ||
267 | // try again, but lookup user this time. | ||
268 | // Warning, this must call the Async version | ||
269 | // of this method or we'll be making thousands of threads | ||
270 | // The version within the spawned thread is SendGridInstantMessageViaXMLRPCAsync | ||
271 | // The version that spawns the thread is SendGridInstantMessageViaXMLRPC | ||
272 | |||
273 | // This is recursive!!!!! | ||
274 | return TrySendInstantMessage(im, upd, false); | ||
275 | } | ||
276 | } | ||
277 | |||
278 | bool ForwardIMToGrid(string url, GridInstantMessage im, UUID toAgentID) | ||
279 | { | ||
280 | if (InstantMessageServiceConnector.SendInstantMessage(url, im)) | ||
281 | { | ||
282 | // IM delivery successful, so store the Agent's location in our local cache. | ||
283 | lock (m_UserLocationMap) | ||
284 | { | ||
285 | if (m_UserLocationMap.ContainsKey(toAgentID)) | ||
286 | { | ||
287 | m_UserLocationMap[toAgentID] = url; | ||
288 | } | ||
289 | else | ||
290 | { | ||
291 | m_UserLocationMap.Add(toAgentID, url); | ||
292 | } | ||
293 | } | ||
294 | |||
295 | return true; | ||
296 | } | ||
297 | else | ||
298 | { | ||
299 | // try again, but lookup user this time. | ||
300 | // Warning, this must call the Async version | ||
301 | // of this method or we'll be making thousands of threads | ||
302 | // The version within the spawned thread is SendGridInstantMessageViaXMLRPCAsync | ||
303 | // The version that spawns the thread is SendGridInstantMessageViaXMLRPC | ||
304 | |||
305 | // This is recursive!!!!! | ||
306 | return TrySendInstantMessage(im, url, false); | ||
307 | } | ||
308 | |||
309 | } | ||
310 | } | ||
311 | } | ||