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

Queue is working

parent c603e864
Branches
No related tags found
No related merge requests found
...@@ -31,10 +31,13 @@ import threading ...@@ -31,10 +31,13 @@ import threading
import sys import sys
import binascii import binascii
import time import time
import Queue
global TOKEN global TOKEN
global StartTime global StartTime
global HighPriority
StartTime = 0 StartTime = 0
HighPriority = 10
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
...@@ -69,7 +72,7 @@ class Protocol(): ...@@ -69,7 +72,7 @@ class Protocol():
print 'Entrada Invalida, Por favor escolha entre 1 a 7' print 'Entrada Invalida, Por favor escolha entre 1 a 7'
sys.stdout.write('Prioridade: ') sys.stdout.write('Prioridade: ')
self.priority = raw_input() self.priority = raw_input()
self.priority = str(self.priority) self.priority = 7 - int(self.priority)
def setType (self): def setType (self):
if (self.msg == 'token'): if (self.msg == 'token'):
...@@ -93,7 +96,7 @@ class Protocol(): ...@@ -93,7 +96,7 @@ class Protocol():
self.crc = "%08X" % buff self.crc = "%08X" % buff
def getEmpacotar(self): def getEmpacotar(self):
return self.marca + self.type + self.destiny + self.origin + self.priority + self.sizemsg + self.marca + self.msg + self.marca + self.crc return self.marca + self.type + self.destiny + self.origin + str(self.priority) + self.sizemsg + self.marca + self.msg + self.marca + self.crc
def setDesempacota(self, msg): def setDesempacota(self, msg):
self.marca = msg[0] self.marca = msg[0]
...@@ -118,7 +121,7 @@ def token (): ...@@ -118,7 +121,7 @@ def token ():
while True: while True:
EndTime = time.time() EndTime = time.time()
if (EndTime - StartTime >= 10 and TOKEN == 1): if (EndTime - StartTime >= 10 and TOKEN == 1):
print 'Vou mandar o token' print 'MANDEI O TOKEN'
TOKEN = 0 TOKEN = 0
tokenMessage.Type = str(0) tokenMessage.Type = str(0)
msg = tokenMessage.getEmpacotar() msg = tokenMessage.getEmpacotar()
...@@ -126,29 +129,28 @@ def token (): ...@@ -126,29 +129,28 @@ def token ():
udp.close() udp.close()
def client (): def client ():
global TOKEN
try: try:
global TOKEN
global HighPriority
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dest = (HOST, SEND_PORT) dest = (HOST, SEND_PORT)
print 'Para sair digite exit\n'
protocolMessage = Protocol()
protocolMessage.setDestiny()
protocolMessage.setPriority()
protocolMessage.setMessage()
protocolMessage.setType()
protocolMessage.setSizeof()
protocolMessage.setCRC32()
msg = protocolMessage.getEmpacotar()
while protocolMessage.msg <> 'exit':
udp.sendto (msg, dest)
protocolMessage = Protocol() protocolMessage = Protocol()
queue = Queue.PriorityQueue()
while True:
protocolMessage.setDestiny() protocolMessage.setDestiny()
protocolMessage.setPriority() protocolMessage.setPriority()
protocolMessage.setMessage() protocolMessage.setMessage()
protocolMessage.setType() protocolMessage.Type = str(1)
protocolMessage.setSizeof() protocolMessage.setSizeof()
protocolMessage.setCRC32() protocolMessage.setCRC32()
msg = protocolMessage.getEmpacotar() msg = protocolMessage.getEmpacotar()
queue.put ((protocolMessage.priority, msg))
print queue.queue
HighPriority = queue.queue[0]
print HighPriority[0]
if (TOKEN == 1):
print 'POSSO ENVIAR MSG'
#udp.sendto (msg, dest)
threadServer._Thread__stop() threadServer._Thread__stop()
threadToken._Thread__stop() threadToken._Thread__stop()
udp.close() udp.close()
...@@ -166,9 +168,7 @@ def server (): ...@@ -166,9 +168,7 @@ def server ():
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment