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
0bcb3b7f
Commit
0bcb3b7f
authored
8 years ago
by
FernandoErd
Browse files
Options
Downloads
Patches
Plain Diff
Protocol End
parent
0537c3e1
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Canguru/Cliente.py
+75
-59
75 additions, 59 deletions
Canguru/Cliente.py
with
75 additions
and
59 deletions
Canguru/Cliente.py
+
75
−
59
View file @
0bcb3b7f
#!/usr/bin/env python
#################### Protocol ######################################
# Marca - Destino - Origem - Prioridade - Tamamnho - Menssagem - CRC#
####################################################################
#Marca - 1 Byte
#Destino - 4 Bytes
#Origem - 4 Bytes
#Prioridade - 4 Bytes
#Tamanho - 32 bytes
#################### 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
#Menssagem - ??
#CRC - 4 bytes
#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
...
...
@@ -22,18 +36,10 @@ SEND_PORT = 5000 # Porta que o Servidor envia
RECV_PORT
=
5001
# Porta que o Servidor recebe
MACHINE_ID
=
1
class
FuncThread
(
threading
.
Thread
):
def
__init__
(
self
,
tarset
,
*
args
):
self
.
_tarset
=
tarset
self
.
_args
=
args
threading
.
Thread
.
__init__
(
self
)
def
run
(
self
):
self
.
_tarset
(
*
self
.
_args
)
class
Protocol
():
def
__init__
(
self
):
self
.
marca
=
"
~
"
#Marca
self
.
type
=
str
(
1
)
#Alterar Depois
self
.
destiny
=
None
self
.
origin
=
str
(
MACHINE_ID
)
self
.
priority
=
None
...
...
@@ -43,26 +49,33 @@ class Protocol():
#set destiny message
def
setDestiny
(
self
):
print
'
Destino:
'
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
):
print
'
Prioridade:
'
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
=
str
(
self
.
priority
)
#set message
def
setMessage
(
self
):
print
'
Mensagem:
'
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
print
self
.
sizemsg
#set CRC32
def
setCRC32
(
self
):
...
...
@@ -70,22 +83,25 @@ class Protocol():
self
.
crc
=
"
%08X
"
%
buff
def
getEmpacotar
(
self
):
return
self
.
marca
+
self
.
destiny
+
self
.
origin
+
self
.
priority
+
self
.
marca
+
self
.
size
msg
+
self
.
m
sg
+
self
.
crc
return
self
.
marca
+
self
.
type
+
self
.
destiny
+
self
.
origin
+
self
.
priority
+
self
.
sizemsg
+
self
.
marca
+
self
.
msg
+
self
.
m
arca
+
self
.
crc
def
d
esempacota
(
self
,
msg
):
def
setD
esempacota
(
self
,
msg
):
self
.
marca
=
msg
[
0
]
self
.
destiny
=
msg
[
1
]
self
.
origin
=
msg
[
2
]
self
.
priority
=
msg
[
3
]
#slint
self
.
type
=
msg
[
1
]
self
.
destiny
=
int
(
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
client
():
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
dest
=
(
HOST
,
SEND_PORT
)
print
'
Para sair digite exit
\n
'
print
'
Mensagem:
'
#hostname = socket.sethostname()
protocolMessage
=
Protocol
()
protocolMessage
.
setDestiny
()
protocolMessage
.
setPriority
()
...
...
@@ -93,11 +109,8 @@ def client ():
protocolMessage
.
setSizeof
()
protocolMessage
.
setCRC32
()
msg
=
protocolMessage
.
getEmpacotar
()
#hostname = socket.sethostname()
while
True
:
#udp.sendto (hostname, dest) # Envia Hostname
udp
.
sendto
(
msg
,
dest
)
#Envia Mensagem
print
'
Mensagem:
'
while
protocolMessage
.
msg
<>
'
exit
'
:
udp
.
sendto
(
msg
,
dest
)
protocolMessage
=
Protocol
()
protocolMessage
.
setDestiny
()
protocolMessage
.
setPriority
()
...
...
@@ -105,30 +118,33 @@ def client ():
protocolMessage
.
setSizeof
()
protocolMessage
.
setCRC32
()
msg
=
protocolMessage
.
getEmpacotar
()
#hostname = socket.sethostname
()
threadServer
.
_Thread__stop
()
udp
.
close
()
def
server
():
print
'
Estou no Server
'
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
orig
=
(
HOST
,
RECV_PORT
)
udp
.
bind
(
orig
)
protocolDescompactMessage
=
Protocol
()
while
True
:
#hostname, cliente = udp.recvfrom(1024)
msg
,
cliente
=
udp
.
recvfrom
(
1024
)
#print hostname,':', msg
protocolDescompactMessage
.
setDesempacota
(
msg
)
if
protocolDescompactMessage
.
destiny
==
MACHINE_ID
:
if
protocolDescompactMessage
.
crc
==
protocolDescompactMessage
.
getCRC32
():
print
msg
print
'
Estou no Server
'
print
'
\n
'
+
protocolDescompactMessage
.
origin
+
'
Escreveu:
'
+
protocolDescompactMessage
.
msg
else
:
print
'
ERRO, A MENSAGEM FOI RECIBIDA ERRADA
'
udp
.
close
()
#def killThread ():
#######################MAIN#############################
#------------------MAIN-----------------#
if
len
(
sys
.
argv
)
>
1
:
AUX
=
RECV_PORT
RECV_PORT
=
SEND_PORT
SEND_PORT
=
AUX
threadServer
=
FuncThread
(
server
)
threadServer
=
threading
.
Thread
(
target
=
server
)
threadClient
=
threading
.
Thread
(
target
=
client
)
threadServer
.
start
()
client
()
threadClient
.
start
()
\ No newline at end of file
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