Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CanguruChatServer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Harbor Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fernando Erd
CanguruChatServer
Commits
c603e864
Commit
c603e864
authored
8 years ago
by
Fernando Erd
Browse files
Options
Downloads
Patches
Plain Diff
Timeout feito
parent
d9df50e5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Canguru/CanguruChat.py
+35
-9
35 additions, 9 deletions
Canguru/CanguruChat.py
with
35 additions
and
9 deletions
Canguru/CanguruChat.py
+
35
−
9
View file @
c603e864
...
@@ -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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment