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