OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
PresenceServicesConnector.cs
Go to the documentation of this file.
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 log4net;
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Reflection;
33 using Nini.Config;
34 using OpenSim.Framework;
35 
36 using OpenSim.Framework.ServiceAuth;
37 using OpenSim.Services.Interfaces;
39 using OpenSim.Server.Base;
40 using OpenMetaverse;
41 
42 namespace OpenSim.Services.Connectors
43 {
45  {
46  private static readonly ILog m_log =
47  LogManager.GetLogger(
48  MethodBase.GetCurrentMethod().DeclaringType);
49 
50  private string m_ServerURI = String.Empty;
51 
53  {
54  }
55 
56  public PresenceServicesConnector(string serverURI)
57  {
58  m_ServerURI = serverURI.TrimEnd('/');
59  }
60 
61  public PresenceServicesConnector(IConfigSource source)
62  {
63  Initialise(source);
64  }
65 
66  public virtual void Initialise(IConfigSource source)
67  {
68  IConfig gridConfig = source.Configs["PresenceService"];
69  if (gridConfig == null)
70  {
71  m_log.Error("[PRESENCE CONNECTOR]: PresenceService missing from OpenSim.ini");
72  throw new Exception("Presence connector init error");
73  }
74 
75  string serviceURI = gridConfig.GetString("PresenceServerURI",
76  String.Empty);
77 
78  if (serviceURI == String.Empty)
79  {
80  m_log.Error("[PRESENCE CONNECTOR]: No Server URI named in section PresenceService");
81  throw new Exception("Presence connector init error");
82  }
83  m_ServerURI = serviceURI;
84 
85  base.Initialise(source, "PresenceService");
86  }
87 
88 
89  #region IPresenceService
90 
91  public bool LoginAgent(string userID, UUID sessionID, UUID secureSessionID)
92  {
93  Dictionary<string, object> sendData = new Dictionary<string, object>();
94  //sendData["SCOPEID"] = scopeID.ToString();
95  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
96  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
97  sendData["METHOD"] = "login";
98 
99  sendData["UserID"] = userID;
100  sendData["SessionID"] = sessionID.ToString();
101  sendData["SecureSessionID"] = secureSessionID.ToString();
102 
103  string reqString = ServerUtils.BuildQueryString(sendData);
104  string uri = m_ServerURI + "/presence";
105  // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
106  try
107  {
108  string reply = SynchronousRestFormsRequester.MakeRequest("POST",
109  uri,
110  reqString,
111  m_Auth);
112  if (reply != string.Empty)
113  {
114  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
115 
116  if (replyData.ContainsKey("result"))
117  {
118  if (replyData["result"].ToString().ToLower() == "success")
119  return true;
120  else
121  return false;
122  }
123  else
124  m_log.DebugFormat("[PRESENCE CONNECTOR]: LoginAgent reply data does not contain result field");
125 
126  }
127  else
128  m_log.DebugFormat("[PRESENCE CONNECTOR]: LoginAgent received empty reply");
129  }
130  catch (Exception e)
131  {
132  m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
133  }
134 
135  return false;
136 
137  }
138 
139  public bool LogoutAgent(UUID sessionID)
140  {
141  Dictionary<string, object> sendData = new Dictionary<string, object>();
142  //sendData["SCOPEID"] = scopeID.ToString();
143  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
144  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
145  sendData["METHOD"] = "logout";
146 
147  sendData["SessionID"] = sessionID.ToString();
148 
149  string reqString = ServerUtils.BuildQueryString(sendData);
150  string uri = m_ServerURI + "/presence";
151  // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
152  try
153  {
154  string reply = SynchronousRestFormsRequester.MakeRequest("POST",
155  uri,
156  reqString,
157  m_Auth);
158  if (reply != string.Empty)
159  {
160  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
161 
162  if (replyData.ContainsKey("result"))
163  {
164  if (replyData["result"].ToString().ToLower() == "success")
165  return true;
166  else
167  return false;
168  }
169  else
170  m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutAgent reply data does not contain result field");
171 
172  }
173  else
174  m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutAgent received empty reply");
175  }
176  catch (Exception e)
177  {
178  m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
179  }
180 
181  return false;
182  }
183 
184  public bool LogoutRegionAgents(UUID regionID)
185  {
186  Dictionary<string, object> sendData = new Dictionary<string, object>();
187  //sendData["SCOPEID"] = scopeID.ToString();
188  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
189  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
190  sendData["METHOD"] = "logoutregion";
191 
192  sendData["RegionID"] = regionID.ToString();
193 
194  string reqString = ServerUtils.BuildQueryString(sendData);
195  string uri = m_ServerURI + "/presence";
196  // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
197  try
198  {
199  string reply = SynchronousRestFormsRequester.MakeRequest("POST",
200  uri,
201  reqString,
202  m_Auth);
203  if (reply != string.Empty)
204  {
205  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
206 
207  if (replyData.ContainsKey("result"))
208  {
209  if (replyData["result"].ToString().ToLower() == "success")
210  return true;
211  else
212  return false;
213  }
214  else
215  m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutRegionAgents reply data does not contain result field");
216 
217  }
218  else
219  m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutRegionAgents received empty reply");
220  }
221  catch (Exception e)
222  {
223  m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
224  }
225 
226  return false;
227  }
228 
229  public bool ReportAgent(UUID sessionID, UUID regionID)
230  {
231  Dictionary<string, object> sendData = new Dictionary<string, object>();
232  //sendData["SCOPEID"] = scopeID.ToString();
233  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
234  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
235  sendData["METHOD"] = "report";
236 
237  sendData["SessionID"] = sessionID.ToString();
238  sendData["RegionID"] = regionID.ToString();
239 
240  string reqString = ServerUtils.BuildQueryString(sendData);
241  string uri = m_ServerURI + "/presence";
242  // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
243  try
244  {
245  string reply = SynchronousRestFormsRequester.MakeRequest("POST",
246  uri,
247  reqString,
248  m_Auth);
249  if (reply != string.Empty)
250  {
251  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
252 
253  if (replyData.ContainsKey("result"))
254  {
255  if (replyData["result"].ToString().ToLower() == "success")
256  return true;
257  else
258  return false;
259  }
260  else
261  m_log.DebugFormat("[PRESENCE CONNECTOR]: ReportAgent reply data does not contain result field");
262 
263  }
264  else
265  m_log.DebugFormat("[PRESENCE CONNECTOR]: ReportAgent received empty reply");
266  }
267  catch (Exception e)
268  {
269  m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
270  }
271 
272  return false;
273  }
274 
275  public PresenceInfo GetAgent(UUID sessionID)
276  {
277  Dictionary<string, object> sendData = new Dictionary<string, object>();
278  //sendData["SCOPEID"] = scopeID.ToString();
279  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
280  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
281  sendData["METHOD"] = "getagent";
282 
283  sendData["SessionID"] = sessionID.ToString();
284 
285  string reply = string.Empty;
286  string reqString = ServerUtils.BuildQueryString(sendData);
287  string uri = m_ServerURI + "/presence";
288  // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
289  try
290  {
291  reply = SynchronousRestFormsRequester.MakeRequest("POST",
292  uri,
293  reqString,
294  m_Auth);
295  if (reply == null || (reply != null && reply == string.Empty))
296  {
297  m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgent received null or empty reply");
298  return null;
299  }
300  }
301  catch (Exception e)
302  {
303  m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
304  return null;
305  }
306 
307  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
308  PresenceInfo pinfo = null;
309 
310  if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
311  {
312  if (replyData["result"] is Dictionary<string, object>)
313  {
314  pinfo = new PresenceInfo((Dictionary<string, object>)replyData["result"]);
315  }
316  else
317  {
318  if (replyData["result"].ToString() == "null")
319  return null;
320 
321  m_log.DebugFormat("[PRESENCE CONNECTOR]: Invalid reply (result not dictionary) received from presence server when querying for sessionID {0}", sessionID.ToString());
322  }
323  }
324  else
325  {
326  m_log.DebugFormat("[PRESENCE CONNECTOR]: Invalid reply received from presence server when querying for sessionID {0}", sessionID.ToString());
327  }
328 
329  return pinfo;
330  }
331 
332  public PresenceInfo[] GetAgents(string[] userIDs)
333  {
334  Dictionary<string, object> sendData = new Dictionary<string, object>();
335  //sendData["SCOPEID"] = scopeID.ToString();
336  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
337  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
338  sendData["METHOD"] = "getagents";
339 
340  sendData["uuids"] = new List<string>(userIDs);
341 
342  string reply = string.Empty;
343  string reqString = ServerUtils.BuildQueryString(sendData);
344  string uri = m_ServerURI + "/presence";
345  //m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
346  try
347  {
348  reply = SynchronousRestFormsRequester.MakeRequest("POST",
349  uri,
350  reqString,
351  m_Auth);
352  if (reply == null || (reply != null && reply == string.Empty))
353  {
354  m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received null or empty reply");
355  return null;
356  }
357  }
358  catch (Exception e)
359  {
360  m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
361  }
362 
363  List<PresenceInfo> rinfos = new List<PresenceInfo>();
364 
365  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
366 
367  if (replyData != null)
368  {
369  if (replyData.ContainsKey("result") &&
370  (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure"))
371  {
372  return new PresenceInfo[0];
373  }
374 
375  Dictionary<string, object>.ValueCollection pinfosList = replyData.Values;
376  //m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
377  foreach (object presence in pinfosList)
378  {
379  if (presence is Dictionary<string, object>)
380  {
381  PresenceInfo pinfo = new PresenceInfo((Dictionary<string, object>)presence);
382  rinfos.Add(pinfo);
383  }
384  else
385  m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received invalid response type {0}",
386  presence.GetType());
387  }
388  }
389  else
390  m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received null response");
391 
392  return rinfos.ToArray();
393  }
394 
395 
396  #endregion
397 
398  }
399 }
OpenSim.Services.Interfaces.GridRegion GridRegion
bool LogoutAgent(UUID sessionID)
Remove session information.
PresenceInfo GetAgent(UUID sessionID)
Get session information for a given session ID.
bool LogoutRegionAgents(UUID regionID)
Remove session information for all agents in the given region.
bool LoginAgent(string userID, UUID sessionID, UUID secureSessionID)
Store session information.
PresenceInfo[] GetAgents(string[] userIDs)
Get session information for a collection of users.
OpenSim.Services.Interfaces.PresenceInfo PresenceInfo
bool ReportAgent(UUID sessionID, UUID regionID)
Update data for an existing session.