Exemple #1
0
	def p2p_recv(self, clientsock, clientaddr):

		# handshake ----------------------------------------------------
		handshake_str = clientsock.recv(1024)
		handshake = handshake_toForm(handshake_str)


		if handshake.Type != "MINET" :
			clientsock.close()
			#print "@@@ connection NOT come from Minet!"
		else:

			handshake = HANDSHAKE()
			handshake.Type = "MINET"
			handshake.Hostname = self.MY_HOST
			clientsock.send(handshake.toStr())
		# --------------------------------------------------------------
			
			while 1:
				sheet_str = clientsock.recv(1024)
				if not sheet_str: break

				print "[320]"
				print sheet_str
				print "[321]"
				sheet = sheet_toForm(sheet_str)
				msg = sheet.Body

				if sheet.Cmd=="P2PMESSAGE":
					self.fileLock.acquire()	
					input = open("./buffer/"+self.Username+"/"+sheet.Arg, "a")
					print "["+sheet.Arg+" ~> msg]" + msg
					input.write("["+sheet.Arg+"] " + msg + "\n")
					input.close()
					self.fileLock.release()
				elif sheet.Cmd=="P2PFILE":
					if sheet.Headline.has_key("Query") and sheet.Headline["Query"]=="1":
						self.RecvCode[sheet.Arg2] = sheet.Headline["SendCode"]
						print "[322] Recv file", sheet.toStr()
						input = open("./system/"+self.Username+"/"+"P2PFILE", "a")
						print "["+sheet.Arg+" ~> file]" + "<"+ sheet.Arg2 +">"
						input.write(sheet.Arg + " " + sheet.Arg2 + "\n")
						input.close()


						self.fileLock.acquire()	
						input = open("./buffer/"+self.Username+"/"+sheet.Arg, "a")
						input.write("["+sheet.Arg+"] " + " <try to send file: " + sheet.Arg2 + ">\n")
						input.close()
						self.fileLock.release()
				elif sheet.Cmd=="P2PFILEACCEPT":
					
					if self.SendCode.has_key(sheet.Arg2) and sheet.Headline["RecvCode"] == self.SendCode[sheet.Arg2]:
						file_send_thread = thread.start_new_thread(self.file_send, (self.NameList[sheet.Arg][1], sheet.Headline["FILEPORT"], sheet.Headline["ReplyCode"], sheet.Arg, sheet.Arg2, ))


				if self.NameList[sheet.Arg][0] == False:
					break

			clientsock.close()
Exemple #2
0
def login(client):
	# handshake ----------------------------------------------------
	handshake = HANDSHAKE()
	handshake.Type = "MINET"
	handshake.Hostname = client.SERVER_HOST
	client.s.send(handshake.toStr())
	handshake_str = client.s.recv(1024)
	handshake = handshake_toForm(handshake_str)
	# --------------------------------------------------------------

	if handshake.Type == "MIRO" and handshake.Hostname == client.SERVER_HOST:
		dialog = LoginDialog(client) 
		if dialog.exec_(): 
			client = dialog.ReturnClient()
			return True, client 
	return False, client 
Exemple #3
0
def p2p_recv(clientsock, clientaddr):

	# handshake ----------------------------------------------------
	handshake_str = clientsock.recv(1024)
	handshake = handshake_toForm(handshake_str)


	if handshake.Type != "MINET" :
		clientsock.close()
		#print "@@@ connection NOT come from Minet!"
	else:

		handshake = HANDSHAKE()
		handshake.Type = "MINET"
		handshake.Hostname = MY_HOST
		clientsock.send(handshake.toStr())
	# --------------------------------------------------------------
		
		while 1:
			sheet_str = clientsock.recv(1024)
			if not sheet_str: break

			sheet = sheet_toForm(sheet_str)

			if sheet.Cmd=="P2PMESSAGE":
				msg = sheet.Body
				print "["+sheet.Arg+" ~> msg]" + msg
			elif sheet.Cmd=="P2PFILE":
				if sheet.Headline.has_key("Query") and sheet.Headline["Query"]=="1":
					RecvCode[sheet.Arg2] = sheet.Headline["SendCode"]
					print "["+sheet.Arg+" ~> file]" + "<"+sheet.Arg2+">"
					print "# Accept reply: 'P2PFILEACCEPT " + sheet.Arg + " " + sheet.Arg2 + "' #"
					print "# non-reply indicate REJECT #"
			elif sheet.Cmd=="P2PFILEACCEPT":
			
				print "[380]",sheet.toStr()
				if SendCode.has_key(sheet.Arg2) and sheet.Headline["RecvCode"] == SendCode[sheet.Arg2]:
					# new a thread to handle file transfer
					
					file_send_thread = thread.start_new_thread(file_send, (NameList[sheet.Arg][1], sheet.Headline["FILEPORT"], sheet.Headline["ReplyCode"], sheet.Arg2,))
			
			if NameList[sheet.Arg][0] == False:
				break

		clientsock.close()
Exemple #4
0
	def p2p_send_station(self, sheet_str, name):

		if self.client.NameList[name][4]==False:
			s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			s.connect((self.client.NameList[name][1], int(self.client.NameList[name][2])))
			self.client.NameList[name][3] = s

			handshake = HANDSHAKE()
			handshake.Type = "MINET"
			handshake.Hostname = self.client.MY_HOST
			s.send(handshake.toStr())

			handshake_reply_str = s.recv(1024)
			handshake_reply = handshake_toForm(handshake_reply_str)
			if handshake_reply.Type=="MINET":
				self.client.NameList[name][4] = True

		if self.client.NameList[name][4]:
			s = self.client.NameList[name][3]
			s.send(sheet_str)
Exemple #5
0
def main(clientsock, clientaddr):

	#-------------------------- handshake --------------------------------	
	handshake_str = clientsock.recv(1024)			# get handshake
	print "[60]",handshake_str
	handshake = handshake_toForm(handshake_str)

	if handshake.Type != "MINET" :
		clientsock.close()
		print "@@@ connection NOT come from Minet!"
	else:
		handshake = HANDSHAKE()
		handshake.Type = "MIRO"
		handshake.Hostname = myhost
		clientsock.send(handshake.toStr())			# send handshake_reply

	#============================ login ==================================
#		try:
		login_tag = False
		while 1:
			cmd_str = clientsock.recv(1024)			# get login 
			login_tag, username = login_handle(cmd_str, clientsock, clientaddr)
			if login_tag: break

		#-------------------- command --------------------------------
		while NameList[username][0]:
			cmd_str = clientsock.recv(1024)			# get login 
			if not cmd_str: break
			handle(cmd_str, username, clientsock)
		#-------------------------------------------------------------
#		except:
		if login_tag:
			print "# normal EXIT."
		else:
			print "# someone LOGIN FAIL(2)."
	#=====================================================================
	print "@@@ Finish connection!"
	clientsock.close()
Exemple #6
0
def main():

	find_MY_PORT()
	t_p2p = thread.start_new_thread(p2p_recv_station, ())

	print MY_PORT

	global Username, NameList, PROCESSING
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect((SERVER_HOST, SERVER_PORT))



	# handshake ----------------------------------------------------
	handshake = HANDSHAKE()
	handshake.Type = "MINET"
	handshake.Hostname = SERVER_HOST
	s.send(handshake.toStr())
	handshake_str = s.recv(1024)
	handshake = handshake_toForm(handshake_str)
	# --------------------------------------------------------------

	if handshake.Type != "MIRO" or handshake.Hostname != SERVER_HOST:
		print "@@@ connection NOT from Miro!"
	else:
		print "# please enter 'LOGIN Username':"******"login"]:
			print "$",
			cmd_input = raw_input()
			cmd = cmd_input.split(" ")

			if cmd_valid(cmd) and cmd[0]=="LOGIN":
				sheet = make_sheet(cmd)
				s.send(sheet.toStr())
				login_handle(s)
			elif cmd_valid(cmd) and cmd[0]=="LEAVE":
				break
			else:
				print "# command ERROR!"
		# -------------------------------------------
				

		# have login and wait for command -----------
		if Status["login"]:
			t = thread.start_new_thread(handle, (s,))
			t_beat = thread.start_new_thread(beat_handle, (s,))

		while Status["login"]:
			if PROCESSING:
				time.sleep(0.1)
			else:
				print "["+Username+"]$",
				cmd_input = raw_input()
				cmd = cmd_input.split(" ")
				if cmd_valid(cmd):
					sheet = make_sheet(cmd)
					if sheet.Version=="CS1.0":
						s.send(sheet.toStr())
						PROCESSING = True
					elif sheet.Version=="P2P1.0":
						if NameList.has_key(cmd[1]) and NameList[cmd[1]][0]==True:
							p2p_send_station(sheet.toStr(), cmd[1])
						else:
							print "# your friend maybe OFFLINE. please GETLIST to check."
						# send to another thread to handle the p2p communication
				else:
					print "# command ERROR!"
		# -------------------------------------------	

	s.close()