Skip to content
Snippets Groups Projects
Commit c603e864 authored by Fernando Erd's avatar Fernando Erd :ok_hand:
Browse files

Timeout feito

parent d9df50e5
No related branches found
No related tags found
No related merge requests found
......@@ -30,8 +30,11 @@ import socket
import threading
import sys
import binascii
import time
global TOKEN
global StartTime
StartTime = 0
TOKEN = 0
MACHINE_ID = 1
SEND_PORT = 5000 # Porta que o Servidor envia
......@@ -40,13 +43,13 @@ RECV_PORT = 5000 # Porta que o Servidor recebe
class Protocol():
def __init__ (self):
self.marca = "~" #Marca
self.type = str(1) #Alterar Depois
self.destiny = None
self.type = str(0) #Alterar Depois
self.destiny = str(0)
self.origin = str(MACHINE_ID)
self.priority = None
self.msg = None
self.sizemsg = None
self.crc = None
self.priority = str(0)
self.msg = str(0)
self.sizemsg = str(0)
self.crc = str(0)
#set destiny message
def setDestiny(self):
......@@ -105,6 +108,23 @@ class Protocol():
buff = (binascii.crc32(self.msg) & 0xFFFFFFFF)
return "%08X" % buff
def token ():
global TOKEN
global StartTime
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tokenMessage = Protocol()
dest = (HOST, SEND_PORT)
while True:
EndTime = time.time()
if (EndTime - StartTime >= 10 and TOKEN == 1):
print 'Vou mandar o token'
TOKEN = 0
tokenMessage.Type = str(0)
msg = tokenMessage.getEmpacotar()
udp.sendto (msg,dest)
udp.close()
def client ():
global TOKEN
try:
......@@ -120,9 +140,6 @@ def client ():
protocolMessage.setCRC32()
msg = protocolMessage.getEmpacotar()
while protocolMessage.msg <> 'exit':
if (protocolMessage.type == 0 and TOKEN == 1):
print 'Vou mandar o token'
TOKEN = 0
udp.sendto (msg, dest)
protocolMessage = Protocol()
protocolMessage.setDestiny()
......@@ -133,23 +150,29 @@ def client ():
protocolMessage.setCRC32()
msg = protocolMessage.getEmpacotar()
threadServer._Thread__stop()
threadToken._Thread__stop()
udp.close()
except:
print 'OPS, ALGO OCORREU ERRADO'
threadServer._Thread__stop()
threadToken._Thread__stop()
def server ():
global TOKEN
global StartTime
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
orig = ('', RECV_PORT)
udp.bind(orig)
protocolDescompactMessage = Protocol()
while True:
msg, cliente = udp.recvfrom(1024)
print msg
protocolDescompactMessage.setDesempacota(msg)
print protocolDescompactMessage.type
if (protocolDescompactMessage.type == '0' and TOKEN == 0):
print 'RECEBI O TOKEN'
TOKEN = 1
StartTime = time.time()
else:
if protocolDescompactMessage.crc == protocolDescompactMessage.getCRC32():
print msg
......@@ -163,10 +186,13 @@ def server ():
if len(sys.argv) > 1:
TOKEN = 1
print 'COMECEI COM O TOKEN'
StartTime = time.time()
print "Ip da proxima maquina"
HOST = raw_input()
threadServer = threading.Thread(target=server)
threadClient = threading.Thread(target=client)
threadToken = threading.Thread(target=token)
threadServer.start()
threadClient.start()
threadToken.start()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment