-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClient_networking.py
More file actions
154 lines (134 loc) · 5.04 KB
/
Client_networking.py
File metadata and controls
154 lines (134 loc) · 5.04 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import socket
import sys
import threading
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import time
from Client_data import client_data
# The main class, which is called from the GUI.
class client_networking(QObject):
# The many types of signals used to communicate with the GUI.
# Each signals sends information to the GUI to a function.
# All of the signals can carry a string as a parameter.
messageSignal = pyqtSignal(str)
errorSignal = pyqtSignal(str)
roomListSignal = pyqtSignal(str)
greenSignal = pyqtSignal(str)
joinRoomSignal = pyqtSignal(str)
delRoomSignal = pyqtSignal(str)
z = None
s = socket.socket()
currentRoom = None
joinedRooms = []
data = client_data()
# run() is called when the thread that holds client_networking first starts.
# It is the main setup and the initial connection to the server.
@pyqtSlot()
def run(self):
# Sets up the socket using the same values as the server.
self.s = socket.socket()
host = socket.gethostname()
port = 9999
self.currentRoom = "general"
# Tries to connect to the server. If this fails, it sends an error to
# the GUI, then shuts down. User must restart the GUI to try again.
try:
self.s.connect((host, port))
except:
self.errorSignal.emit("Could not connect to server.")
return
print 'Connected to', host
self.z = None
#This causes all socket operations on s to timeout (Throw a socket.timeout exception)
#if more than the set time passes
self.s.settimeout(1)
self.receive()
#Continuously look for user and server messages
def receive(self):
# The main part of client_networking runs forever in this while loop.
# Since client_networking is called in a separate thread from the GUI,
# it can run in this loop without freezing anything for the user.
while True:
try:
# Checks to see if the server has sent anything.
received = self.s.recv(1024)
# Breaks apart the received message in order to check the contents
x = received.split(" ")
# If it is a list of rooms it immediately sends it to the GUI to be displayed.
if x[0] == "/roomlist":
self.roomListSignal.emit(" ".join(str(i) for i in x[1:]))
# History of a new room. Stores the history in the data and then
# sends the history of the room to the GUI's textbox.
elif x[0] == "/history":
room = x[1]
# History should only be loaded for a new chatroom or an empty (just created)
# room. Otherwise, discard the data
if self.data.add_chatroom(room) or self.data.load_from_chatroom(room) == []:
y = " ".join(str(i) for i in x[2:])
y = y.split('\n')
for z in y:
if z != '':
self.data.add_message(z,room)
if room == self.currentRoom:
self.messageSignal.emit(z)
pass
self.joinRoomSignal.emit(self.currentRoom)
# When an error message is received it is immediately output in red.
elif x[0] == "/error":
self.errorSignal.emit(" ".join(str(i) for i in x[1:]))
# Sysmessages are output in green.
elif x[0] == "/sysmessage":
self.greenSignal.emit(" ".join(str(i) for i in x[1:]))
# If the message received is for the current room, it's saved
# in the data and then output to the user.
elif x[0] == self.currentRoom:
msg = " ".join(str(i) for i in x[1:])
self.data.add_message(msg,x[0])
self.messageSignal.emit(msg)
# Otherwise, the message is simply stored without being output.
else:
" ".join(str(i) for i in x[1:])
self.data.add_message(msg,x[0])
#print "A message has been sent to another room:"
#print received
except (socket.timeout):
#No input received
pass
# z is a way of sending a signal to the server.
# If z has a value, it is sent, otherwise the loop continues.
if self.z is not None:
self.s.send(self.z)
print self.z
self.z = None
# Sends a standard message to the server, containing the roomname and message.
def send_message(self,msg):
self.s.send("/addmessage {} {}".format(self.currentRoom,msg))
# Commands the server to create a new room.
# Then calls join_room() to join the newly made room.
def create_room(self,msg):
self.s.send("/createchatroom {}".format(msg))
# without a slight delay the /joinchatroom command gets merged with the /createchatroom command.
time.sleep(0.01)
self.join_room(msg)
# Informs the server that the user has left a chatroom.
# Then deletes chatroom from the data.
def leave_room(self,msg):
self.z = "/leavechatroom {}".format(msg)
self.currentRoom = None
self.data.remove_chatroom(msg)
self.delRoomSignal.emit(msg)
# Join an already existing chatroom.
# Adds that chatroom to the data.
def join_room(self,msg):
self.s.send("/joinchatroom {}".format(msg))
self.currentRoom == msg
self.joinRoomSignal.emit(self.currentRoom)
self.data.add_chatroom(msg)
# Requests a list of rooms from the server.
def get_room_list(self):
self.s.send("/listallrooms")
# Sends a room's history to the GUI when the user switches between joined rooms.
def change_room(self,msg):
self.currentRoom = msg
hist = self.data.load_from_chatroom(msg)
return hist