diff options
author | John Hurliman | 2010-03-11 10:05:03 -0800 |
---|---|---|
committer | John Hurliman | 2010-03-11 11:19:02 -0800 |
commit | 20406498711d1f26037ae32e1b7f8378b01ad848 (patch) | |
tree | e4a5a1c2f8eb9730d69a424aad59776f1359a9eb /OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs | |
parent | Merge branch 'master' of ssh://opensimulator.org/var/git/opensim (diff) | |
download | opensim-SC-20406498711d1f26037ae32e1b7f8378b01ad848.zip opensim-SC-20406498711d1f26037ae32e1b7f8378b01ad848.tar.gz opensim-SC-20406498711d1f26037ae32e1b7f8378b01ad848.tar.bz2 opensim-SC-20406498711d1f26037ae32e1b7f8378b01ad848.tar.xz |
Adding the SimianGrid connectors
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs b/OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs new file mode 100644 index 0000000..3952a8c --- /dev/null +++ b/OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs | |||
@@ -0,0 +1,229 @@ | |||
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.Collections.Specialized; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using Mono.Addins; | ||
34 | using Nini.Config; | ||
35 | using OpenMetaverse; | ||
36 | using OpenMetaverse.StructuredData; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Region.Framework.Interfaces; | ||
39 | using OpenSim.Region.Framework.Scenes; | ||
40 | using OpenSim.Services.Interfaces; | ||
41 | |||
42 | using FriendInfo = OpenSim.Services.Interfaces.FriendInfo; | ||
43 | |||
44 | namespace OpenSim.Services.Connectors.SimianGrid | ||
45 | { | ||
46 | /// <summary> | ||
47 | /// Stores and retrieves friend lists from the SimianGrid backend | ||
48 | /// </summary> | ||
49 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")] | ||
50 | public class SimianFriendsServiceConnector : IFriendsService, ISharedRegionModule | ||
51 | { | ||
52 | private static readonly ILog m_log = | ||
53 | LogManager.GetLogger( | ||
54 | MethodBase.GetCurrentMethod().DeclaringType); | ||
55 | |||
56 | private string m_serverUrl = String.Empty; | ||
57 | |||
58 | #region ISharedRegionModule | ||
59 | |||
60 | public Type ReplaceableInterface { get { return null; } } | ||
61 | public void RegionLoaded(Scene scene) { } | ||
62 | public void PostInitialise() { } | ||
63 | public void Close() { } | ||
64 | |||
65 | public SimianFriendsServiceConnector() { } | ||
66 | public string Name { get { return "SimianFriendsServiceConnector"; } } | ||
67 | public void AddRegion(Scene scene) { scene.RegisterModuleInterface<IFriendsService>(this); } | ||
68 | public void RemoveRegion(Scene scene) { scene.UnregisterModuleInterface<IFriendsService>(this); } | ||
69 | |||
70 | #endregion ISharedRegionModule | ||
71 | |||
72 | public SimianFriendsServiceConnector(IConfigSource source) | ||
73 | { | ||
74 | Initialise(source); | ||
75 | } | ||
76 | |||
77 | public void Initialise(IConfigSource source) | ||
78 | { | ||
79 | IConfig assetConfig = source.Configs["FriendsService"]; | ||
80 | if (assetConfig == null) | ||
81 | { | ||
82 | m_log.Error("[FRIENDS CONNECTOR]: FriendsService missing from OpenSim.ini"); | ||
83 | throw new Exception("Friends connector init error"); | ||
84 | } | ||
85 | |||
86 | string serviceURI = assetConfig.GetString("FriendsServerURI"); | ||
87 | if (String.IsNullOrEmpty(serviceURI)) | ||
88 | { | ||
89 | m_log.Error("[FRIENDS CONNECTOR]: No Server URI named in section FriendsService"); | ||
90 | throw new Exception("Friends connector init error"); | ||
91 | } | ||
92 | |||
93 | m_serverUrl = serviceURI; | ||
94 | } | ||
95 | |||
96 | #region IFriendsService | ||
97 | |||
98 | public FriendInfo[] GetFriends(UUID principalID) | ||
99 | { | ||
100 | Dictionary<UUID, FriendInfo> friends = new Dictionary<UUID, FriendInfo>(); | ||
101 | |||
102 | OSDArray friendsArray = GetFriended(principalID); | ||
103 | OSDArray friendedMeArray = GetFriendedBy(principalID); | ||
104 | |||
105 | // Load the list of friends and their granted permissions | ||
106 | for (int i = 0; i < friendsArray.Count; i++) | ||
107 | { | ||
108 | OSDMap friendEntry = friendsArray[i] as OSDMap; | ||
109 | if (friendEntry != null) | ||
110 | { | ||
111 | UUID friendID = friendEntry["Key"].AsUUID(); | ||
112 | |||
113 | FriendInfo friend = new FriendInfo(); | ||
114 | friend.PrincipalID = principalID; | ||
115 | friend.Friend = friendID.ToString(); | ||
116 | friend.MyFlags = friendEntry["Value"].AsInteger(); | ||
117 | friend.TheirFlags = -1; | ||
118 | |||
119 | friends[friendID] = friend; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | // Load the permissions those friends have granted to this user | ||
124 | for (int i = 0; i < friendedMeArray.Count; i++) | ||
125 | { | ||
126 | OSDMap friendedMeEntry = friendedMeArray[i] as OSDMap; | ||
127 | if (friendedMeEntry != null) | ||
128 | { | ||
129 | UUID friendID = friendedMeEntry["OwnerID"].AsUUID(); | ||
130 | |||
131 | FriendInfo friend; | ||
132 | if (friends.TryGetValue(friendID, out friend)) | ||
133 | friend.TheirFlags = friendedMeEntry["Value"].AsInteger(); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | // Convert the dictionary of friends to an array and return it | ||
138 | FriendInfo[] array = new FriendInfo[friends.Count]; | ||
139 | int j = 0; | ||
140 | foreach (FriendInfo friend in friends.Values) | ||
141 | array[j++] = friend; | ||
142 | |||
143 | return array; | ||
144 | } | ||
145 | |||
146 | public bool StoreFriend(UUID principalID, string friend, int flags) | ||
147 | { | ||
148 | NameValueCollection requestArgs = new NameValueCollection | ||
149 | { | ||
150 | { "RequestMethod", "AddGeneric" }, | ||
151 | { "OwnerID", principalID.ToString() }, | ||
152 | { "Type", "Friend" }, | ||
153 | { "Key", friend }, | ||
154 | { "Value", flags.ToString() } | ||
155 | }; | ||
156 | |||
157 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
158 | bool success = response["Success"].AsBoolean(); | ||
159 | |||
160 | if (!success) | ||
161 | m_log.Error("[FRIENDS CONNECTOR]: Failed to store friend " + friend + " for user " + principalID + ": " + response["Message"].AsString()); | ||
162 | |||
163 | return success; | ||
164 | } | ||
165 | |||
166 | public bool Delete(UUID principalID, string friend) | ||
167 | { | ||
168 | NameValueCollection requestArgs = new NameValueCollection | ||
169 | { | ||
170 | { "RequestMethod", "RemoveGeneric" }, | ||
171 | { "OwnerID", principalID.ToString() }, | ||
172 | { "Type", "Friend" }, | ||
173 | { "Key", friend } | ||
174 | }; | ||
175 | |||
176 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
177 | bool success = response["Success"].AsBoolean(); | ||
178 | |||
179 | if (!success) | ||
180 | m_log.Error("[FRIENDS CONNECTOR]: Failed to remove friend " + friend + " for user " + principalID + ": " + response["Message"].AsString()); | ||
181 | |||
182 | return success; | ||
183 | } | ||
184 | |||
185 | #endregion IFriendsService | ||
186 | |||
187 | private OSDArray GetFriended(UUID ownerID) | ||
188 | { | ||
189 | NameValueCollection requestArgs = new NameValueCollection | ||
190 | { | ||
191 | { "RequestMethod", "GetGenerics" }, | ||
192 | { "OwnerID", ownerID.ToString() }, | ||
193 | { "Type", "Friend" } | ||
194 | }; | ||
195 | |||
196 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
197 | if (response["Success"].AsBoolean() && response["Entries"] is OSDArray) | ||
198 | { | ||
199 | return (OSDArray)response["Entries"]; | ||
200 | } | ||
201 | else | ||
202 | { | ||
203 | m_log.Warn("[FRIENDS CONNECTOR]: Failed to retrieve friends for user " + ownerID + ": " + response["Message"].AsString()); | ||
204 | return new OSDArray(0); | ||
205 | } | ||
206 | } | ||
207 | |||
208 | private OSDArray GetFriendedBy(UUID ownerID) | ||
209 | { | ||
210 | NameValueCollection requestArgs = new NameValueCollection | ||
211 | { | ||
212 | { "RequestMethod", "GetGenerics" }, | ||
213 | { "Key", ownerID.ToString() }, | ||
214 | { "Type", "Friend" } | ||
215 | }; | ||
216 | |||
217 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
218 | if (response["Success"].AsBoolean() && response["Entries"] is OSDArray) | ||
219 | { | ||
220 | return (OSDArray)response["Entries"]; | ||
221 | } | ||
222 | else | ||
223 | { | ||
224 | m_log.Warn("[FRIENDS CONNECTOR]: Failed to retrieve reverse friends for user " + ownerID + ": " + response["Message"].AsString()); | ||
225 | return new OSDArray(0); | ||
226 | } | ||
227 | } | ||
228 | } | ||
229 | } | ||