예제 #1
0
파일: events.py 프로젝트: TrevorFSmith/wind
	def to_json(self):
		self.type = self.__class__.__name__
		return to_json(self)
예제 #2
0
파일: server.py 프로젝트: TrevorFSmith/wind
	def handle_incoming_frame(self, frame_data):
		print 'Incoming: %s' % repr(frame_data)
		event = events.parse_event_json(frame_data)
		if not event:
			print "Could not read an event from the data: %s" % frame_data
			return

		response_event = None
		if isinstance(event, events.Heartbeat):
			pass # ignore for now, eventually maybe track dead connections?
		elif isinstance(event, events.AuthenticationRequest):
			if event.session_id == settings.WEB_SOCKETS_SECRET:
				self.user = User.objects.filter(is_staff=True)[0]
				response_event = events.AuthenticationResponse(True, self.user.username)
			else:
				user = self.user_from_session_key(event.session_id)
				if user.is_authenticated():
					self.user = user
					response_event = events.AuthenticationResponse(True, user.username)
				else:
					response_event = events.AuthenticationResponse(False)
		elif isinstance(event, events.ServerInfoRequest):
			if not self.user:
				print 'Attemped unauthed info request'
			else:
				response_event = events.ServerInfo({
					'status':'ok',
					'connections':len(self.server.ws_connections),
					'channel count':len(self.server.channels),
					'channels':[key for key in self.server.channels.keys()]
				})
				
		elif isinstance(event, events.ChannelListRequest):
			if self.user:
				channel_list = []
				for channel_id, channel in self.server.channels.items():
					channel_list.append([channel.channel_id, channel.name])
				response_event = events.ChannelList(channel_list)
			else:
				print 'Attempted unauthed channel list request'
				
		elif isinstance(event, events.CreateChannelRequest):
			if self.user and self.user.is_staff:
				# TODO fix this massive race condition
				if not self.server.channels.has_key(event.channel_id):
					if event.class_name:
						try:
							module_name = '.'.join(event.class_name.split('.')[0:-1])
							class_name = event.class_name.split('.')[-1]
							__import__(module_name)
							modu = sys.modules[module_name]
							clz = getattr(modu, class_name)
							if issubclass(clz, events.Channel):
								channel = clz(self.server, event.channel_id, options=event.options)
								self.server.channels[channel.channel_id] = channel
								response_event = events.ChannelCreated(channel.channel_id)
							else:
								print 'Tried to create Channel using a non Channel class:', clz
						except:
							traceback.print_exc()
					else:
						channel = events.Channel(self.server, event.channel_id)
						self.server.channels[channel.channel_id] = channel
						response_event = events.ChannelCreated(channel.channel_id)
				else:
					response_event = events.ChannelExists(event.channel_id)
					print 'already have that key', event.channel_id
			else:
				print 'Attempted unauthed channel list request'
			
		elif isinstance(event, events.DeleteChannelRequest):
			if self.user and self.user.is_staff:
				# TODO fix this massive race condition
				if self.server.channels.has_key(event.channel_id):
					del(self.server.channels[event.channel_id])
					response_event = events.ChannelDeleted(event.channel_id)
			else:
				print 'Attempted unauthed channel list request'
				
		elif isinstance(event, events.SubscribeRequest):
			if self.channel:
				print 'Attempted to subscribe to more than one channel'
				response_event = events.SubscribeResponse(event.channel_id, False)
			elif not self.server.channels.has_key(event.channel_id):
				print 'Attemped subscription to unknown channel: %s' % event.channel_id
				response_event = events.SubscribeResponse(event.channel_id, False)
			else:
				channel = self.server.channels[event.channel_id]
				success, response_event = channel.handle_subscribe_request(self, event)
				if success:
					self.channel = channel

		elif hasattr(event, 'service'):
			try:
				response_event = event.service(self)
			except:
				pass
				traceback.print_exc()
				#print 'error servicing event:', event
		else:
			print "Received unhandled event %s" % event.to_json()

		if response_event:
			print 'Outgoing: %s' % repr(to_json(response_event))
			self.handler.send(response_event.to_json())
예제 #3
0
파일: models.py 프로젝트: brownman/spaciblo
def default_scene_doc(): return to_json(Scene())
			
class Space(HydrateModel):
예제 #4
0
파일: server.py 프로젝트: TrevorFSmith/wind
	def send_event(self, event):
		print 'Outgoing: %s' % repr(to_json(event))
		self.handler.send(event.to_json())
예제 #5
0
파일: models.py 프로젝트: brownman/spaciblo
	def prep_assets(self):
		obj_assets = []
		mtl_assets = {}
		json_assets = []
		app_asset = None
		for template_asset in TemplateAsset.objects.filter(template=self):
			if template_asset.asset.type == 'geometry' and template_asset.asset.file.name.endswith('.mtl'):
				mtl_assets[template_asset.key] = template_asset.asset
			elif template_asset.asset.type == 'geometry' and template_asset.asset.file.name.endswith('.obj'):
				obj_assets.append(template_asset.asset)
			elif template_asset.asset.type == 'geometry' and template_asset.asset.file.name.endswith('.json'):
				json_assets.append(template_asset.asset)
			elif template_asset.asset.type == 'application':
				app_asset = template_asset.asset
			#else:
			#	print 'asset type %s' % template_asset.asset.type
		
		for json_asset in json_assets:
			loader = JSONLoader()
			geometry = loader.toGeometry(open(json_asset.file.path).read())
			path = '/tmp/prepped_geo-%s' % self.id
			json_file = file(path, 'wb')
			json_file.write(to_json(geometry))
			json_file.close()
			json_file = file(path, 'r')
			json_asset.prepped_file.save(json_file.name, File(json_file), save=False)
			json_asset.save()
			json_file.close()
			os.unlink(path)
		
		for obj_asset in obj_assets: # try to save a prepped geometry JSON
			try:
				loader = ObjLoader()
				obj = loader.parse(open(obj_asset.file.path).read())
				if obj.mtllib and mtl_assets.has_key(obj.mtllib):
					mtllib_loader = MtlLibLoader()
					mtllib = mtllib_loader.parse(open(mtl_assets[obj.mtllib].file.path).read())
				elif obj.mtllib:
					print 'Obj asset %s requires an unknown mtllib: %s' % (obj_asset.file, obj.mtllib)
					mtllib = None
				else:
					print 'Obj has no mtllib'
					mtllib = None
				geometry = obj.toGeometry(mtllib)
				path = '/tmp/prepped_geo-%s' % self.id
				json_file = file(path, 'wb')
				json_file.write(to_json(geometry))
				json_file.close()
				json_file = file(path, 'r')
				obj_asset.prepped_file.save(json_file.name, File(json_file), save=False)
				obj_asset.save()
				json_file.close()
				os.unlink(path)
			except:
				traceback.print_exc()
		
		if app_asset != None:
			target_dir = os.path.join(settings.TEMPLATE_APPS_DIR, self.template_app_module_name)
			if os.path.exists(target_dir): shutil.rmtree(target_dir)
			tar = tarfile.open(app_asset.file.path)
			tar.extractall(path=target_dir)
			tar.close()