OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
NeighbourServicesConnector.cs
Go to the documentation of this file.
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 
39 using OpenSim.Services.Interfaces;
40 using OpenMetaverse;
41 using OpenMetaverse.StructuredData;
42 
44 
45 namespace OpenSim.Services.Connectors
46 {
48  {
49  private static readonly ILog m_log =
50  LogManager.GetLogger(
51  MethodBase.GetCurrentMethod().DeclaringType);
52 
53  protected IGridService m_GridService = null;
54 
56  {
57  }
58 
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  Util.RegionHandleToWorldLoc(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.Warn(string.Format(
101  "[NEIGHBOUR SERVICES CONNECTOR]: Unable to parse uri {0} to send HelloNeighbour from {1} to {2}. Exception {3} ",
102  uri, thisRegion.RegionName, region.RegionName, e.Message), e);
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.Warn(string.Format(
120  "[NEIGHBOUR SERVICES CONNECTOR]: PackRegionInfoData failed for HelloNeighbour from {0} to {1}. Exception {2} ",
121  thisRegion.RegionName, region.RegionName, e.Message), e);
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.Warn(string.Format(
140  "[NEIGHBOUR SERVICES CONNECTOR]: Exception thrown on serialization of HelloNeighbour from {0} to {1}. Exception {2} ",
141  thisRegion.RegionName, region.RegionName, e.Message), e);
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.Dispose();
166  }
167 
168  // Let's wait for the response
169  //m_log.Info("[REST COMMS]: Waiting for a reply after DoHelloNeighbourCall");
170 
171  try
172  {
173  using (WebResponse webResponse = helloNeighbourRequest.GetResponse())
174  {
175  if (webResponse == null)
176  {
177  m_log.DebugFormat(
178  "[NEIGHBOUR SERVICES CONNECTOR]: Null reply on DoHelloNeighbourCall post from {0} to {1}",
179  thisRegion.RegionName, region.RegionName);
180  }
181 
182  using (Stream s = webResponse.GetResponseStream())
183  {
184  using (StreamReader sr = new StreamReader(s))
185  {
186  //reply = sr.ReadToEnd().Trim();
187  sr.ReadToEnd().Trim();
188  //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply);
189  }
190  }
191  }
192  }
193  catch (Exception e)
194  {
195  m_log.Warn(string.Format(
196  "[NEIGHBOUR SERVICES CONNECTOR]: Exception on reply of DoHelloNeighbourCall from {0} back to {1}. Exception {2} ",
197  region.RegionName, thisRegion.RegionName, e.Message), e);
198 
199  return false;
200  }
201 
202  return true;
203  }
204  }
205 }
uint HttpPort
The port by which http communication occurs with the region (most noticeably, CAPS communication) ...
Definition: RegionInfo.cs:382
OpenMetaverse.StructuredData.OSDMap OSDMap
OpenSim.Services.Interfaces.GridRegion GridRegion
bool DoHelloNeighbourCall(GridRegion region, RegionInfo thisRegion)
virtual GridRegion HelloNeighbour(ulong regionHandle, RegionInfo thisRegion)