diff options
Diffstat (limited to 'OpenSim/Region/Environment/Modules')
3 files changed, 241 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/Grid/Interregion/IInterregionModule.cs b/OpenSim/Region/Environment/Modules/Grid/Interregion/IInterregionModule.cs new file mode 100644 index 0000000..ac4062b --- /dev/null +++ b/OpenSim/Region/Environment/Modules/Grid/Interregion/IInterregionModule.cs | |||
@@ -0,0 +1,16 @@ | |||
1 | using OpenSim.Framework; | ||
2 | using OpenSim.Region.Environment.Modules.Communications.Interregion; | ||
3 | using OpenSim.Region.Environment.Scenes; | ||
4 | |||
5 | namespace OpenSim.Region.Environment.Modules.Grid.Interregion | ||
6 | { | ||
7 | public interface IInterregionModule | ||
8 | { | ||
9 | void RegisterMethod<T>(T e); | ||
10 | bool HasInterface<T>(Location loc); | ||
11 | T RequestInterface<T>(Location loc); | ||
12 | T[] RequestInterface<T>(); | ||
13 | Location GetLocationByDirection(Scene scene, InterregionModule.Direction dir); | ||
14 | void internal_CreateRemotingObjects(); | ||
15 | } | ||
16 | } \ No newline at end of file | ||
diff --git a/OpenSim/Region/Environment/Modules/Grid/Interregion/InterregionModule.cs b/OpenSim/Region/Environment/Modules/Grid/Interregion/InterregionModule.cs new file mode 100644 index 0000000..1a77ac8 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/Grid/Interregion/InterregionModule.cs | |||
@@ -0,0 +1,175 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Runtime.Remoting; | ||
4 | using System.Runtime.Remoting.Channels; | ||
5 | using System.Runtime.Remoting.Channels.Tcp; | ||
6 | using Nini.Config; | ||
7 | using OpenSim.Framework; | ||
8 | using OpenSim.Region.Environment.Interfaces; | ||
9 | using OpenSim.Region.Environment.Modules.Grid.Interregion; | ||
10 | using OpenSim.Region.Environment.Scenes; | ||
11 | |||
12 | namespace OpenSim.Region.Environment.Modules.Communications.Interregion | ||
13 | { | ||
14 | public class InterregionModule : IInterregionModule, IRegionModule | ||
15 | { | ||
16 | #region Direction enum | ||
17 | |||
18 | public enum Direction | ||
19 | { | ||
20 | North, | ||
21 | NorthEast, | ||
22 | East, | ||
23 | SouthEast, | ||
24 | South, | ||
25 | SouthWest, | ||
26 | West, | ||
27 | NorthWest | ||
28 | } | ||
29 | |||
30 | #endregion | ||
31 | |||
32 | private readonly Dictionary<Type, Object> m_interfaces = new Dictionary<Type, object>(); | ||
33 | private readonly List<Location> m_myLocations = new List<Location>(); | ||
34 | |||
35 | private readonly Dictionary<Location, string[]> m_neighbourInterfaces = new Dictionary<Location, string[]>(); | ||
36 | private readonly Dictionary<Location, RemotingObject> m_neighbourRemote = new Dictionary<Location, RemotingObject>(); | ||
37 | private IConfigSource m_config; | ||
38 | private RemotingObject m_myRemote; | ||
39 | |||
40 | private Object m_lockObject = new object(); | ||
41 | private TcpChannel m_tcpChannel; | ||
42 | private int m_tcpPort = 10101; | ||
43 | private bool m_enabled = false; | ||
44 | |||
45 | #region IRegionModule Members | ||
46 | |||
47 | //TODO: This prevents us from registering new scenes after PostInitialise if we want comms updated. | ||
48 | public void Initialise(Scene scene, IConfigSource source) | ||
49 | { | ||
50 | if (m_enabled) | ||
51 | { | ||
52 | m_myLocations.Add(new Location((int) scene.RegionInfo.RegionLocX, | ||
53 | (int) scene.RegionInfo.RegionLocY)); | ||
54 | m_config = source; | ||
55 | |||
56 | scene.RegisterModuleInterface<IInterregionModule>(this); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | //TODO: This prevents us from registering new scenes after PostInitialise if we want comms updated. | ||
61 | public void PostInitialise() | ||
62 | { | ||
63 | if (m_enabled) | ||
64 | { | ||
65 | try | ||
66 | { | ||
67 | m_tcpPort = m_config.Configs["Comms"].GetInt("remoting_port", m_tcpPort); | ||
68 | } | ||
69 | catch | ||
70 | { | ||
71 | } | ||
72 | |||
73 | internal_CreateRemotingObjects(); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | public void Close() | ||
78 | { | ||
79 | ChannelServices.UnregisterChannel(m_tcpChannel); | ||
80 | } | ||
81 | |||
82 | public string Name | ||
83 | { | ||
84 | get { return "InterregionModule"; } | ||
85 | } | ||
86 | |||
87 | public bool IsSharedModule | ||
88 | { | ||
89 | get { return true; } | ||
90 | } | ||
91 | |||
92 | #endregion | ||
93 | |||
94 | public void internal_CreateRemotingObjects() | ||
95 | { | ||
96 | lock (m_lockObject) | ||
97 | { | ||
98 | if (m_tcpChannel == null) | ||
99 | { | ||
100 | m_myRemote = new RemotingObject(m_interfaces, m_myLocations.ToArray()); | ||
101 | m_tcpChannel = new TcpChannel(m_tcpPort); | ||
102 | |||
103 | ChannelServices.RegisterChannel(m_tcpChannel, false); | ||
104 | RemotingServices.Marshal(m_myRemote, "OpenSimRemote2", typeof (RemotingObject)); | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | |||
109 | public void RegisterRemoteRegion(string uri) | ||
110 | { | ||
111 | RegisterRemotingInterface((RemotingObject) Activator.GetObject(typeof (RemotingObject), uri)); | ||
112 | } | ||
113 | |||
114 | private void RegisterRemotingInterface(RemotingObject remote) | ||
115 | { | ||
116 | Location[] locs = remote.GetLocations(); | ||
117 | string[] interfaces = remote.GetInterfaces(); | ||
118 | foreach (Location loc in locs) | ||
119 | { | ||
120 | m_neighbourInterfaces[loc] = interfaces; | ||
121 | m_neighbourRemote[loc] = remote; | ||
122 | } | ||
123 | } | ||
124 | |||
125 | public void RegisterMethod<T>(T e) | ||
126 | { | ||
127 | m_interfaces[typeof (T)] = e; | ||
128 | } | ||
129 | |||
130 | public bool HasInterface<T>(Location loc) | ||
131 | { | ||
132 | foreach (string val in m_neighbourInterfaces[loc]) | ||
133 | { | ||
134 | if (val == typeof (T).FullName) | ||
135 | { | ||
136 | return true; | ||
137 | } | ||
138 | } | ||
139 | return false; | ||
140 | } | ||
141 | |||
142 | public T RequestInterface<T>(Location loc) | ||
143 | { | ||
144 | if (m_neighbourRemote.ContainsKey(loc)) | ||
145 | { | ||
146 | return m_neighbourRemote[loc].RequestInterface<T>(); | ||
147 | } | ||
148 | else | ||
149 | { | ||
150 | throw new IndexOutOfRangeException("No neighbour availible at that location"); | ||
151 | } | ||
152 | } | ||
153 | |||
154 | public T[] RequestInterface<T>() | ||
155 | { | ||
156 | List<T> m_t = new List<T>(); | ||
157 | foreach (RemotingObject remote in m_neighbourRemote.Values) | ||
158 | { | ||
159 | try | ||
160 | { | ||
161 | m_t.Add(remote.RequestInterface<T>()); | ||
162 | } | ||
163 | catch (NotSupportedException) | ||
164 | { | ||
165 | } | ||
166 | } | ||
167 | return m_t.ToArray(); | ||
168 | } | ||
169 | |||
170 | public Location GetLocationByDirection(Scene scene, Direction dir) | ||
171 | { | ||
172 | return new Location(0, 0); | ||
173 | } | ||
174 | } | ||
175 | } \ No newline at end of file | ||
diff --git a/OpenSim/Region/Environment/Modules/Grid/Interregion/RemotingObject.cs b/OpenSim/Region/Environment/Modules/Grid/Interregion/RemotingObject.cs new file mode 100644 index 0000000..2c72bb9 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/Grid/Interregion/RemotingObject.cs | |||
@@ -0,0 +1,50 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using OpenSim.Framework; | ||
4 | |||
5 | namespace OpenSim.Region.Environment.Modules.Grid.Interregion | ||
6 | { | ||
7 | public class RemotingObject : MarshalByRefObject | ||
8 | { | ||
9 | private readonly Location[] m_coords; | ||
10 | private readonly Dictionary<Type, Object> m_interfaces = new Dictionary<Type, object>(); | ||
11 | |||
12 | public RemotingObject(Dictionary<Type, Object> myInterfaces, Location[] coords) | ||
13 | { | ||
14 | m_interfaces = myInterfaces; | ||
15 | m_coords = coords; | ||
16 | } | ||
17 | |||
18 | public Location[] GetLocations() | ||
19 | { | ||
20 | return (Location[]) m_coords.Clone(); | ||
21 | } | ||
22 | |||
23 | public string[] GetInterfaces() | ||
24 | { | ||
25 | string[] interfaces = new string[m_interfaces.Count]; | ||
26 | int i = 0; | ||
27 | |||
28 | foreach (KeyValuePair<Type, object> pair in m_interfaces) | ||
29 | { | ||
30 | interfaces[i++] = pair.Key.FullName; | ||
31 | } | ||
32 | |||
33 | return interfaces; | ||
34 | } | ||
35 | |||
36 | /// <summary> | ||
37 | /// Returns a registered interface availible to neighbouring regions. | ||
38 | /// </summary> | ||
39 | /// <typeparam name="T">The type of interface you wish to request</typeparam> | ||
40 | /// <returns>A MarshalByRefObject inherited from this region inheriting the interface requested.</returns> | ||
41 | /// <remarks>All registered interfaces <b>MUST</b> inherit from MarshalByRefObject and use only serialisable types.</remarks> | ||
42 | public T RequestInterface<T>() | ||
43 | { | ||
44 | if (m_interfaces.ContainsKey(typeof (T))) | ||
45 | return (T) m_interfaces[typeof (T)]; | ||
46 | |||
47 | throw new NotSupportedException("No such interface registered."); | ||
48 | } | ||
49 | } | ||
50 | } \ No newline at end of file | ||