diff options
Diffstat (limited to 'OpenSim/Services/Connectors/Friends/FriendsServicesConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/Friends/FriendsServicesConnector.cs | 269 |
1 files changed, 269 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/Friends/FriendsServicesConnector.cs b/OpenSim/Services/Connectors/Friends/FriendsServicesConnector.cs new file mode 100644 index 0000000..74851a9 --- /dev/null +++ b/OpenSim/Services/Connectors/Friends/FriendsServicesConnector.cs | |||
@@ -0,0 +1,269 @@ | |||
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.Framework.Communications; | ||
37 | using OpenSim.Services.Interfaces; | ||
38 | using FriendInfo = OpenSim.Services.Interfaces.FriendInfo; | ||
39 | using OpenSim.Server.Base; | ||
40 | using OpenMetaverse; | ||
41 | |||
42 | namespace OpenSim.Services.Connectors.Friends | ||
43 | { | ||
44 | public class FriendsServicesConnector : BaseServiceConnector, IFriendsService | ||
45 | { | ||
46 | private static readonly ILog m_log = | ||
47 | LogManager.GetLogger( | ||
48 | MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | private string m_ServerURI = String.Empty; | ||
51 | |||
52 | public FriendsServicesConnector() | ||
53 | { | ||
54 | } | ||
55 | |||
56 | public FriendsServicesConnector(string serverURI) | ||
57 | { | ||
58 | m_ServerURI = serverURI.TrimEnd('/'); | ||
59 | } | ||
60 | |||
61 | public FriendsServicesConnector(IConfigSource source) | ||
62 | { | ||
63 | Initialise(source); | ||
64 | } | ||
65 | |||
66 | public virtual void Initialise(IConfigSource source) | ||
67 | { | ||
68 | IConfig gridConfig = source.Configs["FriendsService"]; | ||
69 | if (gridConfig == null) | ||
70 | { | ||
71 | m_log.Error("[FRIENDS SERVICE CONNECTOR]: FriendsService missing from OpenSim.ini"); | ||
72 | throw new Exception("Friends connector init error"); | ||
73 | } | ||
74 | |||
75 | string serviceURI = gridConfig.GetString("FriendsServerURI", | ||
76 | String.Empty); | ||
77 | |||
78 | if (serviceURI == String.Empty) | ||
79 | { | ||
80 | m_log.Error("[FRIENDS SERVICE CONNECTOR]: No Server URI named in section FriendsService"); | ||
81 | throw new Exception("Friends connector init error"); | ||
82 | } | ||
83 | m_ServerURI = serviceURI; | ||
84 | base.Initialise(source, "FriendsService"); | ||
85 | } | ||
86 | |||
87 | |||
88 | #region IFriendsService | ||
89 | |||
90 | public FriendInfo[] GetFriends(UUID PrincipalID) | ||
91 | { | ||
92 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
93 | |||
94 | sendData["PRINCIPALID"] = PrincipalID.ToString(); | ||
95 | sendData["METHOD"] = "getfriends"; | ||
96 | |||
97 | return GetFriends(sendData, PrincipalID.ToString()); | ||
98 | } | ||
99 | |||
100 | public FriendInfo[] GetFriends(string PrincipalID) | ||
101 | { | ||
102 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
103 | |||
104 | sendData["PRINCIPALID"] = PrincipalID; | ||
105 | sendData["METHOD"] = "getfriends_string"; | ||
106 | |||
107 | return GetFriends(sendData, PrincipalID); | ||
108 | } | ||
109 | |||
110 | protected FriendInfo[] GetFriends(Dictionary<string, object> sendData, string PrincipalID) | ||
111 | { | ||
112 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
113 | string uri = m_ServerURI + "/friends"; | ||
114 | |||
115 | try | ||
116 | { | ||
117 | string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString, m_Auth); | ||
118 | if (reply != string.Empty) | ||
119 | { | ||
120 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
121 | |||
122 | if (replyData != null) | ||
123 | { | ||
124 | if (replyData.ContainsKey("result") && (replyData["result"].ToString().ToLower() == "null")) | ||
125 | { | ||
126 | return new FriendInfo[0]; | ||
127 | } | ||
128 | |||
129 | List<FriendInfo> finfos = new List<FriendInfo>(); | ||
130 | Dictionary<string, object>.ValueCollection finfosList = replyData.Values; | ||
131 | //m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: get neighbours returned {0} elements", rinfosList.Count); | ||
132 | foreach (object f in finfosList) | ||
133 | { | ||
134 | if (f is Dictionary<string, object>) | ||
135 | { | ||
136 | FriendInfo finfo = new FriendInfo((Dictionary<string, object>)f); | ||
137 | finfos.Add(finfo); | ||
138 | } | ||
139 | else | ||
140 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: GetFriends {0} received invalid response type {1}", | ||
141 | PrincipalID, f.GetType()); | ||
142 | } | ||
143 | |||
144 | // Success | ||
145 | return finfos.ToArray(); | ||
146 | } | ||
147 | else | ||
148 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: GetFriends {0} received null response", | ||
149 | PrincipalID); | ||
150 | |||
151 | } | ||
152 | } | ||
153 | catch (Exception e) | ||
154 | { | ||
155 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message); | ||
156 | } | ||
157 | |||
158 | return new FriendInfo[0]; | ||
159 | |||
160 | } | ||
161 | |||
162 | public bool StoreFriend(string PrincipalID, string Friend, int flags) | ||
163 | { | ||
164 | |||
165 | Dictionary<string, object> sendData = ToKeyValuePairs(PrincipalID, Friend, flags); | ||
166 | |||
167 | sendData["METHOD"] = "storefriend"; | ||
168 | |||
169 | string reply = string.Empty; | ||
170 | string uri = m_ServerURI + "/friends"; | ||
171 | try | ||
172 | { | ||
173 | reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, ServerUtils.BuildQueryString(sendData), m_Auth); | ||
174 | } | ||
175 | catch (Exception e) | ||
176 | { | ||
177 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message); | ||
178 | return false; | ||
179 | } | ||
180 | |||
181 | if (reply != string.Empty) | ||
182 | { | ||
183 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
184 | |||
185 | if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null)) | ||
186 | { | ||
187 | bool success = false; | ||
188 | Boolean.TryParse(replyData["Result"].ToString(), out success); | ||
189 | return success; | ||
190 | } | ||
191 | else | ||
192 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: StoreFriend {0} {1} received null response", | ||
193 | PrincipalID, Friend); | ||
194 | } | ||
195 | else | ||
196 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: StoreFriend received null reply"); | ||
197 | |||
198 | return false; | ||
199 | |||
200 | } | ||
201 | |||
202 | public bool Delete(string PrincipalID, string Friend) | ||
203 | { | ||
204 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
205 | sendData["PRINCIPALID"] = PrincipalID.ToString(); | ||
206 | sendData["FRIEND"] = Friend; | ||
207 | sendData["METHOD"] = "deletefriend_string"; | ||
208 | |||
209 | return Delete(sendData, PrincipalID, Friend); | ||
210 | } | ||
211 | |||
212 | public bool Delete(UUID PrincipalID, string Friend) | ||
213 | { | ||
214 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
215 | sendData["PRINCIPALID"] = PrincipalID.ToString(); | ||
216 | sendData["FRIEND"] = Friend; | ||
217 | sendData["METHOD"] = "deletefriend"; | ||
218 | |||
219 | return Delete(sendData, PrincipalID.ToString(), Friend); | ||
220 | } | ||
221 | |||
222 | public bool Delete(Dictionary<string, object> sendData, string PrincipalID, string Friend) | ||
223 | { | ||
224 | string reply = string.Empty; | ||
225 | string uri = m_ServerURI + "/friends"; | ||
226 | try | ||
227 | { | ||
228 | reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, ServerUtils.BuildQueryString(sendData), m_Auth); | ||
229 | } | ||
230 | catch (Exception e) | ||
231 | { | ||
232 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message); | ||
233 | return false; | ||
234 | } | ||
235 | |||
236 | if (reply != string.Empty) | ||
237 | { | ||
238 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
239 | |||
240 | if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null)) | ||
241 | { | ||
242 | bool success = false; | ||
243 | Boolean.TryParse(replyData["Result"].ToString(), out success); | ||
244 | return success; | ||
245 | } | ||
246 | else | ||
247 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: DeleteFriend {0} {1} received null response", | ||
248 | PrincipalID, Friend); | ||
249 | } | ||
250 | else | ||
251 | m_log.DebugFormat("[FRIENDS SERVICE CONNECTOR]: DeleteFriend received null reply"); | ||
252 | |||
253 | return false; | ||
254 | } | ||
255 | |||
256 | #endregion | ||
257 | |||
258 | public Dictionary<string, object> ToKeyValuePairs(string principalID, string friend, int flags) | ||
259 | { | ||
260 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
261 | result["PrincipalID"] = principalID; | ||
262 | result["Friend"] = friend; | ||
263 | result["MyFlags"] = flags; | ||
264 | |||
265 | return result; | ||
266 | } | ||
267 | |||
268 | } | ||
269 | } \ No newline at end of file | ||