Exemple #1
0
def retrieve_updates(request):
	"""
	Request handler to update client model after receiving a push notification
	1. Check queue to see if the client has any updates on standby
	2. Send the client their update
	3. Get ack from client
	4. Empty the queue
	"""
	if request.method == 'POST':
		json_data = json.loads(request.body)

		try:
			device_id = json_data['device_id']
		except KeyError:
			print "Error: A posted response did not have a JSON object with the required properties"
		else:

			# try sending the updates to the user
			# if this fails in any way (IOError) then make sure the updates
			# are back on the updates stack (else they won't reach the client).
			try:
				print device_id
				all_devices = Device.objects.all()
				device = all_devices.filter(device_id=device_id)[0]
				
				question_updates = QuestionUpdates.remove_updates(device)
				thread_updates = ThreadUpdates.remove_updates(device)
				response_updates = ResponseUpdates.remove_updates(device)
				serializer = Serializer()
				
				serialized_question_updates = serializer.serialize(question_updates)
				
				serialized_thread_updates = serializer.serialize(thread_updates)
				serialized_response_updates = serializer.serialize(response_updates)
				print serialized_question_updates
				
				
				all_updates = '{ "question_updates" : ' + serialized_question_updates + ', "thread_updates" : ' + serialized_thread_updates+ ', "response_updates" : ' + serialized_response_updates+ '}'
							
				return HttpResponse(all_updates, content_type="application/json")
			except IOError:
				# put back all updates into the update stack
				# This doesn't look like it would actually work
				for update in question_updates:
					QuestionUpdates.add_update(device, update)
					
				for update in thread_updates:
					ThreadUpdates.add_update(device, update)

				for update in response_updates:
					ResponseUpdates.add_update(device, update)

				print "Error: failed to send updates to client"
Exemple #2
0
def ask(request):
	"""
	Request handler when someone posts a question
	1. Add question content to the database
	2. Select random active answerer
	3. Put the question in the answerer's update stack
	4. Send push notification to the answerer's device to retrieve updates
	"""
	if request.method == 'POST':
		json_data = json.loads(request.body)

		try:
			question_content = json_data['content']
			asker_device_id = json_data['device_id']
			max_new_threads = json_data['max_new_threads']
		except KeyError:
			print "Error: A posted question did not have a JSON object with the required properties"
		else:
			
			# then add question to database
			question = Question(asker_device_id=asker_device_id, content=question_content)
			question.save()

			# We are going to start one or more threads with random devices, put the critical information about the thread in this array
			# and send it back to the device
			new_thread_ids = [];
			responder_ids = [];
			
			# then select a random device to send the question to
			all_devices = Device.objects.all()
			asker_device = all_devices.filter(device_id=asker_device_id)[0];
			print "Found asker device"
			print asker_device
			print max_new_threads
			reasonable_number_of_tries = 0
			while len(new_thread_ids) < max_new_threads:
				random_device = random.choice(all_devices) if len(all_devices) > 1 else None
				print "Chosing random device"
				print random_device
				# ensure that we've a valid answerer device
				if random_device is None:
					return
				while len(all_devices) > 1 and random_device.device_id == asker_device_id or random_device.device_id in responder_ids :
					if reasonable_number_of_tries < 5:
						random_device = random.choice(all_devices)
						reasonable_number_of_tries = reasonable_number_of_tries + 1
					else:
						break
					
				if reasonable_number_of_tries >= 5:
					break
				
				print "Chose another random device"
				print random_device
				responder_ids.append(random_device.device_id)
				
				print "But I am"
				print asker_device_id
				
				# find a unique thread id

					

				
				# Start the thread between the asker device and the random device	
				response_thread = Thread(question_id=question.id, asker_device=asker_device, answerer_device=random_device)
				response_thread.save()
				new_thread_ids.append(response_thread.id)
				print "response thread with id: " + str(response_thread.id)
	
				# add question to answerer_device update stack
				QuestionUpdates.add_update(random_device, question)
				ThreadUpdates.add_update(random_device, response_thread)
			
			new_threads = []
			for i in range(0, len(new_thread_ids)):
				new_threads.append({'thread_id':new_thread_ids[i], 'responder_device_id':responder_ids[i]})

			return HttpResponse(json.dumps({ 'question_id' : question.id, 'threads' : new_threads }), content_type="application/json")