rxmain.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # rxmain.py
  2. #print "rxmain.................................."
  3. import gc
  4. import time
  5. import sys
  6. from threading import Thread
  7. # csharp imports
  8. import System
  9. # python imports
  10. import rxactor
  11. import rxworld
  12. import rxeventmanager
  13. import rxevent
  14. import rxworldinfo
  15. # Global variables
  16. globals()["MainScriptThread"] = None
  17. globals()["MyWorld"] = None
  18. globals()["MyWorldInfo"] = None
  19. if globals().has_key('objCSharp'):
  20. globals()["MyWorld"] = rxworld.World(objCSharp)
  21. else:
  22. print "ERROR, OpenSim serverobject objCSharp not found!"
  23. globals()["MyEventManager"] = rxeventmanager.EventManager(globals()["MyWorld"])
  24. # Main thread
  25. class MainThread(Thread):
  26. def __init__ (self):
  27. Thread.__init__(self)
  28. self.prevticktime = 0
  29. self.currtime = 0
  30. self.sleeptime = 0
  31. self.Status = 1
  32. def run(self):
  33. print "Python: Started script thread"
  34. try:
  35. for TempId,TempObj in globals()["MyWorld"].AllActors.iteritems():
  36. if(isinstance(TempObj,rxworldinfo.WorldInfo)):
  37. globals()["MyWorldInfo"] = TempObj
  38. break
  39. except:
  40. print "rxmain,MainThread.run, worldinfo", sys.exc_info()[0]
  41. print sys.exc_info()[1]
  42. print sys.exc_info()[2]
  43. try:
  44. for TempId,TempObj in globals()["MyWorld"].AllActors.iteritems():
  45. TempObj.EventCreated()
  46. except:
  47. print "rxmain,MainThread.run, EventCreated on actors:", sys.exc_info()[0]
  48. print sys.exc_info()[1]
  49. print sys.exc_info()[2]
  50. #print "Printing actor list..."
  51. #print "Length is ",len(MyWorld.AllActors)
  52. #for iid, iactor in MyWorld.AllActors.iteritems():
  53. # print iid,iactor.Id
  54. while self.Status == 1:
  55. try:
  56. # Windows time.clock has better resolution than time.time
  57. self.currtime = time.clock()
  58. globals()["MyEventManager"].CurrTime = self.currtime
  59. timedelta = self.currtime - self.prevticktime
  60. if timedelta < 0:
  61. timedelta = 0
  62. # try to run 25 ticks per second
  63. self.sleeptime = self.currtime - self.prevticktime
  64. if self.sleeptime < 0.033:
  65. self.sleeptime = 0.033 - self.sleeptime
  66. time.sleep(self.sleeptime)
  67. #print "SLEEP!",self.sleeptime
  68. self.prevticktime = self.currtime
  69. # Process events
  70. globals()["MyEventManager"].SwitchActiveList()
  71. globals()["MyEventManager"].ProcessEvents(timedelta)
  72. # Tick and Timer events
  73. globals()["MyEventManager"].CallTimeEvents(timedelta)
  74. except:
  75. print "rxmain,MainThread.run:", sys.exc_info()[0]
  76. print sys.exc_info()[1]
  77. print sys.exc_info()[2]
  78. try:
  79. if self.Status != 1:
  80. globals()["MyEventManager"].ShutDown()
  81. del globals()["MyEventManager"]
  82. globals()["MyWorld"].ShutDown()
  83. del globals()["MyWorld"]
  84. del globals()["objCSharp"]
  85. gc.collect()
  86. except:
  87. print "rxmain,MainThread.run end:", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  88. self.Status = 3
  89. print "Python: Ended script thread"
  90. def StartMainThread():
  91. gc.enable()
  92. globals()["MainScriptThread"] = MainThread()
  93. globals()["MainScriptThread"].start()
  94. def StopMainThread():
  95. globals()["MainScriptThread"].Status = 2
  96. i = 0
  97. while i < 10:
  98. time.sleep(1)
  99. if globals()["MainScriptThread"].Status == 3:
  100. del globals()["MainScriptThread"]
  101. break;
  102. else:
  103. i = i+1
  104. print "StopMainThread Finished"
  105. # Called by c#, creates actors as they are created on the server.
  106. def CreateActorOfClass(vId,vClassName,vTag):
  107. strid = str(vId)
  108. strtag = str(vTag)
  109. # If actor with same id exists, get rid of it.
  110. try:
  111. if globals()["MyWorld"].AllActors.has_key(strid):
  112. globals()["MyEventManager"].DeleteActor(strid)
  113. except:
  114. print "rxmain,CreateActorOfClass,del:", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  115. return
  116. # Try to create the new object using the class. The class name might be bogus.
  117. try:
  118. TempCommand = "tempactor = " + vClassName.GetScriptClassName() + "(" + str(vId) + ")"
  119. exec(TempCommand)
  120. except:
  121. print "rxmain,CreateActorOfClass, bad class:",sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  122. return
  123. # Add actor
  124. try:
  125. tempactor.MyWorld = globals()["MyWorld"]
  126. globals()["MyWorld"].AllActors[strid] = tempactor;
  127. tempactor.MyTag = strtag
  128. # If engine is already running, call EventPreCreated and EventCreated on the actor in the next loop.
  129. if(globals()["MainScriptThread"] != None and globals()["MainScriptThread"].Status == 1):
  130. CreateEventWithName("add_entity",strid)
  131. except:
  132. print "rxmain,CreateActorOfClass,add", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  133. # Called by c#, creates events happening on the server.
  134. def CreateEventWithName(vName,*args):
  135. try:
  136. globals()["MyEventManager"].CreateEventWithName(vName,*args)
  137. except:
  138. print "rxmain,CreateEventWithName:", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]
  139. # Called by c#, gets the start location for agent
  140. def GetAvatarStartLocation():
  141. try:
  142. if(globals()["MyWorldInfo"] != None):
  143. return globals()["MyWorldInfo"].GetAvatarStartLocation()
  144. else:
  145. return None
  146. except:
  147. print "rxmain,GetAvatarStartLocation:", sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2]