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