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
2b70fed1
Commit
2b70fed1
authored
8 years ago
by
Fernando Erd
Browse files
Options
Downloads
Patches
Plain Diff
organizando a pasta
parent
6cb91479
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
Canguru/CanguruChat.py
+0
-198
0 additions, 198 deletions
Canguru/CanguruChat.py
Canguru/README
+0
-3
0 additions, 3 deletions
Canguru/README
Canguru/getIp.py
+0
-6
0 additions, 6 deletions
Canguru/getIp.py
Chat.py
+0
-0
0 additions, 0 deletions
Chat.py
with
0 additions
and
207 deletions
Canguru/CanguruChat.py
deleted
100755 → 0
+
0
−
198
View file @
6cb91479
#!/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
StartTime
global
HighPriority
StartTime
=
0
HighPriority
=
10
TOKEN
=
0
MACHINE_ID
=
1
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
(
0
)
#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
)
def
setType
(
self
):
if
(
self
.
msg
==
'
token
'
):
self
.
type
=
str
(
0
)
else
:
self
.
type
=
str
(
1
)
#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
=
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
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
'
MANDEI O TOKEN
'
TOKEN
=
0
tokenMessage
.
Type
=
str
(
0
)
msg
=
tokenMessage
.
getEmpacotar
()
udp
.
sendto
(
msg
,
dest
)
udp
.
close
()
def
client
():
try
:
global
TOKEN
global
HighPriority
udp
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
dest
=
(
HOST
,
SEND_PORT
)
protocolMessage
=
Protocol
()
queue
=
Queue
.
PriorityQueue
()
while
True
:
protocolMessage
.
setDestiny
()
protocolMessage
.
setPriority
()
protocolMessage
.
setMessage
()
protocolMessage
.
Type
=
str
(
1
)
protocolMessage
.
setSizeof
()
protocolMessage
.
setCRC32
()
msg
=
protocolMessage
.
getEmpacotar
()
queue
.
put
((
protocolMessage
.
priority
,
msg
))
print
queue
.
queue
HighPriority
=
queue
.
queue
[
0
]
print
HighPriority
[
0
]
if
(
TOKEN
==
1
):
print
'
POSSO ENVIAR MSG
'
#udp.sendto (msg, dest)
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
)
protocolDescompactMessage
.
setDesempacota
(
msg
)
if
(
protocolDescompactMessage
.
type
==
'
0
'
and
TOKEN
==
0
):
print
'
RECEBI O TOKEN
'
TOKEN
=
1
StartTime
=
time
.
time
()
else
:
if
protocolDescompactMessage
.
crc
==
protocolDescompactMessage
.
getCRC32
():
print
msg
print
'
\n
'
+
protocolDescompactMessage
.
origin
+
'
Escreveu:
'
+
protocolDescompactMessage
.
msg
else
:
print
'
ERRO, A MENSAGEM FOI RECIBIDA ERRADA
'
udp
.
close
()
#------------------MAIN-----------------#
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
()
This diff is collapsed.
Click to expand it.
Canguru/README
deleted
100644 → 0
+
0
−
3
View file @
6cb91479
Trabalho 2 de Redes 1
Para executar ./CanguruChat.py em um terminal e ./CanguruChat.py 1 em outro terminal
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Canguru/getIp.py
deleted
100644 → 0
+
0
−
6
View file @
6cb91479
import
socket
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
)
s
.
connect
((
"
gmail.com
"
,
80
))
print
(
s
.
getsockname
()[
0
])
s
.
close
()
This diff is collapsed.
Click to expand it.
C
anguru/tentandop
.py
→
C
hat
.py
+
0
−
0
View file @
2b70fed1
File moved
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