Пример #1
0
class Tracker(object):
	track_dict = {}
	
	def __init__(self, port):
		self.track_dict['this'] = ['localhost', port, 0]
		self.p = PunchObject("")
		self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		self.s.bind(('0.0.0.0', self.track_dict['this'][1]))

	def send(self, s, addr):
		b = s
		if isinstance(s, str):
			b = s.encode('utf-8')
		self.s.sendto(b, addr)

	def recv(self, recv_len=1024):
		(data,addr) = self.s.recvfrom(recv_len)
		data = data.decode('utf-8')
		return data, addr

	def listen(self):
		while 1:
			(data,addr)= self.recv()
			data = data.split(',')
			session_name = data[0]
			offset = data[1]
			self.track_dict[session_name] = [addr[0], int(addr[1]), int(offset)]
			msg = self.p.compose('JSON', json.dumps(self.track_dict))
			self.send( msg, addr)
			print(self.track_dict)
Пример #2
0
 def send_request(self, server_ip, server_port, offset):
   self.SERVER_PORT = server_port
   self.SERVER_IP = server_ip
   self.s.sendto("%s,%d" % (self.session_name, offset) , (server_ip, server_port))
   data,addr=self.s.recvfrom(1024)
   self.dat = PunchObject(data)
   if self.dat.JSON:
     self.track_dict = json.loads(data[1:])
Пример #3
0
class Tracker(object):
    track_dict = {"this": ["localhost", 60000, 0]}

    def __init__(self):
        self.p = PunchObject("")
        self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.s.bind(("0.0.0.0", self.track_dict["this"][1]))

    def listen(self):
        while 1:
            (data, addr) = self.s.recvfrom(1024)
            data = data.split(",")
            session_name = data[0]
            offset = data[1]
            self.track_dict[session_name] = [addr[0], int(addr[1]), int(offset)]
            msg = self.p.compose("JSON", json.dumps(self.track_dict))
            self.s.sendto(msg, addr)
            print self.track_dict
Пример #4
0
	def __init__(self, port):
		self.track_dict['this'] = ['localhost', port, 0]
		self.p = PunchObject("")
		self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		self.s.bind(('0.0.0.0', self.track_dict['this'][1]))
Пример #5
0
class Endpoint(object):
  track_dict = {}
  session_name = []
  SERVER_PORT = 60000
  SERVER_IP = '192.43.193.103'
  def __init__(self, session_name):
    self.session_name=session_name
    self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    self.s.setblocking(0)
    self.s.settimeout(5)
    self.rand = random.SystemRandom()
    prt = self.rand.randint(20000,20005)
    self.s.bind(('0.0.0.0', prt))
    print "bound to port ", prt
    
  def send_request(self, server_ip, server_port, offset):
    self.SERVER_PORT = server_port
    self.SERVER_IP = server_ip
    self.s.sendto("%s,%d" % (self.session_name, offset) , (server_ip, server_port))
    data,addr=self.s.recvfrom(1024)
    self.dat = PunchObject(data)
    if self.dat.JSON:
      self.track_dict = json.loads(data[1:])
    
  def connect_endpoint(self, remote_name):
    """connect to another endpoint"""
    acc = 0.5 #TODO: implement sleep "growth"
    print self.track_dict
    connected = False
    synned = False
    remote_offset = None
    my_pub_offset = None
    syn_time=0
    ack_time=0
    self.dat = PunchObject("")
    for count in xrange(550):
      print "versuch %s" % count,
      r,w,x = select([self.s], [self.s], [], 0)
      if w:
	if count%4==0:
	  if my_pub_offset!=None:
	    self.send_request(self.SERVER_IP, self.SERVER_PORT, my_pub_offset)
	  else:
	    self.send_request(self.SERVER_IP, self.SERVER_PORT, 0)
	  r_addr = self.track_dict[remote_name]
	  r_offset = r_addr[2]
	  r_addr = r_addr[0], r_addr[1]
	  print "addr update"

	if connected:
	  print "send msg"
	  self.s.sendto(self.dat.compose('MSG', "%s offsetted msg: %s has public offset: %s" % (count, self.session_name, my_pub_offset)), (r_addr[0], r_addr[1]+remote_offset))
	  print r_addr[1]+remote_offset

        mx = (5 + r_offset + count*2)%65536
        mn =     (r_offset)%65536-count-5

        if mn>mx:
	  mn = mx+1
        if remote_offset==None:# and not connected:
	  print "probing offset in range: ", mn+r_addr[1], mx+r_addr[1]
	  for i in range(mn,mx):
	    #if count<20#if my_pub_offset == None or remote_offset==None:
	    if i!=my_pub_offset:
	      if not self.dat.SYN and not self.dat.ACK or my_pub_offset != None:
		msg = self.dat.compose('SYN', "%s %s,%s"%(count, self.session_name, i) )#SYN mit offset
		self.s.sendto(msg, (r_addr[0], r_addr[1]+i))
		syn_time = time.time()
	
	if self.dat.SYN:
	  for i in range(mn,mx):
	    if i!=my_pub_offset:
		msg = self.dat.compose('ACK',"%s %s,%s" % (count, self.session_name, my_pub_offset) )
		self.s.sendto(msg, (r_addr[0], r_addr[1]+i))
      if r:
        data,addr=self.s.recvfrom(1024)
        print "data: ", data
        self.dat = PunchObject(data)
	if self.dat.MSG:
	  num_nomsg = 0
	  print str(self.dat)
	elif self.dat.SYN:
	  print "recvd SYN ", str(self.dat)
	  if my_pub_offset == None and not self.session_name in data[1:]:
	    my_pub_offset = int(data[1:].split(",")[-1])
	    synned = True
	elif self.dat.ACK:
	  str_ro = data[1:].split(",")[-1]
	  if str_ro != "None":
	    remote_offset = int(str_ro)
	  ack_time = time.time()-syn_time
	  print "recvd ACK ", str(self.dat), ack_time
	  if synned:
	    connected = True
	    print "Connected."
	elif self.dat.JSON:
	  self.track_dict = json.loads(data[1:])
	else:
	  num_nomsg += 1
      time.sleep(acc)
Пример #6
0
  def connect_endpoint(self, remote_name):
    """connect to another endpoint"""
    acc = 0.5 #TODO: implement sleep "growth"
    print self.track_dict
    connected = False
    synned = False
    remote_offset = None
    my_pub_offset = None
    syn_time=0
    ack_time=0
    self.dat = PunchObject("")
    for count in xrange(550):
      print "versuch %s" % count,
      r,w,x = select([self.s], [self.s], [], 0)
      if w:
	if count%4==0:
	  if my_pub_offset!=None:
	    self.send_request(self.SERVER_IP, self.SERVER_PORT, my_pub_offset)
	  else:
	    self.send_request(self.SERVER_IP, self.SERVER_PORT, 0)
	  r_addr = self.track_dict[remote_name]
	  r_offset = r_addr[2]
	  r_addr = r_addr[0], r_addr[1]
	  print "addr update"

	if connected:
	  print "send msg"
	  self.s.sendto(self.dat.compose('MSG', "%s offsetted msg: %s has public offset: %s" % (count, self.session_name, my_pub_offset)), (r_addr[0], r_addr[1]+remote_offset))
	  print r_addr[1]+remote_offset

        mx = (5 + r_offset + count*2)%65536
        mn =     (r_offset)%65536-count-5

        if mn>mx:
	  mn = mx+1
        if remote_offset==None:# and not connected:
	  print "probing offset in range: ", mn+r_addr[1], mx+r_addr[1]
	  for i in range(mn,mx):
	    #if count<20#if my_pub_offset == None or remote_offset==None:
	    if i!=my_pub_offset:
	      if not self.dat.SYN and not self.dat.ACK or my_pub_offset != None:
		msg = self.dat.compose('SYN', "%s %s,%s"%(count, self.session_name, i) )#SYN mit offset
		self.s.sendto(msg, (r_addr[0], r_addr[1]+i))
		syn_time = time.time()
	
	if self.dat.SYN:
	  for i in range(mn,mx):
	    if i!=my_pub_offset:
		msg = self.dat.compose('ACK',"%s %s,%s" % (count, self.session_name, my_pub_offset) )
		self.s.sendto(msg, (r_addr[0], r_addr[1]+i))
      if r:
        data,addr=self.s.recvfrom(1024)
        print "data: ", data
        self.dat = PunchObject(data)
	if self.dat.MSG:
	  num_nomsg = 0
	  print str(self.dat)
	elif self.dat.SYN:
	  print "recvd SYN ", str(self.dat)
	  if my_pub_offset == None and not self.session_name in data[1:]:
	    my_pub_offset = int(data[1:].split(",")[-1])
	    synned = True
	elif self.dat.ACK:
	  str_ro = data[1:].split(",")[-1]
	  if str_ro != "None":
	    remote_offset = int(str_ro)
	  ack_time = time.time()-syn_time
	  print "recvd ACK ", str(self.dat), ack_time
	  if synned:
	    connected = True
	    print "Connected."
	elif self.dat.JSON:
	  self.track_dict = json.loads(data[1:])
	else:
	  num_nomsg += 1
      time.sleep(acc)
Пример #7
0
 def __init__(self):
     self.p = PunchObject("")
     self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     self.s.bind(("0.0.0.0", self.track_dict["this"][1]))
Пример #8
0
	def connect_endpoint(self, remote_name):
		t = 0
		while t < 999999:
			if self.track_dict.get(remote_name):
				break
			time.sleep(1)
			t += 1
			if t > 30:
				raise Exception(f'{remote_name} not registed')

		"""connect to another endpoint"""
		acc = 0.5 #TODO: implement sleep "growth"
		print(self.track_dict)
		connected = False
		synned = False
		remote_offset = None
		my_pub_offset = None
		syn_time=0
		ack_time=0
		self.dat = PunchObject("")
		for count in range(550):
			print("versuch %s" % count)
			r,w,x = select([self.s], [self.s], [], 0)
			if w:
				r_addr = self.track_dict.get(remote_name)
				r_addr = r_addr[0], r_addr[1]
				print("addr update",r_addr)

				if connected:
					print("send msg to", r_addr)
					msg = self.dat.compose('MSG', "%s offsetted msg: %s has public offset: %s" % (count, self.session_name, my_pub_offset))
					self.send(msg, r_addr)
					print('send :', msg)

				if not self.dat.SYN and not self.dat.ACK:
					msg = self.dat.compose('SYN', "%s %s,%s"%(count, self.session_name, 0) )#SYN mit offset
					self.send(msg, r_addr)
					print('No:send :', msg)
					syn_time = time.time()
	
				if self.dat.SYN:
					msg = self.dat.compose('ACK',"%s %s,%s" % \
								(count, self.session_name, 0) )
					self.send(msg, r_addr)
					print('SYNed:send :', msg)
			if r:
				data,addr=self.recv()
				print("data: ", data)
				self.dat = PunchObject(data)
				if self.dat.MSG:
					num_nomsg = 0
					print(str(self.dat), ':', addr)
				elif self.dat.SYN:
					print("recvd SYN ", str(self.dat), ':', addr)
					synned = True
				elif self.dat.ACK:
					ack_time = time.time()-syn_time
					print("recvd ACK ", str(self.dat), ack_time, ':', addr)
					if synned:
						connected = True
						print("Connected.", remote_name, ':', addr)
						self.p2plist[remote_name] = addr
				elif self.dat.JSON:
					self.track_dict = json.loads(data[1:])
				else:
					num_nomsg += 1
			time.sleep(acc)
Пример #9
0
	def send_request(self, offset):
		self.send("%s,%d" % (self.session_name, offset) , self.env.server_addr())
		data,addr=self.recv()
		self.dat = PunchObject(data)
		if self.dat.JSON:
			self.track_dict = json.loads(data[1:])
Пример #10
0
class Endpoint(object):
	track_dict = {}
	session_name = []
	def __init__(self, env, session_name):
		self.env = env
		self.session_name=session_name
		self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		self.s.setblocking(0)
		self.s.settimeout(5)
		self.rand = random.SystemRandom()
		prt = self.rand.randint(20000,20005)
		self.s.bind(('0.0.0.0', prt))
		print("bound to port ", prt)
		self.p2plist = {}
		self.heartbeat()

	def heartbeat(self):
		self.heartbeat = True
		self.hb_time = 5
		def hb():
			while self.heartbeat:
				self.send_request(0)
				time.sleep(self.hb_time)

		b = Background(hb)
		b.start()

	def send(self, s, addr):
		b = s
		if isinstance(s,bytes):
			b = s
		elif isinstance(s,str):
			b = s.encode('utf-8')
		else:
			b = json.dumps(s)
		self.s.sendto(b,addr)

	def recv(self, recv_len=1024):
		data, addr = self.s.recvfrom(recv_len)
		data = data.decode('utf-8')
		return data, addr

	def send_request(self, offset):
		self.send("%s,%d" % (self.session_name, offset) , self.env.server_addr())
		data,addr=self.recv()
		self.dat = PunchObject(data)
		if self.dat.JSON:
			self.track_dict = json.loads(data[1:])
		
	def connect_endpoint(self, remote_name):
		t = 0
		while t < 999999:
			if self.track_dict.get(remote_name):
				break
			time.sleep(1)
			t += 1
			if t > 30:
				raise Exception(f'{remote_name} not registed')

		"""connect to another endpoint"""
		acc = 0.5 #TODO: implement sleep "growth"
		print(self.track_dict)
		connected = False
		synned = False
		remote_offset = None
		my_pub_offset = None
		syn_time=0
		ack_time=0
		self.dat = PunchObject("")
		for count in range(550):
			print("versuch %s" % count)
			r,w,x = select([self.s], [self.s], [], 0)
			if w:
				r_addr = self.track_dict.get(remote_name)
				r_addr = r_addr[0], r_addr[1]
				print("addr update",r_addr)

				if connected:
					print("send msg to", r_addr)
					msg = self.dat.compose('MSG', "%s offsetted msg: %s has public offset: %s" % (count, self.session_name, my_pub_offset))
					self.send(msg, r_addr)
					print('send :', msg)

				if not self.dat.SYN and not self.dat.ACK:
					msg = self.dat.compose('SYN', "%s %s,%s"%(count, self.session_name, 0) )#SYN mit offset
					self.send(msg, r_addr)
					print('No:send :', msg)
					syn_time = time.time()
	
				if self.dat.SYN:
					msg = self.dat.compose('ACK',"%s %s,%s" % \
								(count, self.session_name, 0) )
					self.send(msg, r_addr)
					print('SYNed:send :', msg)
			if r:
				data,addr=self.recv()
				print("data: ", data)
				self.dat = PunchObject(data)
				if self.dat.MSG:
					num_nomsg = 0
					print(str(self.dat), ':', addr)
				elif self.dat.SYN:
					print("recvd SYN ", str(self.dat), ':', addr)
					synned = True
				elif self.dat.ACK:
					ack_time = time.time()-syn_time
					print("recvd ACK ", str(self.dat), ack_time, ':', addr)
					if synned:
						connected = True
						print("Connected.", remote_name, ':', addr)
						self.p2plist[remote_name] = addr
				elif self.dat.JSON:
					self.track_dict = json.loads(data[1:])
				else:
					num_nomsg += 1
			time.sleep(acc)