diff options
author | Cinder | 2015-06-12 18:48:07 -0600 |
---|---|---|
committer | Diva Canto | 2015-06-13 07:27:42 -0700 |
commit | 0fa94f222df8ed7f308730c3692bf2a774138718 (patch) | |
tree | 560efc5542dea946a79dbbaebd39b991a67ede73 /OpenSim/Services/Connectors/AgentPreferences/AgentPreferencesConnector.cs | |
parent | Relicense AgentPreferences files to BSD and OpenSimulator (diff) | |
download | opensim-SC-0fa94f222df8ed7f308730c3692bf2a774138718.zip opensim-SC-0fa94f222df8ed7f308730c3692bf2a774138718.tar.gz opensim-SC-0fa94f222df8ed7f308730c3692bf2a774138718.tar.bz2 opensim-SC-0fa94f222df8ed7f308730c3692bf2a774138718.tar.xz |
Refactor AgentPreferences so that database operations happen centrally. the opensim way.
Signed-off-by: Diva Canto <diva@metaverseink.com>
Diffstat (limited to 'OpenSim/Services/Connectors/AgentPreferences/AgentPreferencesConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/AgentPreferences/AgentPreferencesConnector.cs | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/AgentPreferences/AgentPreferencesConnector.cs b/OpenSim/Services/Connectors/AgentPreferences/AgentPreferencesConnector.cs new file mode 100644 index 0000000..1dbc0c8 --- /dev/null +++ b/OpenSim/Services/Connectors/AgentPreferences/AgentPreferencesConnector.cs | |||
@@ -0,0 +1,230 @@ | |||
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 | using OpenSim.Framework.ServiceAuth; | ||
36 | using OpenSim.Services.Interfaces; | ||
37 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
38 | using IAvatarService = OpenSim.Services.Interfaces.IAvatarService; | ||
39 | using OpenSim.Server.Base; | ||
40 | using OpenMetaverse; | ||
41 | |||
42 | namespace OpenSim.Services.Connectors | ||
43 | { | ||
44 | public class AgentPreferencesServicesConnector : BaseServiceConnector, IAgentPreferencesService | ||
45 | { | ||
46 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
47 | |||
48 | private string m_ServerURI = String.Empty; | ||
49 | |||
50 | public AgentPreferencesServicesConnector() | ||
51 | { | ||
52 | } | ||
53 | |||
54 | public AgentPreferencesServicesConnector(string serverURI) | ||
55 | { | ||
56 | m_ServerURI = serverURI.TrimEnd('/'); | ||
57 | } | ||
58 | |||
59 | public AgentPreferencesServicesConnector(IConfigSource source) | ||
60 | : base(source, "AgentPreferencesService") | ||
61 | { | ||
62 | Initialise(source); | ||
63 | } | ||
64 | |||
65 | public virtual void Initialise(IConfigSource source) | ||
66 | { | ||
67 | IConfig gridConfig = source.Configs["AgentPreferencesService"]; | ||
68 | if (gridConfig == null) | ||
69 | { | ||
70 | m_log.Error("[AGENT PREFERENCES CONNECTOR]: AgentPreferencesService missing from OpenSim.ini"); | ||
71 | throw new Exception("Agent Preferences connector init error"); | ||
72 | } | ||
73 | |||
74 | string serviceURI = gridConfig.GetString("AgentPreferencesServerURI", String.Empty); | ||
75 | |||
76 | if (serviceURI == String.Empty) | ||
77 | { | ||
78 | m_log.Error("[AGENT PREFERENCES CONNECTOR]: No Server URI named in section AgentPreferences"); | ||
79 | throw new Exception("Agent Preferences connector init error"); | ||
80 | } | ||
81 | m_ServerURI = serviceURI; | ||
82 | |||
83 | base.Initialise(source, "AgentPreferencesService"); | ||
84 | } | ||
85 | |||
86 | #region IAgentPreferencesService | ||
87 | |||
88 | public AgentPrefs GetAgentPreferences(UUID principalID) | ||
89 | { | ||
90 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
91 | |||
92 | string reply = string.Empty; | ||
93 | string uri = String.Concat(m_ServerURI, "/agentprefs"); | ||
94 | |||
95 | sendData["METHOD"] = "getagentprefs"; | ||
96 | sendData["UserID"] = principalID; | ||
97 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
98 | // m_log.DebugFormat("[AGENT PREFS CONNECTOR]: queryString = {0}", reqString); | ||
99 | |||
100 | try | ||
101 | { | ||
102 | reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth); | ||
103 | if (String.IsNullOrEmpty(reply)) | ||
104 | { | ||
105 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetAgentPreferences received null or empty reply"); | ||
106 | return null; | ||
107 | } | ||
108 | } | ||
109 | catch (Exception e) | ||
110 | { | ||
111 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: Exception when contacting agent preferences server at {0}: {1}", uri, e.Message); | ||
112 | } | ||
113 | |||
114 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
115 | if (replyData != null) | ||
116 | { | ||
117 | if (replyData.ContainsKey("result") && | ||
118 | (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure")) | ||
119 | { | ||
120 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetAgentPreferences received Failure response"); | ||
121 | return null; | ||
122 | } | ||
123 | } | ||
124 | else | ||
125 | { | ||
126 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetAgentPreferences received null response"); | ||
127 | return null; | ||
128 | } | ||
129 | AgentPrefs prefs = new AgentPrefs(replyData); | ||
130 | return prefs; | ||
131 | } | ||
132 | |||
133 | public bool StoreAgentPreferences(AgentPrefs data) | ||
134 | { | ||
135 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
136 | |||
137 | sendData["METHOD"] = "setagentprefs"; | ||
138 | |||
139 | sendData["PrincipalID"] = data.PrincipalID.ToString(); | ||
140 | sendData["AccessPrefs"] = data.AccessPrefs; | ||
141 | sendData["HoverHeight"] = data.HoverHeight.ToString(); | ||
142 | sendData["Language"] = data.Language; | ||
143 | sendData["LanguageIsPublic"] = data.LanguageIsPublic.ToString(); | ||
144 | sendData["PermEveryone"] = data.PermEveryone.ToString(); | ||
145 | sendData["PermGroup"] = data.PermGroup.ToString(); | ||
146 | sendData["PermNextOwner"] = data.PermNextOwner.ToString(); | ||
147 | |||
148 | string uri = String.Concat(m_ServerURI, "/agentprefs"); | ||
149 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
150 | // m_log.DebugFormat("[AGENT PREFS CONNECTOR]: queryString = {0}", reqString); | ||
151 | |||
152 | try | ||
153 | { | ||
154 | string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth); | ||
155 | if (reply != string.Empty) | ||
156 | { | ||
157 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
158 | |||
159 | if (replyData.ContainsKey("result")) | ||
160 | { | ||
161 | if (replyData["result"].ToString().ToLower() == "success") | ||
162 | return true; | ||
163 | else | ||
164 | return false; | ||
165 | } | ||
166 | else | ||
167 | { | ||
168 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: StoreAgentPreferences reply data does not contain result field"); | ||
169 | } | ||
170 | |||
171 | } | ||
172 | else | ||
173 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: StoreAgentPreferences received empty reply"); | ||
174 | } | ||
175 | catch (Exception e) | ||
176 | { | ||
177 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: Exception when contacting agent preferences server at {0}: {1}", uri, e.Message); | ||
178 | } | ||
179 | |||
180 | return false; | ||
181 | } | ||
182 | |||
183 | public string GetLang(UUID principalID) | ||
184 | { | ||
185 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
186 | string reply = string.Empty; | ||
187 | |||
188 | sendData["METHOD"] = "getagentlang"; | ||
189 | sendData["UserID"] = principalID.ToString(); | ||
190 | |||
191 | string uri = String.Concat(m_ServerURI, "/agentprefs"); | ||
192 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
193 | |||
194 | try | ||
195 | { | ||
196 | reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth); | ||
197 | if (String.IsNullOrEmpty(reply)) | ||
198 | { | ||
199 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received null or empty reply"); | ||
200 | return "en-us"; // I guess? Gotta return somethin'! | ||
201 | } | ||
202 | } | ||
203 | catch (Exception e) | ||
204 | { | ||
205 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: Exception when contacting agent preferences server at {0}: {1}", uri, e.Message); | ||
206 | } | ||
207 | |||
208 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
209 | if (replyData != null) | ||
210 | { | ||
211 | if (replyData.ContainsKey("result") && | ||
212 | (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure")) | ||
213 | { | ||
214 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received Failure response"); | ||
215 | return "en-us"; | ||
216 | } | ||
217 | if (replyData.ContainsKey("Language")) | ||
218 | return replyData["Language"].ToString(); | ||
219 | } | ||
220 | else | ||
221 | { | ||
222 | m_log.DebugFormat("[AGENT PREFERENCES CONNECTOR]: GetLang received null response"); | ||
223 | |||
224 | } | ||
225 | return "en-us"; | ||
226 | } | ||
227 | |||
228 | #endregion IAgentPreferencesService | ||
229 | } | ||
230 | } | ||