-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_utils.py
87 lines (77 loc) · 2.37 KB
/
chat_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import socket
import time
# use local loop back address by default
#CHAT_IP = '127.0.0.1'
CHAT_IP = socket.gethostbyname(socket.gethostname())
CHAT_PORT = 1112
SERVER = (CHAT_IP, CHAT_PORT)
menu = "\n++++ Choose one of the following commands\n \
time: calendar time in the system\n \
who: to find out who else are there\n \
c _peer_: to connect to the _peer_ and chat\n \
? _term_: to search your chat logs where _term_ appears\n \
p _#_: to get number <#> sonnet\n \
q: to leave the chat system\n \
g _*_: to connect with user* and play with * \n"
menu2 = "\n++++ Choose one of the following commands\n \
ready: show you the initial graph\n \
who goes first: to see who goes first and who is the next'\n \
move: type in 'move' before every move\n\
bye: to quit the game"
S_OFFLINE = 0
S_CONNECTED = 1
S_LOGGEDIN = 2
S_CHATTING = 3
S_GAME = 4
S_ENDGAME = 7
S_GAMEWAIT = 6
SIZE_SPEC = 5
CHAT_WAIT = 0.2
def print_state(state):
print('**** State *****::::: ')
if state == S_OFFLINE:
print('Offline')
elif state == S_CONNECTED:
print('Connected')
elif state == S_LOGGEDIN:
print('Logged in')
elif state == S_CHATTING:
print('Chatting')
elif state == S_GAME:
print("On Game")
else:
print('Error: wrong state')
def mysend(s, msg):
#append size to message and send it
msg = ('0' * SIZE_SPEC + str(len(msg)))[-SIZE_SPEC:] + str(msg)
msg = msg.encode()
total_sent = 0
while total_sent < len(msg) :
sent = s.send(msg[total_sent:])
if sent==0:
print('server disconnected')
break
total_sent += sent
def myrecv(s):
#receive size first
size = ''
while len(size) < SIZE_SPEC:
text = s.recv(SIZE_SPEC - len(size)).decode()
if not text:
print('disconnected')
return('')
size += text
size = int(size)
#now receive message
msg = ''
while len(msg) < size:
text = s.recv(size-len(msg)).decode()
if text == b'':
print('disconnected')
break
msg += text
#print ('received '+message)
return (msg)
def text_proc(text, user):
ctime = time.strftime('%d.%m.%y,%H:%M', time.localtime())
return('(' + ctime + ') ' + user + ' : ' + text) # message goes directly to screen