-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_core.py
More file actions
184 lines (148 loc) · 7.86 KB
/
server_core.py
File metadata and controls
184 lines (148 loc) · 7.86 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from loading_modules import loading_modules
class node:
def __init__(self,Sender,Receiver):
self.sock = Sender
self.alt = Receiver
class low_interface_of_server:
def __init__(self, server, HOST, PORT, test=None):
self.modules=loading_modules()
# socket bind method will bind the host with port
server.bind((HOST,PORT))
# now server is going to listen on this port for connections
server.listen()
print('CHATROOM SERVER IS ACTIVATED')
# building arrays to store clients and their information
self.Public_names, self.Private_names, self.Private_clients, self.Public_clients = [], [], [], []
# starting thread of kick method and it will run along with main method
if not test:
Kick_method_thread=self.modules.threading.Thread(target=self.kick)
Kick_method_thread.start()
# main method execution will start the server
self.Main(server)
def Main(self,server):
# Private chat thread is going to execute along with public chat thread
Private_Chat_Thread = self.modules.threading.Thread(target = self.Private_Chat)
Private_Chat_Thread.start()
while True:
conn, addr = server.accept()
# conn is a socket which is connected to server now
print('CONNECTED TO', addr)
# filling client socket in public client array
self.Public_clients.append(conn)
conn.sendall('YOU HAVE JOINDED THE CHATROOM'.encode('ascii'))
# server is recieving names from clients
name = conn.recv(1024)
self.Public_names.append(name.decode())
# Public chat thread is going to excecute
Public_Chat_Thread = self.modules.threading.Thread(target=self.Public_chat_reciever, args=(conn,))
Public_Chat_Thread.start()
def Private_Chat(self):
v = 0
# this loop is going to check private client list that any one is in queue for private chat
# or not so according to that it will create private chat threads.
while True:
if len(self.Private_clients)<1 or v == len(self.Private_clients):
pass
else:
# obj variable will collect the client who requested for private chat.
obj = self.Private_clients[v]
while True:
if obj.alt not in self.Public_clients:
private_thread = self.modules.threading.Thread(target = self.Private_chat_Reciever, args = (obj.alt, obj.sock,))
private_thread.start()
private_alt_thread = self.modules.threading.Thread(target = self.Private_chat_Reciever, args = (obj.sock, obj.alt,))
private_alt_thread.start()
v+=1
break
# broadcast method will broadcast all messages from client to all other public clients.
def broadcast(self, message, ex, Public_clients):
for client in Public_clients:
if client == ex:
pass
else:
client.sendall(message)
# Private_chat_reciever method will be use in private chat threads.
def Private_chat_Reciever(self, sender, reciever):
while True:
try:
data = sender.recv(1024)
reciever.sendall(data)
except:
sender.close()
break
def Enlist_Public_Users(self, Public_names, exuser = None, connection = None):
for name in Public_names:
if exuser == name:
exuser = None
elif connection:
connection.sendall(name.encode('ascii'))
else:
print(name)
# PrivateChat_Permission method is gonna be in process when private chat mode is requested by client.
def PrivateChat_Permission(self, connection):
connection.sendall('SELECT TNE NAME FROM LIST WHOME TOU WANNA CHAT PRIVATE AND TYPE IT'.encode('ascii'))
Index = self.Public_clients.index(connection)
name = self.Public_names[Index]
# Enlist_Public_Users method with connection parameter will forward the connected clients list to user who requested for private chat.
self.Enlist_Public_Users(self.Public_names, exuser = name, connection = connection)
# retrieve a name from client whom it want to do privatechat with.
dataa = connection.recv(1024)
dataa = dataa.decode()
# response will be in specific format so here it is gonna split.
dataa = dataa.split(': ')
recievername = dataa[1]
sendername = dataa[0]
# retrieving sockets and index from arrays to establish the communication between private chat participants.
index=self.Public_names.index(recievername)
recieversocket = self.Public_clients[index]
msgg = sendername+'USER REQUEST YOU FOR PRIVATECHAT ENTER /YES TO JOIN PRIVATECHAT'
recieversocket.sendall(msgg.encode('ascii'))
# filling private chat clients sockets and names to arrays
self.Private_names.append(sendername)
new_connection = node(connection, recieversocket)
self.Private_clients.append(new_connection)
self.Delete_User(sendername,connection)
# Public_chat_reciever method use to recieve messages from public chat users and deliever it to other users.
def Public_chat_reciever(self, connection):
while True:
data = connection.recv(1024)
if data.decode() == '/PRIVATECHAT':
self.PrivateChat_Permission(connection)
break
elif data.decode() == '/YES':
index = self.Public_clients.index(connection)
name = self.Public_names[index]
self.Delete_User(name,connection)
break
elif data.decode() == '/EXIT':
index = self.Public_clients.index(connection)
del_name = self.Public_names[index]
self.Delete_User(del_name,connection)
connection.close()
break
else:
self.broadcast(data, connection, self.Public_clients)
def Delete_User(self,name,client):
self.Public_names.remove(name)
self.Public_clients.remove(client)
# kick method is a feature for server side admin so if admin want to kick any user so it can kick with
# the help of kick method which will be constantly in working because of thread framework.
def kick(self):
while True:
decison = input('If you want to display user list press 0 and if you want to kick user press 1')
# if decison is '0' then it will display connected users list.
if decison == '0':
self.Enlist_Public_Users(self.Public_names)
elif decison == '1':
# If someone press 1 then it will enlist the public chat users list specifically for removing task.
print('USERS LIST')
self.Enlist_Public_Users(self.Public_names)
# it will also ask about name from admin whome admin want to kick out.
Dumping_Name = input('Wanna kick user ENTER THE NAME PLZ')
index = self.Public_names.index(Dumping_Name)
# retrieving socket from array with th help of index
con = self.Public_clients[index]
con.sendall('KICKED'.encode('ascii'))
# broadcasting kicked message to all connected public chat users
msg = 'USER '+Dumping_Name+' IS KICKED BY CHAT ROOM ADMIN'
self.broadcast(msg.encode('ascii'), con, self.Public_clients)