-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.py
67 lines (47 loc) · 2.4 KB
/
star.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
'''server implementation'''
from twisted.internet import protocol,reactor
class StarProtocol(protocol.Protocol):
def __init__(self,factory):
self.factory = factory #factory that stores all the clients connected to the server
self.name = None #name of the client that will connect to the server
def connectionMade(self):
'''establishing a connection to the server'''
print('New client connected: ',self.transport.getPeer())
self.factory.clients.append(self)
def connectionLost(self, reason):
print("Client disconnected")
self.factory.remove(self)
def dataReceived(self, data):
message = data.decode().strip()
if not self.name:
self.name = message
print(self.name,' has connected to the server.') #if the client has not connected to the server before
else:
if message.startswith('@'): #already existing client sends a message
'''one client will send message to another client '''
recipient, private_message = message[1:].split(":", 1)
self.sendthroughServer(recipient, private_message)
else:
'''if destination is not specified, the message is simply sent to the server.'''
self.transport.write(message)
def sendthroughServer(self,recipient,message):
self.transport.write(message) #the message first goes to the server
self.transport.write('message sending.....')
self.sendPrivateMessage(recipient,message) #the message is then sent to the destination through the server
def sendPrivateMessage(self,recipient,message):
for client in self.factory.clients:
if client.name == recipient:
client.transport.write(f"(Private) {self.name}: {message}\n".encode())
break
else:
self.transport.write(f"Error: User {recipient} not found.\n".encode())
class StarFactory(protocol.Factory):
def __init__(self):
self.clients = []
def buildProtocol(self, addr):
return StarProtocol(self)
if __name__ == "__main__":
reactor.listenTCP(8080, StarFactory())
print("Server started. Listening on port 8080...")
print("Enter client name to register. Enter @ before the starting of a message to send message to another client.")
reactor.run()