diff options
author | Diva Canto | 2013-02-18 15:44:00 -0800 |
---|---|---|
committer | Diva Canto | 2013-02-18 15:44:00 -0800 |
commit | 5e709b087ccf380749c6a6da8a8293c23f211efd (patch) | |
tree | 741c82f14bb9ede51b7003fb8f6529df1fc36be6 /OpenSim/Addons/OfflineIM/Remote | |
parent | I need these for OfflineIM and Groups. (diff) | |
download | opensim-SC_OLD-5e709b087ccf380749c6a6da8a8293c23f211efd.zip opensim-SC_OLD-5e709b087ccf380749c6a6da8a8293c23f211efd.tar.gz opensim-SC_OLD-5e709b087ccf380749c6a6da8a8293c23f211efd.tar.bz2 opensim-SC_OLD-5e709b087ccf380749c6a6da8a8293c23f211efd.tar.xz |
Donating Diva OfflineIM as an addon in a single dll, OpenSim.Addons.OpenSim.dll
Diffstat (limited to 'OpenSim/Addons/OfflineIM/Remote')
-rw-r--r-- | OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs | 143 | ||||
-rw-r--r-- | OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs | 215 |
2 files changed, 358 insertions, 0 deletions
diff --git a/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs new file mode 100644 index 0000000..69feb76 --- /dev/null +++ b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs | |||
@@ -0,0 +1,143 @@ | |||
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.Linq; | ||
31 | using System.Reflection; | ||
32 | using System.Text; | ||
33 | |||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Server.Base; | ||
36 | using OpenSim.Services.Interfaces; | ||
37 | |||
38 | using OpenMetaverse; | ||
39 | using log4net; | ||
40 | using Nini.Config; | ||
41 | |||
42 | namespace OpenSim.OfflineIM | ||
43 | { | ||
44 | public class OfflineIMServiceRemoteConnector : IOfflineIMService | ||
45 | { | ||
46 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
47 | |||
48 | private string m_ServerURI = string.Empty; | ||
49 | private object m_Lock = new object(); | ||
50 | |||
51 | public OfflineIMServiceRemoteConnector(string url) | ||
52 | { | ||
53 | m_ServerURI = url; | ||
54 | m_log.DebugFormat("[OfflineIM.V2.RemoteConnector]: Offline IM server at {0}", m_ServerURI); | ||
55 | } | ||
56 | |||
57 | public OfflineIMServiceRemoteConnector(IConfigSource config) | ||
58 | { | ||
59 | IConfig cnf = config.Configs["Messaging"]; | ||
60 | if (cnf == null) | ||
61 | { | ||
62 | m_log.WarnFormat("[OfflineIM.V2.RemoteConnector]: Missing Messaging configuration"); | ||
63 | return; | ||
64 | } | ||
65 | |||
66 | m_ServerURI = cnf.GetString("OfflineMessageURL", string.Empty); | ||
67 | |||
68 | } | ||
69 | |||
70 | #region IOfflineIMService | ||
71 | public List<GridInstantMessage> GetMessages(UUID principalID) | ||
72 | { | ||
73 | List<GridInstantMessage> ims = new List<GridInstantMessage>(); | ||
74 | |||
75 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
76 | sendData["PrincipalID"] = principalID; | ||
77 | Dictionary<string, object> ret = MakeRequest("GET", sendData); | ||
78 | |||
79 | if (ret == null) | ||
80 | return ims; | ||
81 | |||
82 | if (!ret.ContainsKey("RESULT")) | ||
83 | return ims; | ||
84 | |||
85 | if (ret["RESULT"].ToString() == "NULL") | ||
86 | return ims; | ||
87 | |||
88 | foreach (object v in ((Dictionary<string, object>)ret["RESULT"]).Values) | ||
89 | { | ||
90 | GridInstantMessage m = OfflineIMDataUtils.GridInstantMessage((Dictionary<string, object>)v); | ||
91 | ims.Add(m); | ||
92 | } | ||
93 | |||
94 | return ims; | ||
95 | } | ||
96 | |||
97 | public bool StoreMessage(GridInstantMessage im, out string reason) | ||
98 | { | ||
99 | reason = string.Empty; | ||
100 | Dictionary<string, object> sendData = OfflineIMDataUtils.GridInstantMessage(im); | ||
101 | |||
102 | Dictionary<string, object> ret = MakeRequest("STORE", sendData); | ||
103 | |||
104 | if (ret == null) | ||
105 | { | ||
106 | reason = "Bad response from server"; | ||
107 | return false; | ||
108 | } | ||
109 | |||
110 | string result = ret["RESULT"].ToString(); | ||
111 | if (result == "NULL" || result.ToLower() == "false") | ||
112 | { | ||
113 | reason = ret["REASON"].ToString(); | ||
114 | return false; | ||
115 | } | ||
116 | |||
117 | return true; | ||
118 | } | ||
119 | |||
120 | #endregion | ||
121 | |||
122 | |||
123 | #region Make Request | ||
124 | |||
125 | private Dictionary<string, object> MakeRequest(string method, Dictionary<string, object> sendData) | ||
126 | { | ||
127 | sendData["METHOD"] = method; | ||
128 | |||
129 | string reply = string.Empty; | ||
130 | lock (m_Lock) | ||
131 | reply = SynchronousRestFormsRequester.MakeRequest("POST", | ||
132 | m_ServerURI + "/offlineim", | ||
133 | ServerUtils.BuildQueryString(sendData)); | ||
134 | |||
135 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse( | ||
136 | reply); | ||
137 | |||
138 | return replyData; | ||
139 | } | ||
140 | #endregion | ||
141 | |||
142 | } | ||
143 | } | ||
diff --git a/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs new file mode 100644 index 0000000..2b3a01d --- /dev/null +++ b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs | |||
@@ -0,0 +1,215 @@ | |||
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.Reflection; | ||
30 | using System.Text; | ||
31 | using System.Xml; | ||
32 | using System.Collections.Generic; | ||
33 | using System.IO; | ||
34 | using Nini.Config; | ||
35 | using OpenSim.Framework; | ||
36 | using OpenSim.Server.Base; | ||
37 | using OpenSim.Services.Interfaces; | ||
38 | using OpenSim.Framework.Servers.HttpServer; | ||
39 | using OpenSim.Server.Handlers.Base; | ||
40 | using log4net; | ||
41 | using OpenMetaverse; | ||
42 | |||
43 | namespace OpenSim.OfflineIM | ||
44 | { | ||
45 | public class OfflineIMServiceRobustConnector : ServiceConnector | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | private IOfflineIMService m_OfflineIMService; | ||
50 | private string m_ConfigName = "Messaging"; | ||
51 | |||
52 | public OfflineIMServiceRobustConnector(IConfigSource config, IHttpServer server, string configName) : | ||
53 | base(config, server, configName) | ||
54 | { | ||
55 | if (configName != String.Empty) | ||
56 | m_ConfigName = configName; | ||
57 | |||
58 | m_log.DebugFormat("[OfflineIM.V2.RobustConnector]: Starting with config name {0}", m_ConfigName); | ||
59 | |||
60 | m_OfflineIMService = new OfflineIMService(config); | ||
61 | |||
62 | server.AddStreamHandler(new OfflineIMServicePostHandler(m_OfflineIMService)); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | public class OfflineIMServicePostHandler : BaseStreamHandler | ||
67 | { | ||
68 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
69 | |||
70 | private IOfflineIMService m_OfflineIMService; | ||
71 | |||
72 | public OfflineIMServicePostHandler(IOfflineIMService service) : | ||
73 | base("POST", "/offlineim") | ||
74 | { | ||
75 | m_OfflineIMService = service; | ||
76 | } | ||
77 | |||
78 | public override byte[] Handle(string path, Stream requestData, | ||
79 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
80 | { | ||
81 | StreamReader sr = new StreamReader(requestData); | ||
82 | string body = sr.ReadToEnd(); | ||
83 | sr.Close(); | ||
84 | body = body.Trim(); | ||
85 | |||
86 | //m_log.DebugFormat("[XXX]: query String: {0}", body); | ||
87 | |||
88 | try | ||
89 | { | ||
90 | Dictionary<string, object> request = | ||
91 | ServerUtils.ParseQueryString(body); | ||
92 | |||
93 | if (!request.ContainsKey("METHOD")) | ||
94 | return FailureResult(); | ||
95 | |||
96 | string method = request["METHOD"].ToString(); | ||
97 | request.Remove("METHOD"); | ||
98 | |||
99 | m_log.DebugFormat("[OfflineIM.V2.Handler]: {0}", method); | ||
100 | switch (method) | ||
101 | { | ||
102 | case "GET": | ||
103 | return HandleGet(request); | ||
104 | case "STORE": | ||
105 | return HandleStore(request); | ||
106 | } | ||
107 | m_log.DebugFormat("[OFFLINE IM HANDLER]: unknown method request: {0}", method); | ||
108 | } | ||
109 | catch (Exception e) | ||
110 | { | ||
111 | m_log.DebugFormat("[OFFLINE IM HANDLER]: Exception {0}", e.StackTrace); | ||
112 | } | ||
113 | |||
114 | return FailureResult(); | ||
115 | } | ||
116 | |||
117 | byte[] HandleStore(Dictionary<string, object> request) | ||
118 | { | ||
119 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
120 | |||
121 | GridInstantMessage im = OfflineIMDataUtils.GridInstantMessage(request); | ||
122 | |||
123 | string reason = string.Empty; | ||
124 | |||
125 | bool success = m_OfflineIMService.StoreMessage(im, out reason); | ||
126 | |||
127 | result["RESULT"] = success.ToString(); | ||
128 | if (!success) | ||
129 | result["REASON"] = reason; | ||
130 | |||
131 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
132 | |||
133 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
134 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
135 | } | ||
136 | |||
137 | byte[] HandleGet(Dictionary<string, object> request) | ||
138 | { | ||
139 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
140 | |||
141 | if (!request.ContainsKey("PrincipalID")) | ||
142 | NullResult(result, "Bad network data"); | ||
143 | else | ||
144 | { | ||
145 | UUID principalID = new UUID(request["PrincipalID"].ToString()); | ||
146 | List<GridInstantMessage> ims = m_OfflineIMService.GetMessages(principalID); | ||
147 | |||
148 | Dictionary<string, object> dict = new Dictionary<string, object>(); | ||
149 | int i = 0; | ||
150 | foreach (GridInstantMessage m in ims) | ||
151 | dict["im-" + i++] = OfflineIMDataUtils.GridInstantMessage(m); | ||
152 | |||
153 | result["RESULT"] = dict; | ||
154 | } | ||
155 | |||
156 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
157 | |||
158 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
159 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
160 | } | ||
161 | |||
162 | #region Helpers | ||
163 | |||
164 | private void NullResult(Dictionary<string, object> result, string reason) | ||
165 | { | ||
166 | result["RESULT"] = "NULL"; | ||
167 | result["REASON"] = reason; | ||
168 | } | ||
169 | |||
170 | private byte[] FailureResult() | ||
171 | { | ||
172 | return BoolResult(false); | ||
173 | } | ||
174 | |||
175 | private byte[] SuccessResult() | ||
176 | { | ||
177 | return BoolResult(true); | ||
178 | } | ||
179 | |||
180 | private byte[] BoolResult(bool value) | ||
181 | { | ||
182 | XmlDocument doc = new XmlDocument(); | ||
183 | |||
184 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
185 | "", ""); | ||
186 | |||
187 | doc.AppendChild(xmlnode); | ||
188 | |||
189 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
190 | ""); | ||
191 | |||
192 | doc.AppendChild(rootElement); | ||
193 | |||
194 | XmlElement result = doc.CreateElement("", "RESULT", ""); | ||
195 | result.AppendChild(doc.CreateTextNode(value.ToString())); | ||
196 | |||
197 | rootElement.AppendChild(result); | ||
198 | |||
199 | return DocToBytes(doc); | ||
200 | } | ||
201 | |||
202 | private byte[] DocToBytes(XmlDocument doc) | ||
203 | { | ||
204 | MemoryStream ms = new MemoryStream(); | ||
205 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
206 | xw.Formatting = Formatting.Indented; | ||
207 | doc.WriteTo(xw); | ||
208 | xw.Flush(); | ||
209 | |||
210 | return ms.ToArray(); | ||
211 | } | ||
212 | |||
213 | #endregion | ||
214 | } | ||
215 | } | ||