Esempio n. 1
0
	def run(self):
		"""The simulation loop"""
		if not self.state == Simulator.ready:
			print "Tried to run a simulator in an unready state: %s" % self
			return
		self.state = Simulator.running
		self.should_run = True
		while self.should_run:
			try:
				event = self.event_queue.get(block=True, timeout=5)
			except Queue.Empty:
				self.pool.sim_server.send_space_event(self.space.id, Heartbeat())
				continue
			
			if not self.should_run: return
			
			if event.event_name() == 'AddUserRequest':
				user_node = self.scene.get_user(event.username)
				if user_node is None:
					user_node = Group()
					user_node.username = event.username
					user_node.set_loc(event.position)
					user_node.set_quat(event.orientation)
					self.scene.children.append(user_node)
					self.pool.sim_server.send_space_event(self.space.id, NodeAdded(self.space.id, self.scene.uid, to_json(user_node)))
				else:
					print "Already have a user with id", event.username

			elif event.event_name() == 'UserExited':
				#print 'User exited', event.username
				user_node = self.scene.get_user(event.username)
				if user_node:
					self.scene.remove_node(user_node.uid)
					self.pool.sim_server.send_space_event(self.space.id, NodeRemoved(self.space.id, user_node.uid))

			elif event.event_name() == 'UserMessage':
				if event.connection.user != None and event.username == event.connection.user.username:
					self.pool.sim_server.send_space_event(self.space.id, UserMessage(self.space.id, event.username, event.message))

			elif event.event_name() == 'UserMoveRequest':
				if event.connection.user != None and event.username == event.connection.user.username:
					user_node = self.scene.get_user(event.username)
					if user_node == None:
						print "No such user node: %s" % event.username
					else:
						user_node.set_loc(event.position)
						user_node.set_quat(event.orientation)
						response = PlaceableMoved(self.space.id, user_node.uid, user_node.loc, user_node.quat)
						self.pool.sim_server.send_space_event(self.space.id, response)

			elif event.event_name() == 'TemplateUpdated':
				if event.connection.user != None and event.connection.user.is_staff:
					self.pool.sim_server.send_space_event(self.space.id, TemplateUpdated(self.space.id, event.template_id, event.url, event.key))

			else:
				print "Unknown event: %s" % event.event_name()

		print "Exiting %s %s"  % (self, datetime.datetime.now())
Esempio n. 2
0
	def load(self, space_dir_path, owner):
		space_name = os.path.basename(space_dir_path)
		things_path = os.path.join(space_dir_path, SPACE_TEMPLATE_FILE_NAME)
		if not os.path.isfile(things_path):
			print 'No things.csv in space %s, ignoring' % space_name
			return None
		properties_path = os.path.join(space_dir_path, SPACE_PROPERTIES_FILE_NAME)
		if not os.path.isfile(properties_path):
			print 'No %s in space %s, ignoring' % (SPACE_PROPERTIES_FILE_NAME, space_name)
			return None
		config = ConfigParser.ConfigParser()
		config.readfp(open(properties_path))
		if config.has_option(SPACE_INFO_SECTION, DEFAULT_BODY_OPTION):
			body_name = config.get(SPACE_INFO_SECTION, DEFAULT_BODY_OPTION)
		else:
			print 'No %s option in %s.  Cannot create the space.' % (DEFAULT_BODY_OPTION, SPACE_PROPERTIES_FILE_NAME)
			return None
		if Template.objects.filter(name=body_name).count() != 1:
			print 'An unknown template is specified for default-body: %s' % body_name
			return None
		body_template = Template.objects.get(name=body_name)
		space, created = Space.objects.get_or_create(name=space_name, default_body=body_template, slug=slugify(space_name))
		space.add_member(owner, is_admin=True, is_editor=True)
		
		things_reader = csv.reader(open(things_path))
		scene = Scene()
		for thing_row in things_reader:
			template_name = thing_row[0]
			if Template.objects.filter(name=template_name).count() != 1:
				print 'things.csv references an unknown template: %s' % template_name
				continue
			template = Template.objects.get(name=template_name)
			node = Group()
			node.group_template = GroupTemplate(template_id=template.id, name=template.name)
			node.set_loc([float(thing_row[1]), float(thing_row[2]), float(thing_row[3])])
			node.set_quat([float(thing_row[4]), float(thing_row[5]), float(thing_row[6]), float(thing_row[7])])
			node.set_scale([float(thing_row[8]), float(thing_row[9]), float(thing_row[10])]) 
			#TODO hook the template data and ID
			scene.children.append(node)
		space.scene_document = to_json(scene)
		space.save()
		return space