OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SceneBase.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 System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 using System.Threading;
32 using OpenMetaverse;
33 using log4net;
34 using Nini.Config;
35 using OpenSim.Framework;
36 using OpenSim.Framework.Console;
37 
38 using OpenSim.Region.Framework.Interfaces;
40 
41 namespace OpenSim.Region.Framework.Scenes
42 {
43  public abstract class SceneBase : IScene
44  {
45  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46 
47 #pragma warning disable 414
48  private static readonly string LogHeader = "[SCENE]";
49 #pragma warning restore 414
50 
51  #region Events
52 
53  public event restart OnRestart;
54 
55  #endregion
56 
57  #region Fields
58 
59  public string Name { get { return RegionInfo.RegionName; } }
60 
61  public IConfigSource Config
62  {
63  get { return GetConfig(); }
64  }
65 
66  protected virtual IConfigSource GetConfig()
67  {
68  return null;
69  }
70 
74  public Dictionary<string, IRegionModuleBase> RegionModules
75  {
76  get { return m_regionModules; }
77  }
78  private Dictionary<string, IRegionModuleBase> m_regionModules = new Dictionary<string, IRegionModuleBase>();
79 
83  protected Dictionary<Type, List<object>> ModuleInterfaces = new Dictionary<Type, List<object>>();
84 
89  protected List<UUID> FormatsOffered = new List<UUID>();
90  protected Dictionary<object, List<UUID>> FormatsWanted = new Dictionary<object, List<UUID>>();
91 
92  protected Dictionary<string, object> ModuleAPIMethods = new Dictionary<string, object>();
93 
97  protected Dictionary<string, ICommander> m_moduleCommanders = new Dictionary<string, ICommander>();
98 
102  protected Dictionary<PCode, IEntityCreator> m_entityCreators = new Dictionary<PCode, IEntityCreator>();
103 
108  protected uint m_lastAllocatedLocalId = 720000;
109 
110  private readonly Mutex _primAllocateMutex = new Mutex(false);
111 
112  protected readonly ClientManager m_clientManager = new ClientManager();
113 
114  public bool LoginsEnabled
115  {
116  get
117  {
118  return m_loginsEnabled;
119  }
120 
121  set
122  {
123  if (m_loginsEnabled != value)
124  {
125  m_loginsEnabled = value;
126  EventManager.TriggerRegionLoginsStatusChange(this);
127  }
128  }
129  }
130  private bool m_loginsEnabled;
131 
132  public bool Ready
133  {
134  get
135  {
136  return m_ready;
137  }
138 
139  set
140  {
141  if (m_ready != value)
142  {
143  m_ready = value;
144  EventManager.TriggerRegionReadyStatusChange(this);
145  }
146  }
147  }
148  private bool m_ready;
149 
150  public float TimeDilation
151  {
152  get { return 1.0f; }
153  }
154 
156 
161 
167  {
168  get { return m_eventManager; }
169  }
171 
173  public ScenePermissions Permissions
174  {
175  get { return m_permissions; }
176  }
177 
178  /* Used by the loadbalancer plugin on GForge */
181  {
182  get { return m_regStatus; }
183  set { m_regStatus = value; }
184  }
185 
186  #endregion
187 
188  public SceneBase(RegionInfo regInfo)
189  {
190  RegionInfo = regInfo;
191  }
192 
193  #region Update Methods
194 
203  public abstract void Update(int frames);
204 
205  #endregion
206 
207  #region Terrain Methods
208 
212  public abstract void LoadWorldMap();
213 
218  public virtual void SendLayerData(IClientAPI RemoteClient)
219  {
220  // RemoteClient.SendLayerData(Heightmap.GetFloatsSerialised());
221  ITerrainModule terrModule = RequestModuleInterface<ITerrainModule>();
222  if (terrModule != null)
223  {
224  terrModule.PushTerrain(RemoteClient);
225  }
226  }
227 
228  #endregion
229 
230  #region Add/Remove Agent/Avatar
231 
232  public abstract ISceneAgent AddNewAgent(IClientAPI client, PresenceType type);
233 
234  public abstract bool CloseAgent(UUID agentID, bool force);
235 
236  public bool TryGetScenePresence(UUID agentID, out object scenePresence)
237  {
238  scenePresence = null;
239  ScenePresence sp = null;
240  if (TryGetScenePresence(agentID, out sp))
241  {
242  scenePresence = sp;
243  return true;
244  }
245 
246  return false;
247  }
248 
255  public abstract bool TryGetScenePresence(UUID agentID, out ScenePresence scenePresence);
256 
257  #endregion
258 
263  public virtual RegionInfo RegionInfo { get; private set; }
264 
265  #region admin stuff
266 
267  public abstract void OtherRegionUp(GridRegion otherRegion);
268 
269  public virtual string GetSimulatorVersion()
270  {
271  return "OpenSimulator Server";
272  }
273 
274  #endregion
275 
276  #region Shutdown
277 
281  public virtual void Close()
282  {
283  try
284  {
285  EventManager.TriggerShutdown();
286  }
287  catch (Exception e)
288  {
289  m_log.Error(string.Format("[SCENE]: SceneBase.cs: Close() - Failed with exception ", e));
290  }
291  }
292 
293  #endregion
294 
299  public uint AllocateLocalId()
300  {
301  uint myID;
302 
303  _primAllocateMutex.WaitOne();
304  myID = ++m_lastAllocatedLocalId;
305  _primAllocateMutex.ReleaseMutex();
306 
307  return myID;
308  }
309 
310  #region Module Methods
311 
317  public void AddRegionModule(string name, IRegionModuleBase module)
318  {
319  if (!RegionModules.ContainsKey(name))
320  {
321  RegionModules.Add(name, module);
322  }
323  }
324 
325  public void RemoveRegionModule(string name)
326  {
327  RegionModules.Remove(name);
328  }
329 
334  public void RegisterModuleCommander(ICommander commander)
335  {
336  lock (m_moduleCommanders)
337  {
338  m_moduleCommanders.Add(commander.Name, commander);
339  }
340  }
341 
346  public void UnregisterModuleCommander(string name)
347  {
348  lock (m_moduleCommanders)
349  {
350  ICommander commander;
351  if (m_moduleCommanders.TryGetValue(name, out commander))
352  m_moduleCommanders.Remove(name);
353  }
354  }
355 
361  public ICommander GetCommander(string name)
362  {
363  lock (m_moduleCommanders)
364  {
365  if (m_moduleCommanders.ContainsKey(name))
366  return m_moduleCommanders[name];
367  }
368 
369  return null;
370  }
371 
372  public Dictionary<string, ICommander> GetCommanders()
373  {
374  return m_moduleCommanders;
375  }
376 
377  public List<UUID> GetFormatsOffered()
378  {
379  List<UUID> ret = new List<UUID>(FormatsOffered);
380 
381  return ret;
382  }
383 
384  protected void CheckAndAddAgentDataFormats(object mod)
385  {
386  if (!(mod is IAgentStatefulModule))
387  return;
388 
389  IAgentStatefulModule m = (IAgentStatefulModule)mod;
390 
391  List<UUID> renderFormats = m.GetRenderStateFormats();
392  List<UUID> acceptFormats = m.GetAcceptStateFormats();
393 
394  foreach (UUID render in renderFormats)
395  {
396  if (!(FormatsOffered.Contains(render)))
397  FormatsOffered.Add(render);
398  }
399 
400  if (acceptFormats.Count == 0)
401  return;
402 
403  if (FormatsWanted.ContainsKey(mod))
404  return;
405 
406  FormatsWanted[mod] = acceptFormats;
407  }
408 
415  public void RegisterModuleInterface<M>(M mod)
416  {
417 // m_log.DebugFormat("[SCENE BASE]: Registering interface {0}", typeof(M));
418 
419  List<Object> l = null;
420  if (!ModuleInterfaces.TryGetValue(typeof(M), out l))
421  {
422  l = new List<Object>();
423  ModuleInterfaces.Add(typeof(M), l);
424  }
425 
426  if (l.Count > 0)
427  return;
428 
429  l.Add(mod);
430 
431  CheckAndAddAgentDataFormats(mod);
432 
433  if (mod is IEntityCreator)
434  {
435  IEntityCreator entityCreator = (IEntityCreator)mod;
436  foreach (PCode pcode in entityCreator.CreationCapabilities)
437  {
438  m_entityCreators[pcode] = entityCreator;
439  }
440  }
441  }
442 
443  public void UnregisterModuleInterface<M>(M mod)
444  {
445  // We can't unregister agent stateful modules because
446  // that would require much more data to be held about formats
447  // and would make that code slower and less efficient.
448  // No known modules are unregistered anyway, ever, unless
449  // the simulator shuts down anyway.
450  if (mod is IAgentStatefulModule)
451  return;
452 
453  List<Object> l;
454  if (ModuleInterfaces.TryGetValue(typeof(M), out l))
455  {
456  if (l.Remove(mod))
457  {
458  if (mod is IEntityCreator)
459  {
460  IEntityCreator entityCreator = (IEntityCreator)mod;
461  foreach (PCode pcode in entityCreator.CreationCapabilities)
462  {
463  m_entityCreators[pcode] = null;
464  }
465  }
466  }
467  }
468  }
469 
470  public void StackModuleInterface<M>(M mod)
471  {
472  List<Object> l;
473  if (ModuleInterfaces.ContainsKey(typeof(M)))
474  l = ModuleInterfaces[typeof(M)];
475  else
476  l = new List<Object>();
477 
478  if (l.Contains(mod))
479  return;
480 
481  l.Add(mod);
482 
483  CheckAndAddAgentDataFormats(mod);
484 
485  if (mod is IEntityCreator)
486  {
487  IEntityCreator entityCreator = (IEntityCreator)mod;
488  foreach (PCode pcode in entityCreator.CreationCapabilities)
489  {
490  m_entityCreators[pcode] = entityCreator;
491  }
492  }
493 
494  ModuleInterfaces[typeof(M)] = l;
495  }
496 
501  public T RequestModuleInterface<T>()
502  {
503  if (ModuleInterfaces.ContainsKey(typeof(T)) &&
504  (ModuleInterfaces[typeof(T)].Count > 0))
505  return (T)ModuleInterfaces[typeof(T)][0];
506  else
507  return default(T);
508  }
509 
514  public T[] RequestModuleInterfaces<T>()
515  {
516  if (ModuleInterfaces.ContainsKey(typeof(T)))
517  {
518  List<T> ret = new List<T>();
519 
520  foreach (Object o in ModuleInterfaces[typeof(T)])
521  ret.Add((T)o);
522  return ret.ToArray();
523  }
524  else
525  {
526  return new T[] {};
527  }
528  }
529 
530  #endregion
531 
540  public void AddCommand(IRegionModuleBase module, string command, string shorthelp, string longhelp, CommandDelegate callback)
541  {
542  AddCommand(module, command, shorthelp, longhelp, string.Empty, callback);
543  }
544 
559  public void AddCommand(
560  string category, IRegionModuleBase module, string command, string shorthelp, string longhelp, CommandDelegate callback)
561  {
562  AddCommand(category, module, command, shorthelp, longhelp, string.Empty, callback);
563  }
564 
574  public void AddCommand(IRegionModuleBase module, string command, string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
575  {
576  string moduleName = "";
577 
578  if (module != null)
579  moduleName = module.Name;
580 
581  AddCommand(moduleName, module, command, shorthelp, longhelp, descriptivehelp, callback);
582  }
583 
596  public void AddCommand(
597  string category, IRegionModuleBase module, string command,
598  string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
599  {
600  if (MainConsole.Instance == null)
601  return;
602 
603  bool shared = false;
604 
605  if (module != null)
606  shared = module is ISharedRegionModule;
607 
608  MainConsole.Instance.Commands.AddCommand(
609  category, shared, command, shorthelp, longhelp, descriptivehelp, callback);
610  }
611 
612  public virtual ISceneObject DeserializeObject(string representation)
613  {
614  return null;
615  }
616 
617  public virtual bool AllowScriptCrossings
618  {
619  get { return false; }
620  }
621 
622  public virtual void Start()
623  {
624  }
625 
626  public void Restart()
627  {
628  // This has to be here to fire the event
629  restart handlerPhysicsCrash = OnRestart;
630  if (handlerPhysicsCrash != null)
631  handlerPhysicsCrash(RegionInfo);
632  }
633 
634  public abstract bool CheckClient(UUID agentID, System.Net.IPEndPoint ep);
635  }
636 }
uint AllocateLocalId()
Returns a new unallocated local ID
Definition: SceneBase.cs:299
OpenSim.Services.Interfaces.GridRegion GridRegion
Definition: SceneBase.cs:39
void AddCommand(IRegionModuleBase module, string command, string shorthelp, string longhelp, CommandDelegate callback)
Call this from a region module to add a command to the OpenSim console.
Definition: SceneBase.cs:540
ICommander GetCommander(string name)
Get a module commander
Definition: SceneBase.cs:361
void AddRegionModule(string name, IRegionModuleBase module)
Add a region-module to this scene. TODO: This will replace AddModule in the future.
Definition: SceneBase.cs:317
delegate void CommandDelegate(string module, string[] cmd)
Dictionary< string, ICommander > GetCommanders()
Definition: SceneBase.cs:372
virtual void Start()
Start the scene and associated scripts within it.
Definition: SceneBase.cs:622
virtual IConfigSource GetConfig()
Definition: SceneBase.cs:66
bool TryGetScenePresence(UUID agentID, out object scenePresence)
Definition: SceneBase.cs:236
void UnregisterModuleCommander(string name)
Unregister a module commander and all its commands
Definition: SceneBase.cs:346
virtual void SendLayerData(IClientAPI RemoteClient)
Send the region heightmap to the client
Definition: SceneBase.cs:218
void RegisterModuleCommander(ICommander commander)
Register a module commander.
Definition: SceneBase.cs:334
PresenceType
Indicate the type of ScenePresence.
Definition: PresenceType.cs:34
virtual ISceneObject DeserializeObject(string representation)
Definition: SceneBase.cs:612
An agent in the scene.
Definition: ISceneAgent.cs:39
static ICommandConsole Instance
Definition: MainConsole.cs:35
void AddCommand(string category, IRegionModuleBase module, string command, string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
Call this from a region module to add a command to the OpenSim console.
Definition: SceneBase.cs:596
Interface to a class that is capable of creating entities
delegate void restart(RegionInfo thisRegion)
A class for triggering remote scene events.
Definition: EventManager.cs:44
virtual void Close()
Tidy before shutdown
Definition: SceneBase.cs:281
Maps from client AgentID and RemoteEndPoint values to IClientAPI references for all of the connected ...
void AddCommand(string category, IRegionModuleBase module, string command, string shorthelp, string longhelp, CommandDelegate callback)
Call this from a region module to add a command to the OpenSim console.
Definition: SceneBase.cs:559
OpenSim.Services.Interfaces.GridRegion GridRegion
void AddCommand(IRegionModuleBase module, string command, string shorthelp, string longhelp, string descriptivehelp, CommandDelegate callback)
Call this from a region module to add a command to the OpenSim console.
Definition: SceneBase.cs:574