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
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
5a0e0c22
Commit
5a0e0c22
authored
8 years ago
by
Fernando Erd
Browse files
Options
Downloads
Patches
Plain Diff
name
parent
8910e762
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Chat.py
+61
-76
61 additions, 76 deletions
Chat.py
repo.py
+0
-230
0 additions, 230 deletions
repo.py
with
61 additions
and
306 deletions
Chat.py
+
61
−
76
View file @
5a0e0c22
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
###############################################################################################
###############################################################################################
#Marca - 1 Byte Marca inicio de transmissao, inicio de msg fim de msg
#Marca - 1 Byte Marca inicio de transmissao, inicio de msg fim de msg
#Tipo - 0 Token, 1 - Msg,
2 - ACK, 3 - NACK
#Tipo - 0 Token, 1 - Msg,
#Destino - pode variar de 1 a 4
#Destino - pode variar de 1 a 4
#Origem - pode variar de 1 a 4
#Origem - pode variar de 1 a 4
#Prioridade - pode variar de 0 a 7
#Prioridade - pode variar de 0 a 7
...
@@ -34,11 +34,11 @@ import time
...
@@ -34,11 +34,11 @@ import time
import
Queue
import
Queue
global
TOKEN
global
TOKEN
global
StartTime
global
HighPriority
global
HighPriority
global
RECEBIDA
global
RECEBIDA
global
PRIORITY_TOKEN
PRIORITY_TOKEN
=
8
RECEBIDA
=
1
RECEBIDA
=
1
StartTime
=
0
HighPriority
=
10
HighPriority
=
10
TOKEN
=
0
TOKEN
=
0
SEND_PORT
=
3131
# Porta que o Servidor envia
SEND_PORT
=
3131
# Porta que o Servidor envia
...
@@ -76,12 +76,6 @@ class Protocol():
...
@@ -76,12 +76,6 @@ class Protocol():
self
.
priority
=
raw_input
()
self
.
priority
=
raw_input
()
self
.
priority
=
7
-
int
(
self
.
priority
)
self
.
priority
=
7
-
int
(
self
.
priority
)
def
setType
(
self
):
if
(
self
.
msg
==
'
token
'
):
self
.
type
=
str
(
0
)
else
:
self
.
type
=
str
(
1
)
#set message
#set message
def
setMessage
(
self
):
def
setMessage
(
self
):
sys
.
stdout
.
write
(
'
Mensagem:
'
)
sys
.
stdout
.
write
(
'
Mensagem:
'
)
...
@@ -103,7 +97,7 @@ class Protocol():
...
@@ -103,7 +97,7 @@ class Protocol():
def
setDesempacota
(
self
,
msg
):
def
setDesempacota
(
self
,
msg
):
self
.
marca
=
msg
[
0
]
self
.
marca
=
msg
[
0
]
self
.
type
=
msg
[
1
]
self
.
type
=
msg
[
1
]
self
.
destiny
=
int
(
msg
[
2
])
self
.
destiny
=
str
(
msg
[
2
])
self
.
origin
=
msg
[
3
]
self
.
origin
=
msg
[
3
]
self
.
priority
=
msg
[
4
]
self
.
priority
=
msg
[
4
]
self
.
msg
=
msg
.
split
(
'
~
'
,
3
)[
2
]
self
.
msg
=
msg
.
split
(
'
~
'
,
3
)[
2
]
...
@@ -116,41 +110,38 @@ class Protocol():
...
@@ -116,41 +110,38 @@ class Protocol():
def
token
():
def
token
():
global
TOKEN
global
TOKEN
global
StartTime
global
queue
global
queue
global
RECEBIDA
global
PRIORITY_TOKEN
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
tokenMessage
=
Protocol
()
tokenMessage
=
Protocol
()
msgProtocol
=
Protocol
()
dest
=
(
HOST
,
SEND_PORT
)
dest
=
(
HOST
,
SEND_PORT
)
while
True
:
time
.
sleep
(
2
)
EndTime
=
time
.
time
()
if
(
TOKEN
==
2
):
if
(
EndTime
-
StartTime
>=
2
and
TOKEN
==
1
):
print
'
MANDEI O TOKEN
'
TOKEN
=
0
TOKEN
=
0
tokenMessage
.
type
=
str
(
0
)
tokenMessage
.
type
=
str
(
0
)
tokenMessage
.
priority
=
str
(
PRIORITY_TOKEN
)
msg
=
tokenMessage
.
getEmpacotar
()
msg
=
tokenMessage
.
getEmpacotar
()
udp
.
sendto
(
msg
,
dest
)
udp
.
sendto
(
msg
,
dest
)
#time.sleep(2)
if
(
not
queue
.
empty
()
and
TOKEN
==
1
):
if
(
TOKEN
==
1
):
top
=
queue
.
queue
[
0
]
#print 'ESTOU COM O TOKEN'
if
(
int
(
PRIORITY_TOKEN
)
>=
int
(
top
[
0
])):
if
(
not
queue
.
empty
()):
top
=
queue
.
get
()
msg
=
queue
.
get
()
msgProtocol
.
setDesempacota
(
top
[
1
])
udp
.
sendto
(
msg
[
1
],
dest
)
msgProtocol
.
priority
=
str
(
7
)
print
'
ENVIANDO
'
msg
=
msgProtocol
.
getEmpacotar
()
time
.
sleep
(
1
)
udp
.
sendto
(
top
[
1
],
dest
)
while
(
RECEBIDA
==
0
):
print
'
ENVIANDO NOVAMENTE A MSG
'
udp
.
sendto
(
msg
[
1
],
dest
)
time
.
sleep
(
1
)
time
.
sleep
(
1
)
return
True
else
:
return
False
udp
.
close
()
udp
.
close
()
def
client
():
def
client
():
try
:
try
:
global
TOKEN
global
TOKEN
global
HighPriority
protocolMessage
=
Protocol
()
protocolMessage
=
Protocol
()
global
queue
global
queue
queue
=
Queue
.
PriorityQueue
()
while
True
:
while
True
:
protocolMessage
.
setDestiny
()
protocolMessage
.
setDestiny
()
protocolMessage
.
setPriority
()
protocolMessage
.
setPriority
()
...
@@ -160,86 +151,80 @@ def client ():
...
@@ -160,86 +151,80 @@ def client ():
protocolMessage
.
setCRC32
()
protocolMessage
.
setCRC32
()
msg
=
protocolMessage
.
getEmpacotar
()
msg
=
protocolMessage
.
getEmpacotar
()
queue
.
put
((
protocolMessage
.
priority
,
msg
))
queue
.
put
((
protocolMessage
.
priority
,
msg
))
if
(
not
queue
.
empty
()):
HighPriority
=
queue
.
queue
[
0
]
threadServer
.
_Thread__stop
()
threadServer
.
_Thread__stop
()
threadToken
.
_Thread__stop
()
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
global
RECEBIDA
global
RECEBIDA
global
queue
global
PRIORITY_TOKEN
global
TOKEN_MSG
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
orig
=
(
''
,
RECV_PORT
)
orig
=
(
''
,
RECV_PORT
)
dest
=
(
HOST
,
SEND_PORT
)
dest
=
(
HOST
,
SEND_PORT
)
udp
.
bind
(
orig
)
udp
.
bind
(
orig
)
protocolDescompactMessage
=
Protocol
()
protocolDescompactMessage
=
Protocol
()
aux1
=
Protocol
()
aux2
=
Protocol
()
while
True
:
while
True
:
msg
,
cliente
=
udp
.
recvfrom
(
1024
)
msg
,
cliente
=
udp
.
recvfrom
(
1024
)
protocolDescompactMessage
.
setDesempacota
(
msg
)
protocolDescompactMessage
.
setDesempacota
(
msg
)
#Recebi o token
if
(
protocolDescompactMessage
.
type
==
'
1
'
and
int
(
MACHINE_ID
)
==
int
(
protocolDescompactMessage
.
origin
)):
if
(
protocolDescompactMessage
.
type
==
'
0
'
and
TOKEN
==
0
):
#print 'SETANDO A PRIORIDADE do TOKEN'
print
'
RECEBI O TOKEN
'
#print protocolDescompactMessage.priority
TOKEN
=
1
StartTime
=
time
.
time
()
#A msg foi enviada com sucesso
PRIORITY_TOKEN
=
protocolDescompactMessage
.
priority
elif
(
msg
[
1
]
==
'
2
'
and
int
(
MACHINE_ID
)
==
int
(
protocolDescompactMessage
.
destiny
)):
TOKEN
=
2
print
'
A MENSAGEM FOI ENVIADA COM SUCESSO
'
token
()
RECEBI
D
A
=
1
#print 'EU
RECEBI
A
MSG QUE ENVIEI'
#Erro no envio
elif
(
protocolDescompactMessage
.
type
==
'
0
'
and
TOKEN
==
0
):
elif
(
msg
[
1
]
==
'
3
'
and
int
(
MACHINE_ID
)
==
int
(
protocolDescompactMessage
.
destiny
)):
#print 'RECEBI O TOKEN'
RECEBIDA
=
0
TOKEN
=
1
if
(
token
()
==
False
):
#print 'Vou enviar o token'
TOKEN
=
0
udp
.
sendto
(
msg
,
dest
)
#A msg e pra mim
#A msg e pra mim
elif
(
msg
[
1
]
==
'
1
'
and
int
(
MACHINE_ID
)
==
int
(
protocolDescompactMessage
.
destiny
)):
elif
(
msg
[
1
]
==
'
1
'
and
int
(
MACHINE_ID
)
==
int
(
protocolDescompactMessage
.
destiny
)):
#protocolDescompactMessage.crc = "8888888"#caga tdo
if
(
protocolDescompactMessage
.
crc
==
protocolDescompactMessage
.
getCRC32
()):
print
'
\n
'
+
protocolDescompactMessage
.
origin
+
'
Escreveu:
'
+
protocolDescompactMessage
.
msg
#envia ack
protocolDescompactMessage
.
type
=
str
(
2
)
protocolDescompactMessage
.
destiny
=
protocolDescompactMessage
.
origin
msg
=
protocolDescompactMessage
.
getEmpacotar
()
udp
.
sendto
(
msg
,
dest
)
udp
.
sendto
(
msg
,
dest
)
if
(
protocolDescompactMessage
.
crc
==
protocolDescompactMessage
.
getCRC32
()):
print
'
\n
'
+
protocolDescompactMessage
.
origin
+
'
Escreveu:
'
+
protocolDescompactMessage
.
msg
else
:
else
:
print
'
ERRO DE MENSAGEM
'
print
'
ERRO DE MENSAGEM
'
#envia nack
protocolDescompactMessage
.
type
=
str
(
3
)
protocolDescompactMessage
.
destiny
=
protocolDescompactMessage
.
origin
msg
=
protocolDescompactMessage
.
getEmpacotar
()
udp
.
sendto
(
msg
,
dest
)
#A msg n e pra mim
#A msg n e pra mim
elif
(
msg
[
1
]
==
'
1
'
or
msg
[
2
]
==
'
2
'
or
msg
[
3
]
==
'
3
'
):
elif
(
msg
[
1
]
==
'
1
'
):
if
(
not
queue
.
empty
()):
top
=
queue
.
queue
[
0
]
HighPriorat
=
int
(
top
[
0
])
if
(
HighPriorat
<
int
(
msg
[
4
])):
aux1
.
setDesempacota
(
msg
)
aux1
.
priority
=
str
(
HighPriorat
)
msg
=
aux1
.
getEmpacotar
()
udp
.
sendto
(
msg
,
dest
)
udp
.
sendto
(
msg
,
dest
)
udp
.
close
()
udp
.
close
()
#------------------MAIN-----------------#
#------------------MAIN-----------------#
if
len
(
sys
.
argv
)
>
1
:
if
len
(
sys
.
argv
)
>
1
:
TOKEN
=
1
TOKEN
=
2
print
'
COMECEI COM O TOKEN
'
print
'
COMECEI COM O TOKEN
'
StartTime
=
time
.
time
()
PRIORITY_TOKEN
=
7
print
'
ID Dessa maquina, escolha entre 1 e 4
'
print
'
ID Dessa maquina, escolha entre 1 e 4
'
MACHINE_ID
=
raw_input
()
MACHINE_ID
=
raw_input
()
print
"
Ip da proxima maquina
"
print
"
Ip da proxima maquina
"
HOST
=
raw_input
()
HOST
=
raw_input
()
if
TOKEN
==
1
:
queue
=
Queue
.
PriorityQueue
()
StartTime
=
time
.
time
()
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
()
t
hreadToken
.
start
()
t
oken
()
This diff is collapsed.
Click to expand it.
repo.py
deleted
100755 → 0
+
0
−
230
View file @
8910e762
#!/usr/bin/env python
#################### Protocol #################################################################
# Marca - Tipo - Destino - Origem - Prioridade - Tamanho - Inic MSG - Mensagem - Fim MSG - CRC#
###############################################################################################
#Marca - 1 Byte Marca inicio de transmissao, inicio de msg fim de msg
#Tipo - 0 Token, 1 - Msg,
#Destino - pode variar de 1 a 4
#Origem - pode variar de 1 a 4
#Prioridade - pode variar de 0 a 7
#Tamanho - tanto faz
#Marca (inicio msg) - 1 Byte
#Mensagem
#CRC - 8 bytes
#Exemplo msg#
#~11313~oie~352D1036
# ~ - inic transmissao
# 1 - tipo da mensagem
# 1 - destino da mensagem
# 3 - origem da mensagem
# 1 - prioridade da mensagem
# 3 - tamanho da string da mensagem
# ~ - delimitador de inicio da mensagem
# oie - mensagem
# ~ - delimitador de fim da mensagem
# 352D1036 - CRC
import
socket
import
threading
import
sys
import
binascii
import
time
import
Queue
global
TOKEN
global
HighPriority
global
RECEBIDA
global
PRIORITY_TOKEN
PRIORITY_TOKEN
=
8
RECEBIDA
=
1
HighPriority
=
10
TOKEN
=
0
SEND_PORT
=
3131
# Porta que o Servidor envia
RECV_PORT
=
3131
# Porta que o Servidor recebe
class
Protocol
():
def
__init__
(
self
):
self
.
marca
=
"
~
"
#Marca
self
.
type
=
str
(
1
)
#Alterar Depois
self
.
destiny
=
str
(
0
)
self
.
origin
=
str
(
MACHINE_ID
)
self
.
priority
=
str
(
0
)
self
.
msg
=
str
(
0
)
self
.
sizemsg
=
str
(
0
)
self
.
crc
=
str
(
0
)
#set destiny message
def
setDestiny
(
self
):
sys
.
stdout
.
write
(
'
Destino:
'
)
self
.
destiny
=
raw_input
()
while
(
0
>=
int
(
self
.
destiny
))
or
(
int
(
self
.
destiny
)
>=
5
):
print
'
Entrada Invalida, Por favor escolha entre 1 a 4
'
sys
.
stdout
.
write
(
'
Destino:
'
)
self
.
destiny
=
raw_input
()
self
.
destiny
=
str
(
self
.
destiny
)
#set Priority messsage
def
setPriority
(
self
):
sys
.
stdout
.
write
(
'
Prioridade:
'
)
self
.
priority
=
raw_input
()
while
(
0
>=
int
(
self
.
priority
))
or
(
int
(
self
.
priority
)
>=
8
):
print
'
Entrada Invalida, Por favor escolha entre 1 a 7
'
sys
.
stdout
.
write
(
'
Prioridade:
'
)
self
.
priority
=
raw_input
()
self
.
priority
=
7
-
int
(
self
.
priority
)
#set message
def
setMessage
(
self
):
sys
.
stdout
.
write
(
'
Mensagem:
'
)
self
.
msg
=
raw_input
()
#set sizeof message
def
setSizeof
(
self
):
self
.
sizemsg
=
len
(
self
.
msg
)
self
.
sizemsg
=
str
(
self
.
sizemsg
)
#String convert
#set CRC32
def
setCRC32
(
self
):
buff
=
(
binascii
.
crc32
(
self
.
msg
)
&
0xFFFFFFFF
)
self
.
crc
=
"
%08X
"
%
buff
def
getEmpacotar
(
self
):
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
):
self
.
marca
=
msg
[
0
]
self
.
type
=
msg
[
1
]
self
.
destiny
=
str
(
msg
[
2
])
self
.
origin
=
msg
[
3
]
self
.
priority
=
msg
[
4
]
self
.
msg
=
msg
.
split
(
'
~
'
,
3
)[
2
]
self
.
crc
=
msg
.
split
(
'
~
'
,
3
)[
3
]
def
getCRC32
(
self
):
buff
=
(
binascii
.
crc32
(
self
.
msg
)
&
0xFFFFFFFF
)
return
"
%08X
"
%
buff
def
token
():
global
TOKEN
global
queue
global
PRIORITY_TOKEN
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
tokenMessage
=
Protocol
()
msgProtocol
=
Protocol
()
dest
=
(
HOST
,
SEND_PORT
)
time
.
sleep
(
2
)
if
(
TOKEN
==
2
):
TOKEN
=
0
tokenMessage
.
type
=
str
(
0
)
tokenMessage
.
priority
=
str
(
PRIORITY_TOKEN
)
msg
=
tokenMessage
.
getEmpacotar
()
udp
.
sendto
(
msg
,
dest
)
if
(
not
queue
.
empty
()
and
TOKEN
==
1
):
top
=
queue
.
queue
[
0
]
if
(
int
(
PRIORITY_TOKEN
)
>=
int
(
top
[
0
])):
top
=
queue
.
get
()
msgProtocol
.
setDesempacota
(
top
[
1
])
msgProtocol
.
priority
=
str
(
7
)
msg
=
msgProtocol
.
getEmpacotar
()
udp
.
sendto
(
top
[
1
],
dest
)
time
.
sleep
(
1
)
return
True
else
:
return
False
udp
.
close
()
def
client
():
try
:
global
TOKEN
protocolMessage
=
Protocol
()
global
queue
while
True
:
protocolMessage
.
setDestiny
()
protocolMessage
.
setPriority
()
protocolMessage
.
setMessage
()
protocolMessage
.
type
=
str
(
1
)
protocolMessage
.
setSizeof
()
protocolMessage
.
setCRC32
()
msg
=
protocolMessage
.
getEmpacotar
()
queue
.
put
((
protocolMessage
.
priority
,
msg
))
threadServer
.
_Thread__stop
()
except
:
print
'
OPS, ALGO OCORREU ERRADO
'
threadServer
.
_Thread__stop
()
def
server
():
global
TOKEN
global
RECEBIDA
global
queue
global
PRIORITY_TOKEN
global
TOKEN_MSG
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
orig
=
(
''
,
RECV_PORT
)
dest
=
(
HOST
,
SEND_PORT
)
udp
.
bind
(
orig
)
protocolDescompactMessage
=
Protocol
()
aux1
=
Protocol
()
aux2
=
Protocol
()
while
True
:
msg
,
cliente
=
udp
.
recvfrom
(
1024
)
protocolDescompactMessage
.
setDesempacota
(
msg
)
if
(
protocolDescompactMessage
.
type
==
'
1
'
and
int
(
MACHINE_ID
)
==
int
(
protocolDescompactMessage
.
origin
)):
#print 'SETANDO A PRIORIDADE do TOKEN'
#print protocolDescompactMessage.priority
PRIORITY_TOKEN
=
protocolDescompactMessage
.
priority
TOKEN
=
2
token
()
#print 'EU RECEBI A MSG QUE ENVIEI'
elif
(
protocolDescompactMessage
.
type
==
'
0
'
and
TOKEN
==
0
):
#print 'RECEBI O TOKEN'
TOKEN
=
1
if
(
token
()
==
False
):
#print 'Vou enviar o token'
TOKEN
=
0
udp
.
sendto
(
msg
,
dest
)
#A msg e pra mim
elif
(
msg
[
1
]
==
'
1
'
and
int
(
MACHINE_ID
)
==
int
(
protocolDescompactMessage
.
destiny
)):
udp
.
sendto
(
msg
,
dest
)
if
(
protocolDescompactMessage
.
crc
==
protocolDescompactMessage
.
getCRC32
()):
print
'
\n
'
+
protocolDescompactMessage
.
origin
+
'
Escreveu:
'
+
protocolDescompactMessage
.
msg
else
:
print
'
ERRO DE MENSAGEM
'
#A msg n e pra mim
elif
(
msg
[
1
]
==
'
1
'
):
if
(
not
queue
.
empty
()):
top
=
queue
.
queue
[
0
]
HighPriorat
=
int
(
top
[
0
])
if
(
HighPriorat
<
int
(
msg
[
4
])):
aux1
.
setDesempacota
(
msg
)
aux1
.
priority
=
str
(
HighPriorat
)
msg
=
aux1
.
getEmpacotar
()
udp
.
sendto
(
msg
,
dest
)
udp
.
close
()
#------------------MAIN-----------------#
if
len
(
sys
.
argv
)
>
1
:
TOKEN
=
2
print
'
COMECEI COM O TOKEN
'
PRIORITY_TOKEN
=
7
print
'
ID Dessa maquina, escolha entre 1 e 4
'
MACHINE_ID
=
raw_input
()
print
"
Ip da proxima maquina
"
HOST
=
raw_input
()
queue
=
Queue
.
PriorityQueue
()
threadServer
=
threading
.
Thread
(
target
=
server
)
threadClient
=
threading
.
Thread
(
target
=
client
)
threadServer
.
start
()
threadClient
.
start
()
token
()
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