-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_state_machine_student.py
139 lines (118 loc) · 5.18 KB
/
client_state_machine_student.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
Created on Sun Apr 5 00:00:32 2015
@author: zhengzhang
"""
from chat_utils import *
import json
class ClientSM:
def __init__(self, s):
self.state = S_OFFLINE
self.peer = ''
self.me = ''
self.out_msg = ''
self.s = s
def set_state(self, state):
self.state = state
def get_state(self):
return self.state
def set_myname(self, name):
self.me = name
def get_myname(self):
return self.me
def connect_to(self, peer):
msg = json.dumps({"action":"connect", "target":peer})
mysend(self.s, msg)
response = json.loads(myrecv(self.s))
if response["status"] == "success":
self.peer = peer
self.out_msg += 'You are connected with '+ self.peer + '\n'
return (True)
elif response["status"] == "busy":
self.out_msg += 'User is busy. Please try again later\n'
elif response["status"] == "self":
self.out_msg += 'Cannot talk to yourself (sick)\n'
else:
self.out_msg += 'User is not online, try again later\n'
return(False)
def disconnect(self):
msg = json.dumps({"action":"disconnect"})
mysend(self.s, msg)
self.out_msg += 'You are disconnected from ' + self.peer + '\n'
self.peer = ''
def proc(self, my_msg, peer_msg):
self.out_msg = ''
#==============================================================================
# Once logged in, do a few things: get peer listing, connect, search
# And, of course, if you are so bored, just go
# This is event handling instate "S_LOGGEDIN"
#==============================================================================
if self.state == S_LOGGEDIN:
# todo: can't deal with multiple lines yet
if len(my_msg) > 0:
if my_msg == 'q':
self.out_msg += 'See you next time!\n'
self.state = S_OFFLINE
elif my_msg == 'time':
mysend(self.s, json.dumps({"action":"time"}))
time_in = json.loads(myrecv(self.s))["results"]
self.out_msg += "Time is: " + time_in
elif my_msg == 'who':
mysend(self.s, json.dumps({"action":"list"}))
logged_in = json.loads(myrecv(self.s))["results"]
self.out_msg += 'Here are all the users in the system:\n'
self.out_msg += logged_in
elif my_msg[0] == 'c':
peer = my_msg[1:]
peer = peer.strip()
if self.connect_to(peer) == True:
self.state = S_CHATTING
self.out_msg += 'Connect to ' + peer + '. Chat away!\n\n'
self.out_msg += '-----------------------------------\n'
else:
self.out_msg += 'Connection unsuccessful\n'
elif my_msg[0] == '?':
term = my_msg[1:].strip()
mysend(self.s, json.dumps({"action":"search", "target":term}))
search_rslt = json.loads(myrecv(self.s))["results"][1:].strip()
if (len(search_rslt)) > 0:
self.out_msg += search_rslt + '\n\n'
else:
self.out_msg += '\'' + term + '\'' + ' not found\n\n'
elif my_msg[0] == 'p':
poem_idx = my_msg[1:].strip()
mysend(self.s, json.dumps({"action":"poem", "target":poem_idx}))
poem = json.loads(myrecv(self.s))["results"][1:].strip()
if (len(poem) > 0):
self.out_msg += poem + '\n\n'
else:
self.out_msg += 'Sonnet ' + poem_idx + ' not found\n\n'
else:
self.out_msg += menu
if len(peer_msg) > 0:
peer_msg = json.loads(peer_msg)
if peer_msg["action"] == "connect":
pass
#==============================================================================
# Start chatting, 'bye' for quit
# This is event handling instate "S_CHATTING"
#==============================================================================
elif self.state == S_CHATTING:
if len(my_msg) > 0: # my stuff going out
mysend(self.s, json.dumps({"action":"exchange", "from":"[" + self.me + "]", "message":my_msg}))
if my_msg == 'bye':
self.disconnect()
self.state = S_LOGGEDIN
self.peer = ''
if len(peer_msg) > 0: # peer's stuff, coming in
peer_msg = json.loads(peer_msg)
pass
# Display the menu again
if self.state == S_LOGGEDIN:
self.out_msg += menu
#==============================================================================
# invalid state
#==============================================================================
else:
self.out_msg += 'How did you wind up here??\n'
print_state(self.state)
return self.out_msg