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
0bcb3b7f
Commit
0bcb3b7f
authored
8 years ago
by
FernandoErd
Browse files
Options
Downloads
Patches
Plain Diff
Protocol End
parent
0537c3e1
No related branches found
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
#!/usr/bin/env python
#################### Protocol ######################################
#################### Protocol #################################################################
# Marca - Destino - Origem - Prioridade - Tamamnho - Menssagem - CRC#
# Marca - Tipo - Destino - Origem - Prioridade - Tamanho - Inic MSG - Mensagem - Fim MSG - CRC#
####################################################################
###############################################################################################
#Marca - 1 Byte
#Marca - 1 Byte Marca inicio de transmissao, inicio de msg fim de msg
#Destino - 4 Bytes
#Tipo - 0 Token, 1 - Msg
#Origem - 4 Bytes
#Destino - pode variar de 1 a 4
#Prioridade - 4 Bytes
#Origem - pode variar de 1 a 4
#Tamanho - 32 bytes
#Prioridade - pode variar de 0 a 7
#Tamanho - tanto faz
#Marca (inicio msg) - 1 Byte
#Marca (inicio msg) - 1 Byte
#Menssagem - ??
#Mensagem
#CRC - 4 bytes
#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
socket
import
threading
import
threading
...
@@ -22,18 +36,10 @@ SEND_PORT = 5000 # Porta que o Servidor envia
...
@@ -22,18 +36,10 @@ SEND_PORT = 5000 # Porta que o Servidor envia
RECV_PORT
=
5001
# Porta que o Servidor recebe
RECV_PORT
=
5001
# Porta que o Servidor recebe
MACHINE_ID
=
1
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
():
class
Protocol
():
def
__init__
(
self
):
def
__init__
(
self
):
self
.
marca
=
"
~
"
#Marca
self
.
marca
=
"
~
"
#Marca
self
.
type
=
str
(
1
)
#Alterar Depois
self
.
destiny
=
None
self
.
destiny
=
None
self
.
origin
=
str
(
MACHINE_ID
)
self
.
origin
=
str
(
MACHINE_ID
)
self
.
priority
=
None
self
.
priority
=
None
...
@@ -43,26 +49,33 @@ class Protocol():
...
@@ -43,26 +49,33 @@ class Protocol():
#set destiny message
#set destiny message
def
setDestiny
(
self
):
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
=
raw_input
()
self
.
destiny
=
str
(
self
.
destiny
)
self
.
destiny
=
str
(
self
.
destiny
)
#set Priority messsage
#set Priority messsage
def
setPriority
(
self
):
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
=
raw_input
()
self
.
priority
=
str
(
self
.
priority
)
self
.
priority
=
str
(
self
.
priority
)
#set message
#set message
def
setMessage
(
self
):
def
setMessage
(
self
):
print
'
Mensagem:
'
sys
.
stdout
.
write
(
'
Mensagem:
'
)
self
.
msg
=
raw_input
()
self
.
msg
=
raw_input
()
#set sizeof message
#set sizeof message
def
setSizeof
(
self
):
def
setSizeof
(
self
):
self
.
sizemsg
=
len
(
self
.
msg
)
self
.
sizemsg
=
len
(
self
.
msg
)
self
.
sizemsg
=
str
(
self
.
sizemsg
)
#String convert
self
.
sizemsg
=
str
(
self
.
sizemsg
)
#String convert
print
self
.
sizemsg
#set CRC32
#set CRC32
def
setCRC32
(
self
):
def
setCRC32
(
self
):
...
@@ -70,22 +83,25 @@ class Protocol():
...
@@ -70,22 +83,25 @@ class Protocol():
self
.
crc
=
"
%08X
"
%
buff
self
.
crc
=
"
%08X
"
%
buff
def
getEmpacotar
(
self
):
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
.
marca
=
msg
[
0
]
self
.
destiny
=
msg
[
1
]
self
.
type
=
msg
[
1
]
self
.
origin
=
msg
[
2
]
self
.
destiny
=
int
(
msg
[
2
])
self
.
priority
=
msg
[
3
]
self
.
origin
=
msg
[
3
]
#slint
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
():
def
client
():
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
'
print
'
Para sair digite exit
\n
'
print
'
Mensagem:
'
#hostname = socket.sethostname()
protocolMessage
=
Protocol
()
protocolMessage
=
Protocol
()
protocolMessage
.
setDestiny
()
protocolMessage
.
setDestiny
()
protocolMessage
.
setPriority
()
protocolMessage
.
setPriority
()
...
@@ -93,11 +109,8 @@ def client ():
...
@@ -93,11 +109,8 @@ def client ():
protocolMessage
.
setSizeof
()
protocolMessage
.
setSizeof
()
protocolMessage
.
setCRC32
()
protocolMessage
.
setCRC32
()
msg
=
protocolMessage
.
getEmpacotar
()
msg
=
protocolMessage
.
getEmpacotar
()
#hostname = socket.sethostname()
while
protocolMessage
.
msg
<>
'
exit
'
:
while
True
:
udp
.
sendto
(
msg
,
dest
)
#udp.sendto (hostname, dest) # Envia Hostname
udp
.
sendto
(
msg
,
dest
)
#Envia Mensagem
print
'
Mensagem:
'
protocolMessage
=
Protocol
()
protocolMessage
=
Protocol
()
protocolMessage
.
setDestiny
()
protocolMessage
.
setDestiny
()
protocolMessage
.
setPriority
()
protocolMessage
.
setPriority
()
...
@@ -105,30 +118,33 @@ def client ():
...
@@ -105,30 +118,33 @@ def client ():
protocolMessage
.
setSizeof
()
protocolMessage
.
setSizeof
()
protocolMessage
.
setCRC32
()
protocolMessage
.
setCRC32
()
msg
=
protocolMessage
.
getEmpacotar
()
msg
=
protocolMessage
.
getEmpacotar
()
#hostname = socket.sethostname
()
threadServer
.
_Thread__stop
()
udp
.
close
()
udp
.
close
()
def
server
():
def
server
():
print
'
Estou no Server
'
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
orig
=
(
HOST
,
RECV_PORT
)
orig
=
(
HOST
,
RECV_PORT
)
udp
.
bind
(
orig
)
udp
.
bind
(
orig
)
protocolDescompactMessage
=
Protocol
()
while
True
:
while
True
:
#hostname, cliente = udp.recvfrom(1024)
msg
,
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
msg
print
'
Estou no Server
'
print
'
\n
'
+
protocolDescompactMessage
.
origin
+
'
Escreveu:
'
+
protocolDescompactMessage
.
msg
else
:
print
'
ERRO, A MENSAGEM FOI RECIBIDA ERRADA
'
udp
.
close
()
udp
.
close
()
#def killThread ():
#------------------MAIN-----------------#
#######################MAIN#############################
if
len
(
sys
.
argv
)
>
1
:
if
len
(
sys
.
argv
)
>
1
:
AUX
=
RECV_PORT
AUX
=
RECV_PORT
RECV_PORT
=
SEND_PORT
RECV_PORT
=
SEND_PORT
SEND_PORT
=
AUX
SEND_PORT
=
AUX
threadServer
=
FuncThread
(
server
)
threadServer
=
threading
.
Thread
(
target
=
server
)
threadClient
=
threading
.
Thread
(
target
=
client
)
threadServer
.
start
()
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