Пример #1
0
def pull_profile_from_node(node_id):
	"""Pull the user profile from the given node id
	Only update the user if data has changed."""
	print "pull_profile_from_node %d" % node_id

	save = False # Flag to save user
	node = None
	try:
		node = Node.objects.get(pk=node_id)
	except Node.DoesNotExist:
		# TODO: ERROR LOG FILE
		print "Node does not exist!!!"
		return

	user = None
	try:
		user = User.objects.get(node=node)
	except User.DoesNotExist:
		print "We don't have a user for the node..."
		user = User() # We may not yet have a user for the node (rare)
		save = True

	# Perform communications
	message = SylphMessage(node.uri)
	message.set_post('dispatch', 'user_pull')
	response = send(message)

	if response.has_errors():
		print "No communication return data!!" # TODO: Error log
		node.just_failed(save=True)
		return

	try:
		user_data = response.extract('User')
		if not user_data or len(user_data) != 1:
			raise Exception, "Error with data"
		user_data = user_data[0]
	except Exception: 
		print "No user data, or error. Ignoring."
		node.just_failed(save=True)
		return

	node.just_pulled_from(save=True)

	for k, v in user_data.iteritems():
		chk = getattr(user, k)
		if v == chk:
			continue
		if k == 'node':
			if v == node.pk:
				continue
			# XXX: The following could be used to hijack other users
			user.node = node # TODO: This isn't correct.
			save = True
			continue
		save = True
		setattr(user, k, v) # TODO: Might not work for 'uri' or 'node'

	if save:
		user.save()
Пример #2
0
def push_profile_to_node(node_uri):
	"""Send an updated copy of our profile to a specified node."""
	print "push_profile_to_node %s" % node_uri

	node_uri = hashless(node_uri)
	node = None
	user = None
	try:
		user = User.objects.get(pk=settings.OUR_USER_PK)
		node = Node.objects.get(uri=node_uri)
	except User.DoesNotExist:
		raise Exception, "The main user MUST exist!"
	except Node.DoesNotExist:
		raise Exception, "The node requested does not exist."

	# TODO: Granularized Privacy

	message = SylphMessage(node_uri)
	message.set_post('dispatch', 'user_push')
	message.add(user)
	response = send(message)

	if not response.has_errors():
		node.just_failed(save=True)
		print "User profile push failed." # TODO: Error log
		return

	node.just_pushed_to(save=True)
Пример #3
0
def do_add_node_lookup(uri):
	"""
	Nodes will get added all the time and in various contexts.
	This is the task that must run for each of them.

	Query every single added node -- this MUST occur.
	Remember, adding a node doesn't require permission. You can
	navigate to a URI without permission (though you may be blocked)

	The response may provide some additional details:

		* Person (with varying amount of data)
		* etc.

			> Add them
			> Maybe with an additional footnote? (TODO)

	after done, set is_remaining_to_query or whatever = False

	Pinging a node does not require us to give identity unless the
	remote node requests it.
	"""
	node = None
	try:
		node = Node.objects.get(uri=uri)
	except Node.DoesNotExist:
		# TODO: ERROR LOG FILE
		print "cannot do_add_node_lookup: Node does not exist!!!"
		return

	if node.node_class in ['webpage', 'service', 'feed']:
		print "cannot do_add_node_lookup: non-sylph or unknown nodes"
		return

	if node.node_class_guess in ['webpage', 'service', 'feed']:
		print "(TEMP) cannot do_add_node_lookup: non-sylph"
		return

	user = None
	try:
		user = User.objects.get(node=node)
	except User.DoesNotExist:
		user = User()

	# Perform communications
	message = SylphMessage(uri)
	message.set_post('dispatch', 'ping')
	response = send(message)

	if response.has_errors():
		print "No communication return data!!" # TODO: Error log
		node.just_failed(save=True)
		return

	parser = None
	node_data = None

	try:
		node_data = response.extract('Node')[0]
	except Exception:
		print "Error parsing payload" # TODO: Error log
		node.just_failed(save=True)
		return

	user_data = None
	try:
		user_data = response.extract('User')[0]
	except Exception:
		print "1. No user data, or error. Ignoring."

	# Update the node's status
	node.is_yet_to_resolve = False
	node.datetime_last_resolved = datetime.today()
	node.status = 'AVAIL'
	node.protocol_version = node_data['protocol_version']
	node.software_name = node_data['software_name']
	node.software_version = node_data['software_version']
	node.node_class = 'sylph'
	node.node_class_guess = ''
	node.name = node_data['name']
	node.description = node_data['description']
	#node.datetime_edited = node_data['datetime_edited'] # TODO
	node.save()

	# Update the user's status, if we have user data.
	if user_data:
		user.username = user_data['username']
		user.first_name = user_data['first_name']
		user.middle_name = user_data['middle_name']
		user.last_name = user_data['last_name']
		user.bio = user_data['bio']
		user.title = user_data['title']
		user.suffix = user_data['suffix']
		#user.datetime_created = user_data['datetime_created']
		#user.datetime_edited = user_data['datetime_edited']
		user.node = node
		user.save()

	# Now have the node add us
	message = SylphMessage(uri)
	post = {
		'dispatch': 'node_ask_to_add',
		'uri': settings.FULL_ENDPOINT_URI, # XXX XXX XXX: Very bad protocol!
	}
	message.set_post(post)
	response = send(message)

	if response.has_errors():
		print "Failed to ask node to add us!!"
		node.just_failed(save=True)

	print "CREATING SUBSCRIPTIONS:"
	create_subscriptions_to(node)
	create_subscriptions_from(node)
Пример #4
0
def ping_node(id):
	"""Ping a node that has already been added and succesfully resolved
	in the past. This keeps info up to date."""
	print "node.ping_node"
	node = None
	try:
		node = Node.objects.get(pk=id)
	except Node.DoesNotExist:
		# TODO: Error logging facility (log to db)
		print "Node does not exist!!!"
		return

	# XXX: Temporarily disabled
	#user = None
	#try:
	#	user = User.objects.get(node=node)
	#except User.DoesNotExist:
	#	user = User()

	# Perform communications
	message = SylphMessage(node.uri)
	message.set_post('dispatch', 'ping')
	response = send(message)

	if response.has_errors():
		print "No communication return data!!" # TODO: Error log
		node.just_failed(save=True)
		return

	node_data = None
	try:
		node_data = ret.extract('Node')
		if not node_data or len(node_data) != 1:
			raise Exception, "Error with data"
		node_data = node_data[0]

	except:
		print "Error parsing RDF" # TODO: Error log
		node.just_failed(save=True)
		return

	# XXX: Temporarily disabled
	#try:
	#	user_data = ret.extract('User')
	#	if not user_data or len(user_data) != 1:
	#		raise Exception, "Error with data"
	#	user_data = user_data[0]
	#except:
	#	pass

	# Update the node's status
	node.datetime_last_resolved = datetime.today()
	node.status = 'AVAIL'
	node.protocol_version = node_data['protocol_version']
	node.software_name = node_data['software_name']
	node.software_version = node_data['software_version']
	node.node_class = 'sylph'
	node.node_class_guess = ''
	node.name = node_data['name']
	node.description = node_data['description']
	#node.datetime_edited = node_data['datetime_edited'] # TODO
	node.save()

	node.just_pulled_from(save=True)