Example #1
0
	def dispatch(self, request, *args, **kwargs):
		with Session() as s:
			addr = kwargs['node'].replace('+', '/')
			if addr.startswith(':') or addr.startswith('/'):
				addr = request.META['REMOTE_ADDR'] + addr
			response = HttpResponse()
			thisNode = Node.getThisNode(s, addr).first()
			otherNode = Node.getOtherNode(s, addr).first()
			linkedNodeCount = Node.getLinkedNode(s).count()

			welcome = False
			if thisNode:
				welcome = True
				thisNode.linked = True
				thisNode.updateTimestamp()
				s.commit()
			elif linkedNodeCount < settings.MAX_NODES:
				welcome = True
				s.add(Node(host=addr, linked=True))
				s.commit()

			if welcome:
				response.write('WELCOME')
				if otherNode:
					response.write('\n'+str(otherNode.host))
			return response
Example #2
0
def _joinNetwork_doByeByeWorker(host):
	try:
		httpGet('http://' + host + '/bye/' + settings.NODE_NAME)
		with Session() as s:
			node = Node.getThisNode(s, host).first()
			if node:
				node.linked = False
			s.commit()
	except URLError:
		pass
Example #3
0
def _joinNetwork_findNodeWorker(host):
	try:
		newHost = httpGet('http://' + host + '/node').strip()
		with Session() as s:
			if Node.getThisNode(s, newHost).first():
				return
			Node.add(s, newHost)
			s.commit()
	except URLError:
		pass
Example #4
0
def _doPing_worker(host):
	try:
		response = httpGet('http://' + host + '/ping').splitlines()
		with Session() as s:
			node = Node.getThisNode(s, host).first()

			if response[0].strip() != 'PONG':
				node.linked = False
			else:
				node.updateTimestamp()

			if len(response) > 1:
				newNodeHost = response[1].strip()
				newNode = Node.getThisNode(s, newNodeHost).first()
				if not newNode:
					Node.add(s, newNodeHost)
			s.commit()
	except URLError:
		pass
Example #5
0
	def dispatch(self, request, *args, **kwargs):
		with Session() as s:
			addr = kwargs['node'].replace('+', '/')
			if addr.startswith(':') or addr.startswith('/'):
				addr = request.META['REMOTE_ADDR'] + addr
			response = HttpResponse()
			thisNode = Node.getThisNode(s, addr).first()
			if thisNode:
				thisNode.linked = False
				s.commit()
				response.write('BYEBYE')
			return response
Example #6
0
def _joinNetwork_joinWorker(host):
	try:
		response = httpGet('http://' + host + '/join/' + settings.NODE_NAME).splitlines()
		if response[0] != 'WELCOME':
			return

		with Session() as s:
			node = Node.getThisNode(s, host).first()
			if node:
				node.linked = True
			else:
				Node.add(s, host)
			s.commit()

		if len(response) == 2:
			newHost = response[1]
			with Session() as s:
				if Node.getThisNode(s, newHost).first():
					return
				Node.add(s, newHost)
				s.commit()
	except URLError:
		pass