Ejemplo n.º 1
0
	def setUp(self):
		self.scene = Scene()
		self.scene.locX = 10
		self.scene.locY = -20
		self.scene.children.append(create_plane())
		self.scene.children.append(create_box())
		self.scene.backgroundColor = [0.5, 0, 0.23]
		self.scene_json = to_json(self.scene)
		self.parsed_json = simplejson.loads(self.scene_json)
Ejemplo 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
Ejemplo n.º 3
0
	def handle_incoming(self):
		"""A loop which handles incoming messages.  Does not return until the connection closes."""
		while not self.disconnected:
			incoming_data = receive_web_socket_message(self.client_socket)
			if incoming_data == None: 
				break
			#print 'Incoming:', incoming_data
			event = events.parse_event_json(incoming_data)
			if not event:
				print "Could not read an event from the data: %s" % data
				continue

			response_event = None
			if isinstance(event, events.AuthenticationRequest):
				user = self.user_from_session_key(event.session_id)
				if user.is_authenticated():
					#print "Authed: %s" % user.username
					self.user = user
					response_event = events.AuthenticationResponse(True, user.username)
				else:
					print "Auth failure with session id %s" % event.session_id
					response_event = events.AuthenticationResponse(False)
			elif isinstance(event, events.PoolInfoRequest):
				if not self.user:
					print 'Attemped unauthed pool info request'
				else:
					space_infos = [{'name':sim.space.name, 'url':sim.space.get_absolute_url()} for sim in self.server.sim_pool.simulators]
					response_event = events.PoolInfo({ 'space_infos':space_infos })
			elif isinstance(event, events.JoinSpaceRequest):
				if not self.user:
					print 'Attemped unauthed join space'
					response_event = events.JoinSpaceResponse(event.space_id)
				else:
					try:
						space = Space.objects.get(pk=event.space_id)
						sim = self.server.sim_pool.get_simulator(space.id)
						if not sim: print 'Could not find a sim for space: %s' % space.id
						allow_join, space_member = Space.objects.get_membership(space, self.user)
						response_event = events.JoinSpaceResponse(space.id, allow_join)
						if allow_join:
							self.space_id = space.id
							try:
								response_event.scene_doc = to_json(sim.scene)
							except:
								print "Could not log in: %s" % pprint.pformat(traceback.format_exc())
						else:
							print 'Attempted disallowed join space'
						if space_member:
							response_event.is_member = True
							response_event.is_admin = space_member.is_admin
							response_event.is_editor = space_member.is_editor
					except:
						traceback.print_exc()
						response_event = events.JoinSpaceResponse(event.space_id)
			elif isinstance(event, events.TemplateUpdated):
				if self.user and self.user.is_staff:
					event.connection = self
					for simulator in self.server.sim_pool.simulators:
						simulator.event_queue.put(event)
			elif self.space_id:
				simulator = self.server.sim_pool.get_simulator(self.space_id)
				if simulator:
					event.connection = self
					simulator.event_queue.put(event)
				else:
					print "Received an event for an absent simulator %s" % event
			else:
				print "Received unhandled event %s" % event.to_json()

			if response_event:
				#print 'Outgoing: ', to_json(response_event)
				self.client_socket.send('\x00')
				self.client_socket.send(response_event.to_json())
				self.client_socket.send('\xff')
		self.finish()