From c603e864060b87fa9e53dda0916d0cfdf73f382d Mon Sep 17 00:00:00 2001
From: Fernando Erd <fce15@inf.ufpr.br>
Date: Sun, 6 Nov 2016 18:52:42 -0200
Subject: [PATCH] Timeout feito

---
 Canguru/CanguruChat.py | 44 +++++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 9 deletions(-)

diff --git a/Canguru/CanguruChat.py b/Canguru/CanguruChat.py
index 7a46bd9..e10af47 100755
--- a/Canguru/CanguruChat.py
+++ b/Canguru/CanguruChat.py
@@ -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()
-- 
GitLab