diff options
Diffstat (limited to 'OpenSim/Services/Connectors/Neighbour/NeighbourServicesConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/Neighbour/NeighbourServicesConnector.cs | 206 |
1 files changed, 206 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..888b072 --- /dev/null +++ b/OpenSim/Services/Connectors/Neighbour/NeighbourServicesConnector.cs | |||
@@ -0,0 +1,206 @@ | |||
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 | UTF8Encoding str = new UTF8Encoding(); | ||
136 | buffer = str.GetBytes(strBuffer); | ||
137 | } | ||
138 | catch (Exception e) | ||
139 | { | ||
140 | m_log.WarnFormat( | ||
141 | "[NEIGHBOUR SERVICE CONNCTOR]: Exception thrown on serialization of HelloNeighbour from {0} to {1}. Exception {2}{3}", | ||
142 | thisRegion.RegionName, region.RegionName, e.Message, e.StackTrace); | ||
143 | |||
144 | return false; | ||
145 | } | ||
146 | |||
147 | Stream os = null; | ||
148 | try | ||
149 | { // send the Post | ||
150 | helloNeighbourRequest.ContentLength = buffer.Length; //Count bytes to send | ||
151 | os = helloNeighbourRequest.GetRequestStream(); | ||
152 | os.Write(buffer, 0, strBuffer.Length); //Send it | ||
153 | //m_log.InfoFormat("[REST COMMS]: Posted HelloNeighbour request to remote sim {0}", uri); | ||
154 | } | ||
155 | catch (Exception e) | ||
156 | { | ||
157 | m_log.WarnFormat( | ||
158 | "[NEIGHBOUR SERVICE CONNCTOR]: Unable to send HelloNeighbour from {0} to {1}. Exception {2}{3}", | ||
159 | thisRegion.RegionName, region.RegionName, e.Message, e.StackTrace); | ||
160 | |||
161 | return false; | ||
162 | } | ||
163 | finally | ||
164 | { | ||
165 | if (os != null) | ||
166 | os.Close(); | ||
167 | } | ||
168 | |||
169 | // Let's wait for the response | ||
170 | //m_log.Info("[REST COMMS]: Waiting for a reply after DoHelloNeighbourCall"); | ||
171 | |||
172 | StreamReader sr = null; | ||
173 | try | ||
174 | { | ||
175 | WebResponse webResponse = helloNeighbourRequest.GetResponse(); | ||
176 | if (webResponse == null) | ||
177 | { | ||
178 | m_log.DebugFormat( | ||
179 | "[REST COMMS]: Null reply on DoHelloNeighbourCall post from {0} to {1}", | ||
180 | thisRegion.RegionName, region.RegionName); | ||
181 | } | ||
182 | |||
183 | sr = new StreamReader(webResponse.GetResponseStream()); | ||
184 | //reply = sr.ReadToEnd().Trim(); | ||
185 | sr.ReadToEnd().Trim(); | ||
186 | //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply); | ||
187 | |||
188 | } | ||
189 | catch (Exception e) | ||
190 | { | ||
191 | m_log.WarnFormat( | ||
192 | "[NEIGHBOUR SERVICE CONNCTOR]: Exception on reply of DoHelloNeighbourCall from {0} back to {1}. Exception {2}{3}", | ||
193 | region.RegionName, thisRegion.RegionName, e.Message, e.StackTrace); | ||
194 | |||
195 | return false; | ||
196 | } | ||
197 | finally | ||
198 | { | ||
199 | if (sr != null) | ||
200 | sr.Close(); | ||
201 | } | ||
202 | |||
203 | return true; | ||
204 | } | ||
205 | } | ||
206 | } \ No newline at end of file | ||