diff options
Diffstat (limited to 'OpenSim/Services/Connectors/Neighbour/NeighbourServicesConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/Neighbour/NeighbourServicesConnector.cs | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/Neighbour/NeighbourServicesConnector.cs b/OpenSim/Services/Connectors/Neighbour/NeighbourServicesConnector.cs new file mode 100644 index 0000000..7429293 --- /dev/null +++ b/OpenSim/Services/Connectors/Neighbour/NeighbourServicesConnector.cs | |||
@@ -0,0 +1,205 @@ | |||
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; | ||
31 | using System.Collections.Generic; | ||
32 | using System.IO; | ||
33 | using System.Net; | ||
34 | using System.Reflection; | ||
35 | using System.Text; | ||
36 | using Nini.Config; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Framework.Communications; | ||
39 | using OpenSim.Services.Interfaces; | ||
40 | using OpenMetaverse; | ||
41 | using OpenMetaverse.StructuredData; | ||
42 | |||
43 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
44 | |||
45 | namespace OpenSim.Services.Connectors | ||
46 | { | ||
47 | public class NeighbourServicesConnector : INeighbourService | ||
48 | { | ||
49 | private static readonly ILog m_log = | ||
50 | LogManager.GetLogger( | ||
51 | MethodBase.GetCurrentMethod().DeclaringType); | ||
52 | |||
53 | protected IGridService m_GridService = null; | ||
54 | |||
55 | public NeighbourServicesConnector() | ||
56 | { | ||
57 | } | ||
58 | |||
59 | public NeighbourServicesConnector(IGridService gridServices) | ||
60 | { | ||
61 | Initialise(gridServices); | ||
62 | } | ||
63 | |||
64 | public virtual void Initialise(IGridService gridServices) | ||
65 | { | ||
66 | m_GridService = gridServices; | ||
67 | } | ||
68 | |||
69 | public virtual GridRegion HelloNeighbour(ulong regionHandle, RegionInfo thisRegion) | ||
70 | { | ||
71 | uint x = 0, y = 0; | ||
72 | Utils.LongToUInts(regionHandle, out x, out y); | ||
73 | GridRegion regInfo = m_GridService.GetRegionByPosition(thisRegion.ScopeID, (int)x, (int)y); | ||
74 | if ((regInfo != null) && | ||
75 | // Don't remote-call this instance; that's a startup hickup | ||
76 | !((regInfo.ExternalHostName == thisRegion.ExternalHostName) && (regInfo.HttpPort == thisRegion.HttpPort))) | ||
77 | { | ||
78 | if (!DoHelloNeighbourCall(regInfo, thisRegion)) | ||
79 | return null; | ||
80 | } | ||
81 | else | ||
82 | return null; | ||
83 | |||
84 | return regInfo; | ||
85 | } | ||
86 | |||
87 | public bool DoHelloNeighbourCall(GridRegion region, RegionInfo thisRegion) | ||
88 | { | ||
89 | string uri = region.ServerURI + "region/" + thisRegion.RegionID + "/"; | ||
90 | // m_log.Debug(" >>> DoHelloNeighbourCall <<< " + uri); | ||
91 | |||
92 | WebRequest helloNeighbourRequest; | ||
93 | |||
94 | try | ||
95 | { | ||
96 | helloNeighbourRequest = WebRequest.Create(uri); | ||
97 | } | ||
98 | catch (Exception e) | ||
99 | { | ||
100 | m_log.WarnFormat( | ||
101 | "[NEIGHBOUR SERVICE CONNCTOR]: Unable to parse uri {0} to send HelloNeighbour from {1} to {2}. Exception {3}{4}", | ||
102 | uri, thisRegion.RegionName, region.RegionName, e.Message, e.StackTrace); | ||
103 | |||
104 | return false; | ||
105 | } | ||
106 | |||
107 | helloNeighbourRequest.Method = "POST"; | ||
108 | helloNeighbourRequest.ContentType = "application/json"; | ||
109 | helloNeighbourRequest.Timeout = 10000; | ||
110 | |||
111 | // Fill it in | ||
112 | OSDMap args = null; | ||
113 | try | ||
114 | { | ||
115 | args = thisRegion.PackRegionInfoData(); | ||
116 | } | ||
117 | catch (Exception e) | ||
118 | { | ||
119 | m_log.WarnFormat( | ||
120 | "[NEIGHBOUR SERVICE CONNCTOR]: PackRegionInfoData failed for HelloNeighbour from {0} to {1}. Exception {2}{3}", | ||
121 | thisRegion.RegionName, region.RegionName, e.Message, e.StackTrace); | ||
122 | |||
123 | return false; | ||
124 | } | ||
125 | |||
126 | // Add the regionhandle of the destination region | ||
127 | args["destination_handle"] = OSD.FromString(region.RegionHandle.ToString()); | ||
128 | |||
129 | string strBuffer = ""; | ||
130 | byte[] buffer = new byte[1]; | ||
131 | |||
132 | try | ||
133 | { | ||
134 | strBuffer = OSDParser.SerializeJsonString(args); | ||
135 | buffer = Util.UTF8NoBomEncoding.GetBytes(strBuffer); | ||
136 | } | ||
137 | catch (Exception e) | ||
138 | { | ||
139 | m_log.WarnFormat( | ||
140 | "[NEIGHBOUR SERVICE CONNCTOR]: Exception thrown on serialization of HelloNeighbour from {0} to {1}. Exception {2}{3}", | ||
141 | thisRegion.RegionName, region.RegionName, e.Message, e.StackTrace); | ||
142 | |||
143 | return false; | ||
144 | } | ||
145 | |||
146 | Stream os = null; | ||
147 | try | ||
148 | { // send the Post | ||
149 | helloNeighbourRequest.ContentLength = buffer.Length; //Count bytes to send | ||
150 | os = helloNeighbourRequest.GetRequestStream(); | ||
151 | os.Write(buffer, 0, strBuffer.Length); //Send it | ||
152 | //m_log.InfoFormat("[REST COMMS]: Posted HelloNeighbour request to remote sim {0}", uri); | ||
153 | } | ||
154 | catch (Exception e) | ||
155 | { | ||
156 | m_log.WarnFormat( | ||
157 | "[NEIGHBOUR SERVICE CONNCTOR]: Unable to send HelloNeighbour from {0} to {1}. Exception {2}{3}", | ||
158 | thisRegion.RegionName, region.RegionName, e.Message, e.StackTrace); | ||
159 | |||
160 | return false; | ||
161 | } | ||
162 | finally | ||
163 | { | ||
164 | if (os != null) | ||
165 | os.Close(); | ||
166 | } | ||
167 | |||
168 | // Let's wait for the response | ||
169 | //m_log.Info("[REST COMMS]: Waiting for a reply after DoHelloNeighbourCall"); | ||
170 | |||
171 | StreamReader sr = null; | ||
172 | try | ||
173 | { | ||
174 | WebResponse webResponse = helloNeighbourRequest.GetResponse(); | ||
175 | if (webResponse == null) | ||
176 | { | ||
177 | m_log.DebugFormat( | ||
178 | "[REST COMMS]: Null reply on DoHelloNeighbourCall post from {0} to {1}", | ||
179 | thisRegion.RegionName, region.RegionName); | ||
180 | } | ||
181 | |||
182 | sr = new StreamReader(webResponse.GetResponseStream()); | ||
183 | //reply = sr.ReadToEnd().Trim(); | ||
184 | sr.ReadToEnd().Trim(); | ||
185 | //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply); | ||
186 | |||
187 | } | ||
188 | catch (Exception e) | ||
189 | { | ||
190 | m_log.WarnFormat( | ||
191 | "[NEIGHBOUR SERVICE CONNCTOR]: Exception on reply of DoHelloNeighbourCall from {0} back to {1}. Exception {2}{3}", | ||
192 | region.RegionName, thisRegion.RegionName, e.Message, e.StackTrace); | ||
193 | |||
194 | return false; | ||
195 | } | ||
196 | finally | ||
197 | { | ||
198 | if (sr != null) | ||
199 | sr.Close(); | ||
200 | } | ||
201 | |||
202 | return true; | ||
203 | } | ||
204 | } | ||
205 | } \ No newline at end of file | ||